DBus笔记
- 包含2套总线:system bus,service bus,权限不同,system bus需要kernel或root权限
- dbus daemon进程提供服务,
有systemd启动dbus daemon,为system bus和service bus分别启动一个daemon。 - native object:收发消息的应用需要向dbus注册object(一个或多个),object包括接口、消息类型等定义。
- object path:类似文件路径名,也可用域名等,例如:/org/kde/kspread/sheets/3/cells/4/5
- object有2种成员:method和signal(广播,有data payload)
- interface:dbus支持interface,带namespace,如:org.freedesktop.Introspectable。
在不同的语言的实现中(不同语言对dbus的封装)有不同的对应机制,如java interface或C++ pure virtual class。 proxy object:
手动处理method call:创建message,发送msg,等待reply,处理reply。
使用proxy object:上述步骤会被封装,类似调用本地对象的方法。// without proxy object
Message message = new Message(“/remote/object/path”, “MethodName”, arg1, arg2);
Connection connection = getBusConnection();
connection.send(message);
Message reply = connection.waitForReply(message);
if (reply.isError()) {
} else {
Object returnValue = reply.getReturnValue();
}
// with proxy object
Proxy proxy = new Proxy(getBusConnection(), "/remote/object/path");
Object returnValue = proxy.MethodName(arg1, arg2);
- unique connection name:(类似于ip地址)
1)app连接bus daemon会分配一个连接名。
2)在daemon声明周期内连接名不会重用,一个名字只会对应一个app(一个name只能被一个应用own)。
3)以冒号开头,例如“:34-908”,数字没有含义,只保证唯一性。 - well-known name(类似于域名)
除了unique connection name,应用可申请well-known name。
例如,com.mycompany.TextEditor,对应object path: /com/mycompany/TextFileManager - well-known name用途
1)message routing,通过well known name找到接手着
2)track life-cycle,application exit/crash notification,当app退出时,message bus通知其他应用对应的app name失去owner。
3)single instance app:通过well-known name检测服务是否已经启动 - dbus中server和client仅在创建连接时有不同,建立连接后通信时对称的(双向通信,不区分client,server)。
- dbus daemon的监听地址
1)UNIX domain socket: unxi:path=/tmp/socket_name
2)tcp/ip socket
3)默认daemon从环境变量中获取监听地址:
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
4)特例,不使用dbus daemon,需要明确server app和client app,以及它们的地址。
不是总线机制,一对一通信。 一个方法的调用链路
Address -> [Bus Name] -> Path -> Interface -> Method
1)[Bus Name],可选,如果没有使用dbus daemon就不需要bus name,相当于点对点通信。
2)interface可选,如果没有interface,则不允许在一个对象上有重命名方法;使用interface,不同的interface中允许有重名方法。
3)Address和Bus Name定位到application;path定位到object(带包名限定对象名,FQDN),提供服务的对象;interface.method定位到具体的执行方法上面的调用链路,相当于:
application . package-name.object . interface . method
4种message:
1)method call message
2)method return message
3)error message by invoking a method
4)signal message / event message,广播,单向,一对多
消息分为2类:
1)方法调用,前3种消息,调用和返回双向,一一对应。
2)广播消息,第4中,单向,一发多收。
- 函数调用对应各种类型的消息。
- 消息格式:
1)header,包括多个field。
2)body,包括多个argument。 - 内部检查接口:org.freedesktop.DBus.Introspectable
只有一个方法:Introspect():返回一个xml串,描述对象的接口、方法和信号等信息。 - 数据量限制:一次最大发送32K。
还没有评论,来说两句吧...