AllJoyn Overview
Feb. 2012
E-mail: zhuangwf2007@https://www.doczj.com/doc/b59448002.html,
AllJoyn是什么?
?2011年2月9日发布,由QuiC(高通创新中心)开发维护的开源软件项目,采用Apache license 2.0 许可协议
?AllJoyn名字的由来:A ll to Jo in in the fun
?面向移动设备的secure, ad hoc, proximity-based P2P通信框架?提供简单的API,跨平台、设备无关,支持多种编程语言,易于集成到现有应用中
–目前支持的OS:Linux, Android, WinXP, Win7
–目前支持的编程语言:C++, Java (对JavaScript的支持正在开发中)
?支持各种短距离无线通信技术,目前支持WiFi 和Bluetooth,下一步会支持WiFi-Direct,将来基于高通FlashLinq 技术可扩展到1km 范围内P2P 应用
?官方网站:
–https://https://www.doczj.com/doc/b59448002.html,
–https://https://www.doczj.com/doc/b59448002.html,/develop/mobile-technologies/peer-peer-alljoyn
AllJoyn的特点
?真正的P2P通信,无需3G网,无需访问云端,无需服务器–Simple device and service discovery
–Security framework for authenticated and encrypted communications per application/service
–Managed networking and message routing
–Object-oriented programming model
?针对移动嵌入式环境而优化
–Low latency
–Header compression
–Reliable and unreliable transport
–Point-to-multipoint communications
?主要应用场景
–secure file sharing
–Multi-player gaming
–Social media sharing
–Multi-user productivity tools
AllJoyn与其他类似技术的区别
?提供了完整全面的P2P 解决方案,解决了移动设备间
P2P 通信中存在的很多问题:设备发现、配对、消息路由、安全、与底层传输技术无关,等等
?针对移动设备专门优化
?支持认证和加密的安全机制
?为开发者提供了简单易用而功能强大的API
AllJoyn官方文档?Documentation
–Introduction to AllJoyn [PDF]
–AllJoyn Android Environment Setup Guide [PDF]
–Guide to AllJoyn Development Using the Java SDK [PDF]–AllJoyn Android C++ Sample Programs Walkthrough [PDF]
?API Reference
–C++ API Reference Manual
–Java API Reference Manual
–C++ API changes between 2.2.0 and 2.3.0 [TEXT]
–Java API changes between 2.2.0 and 2.3.0 [TEXT]
?Build Environment
–Configuring the Build Environment (Linux Platform) [PDF]–Configuring the Build Environment (Windows Platform) [PDF]
AllJoyn官方技术博文
?What is AllJoyn? How can I use it?
https://https://www.doczj.com/doc/b59448002.html,/blog/what-alljoyn-how-can-i-use-it
?Key Concepts of AllJoyn
https://https://www.doczj.com/doc/b59448002.html,/blog/key-concepts-alljoyn
?Developing Peer-to-Peer Apps with AllJoyn –Let Us Do the Heavy Lifting
https://https://www.doczj.com/doc/b59448002.html,/blog/developing-peer-peer-apps-alljoyn-%E2%80%93-let-us-do-heavy-lifting ?Making It Easy for Devices to Connect –The AllJoyn Peer-to-Peer Architecture
https://https://www.doczj.com/doc/b59448002.html,/blog/making-it-easy-devices-connect-%E2%80%93-alljoyn-peer-peer-architecture ?Making It Easy for Devices to Connect –AllJoyn Code Snippet Walk-Through
https://https://www.doczj.com/doc/b59448002.html,/blog/making-it-easy-devices-connect-%E2%80%93-alljoyn-code-snippet-walk-through ?Making It Easy for Devices to Connect –The AllJoyn Security Framework
https://https://www.doczj.com/doc/b59448002.html,/blog/making-it-easy-devices-connect-%E2%80%93-alljoyn-security-framework ?Chinese Developers Showcase Apps Using AllJoyn https://https://www.doczj.com/doc/b59448002.html,/blog/chinese-developers-showcase-apps-using-alljoyn
AllJoyn FAQ
?https://https://www.doczj.com/doc/b59448002.html,/develop/mobile-technologies/peer-peer-alljoyn/faq
?https://https://www.doczj.com/doc/b59448002.html,/forums/developers/faqs
?总线(Bus)
–实现P2P通信的基础
–AllJoyn 的底层协议类似于D-Bus,相当于是跨设备分布式的D-Bus
?总线附件(Bus Attachment)
–每一个连接到总线上的Alljoyn应用程序被称为总线附件,可用C++或Java编写
–每个总线附件有独一的名称(unique name),当每次连接到总线时自动分配
–每个总线附件可以有一个易读的名称(well-known name),用于标识服务,例如“org.alljoyn.bus.addressbook”
?总线接口(Bus Interfaces)
–类似于Java中的接口,定义了方法、信号处理函数和属性
–所有总线方法(Bus Methods)可使用简单或复杂的数据类型(数组、结构等)作为参数和返回值
?总线对象(Bus Objects)
–用于实现总线接口,每个总线对象实现一个或多个总线接口
–处理远程方法调用(Remote Method Calls)和发出信号(signals)、消息(messages)
–总线对象通过总线附件注册到总线上
–每个总线对象有一个类似于文件路径的路径名,例如/org/AllJoyn/Games/chess,用于远程方法调用?代理总线对象(Proxy Bus Object)
–一旦总线附件之间建立连接,应用程序创建一个代理总线对象,实现远程方法调用(Remote Method Calls)并准备好从总线上接收信号(signals)
–远程方法调用是同步的,信号则是异步的且可以一对多(广播)或一对一
?service 和client
–P2P 应用中提供服务的一方称为service,使用服务的一方称为client
–一个应用程序可以同时是service 和client(例如chat)
?session
–在宣告服务后,service 需创建一个或多个session,client 发现服务后加入session
–每个session 有一个session port,类似于socket 通信中的port
–session 的两种情形:
?point-to-point session:两个peer 之间交互
?multipoint session:多个peer 加入同一个session,组成一个group
?AllJoyn daemon——实现P2P通信的核心
–daemon通过进程间通信(IPC)与应用程序通信,应用程序只与daemon打交道–daemon提供一个抽象层处理所有的网络传输、消息路由、命名空间管理等
–整个AllJoyn系统相当于一个虚拟总线,连接多个AllJoyn daemons和总线附件
–daemon是用C++编写的native程序,运行在不同操作系统上的daemons可实现互联–在应用程序启动之前必须先启动daemon
?AllJoyn daemon 在Android 上的三种实现形式–第一种:Android app(AllJoyn.apk)
?只支持WiFi/TCP 传输
?无需root 权限
–第二种:纯C++编写的可执行程序,在adb shell 下运行(alljoyn-daemon)?若使用WiFi 传输无需root 权限
?若使用Bluetooth 传输需root 权限,且蓝牙协议栈限定用bluez
?可在init.rc 中自动加载
–第三种:与应用程序捆绑在一起(bundle),不需要单独启动daemon ?适用于发布基于AllJoyn开发的应用程序
?设备发现和建立连接的过程
–注册(Register): 连接在总线上的对象为自身进行注册
–宣告(Advertise): 连接在总线上的对象通过IP组播(multicast)宣告自身的存在–发现(Discover): 发现其他对象的存在
?在使用蓝牙时借助蓝牙本身的SDP 进行发现
?设备连接过程示意图:
?设备之间通信的方式
–远程方法调用(同步)
–Signal(异步)
–Raw session(直接socket 通信,应用程序可选择基于TCP 或UDP)
简单应用程序代码示例——echo 总线接口定义:
import org.alljoyn.bus.BusException;
import org.alljoyn.bus.annotation.BusInterface;
import org.alljoyn.bus.annotation.BusMethod;
/*
* This interface implements a simple echo method that returns the same string * that is receives.
*/
@BusInterface
public interface EchoInterface {
/*
* Echo a string.
*
* inStr is the string to be echoed by the service, returns the echoed string.
*/
@BusMethod(signature="s", replySignature="s")
public String Echo(String inStr) throws BusException;
}
Service端:
public class Service implements SimpleInterface, BusObject {
public static void main(String[] args) {
/* Create a bus connection and connect to the bus */
BusAttachment bus = new BusAttachment(Service.class.getName());
bus.connect();
/* Register the service */
Service service = new Service();
bus.registerBusObject(service, "/myobject");
/* Request a well-known name */
try{
bus.RequestName("org.alljoyn.echo", REQUEST_NAME_NO_FLAGS);
} catch(BusException ex) {
return;
}
/* Echo until told to stop */
while(!stop) { Thread.sleep(10000); }
}
/* Implementation of the echo method */
public String Echo(String inStr) {
return inStr;
}
}
Client端:
public class Client {
public static void main(String[] args) {
/* Create a bus connection and connect to the bus */
BusAttachment bus = new BusAttachment(Client.class.getName());
bus.connect();
/* Get a remote object */
Class[] ifaces = { EchoInterface.class };
ProxyBusObject proxyObj = bus.getProxyBusObject("org.alljoyn.echo", "/myobject", ifaces);
SimpleInterface proxy = proxyObj.getInterface(EchoInterface.class);
/* Call the ping method on the remote object */
try{
String ret = proxy.Echo("Hello World");
System.out.println(“Echo returned: " + ret);
} catch(BusException ex) {
return;
}
}
}
AllJoyn 应用程序典型API 调用流程示意
registerBusListener
registerBusObject
connect
bindSessionPort
(callback: acceptSessionJoiner,
sessionJoined)
requestName
advertiseName
service
端:
registerBusListener
(callback:foundAdvertisedName,
lostAdvertisedName)
connect
findAdvertisedName
Remote Method Calls
joinSession
(callback: sessionLost)client 端:
getProxyBusObject
AllJoyn的安全机制
?AllJoyn的安全模型——认证和加密(Authentication and encryption)
?认证和加解密是基于应用程序实现的,总线只负责传输和路由而并不参与认证和加解密
?每个应用程序维护各自的key store并采用密码保护
?AllJoyn支持3种安全机制,基于Transport Layer Security (TLS)协议,应用开发者根据应用程序的类型和所传输数据的种类来选择3种机制之一:
–PIN-code
–logon
–certificate-based
?安全和非安全应用程序的差别:
–前者在定义总线接口时需使用@Secure注释符,后者无
–前者需定义一个class实现AuthListener 接口,在该类中实现两个方法:requested 和completed
AllJoyn的安全机制(续)安全机制工作原理:
加入安全机制的应用程序代码示例总线接口定义:
import org.alljoyn.bus.BusException;
import org.alljoyn.bus.annotation.BusInterface;
import org.alljoyn.bus.annotation.BusMethod;
import org.alljoyn.bus.annotation.Secure; @BusInterface(name =
"org.alljoyn.bus.samples.secure.SecureInterface")
/*
* The @Secure annotation marks requests any method calls on this interface to first authenticate * then encrypt the data between the AllJoyn peers.
*/
@Secure
public interface SecureInterface {
@BusMethod
String Ping(String inStr) throws BusException;
}
加入安全机制的应用程序代码示例(续)Service端:
......
/*
* The main differences between a secure application and a plain application, besides the
* @Secure annotations of the interfaces, are encapsulated in the AuthListener. The
* BusAttachment calls the listener with various authentication requests in the process of
* authenticating a peer. The requests made are dependent on the specific authentication
* mechanism negotiated between the peers.
*
* This class, registered with the BusAttachment, supports all the available authentication
* mechanisms.
*/
class AuthListeners implements AuthListener{
/*
* Authentication requests are being made. Contained in this call are the mechanism in use,
* the number of attempts made so far, the desired user name for the requests, and the
* specific credentials being requested in the form of AuthRequests.
*
* A true return value tells the BusAttachment that the requests have been handled.
*
* This simply defers to the specific listener based on the mechanism in use.
*/
public boolean requested(String mechanism, String peer, int count, String userName, AuthRequest[] requests) { ....
....
}
/*
* An authentication attempt has completed, either successfully or unsuccessfully.
* This simply defers to the specific listener based on the mechanism in use.
*/
public void completed(String mechanism, String authPeer, boolean authenticated) {
....
....
}
}
......