AllJoyn Bundled Daemon 使用方式研究
关于AllJoyn不多做介绍,请看官网:www.alljoyn.org/
0. 问题来源:
应用程序要使用AllJoyn库,就必须启动deamon。
目前有两种方式:
- 使用standalone形式,单独启动alljoyn-daemon进程。
- 使用bundled daemon形式,应用程序在连接AllJoyn时自己启动该deamon。
AllJoyn的开发者解释如下:https://www.alljoyn.org/forums/developers/building-embedded-linux-system
The decision to use standalone or bundled daemon is made runtime but there is also an additional component that specifies whether an AllJoyn library supports bundled daemon at all. This the BD variable that we use when we build AllJoyn using scons.
More specifically, the check to choose which daemon is always made in the same manner irrespective of whether we have a standalone daemon or bundled daemon. The only thing BD signifies during build is whether the library will actually include code to run bundled daemon.
An AllJoyn application will always see is there is a standalone daemon running. If yes it will connect to that daemon else it will try and use the bundled daemon which may or may not be a part of the library depending on whether you said BD=on or off.
bundled daemon形式不需要单独启动进程,比较方便。因此就来研究如何使用该种方式。
1. 从入口看起:
BusAttachment::Connect()
{
#ifdef _WIN32
const char* connectArgs = "tcp:addr=127.0.0.1,port=9956";
#else
const char* connectArgs = "unix:abstract=alljoyn";
#endif
return Connect(connectArgs);
}
很明显,针对windows使用tcp transport,其他平台使用“unix:abstract=alljoyn“transport。
然后调用内部函数BusAttachment::Connect(const char* connectSpec)进行连接。
2. BusAttachment::Connect(const char* connectSpec)函数,重点如下:
...
this->connectSpec = connectSpec;
status = TryConnect(connectSpec);
/*
* Try using the null transport to connect to a bundled daemon if there is one
*/
if (status != ER_OK && !isDaemon) {
printf("TryConnect() failed.\n"); qcc::String bundledConnectSpec = "null:";
if (bundledConnectSpec != connectSpec) {
status = TryConnect(bundledConnectSpec.c_str());
if (ER_OK == status) {
this->connectSpec = bundledConnectSpec;
}
}
}
...
首先尝试连接指定的"unix:abstract=alljoyn";
如果连接失败就尝试连接名为”null:”的transport(对应的类是NullTransport )------该transport 即使用bundled daemon
3. 再看BusAttachment::TryConnect(const char* connectSpec)函数,重点如下:
...
/* Get or create transport for connection */
Transport* trans = busInternal->transportList.GetTransport(connectSpec);
if (trans) {
SessionOpts emptyOpts;
status = trans->Connect(connectSpec, emptyOpts, tempEp);
...
}
...
从busInternal中获取指定的transport,再进行连接。
那么busInternal是如何存储transport的呢?
4. 看busInternal的初始化过程:
BusAttachment::Internal::Internal(const char* appName,
BusAttachment& bus,
TransportFactoryContainer& factories,
Router* router,
bool allowRemoteMessages,
const char* listenAddresses,
uint32_t concurrency) :
application(appName ? appName : "unknown"),
bus(bus),
listenersLock(),
listeners(),
m_ioDispatch("iodisp", 16),
transportList(bus, factories, &m_ioDispatch, concurrency),
keyStore(application),
authManager(keyStore),
globalGuid(qcc::GUID128()),
msgSerial(1),
router(router ? router : new ClientRouter),
localEndpoint(transportList.GetLocalTransport()->GetLocalEndpoint()),
allowRemoteMessages(allowRemoteMessages),
listenAddresses(listenAddresses ? listenAddresses : ""),
stopLock(),
stopCount(0)
{
...
}
可以看到,这里是通过TransportFactoryContainer(一组transport factory)对象来初始化所支持的transport list。
5.
QStatus TransportList::Start(const String& transportSpecs)
{
...
for (uint32_t i = 0; i < m_factories.Size(); ++i) {
TransportFactoryBase* factory = m_factories.Get(i);
if (factory->GetType() == ttype && factory->IsDefault() == false) {
transportList.push_back(factory->Create(bus));
}
}
...
}
可以看到,这里根据每个transport factory来创建对应的transport,并添加到transportList.中。
所以问题的关键在于:TransportFactoryContainer是如何初始化的呢(由transport factory 来决定支持的transport list)?
6. 再回过头来看BusAttachment的构造函数:
BusAttachment::BusAttachment(const char* applicationName, bool allowRemoteMessages, uint32_t concurrency) :
isStarted(false),
isStopping(false),
concurrency(concurrency),
busInternal(new Internal(applicationName, *this, clientTransportsContainer, NULL, allowRemoteMessages, NULL, concurrency)),
joinObj(this)
{
...
}
原来是BusAttachment在构造对象时初始化了Internal对象, 参数TransportFactoryContainer就是clientTransportsContainer
7. 继续跟踪clientTransportsContainer:
/*
* Transport factory container for transports this bus attachment uses to communicate with the daemon.
*/
static class ClientTransportFactoryContainer : public TransportFactoryContainer {
public:
ClientTransportFactoryContainer() : transportInit(0) { } void Init()
{
/*
* Registration of transport factories is a one time operation.
*/
if (IncrementAndFetch(&transportInit) == 1) {
if (ClientTransport::IsAvailable()) {
Add(new TransportFactory<ClientTransport>(ClientTransport::TransportName, true));
}
if (NullTransport::IsAvailable()) {
Add(new TransportFactory<NullTransport>(NullTransport::TransportName, true));
}
} else {
DecrementAndFetch(&transportInit);
}
} private:
volatile int32_t transportInit;
} clientTransportsContainer;
可以看到这是一个静态类,在初始化时检查是否支持NullTransport,如果支持就将它添加到TransportFactoryContainer中,在start时就会创建对应的transport 对象,供connect进行连接。
所以,现在问题的关键在于:NullTransport::IsAvailable()是否返回true。
8. 跟踪NullTransport::IsAvailable()函数:
/**
* The null transport is only available if the application has been linked with bundled daemon
* support. Check if the null transport is available.
*
* @return Returns true if the null transport is available.
*/
static bool IsAvailable() { return daemonLauncher != NULL; } 该函数通过检查daemonLauncher 变量是否为空,来决定是否支持NullTransport。其声明如下:
class NullTransport : public Transport {
private:
...
static DaemonLauncher* daemonLauncher; /**< The daemon launcher if there is bundled daemon present */
};
初始化:
DaemonLauncher* NullTransport::daemonLauncher;
可知:daemonLauncher变量默认为空,即默认是不支持NullTransport的。
9. 检查整个工程代码,发现为其赋值的代码如下:
BundledDaemon::BundledDaemon() : transportsInitialized(false), stopping(false), ajBus(NULL), ajBusController(NULL)
{
NullTransport::RegisterDaemonLauncher(this);
}
void NullTransport::RegisterDaemonLauncher(DaemonLauncher* launcher)
{
daemonLauncher = launcher;
}
说明:只有在声明BundledDaemon对象的情况下daemonLauncher才不为空,才能支持NullTransport(即Bundled Daemon 模式)。
10. 查看文件daemon/bundled/BundledDaemon.cc230行,发现已声明BundledDaemon 的静态对象
static BundledDaemon bundledDaemon;
这是不是意味着,只需要连接该文件就可以声明BundledDaemon 对象, daemonLauncher才不为空,进而支持NullTransport(即Bundled Daemon 模式)?
以AllJoyn自带的chat做实验,位于:alljoyn-3.3.0-src/build/linux/x86-64/debug/dist/samples/chat
修改Makefile的连接选项,发现只需要按如下顺序添加连接选项:
LIBS = -lalljoyn ../../lib/BundledDaemon.o -lajdaemon -lstdc++ -lcrypto -lpthread –lrt
程序就会自动启用Bundled Daemon,而不需要手动启动alljoyn-daemon进程
$ ./chat -s a
BusAttachment started.
RegisterBusObject succeeded.
0.063 ****** ERROR NETWORK external Socket.cc:249 | Connecting (sockfd = 15) to @alljoyn : 111 - Connection refused: ER_OS_ERROR
0.063 ****** ERROR ALLJOYN external posix/ClientTransport.cc:258 | ClientTransport(): socket Connect(15, @alljoyn) failed: ER_OS_ERROR
Using BundledDaemon
AllJoyn Daemon GUID = 8216a0fc60832b5f50c2111527f89fc1 (2MWyW3hV)
StartListen: tcp:r4addr=0.0.0.0,r4port=0
StartListen: ice:
Connect to ‘null:’ succeeded-----------------------------已经连上NullTransport
最终结论:
应用程序如果想使用Bundled Daemon,只需要在连接选项中添加如下库即可(不可改变连接库顺序):
-lalljoyn ../../lib/BundledDaemon.o -lajdaemon
AllJoyn Bundled Daemon 使用方式研究的更多相关文章
- Tomcat以Daemon的方式启动(CentOS6&7)
1 前言 一直以来都觉得Tomcat以root身份运行非常不安全,故研究Tomcat如何以普通用户身份运行,以下是参考网络上的一些配置实现Tomcat以daemon方式运行于CentOS 6& ...
- Docker Daemon 连接方式详解
前言 在 Docker 常用详解指令 一文中粗粗提了一下, Docker 是分为客户端和服务端两部分的, 本文将介绍客户端是如何连接服务端的. 连接方式 1. UNIX域套接字 默认就是这种方式, 会 ...
- Docker客户端连接Docker Daemon的方式
Docker为C/S架构,服务端为docker daemon,客户端为docker.service,支持本地unix socket域套接字通信与远程socket通信. 默认为本地unix socket ...
- Windows下非PE方式载荷投递方式研究
0. 引言 0x1:载荷是什么?在整个入侵过程中起到什么作用? 载荷的作用在整个入侵链路的作用起到纽带的作用,它借助于目标系统提供的某些功能:组件:执行环境,将攻击者的传递的恶意payload包裹起来 ...
- UDP打洞、P2P组网方式研究
catalogue . NAT概念 . P2P概念 . UDP打洞 . P2P DEMO . ZeroNet P2P 1. NAT概念 在STUN协议中,根据内部终端的地址(LocalIP:Local ...
- 恶意软件/BOT/C2隐蔽上线方式研究
catalogue . 传统木马上线方式 . 新型木马上线方式 . QQ昵称上线 . QQ空间资料上线 . 第三方域名上线 . UDP/TCP二阶段混合上线 . Gmail CNC . NetBot两 ...
- DedeCMS顽固木马后门专杀工具V2.0实现方式研究
catalog . 安装及使用方式 . 检查DEDECMS是否为最新版本 . 检查默认安装(install)目录是否存在 . 检查默认后台目录(dede)是否存在 . 检查DedeCMS会员中心是否关 ...
- linux标准daemon编写方式
daemon定义 运行在后台的程序,通常不需要与用户进行交互的. 任何父进程id是0的通常是kernel进程,作为系统启动的一部分,除了init是用户态的命令. 规则 第一件事情是调用umask设置文 ...
- IPC$概念及入侵方式研究
catalogue . 什么是IPC$ . IPC$攻击方式 . 漏洞检测与防御措施 1. 什么是IPC$ IPC$(空会话连接)是windows系统内置的一个功能模块,它的作用有很多(包括域帐号枚举 ...
随机推荐
- Northwind数据库表字段介绍
① Categories:种类表 相应字段: CategoryID :类型ID: CategoryName:类型名; Description:类型说明; Picture:产品样本 ② Customer ...
- JavaScript 继承方式详解
js继承的概念 js里常用的如下两种继承方式: 原型链继承(对象间的继承)类式继承(构造函数间的继承) 由于js不像java那样是真正面向对象的语言,js是基于对象的,它没有类的概念.所以,要想实现继 ...
- mvn安装jar文件到本地
mvn install:install-file -DgroupId=com.jfinal -DartifactId=jfinal -Dversion=2.3 -Dpackaging=jar -Dfi ...
- c# 解决IIS写Excel的权限问题
c# 解决IIS写Excel的权限问题 from: http://www.jb51.net/article/31473.htm 发布:mdxy-dxy 字体:[增加 减小] 类型:转载 使用以上方法必 ...
- HDU1963Investment(DP)
简单DP,题解见代码
- HDU1151Air Raid(二分图的最大匹配)
题目大意: 有一个城镇,它的所有街道都是单行的,并且每条街道都是和两个路口相连.同时已知街道不会形成回路. 你的任务是编写程序求最小数量的伞兵,这些伞兵可以访问(visit)所有的路口.
- Lotus 迁移到Exchange POC 之 新建2007 服务器!
我们登录到Exchange 2007 服务器,由于需要对AD进行扩展,我们首先必须完成架构扩展,由于默认没有ldifde工具,所以我们需要执行servermanagercmd –I rsat-adds ...
- SharePoint 2013 自定义翻页显示列表项
项目需求:自定义开发一个能分页显示列表项的小部件,允许左右翻页,能根据用户权限来显示管理链接等. 效果如下: 技术要求:使用sharepoint rest API 来获取列表项,这样性能高,能够快速响 ...
- 7. 泛化(Generalization)
什么是泛化关系?用一个例子简单的说:假设A是B和C的父类,B.C具有公共类(父类)A,说明A是B.C的一般化(概括,也称泛化),B.C是A的特殊化. 在编程上,泛化关系(Generalization) ...
- 为什么要把js代码写到<!--//-->中
是为了兼容,不支持js的浏览器会把其中的内容当做html注释.