开辟两个线程,一个负责发送消息,另一个负责接收.
源码如下:
main.c
#include
#include "DBus.h"
#include
#include
using namespace std;
void send()
{
DBusError error;
DBusConnection* connection;
DBusMessage * msg;
DBusMessageIter iter;
dbus_uint32_t serial =0;
int ret;
int64_t count = 0;
//1.建立连接
dbus_error_init(&error);
connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
if(dbus_error_is_set(&error)){
fprintf(stderr, "Connection Error (%s)\n", error.message);
dbus_error_free(&error);
}
if(connection == NULL)
{
fprintf(stderr,"connection == NULL\n");
return ;
}
//2.注册well-known公共名 用户独占名称
ret = dbus_bus_request_name(connection,"test.singal.source",DBUS_NAME_FLAG_REPLACE_EXISTING,&error);
if(dbus_error_is_set(&error)){
fprintf(stderr,"Name Err :%s\n",error.message);
dbus_error_free(&error);
}
if(ret !=DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
fprintf(stderr,"OWNER REPEAT\n");
return ;
}
while(1)
{
//3.发送信号
//构造表示信号发射的新消息
if((msg =dbus_message_new_signal("/test/signal/Object","test.signal.Type","Test"))== NULL){
fprintf(stderr,"MessageNULL\n");
return ;
}
//给这个信号(messge)具体的内容
char * sigvalue = "hello world";
dbus_message_iter_init_append(msg,&iter);
if(!dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&sigvalue)){
fprintf(stderr,"Out OfMemory!\n");
return ;
}
if( !dbus_connection_send(connection,msg,&serial)){
fprintf(stderr,"Out of Memory!\n");
return ;
}
dbus_connection_flush(connection);
//printf("Signal Send\n");
cout << "Send msg:" << ++count << endl;
sleep(2);
}
//步骤5: 释放相关的分配的内存。
dbus_message_unref(msg );
}
void listen()
{
while(1)
{
DBusMessage * msg;
DBusMessageIter iter;
DBusConnection * connection;
DBusError error;
int ret;
char * sigvalue;
int64_t count = 0;
//1.建立连接
dbus_error_init(&error);
connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
if(dbus_error_is_set(&error)){
fprintf(stderr, "Connection Error (%s)\n", error.message);
dbus_error_free(&error);
}
if(connection == NULL)
{
fprintf(stderr,"connection == NULL\n");
return ;
}
//2.注册well-known公共名 用户独占名称
ret = dbus_bus_request_name(connection,"test.singal.dest",DBUS_NAME_FLAG_REPLACE_EXISTING,&error);
if(dbus_error_is_set(&error)){
fprintf(stderr,"Name Err :%s\n",error.message);
dbus_error_free(&error);
}
if(ret !=DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
fprintf(stderr,"OWNER REPEAT\n");
return ;
}
//步骤3:通知D-Bus daemon,希望监听来行接口test.signal.Type的信号
dbus_bus_add_match(connection,"type='signal',interface='test.signal.Type'",&error);
//实际需要发送东西给daemon来通知希望监听的内容,所以需要flush
dbus_connection_flush(connection);
if(dbus_error_is_set(&error)){
fprintf(stderr,"Match Error%s\n",error.message);
dbus_error_free(&error);
}
//步骤4:在循环中监听,每隔开1秒,就去试图自己的连接中获取这个信号。这里给出的是中连接中获取任何消息的方式,所以获取后去检查一下这个消息是否我们期望的信号,并获取内容。我们也可以通过这个方式来获取method call消息。
while(1){
dbus_connection_read_write(connection,0);//这里会浪费cpu资源
msg =dbus_connection_pop_message (connection);
if(msg == NULL){
//cout << "Idle state" << endl;
sleep(1);
continue;
}
if(dbus_message_is_signal(msg,"test.signal.Type","Test")){
if(!dbus_message_iter_init(msg,&iter))
fprintf(stderr,"MessageHas no Param");
else if(dbus_message_iter_get_arg_type(&iter)!= DBUS_TYPE_STRING)
cout << "Param is not string" << endl;
else
dbus_message_iter_get_basic(&iter,&sigvalue);
//printf("Got Singal withvalue : %s\n",sigvalue);
cout << "Receive msg:" << ++count << endl;
}
dbus_message_unref(msg);
}
}
}
int main() {
std::thread th1(send);
std::thread th2(listen);
th1.join();
th2.join();
return 0;
}
头文件DBus.h
#ifndef DBUS_TEST_DBUS_H
#define DBUS_TEST_DBUS_H
#include
#include
#include
class DBus {
};
#endif //DBUS_TEST_DBUS_H