**
发送Method call消息的场景
**
一个method call消息从进程A到进程B,B将应答一个method return消息或者error消息。在每个call消息带有一个序列号,应答消息也包含同样的号码,使之可以对应起来。他们的处理过程如下:
如果提供proxy,通过触发本地一个对象的方法从而触发另一个进程的远端对象的方法。应用调用proxy的一个方法,proxy构造一个method call消息发送到远端进程。
对于底层的API,不使用proxy,应用需要自己构造method call消息。
一个method call消息包含:远端进程的bus name,方法名字,方法的参数,远端进程中object path,可选的接口名字。
method call消息发送到bus daemon
bus daemon查看目的地的bus name。如果一个进程对应这个名字,bus daemon将method call消息发送到该进程中。如果没有发现匹配,bus daemon创建一个error消息作为应答返回。
进程接收后将method call消息分拆。对于简单的底层API情况,将立即执行方法,并发送一个method reply消息给bus daemon。对于高层的API,将检查对象path,interface和method,触发一个native object的方法,并将返回值封装在一个method reply消息中。
bus daemon收到method reply消息,将其转发到原来的进程中进程查看method reply消息,获取返回值。这个响应也可以标识一个error的残生。当使用高级的捆绑,method reply消息将转换为proxy方法的返回值或者一个exception。
Bus daemon保证message的顺序,不会乱序。例如我们发送两个method call消息到同一个接受方,他们将按顺序接受。接收方并不要求一定按顺序回复。消息有一个序列号了匹配收发消息。
发送Signal的场景
signal是个广播的消息,不需要响应,接收方向daemon注册匹配的条件,包括发送方和信号名,bus守护只将信号发送给希望接受的进程。处理流程如下:
一个signal消息发送到bus daemon。
signal消息包含发布该信号的interface名字,signal的名字,进程的bus名字,以及参数。
任何进程都可以注册的匹配条件(match rules)表明它所感兴趣的signal。总线有个注册match rules列表。
bus daemon检查那些进程对该信号有兴趣,将信号消息发送到这些进程中。
收到信号的进程决定如何处理。如果使用高层的捆绑,一个porxy对象将会十分一个native的信号。如果使用底层的API,进程需要检查信号的发送发和信号的名字决定如果进行处理。
发送信号客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus.h>
#include <unistd.h>
int send_a_signal( char * sigvalue)
{
DBusError err;
DBusConnection * connection;
DBusMessage * msg;
DBusMessageIter arg;
dbus_uint32_t serial =0;
int ret;
//步骤1:建立与D-Bus后台的连接
dbus_error_init(&err);
connection =dbus_bus_get(DBUS_BUS_SESSION ,&err );
if(dbus_error_is_set(&err)){
fprintf(stderr,"ConnectionErr : %s\n",err.message);
dbus_error_free(&err);
}
if(connection == NULL)
return -1;
//步骤2:给连接名分配一个well-known的名字作为Bus name,这个步骤不是必须的,可以用if 0来注释着一段代码,我们可以用这个名字来检查,是否已经开启了这个应用的另外的进程。
#if 1
ret =dbus_bus_request_name(connection,"test.singal.source",DBUS_NAME_FLAG_REPLACE_EXISTING,&err);
if(dbus_error_is_set(&err)){
fprintf(stderr,"Name Err :%s\n",err.message);
dbus_error_free(&err);
}
if(ret !=DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
return -1;
#endif
//步骤3:发送一个信号
//根据图,我们给出这个信号的路径(即可以指向对象),接口,以及信号名,创建一个Message
if((msg =dbus_message_new_signal("/test/signal/Object","test.signal.Type","Test"))== NULL){
fprintf(stderr,"MessageNULL\n");
return -1;
}
//给这个信号(messge)具体的内容
dbus_message_iter_init_append(msg,&arg);
if(!dbus_message_iter_append_basic(&arg,DBUS_TYPE_STRING,&sigvalue)){
fprintf(stderr,"Out OfMemory!\n");
return -1;
}
//步骤4: 将信号从连接中发送
if( !dbus_connection_send(connection,msg,&serial)){
fprintf(stderr,"Out of Memory!\n");
return -1;
}
dbus_connection_flush(connection);
printf("Signal Send\n");
//步骤5: 释放相关的分配的内存。
dbus_message_unref(msg );
return 0;
}
int main( int argc , char ** argv){
send_a_signal("Hello,world!");
return 0;
}
接受信号服务端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus.h>
#include <unistd.h>
void listen_signal()
{
DBusMessage * msg;
DBusMessageIter arg;
DBusConnection * connection;
DBusError err;
int ret;
char * sigvalue;
//步骤1:建立与D-Bus后台的连接
dbus_error_init(&err);
connection =dbus_bus_get(DBUS_BUS_SESSION, &err);
if(dbus_error_is_set(&err)){
fprintf(stderr,"ConnectionError %s\n",err.message);
dbus_error_free(&err);
}
if(connection == NULL)
return;
//步骤2:给连接名分配一个可记忆名字test.singal.dest作为Bus name,这个步骤不是必须的,但推荐这样处理
ret =dbus_bus_request_name(connection,"test.singal.dest",DBUS_NAME_FLAG_REPLACE_EXISTING,&err);
if(dbus_error_is_set(&err)){
fprintf(stderr,"Name Error%s\n",err.message);
dbus_error_free(&err);
}
if(ret !=DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
return;
//步骤3:通知D-Bus daemon,希望监听来行接口test.signal.Type的信号
dbus_bus_add_match(connection,"type='signal',interface='test.signal.Type'",&err);
//实际需要发送东西给daemon来通知希望监听的内容,所以需要flush
dbus_connection_flush(connection);
if(dbus_error_is_set(&err)){
fprintf(stderr,"Match Error%s\n",err.message);
dbus_error_free(&err);
}
//步骤4:在循环中监听,每隔开1秒,就去试图自己的连接中获取这个信号。这里给出的是中连接中获取任何消息的方式,所以获取后去检查一下这个消息是否我们期望的信号,并获取内容。我们也可以通过这个方式来获取method call消息。
while(1){
dbus_connection_read_write(connection,0);
msg =dbus_connection_pop_message (connection);
if(msg == NULL){
sleep(1);
continue;
}
if(dbus_message_is_signal(msg,"test.signal.Type","Test")){
if(!dbus_message_iter_init(msg,&arg))
fprintf(stderr,"MessageHas no Param");
else if(dbus_message_iter_get_arg_type(&arg)!= DBUS_TYPE_STRING)
g_printerr("Param isnot string");
else
dbus_message_iter_get_basic(&arg,&sigvalue);
printf("Got Singal withvalue : %s\n",sigvalue);
}
dbus_message_unref(msg);
}//End of while
}
int main( int argc , char ** argv){
listen_signal();
return 0;
}
method发送客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus.h>
#include <unistd.h>
//建立与session D-Bus daemo的连接,并设定连接的名字,相关的代码已经多次使用过了
DBusConnection * connect_dbus(){
DBusError err;
DBusConnection * connection;
int ret;
//Step 1: connecting session bus
dbus_error_init(&err);
connection =dbus_bus_get(DBUS_BUS_SESSION, &err);
if(dbus_error_is_set(&err)){
fprintf(stderr,"ConnectionErr : %s\n",err.message);
dbus_error_free(&err);
}
if(connection == NULL)
return NULL;
//step 2: 设置BUS name,也即连接的名字。
ret =dbus_bus_request_name(connection,"test.wei.source",DBUS_NAME_FLAG_REPLACE_EXISTING,&err);
if(dbus_error_is_set(&err)){
fprintf(stderr,"Name Err :%s\n",err.message);
dbus_error_free(&err);
}
if(ret !=DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
return NULL;
return connection;
}
void send_a_method_call(DBusConnection * connection,char * param)
{
DBusError err;
DBusMessage * msg;
DBusMessageIter arg;
DBusPendingCall * pending;
dbus_bool_t * stat;
dbus_uint32_t * level;
dbus_error_init(&err);
//针对目的地地址,请参考图,创建一个method call消息。Constructs a new message to invoke a method on a remote object.
msg =dbus_message_new_method_call ("test.wei.dest","/test/method/Object","test.method.Type","Method");
if(msg == NULL){
g_printerr("MessageNULL");
return;
}
//为消息添加参数。Appendarguments
dbus_message_iter_init_append(msg, &arg);
if(!dbus_message_iter_append_basic(&arg, DBUS_TYPE_STRING,¶m)){
g_printerr("Out of Memory!");
exit(1);
}
//发送消息并获得reply的handle。Queues amessage to send, as withdbus_connection_send() , but also returns aDBusPendingCall used to receive a reply to the message.
if(!dbus_connection_send_with_reply (connection, msg,&pending, -1)){
g_printerr("Out of Memory!");
exit(1);
}
if(pending == NULL){
g_printerr("Pending CallNULL: connection is disconnected ");
dbus_message_unref(msg);
return;
}
dbus_connection_flush(connection);
dbus_message_unref(msg);
//waiting a reply,在发送的时候,已经获取了methodreply的handle,类型为DBusPendingCall。
// block until we recieve a reply, Block until the pendingcall is completed.
dbus_pending_call_block (pending);
//get the reply message,Gets thereply, or returns NULL if none has been received yet.
msg =dbus_pending_call_steal_reply (pending);
if (msg == NULL) {
fprintf(stderr, "ReplyNull\n");
exit(1);
}
// free the pendingmessage handle
dbus_pending_call_unref(pending);
// read the parameters
if(!dbus_message_iter_init(msg, &arg))
fprintf(stderr, "Message hasno arguments!\n");
else if (dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_BOOLEAN)
fprintf(stderr, "Argument isnot boolean!\n");
else
dbus_message_iter_get_basic(&arg, &stat);
if (!dbus_message_iter_next(&arg))
fprintf(stderr, "Message hastoo few arguments!\n");
else if (dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_UINT32 )
fprintf(stderr, "Argument isnot int!\n");
else
dbus_message_iter_get_basic(&arg, &level);
printf("Got Reply: %d,%d\n", stat, level);
dbus_message_unref(msg);
}
int main( int argc , char ** argv){
DBusConnection * connection;
connection = connect_dbus();
if(connection == NULL)
return -1;
send_a_method_call(connection,"Hello, D-Bus");
return 0;
}
method接受服务端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus.h>
#include <unistd.h>
void reply_to_method_call(DBusMessage * msg, DBusConnection * conn){
DBusMessage * reply;
DBusMessageIter arg;
char * param = NULL;
dbus_bool_t stat = TRUE;
dbus_uint32_t level = 2010;
dbus_uint32_t serial = 0;
//从msg中读取参数,这个在上一次学习中学过
if(!dbus_message_iter_init(msg,&arg))
printf("Message has noargs\n");
else if(dbus_message_iter_get_arg_type(&arg)!= DBUS_TYPE_STRING)
printf("Arg is notstring!\n");
else
dbus_message_iter_get_basic(&arg,& param);
if(param == NULL) return;
//创建返回消息reply
reply = dbus_message_new_method_return(msg);
//在返回消息中填入两个参数,和信号加入参数的方式是一样的。这次我们将加入两个参数。
dbus_message_iter_init_append(reply,&arg);
if(!dbus_message_iter_append_basic(&arg,DBUS_TYPE_BOOLEAN,&stat)){
printf("Out ofMemory!\n");
exit(1);
}
if(!dbus_message_iter_append_basic(&arg,DBUS_TYPE_UINT32,&level)){
printf("Out ofMemory!\n");
exit(1);
}
//发送返回消息
if( !dbus_connection_send(conn, reply,&serial)){
printf("Out of Memory\n");
exit(1);
}
dbus_connection_flush (conn);
dbus_message_unref (reply);
}
void listen_dbus()
{
DBusMessage * msg;
DBusMessageIter arg;
DBusConnection * connection;
DBusError err;
int ret;
char * sigvalue;
dbus_error_init(&err);
//创建于session D-Bus的连接
connection =dbus_bus_get(DBUS_BUS_SESSION, &err);
if(dbus_error_is_set(&err)){
fprintf(stderr,"ConnectionError %s\n",err.message);
dbus_error_free(&err);
}
if(connection == NULL)
return;
//设置一个BUS name:test.wei.dest
ret =dbus_bus_request_name(connection,"test.wei.dest",DBUS_NAME_FLAG_REPLACE_EXISTING,&err);
if(dbus_error_is_set(&err)){
fprintf(stderr,"Name Error%s\n",err.message);
dbus_error_free(&err);
}
if(ret !=DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
return;
//要求监听某个singal:来自接口test.signal.Type的信号
dbus_bus_add_match(connection,"type='signal',interface='test.signal.Type'",&err);
dbus_connection_flush(connection);
if(dbus_error_is_set(&err)){
fprintf(stderr,"Match Error%s\n",err.message);
dbus_error_free(&err);
}
while(true){
dbus_connection_read_write(connection,0);
msg =dbus_connection_pop_message (connection);
if(msg == NULL){
sleep(1);
continue;
}
if(dbus_message_is_signal(msg,"test.signal.Type","Test")){
if(!dbus_message_iter_init(msg,&arg))
fprintf(stderr,"Message Has no Param");
elseif(dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_STRING)
g_printerr("Param isnot string");
else
dbus_message_iter_get_basic(&arg,&sigvalue);
}else if(dbus_message_is_method_call(msg,"test.method.Type","Method")){
//我们这里面先比较了接口名字和方法名字,实际上应当现比较路径
if(strcmp(dbus_message_get_path(msg),"/test/method/Object") == NULL)
reply_to_method_call(msg,connection);
}
dbus_message_unref(msg);
}
}
int main( int argc , char ** argv){
listen_dbus();
return 0;
}
转载:
https://blog.csdn.net/eastmoon502136/article/details/10044993