简介

D-Bus是一种高级的进程间通信机制,它由freedesktop.org项目提供,使用GPL许可证发行。D-Bus最主要的用途是在Linux桌面环境为进程提供通信,同时能将Linux桌面环境和Linux内核事件作为消息传递到进程。D-Bus的主要概念为总线,注册后的进程可通过总线接收或传递消息,进程也可注册后等待内核事件响应,例如等待网络状态的转变或者计算机发出关机指令。D-Bus已被大多数Linux发行版所采用,开发者可使用D-Bus实现各种复杂的进程间通信任务。

注意,dbus是一个协议,任何人只要按照这个协议就可以和其他符合协议的程序进行通信。官方实现是 libdbus,包含 dbus-monitor等调试命令程序;也还有其他版本的实现,比如systemd库的sd-bus,新版本glib库的GDBus libdbus库直接使用很晦涩难懂,所以有多基于libdbus的封装,称为 dbus binding,比如qt的QtDBus。

有些dbus实现就直接从头到尾自己实现协议,并提供api封装,和libdbus没有关系,比如目前引入的dbus-cxx库

参考资料

各种dbus binding
dbus-cxx官方网站

环境配置

参考此博文

例程

官方文档例程

例程来自官方文档例程,在此我只是把注解都添加了上去,并且添加了多个method。

dbus_cxx_server.cpp


#include <dbus-cxx.h>
#include <unistd.h>/*
We will define a function that will be our workhorse on the server side.
This function will be the actual function that will be called when the dbus add method is invoked,
so we will name our function similarly.
But, note that the actual name of the function in our program and the name in our dbus server do not have to match.
*/
double add( double param1, double param2 )      { return param1 + param2; }
double sub( double param1, double param2 )      { return param1 - param2; }
double mul( double param1, double param2 )      { return param1 * param2; }
double divv( double param1, double param2 )      { return param1 / param2; }
int main()
{//Now, we will create a dispatcher(调度) to handle incoming and outgoing messages.//Handling incoming and outgoing messages can be messy and the Dispatcher class handles much of this for us.std::shared_ptr<DBus::Dispatcher> dispatcher = DBus::StandaloneDispatcher::create();  //standalone表示独立//Now that we have a dispatcher we need to create a connection to the session bus.std::shared_ptr<DBus::Connection> conn = dispatcher->create_connection(DBus::BusType::SESSION);//Next, we need to request a name that will identify our application on the session bus.if( conn->request_name( "dbuscxx.quickstart_0.server",    DBUSCXX_NAME_FLAG_REPLACE_EXISTING ) != DBus::RequestNameResponse::PrimaryOwner )return 1;//Now that our application has a name on the bus we need to add an object with it's path.std::shared_ptr<DBus::Object> object = conn->create_object("/dbuscxx/quickstart_0",       DBus::ThreadForCalling::DispatcherThread);//We will now create a method named add for our object.//The functionality of the method will be provided by the function we declared above also named add().//We must add this to an interface,//and the D-Bus specification required that interface names must contain at least one . (period) character so we will use the interface name "dbuscxx.Quickstart".object->create_method<double(double,double)>("dbuscxx.Quickstart", "add",         sigc::ptr_fun(add));object->create_method<double(double,double)>("dbuscxx.Quickstart", "sub", sigc::ptr_fun(sub));object->create_method<double(double,double)>("dbuscxx.Quickstart", "mul", sigc::ptr_fun(mul));object->create_method<double(double,double)>("dbuscxx.Quickstart", "divv", sigc::ptr_fun(divv));  //这个名字是div的话会出现重载问题
/*sigc::ptr_fun(T_return(* func)(T_args...))Creates a functor of type sigc::pointer_functor which wraps an existing non-member function.
*/sleep(10000);return 0;
}

dbus_cxx_client.cpp


#include <dbus-cxx.h>
#include <iostream>int main()
{//Create a dispatcher to manage threads, timeouts and I/O watchesstd::shared_ptr<DBus::Dispatcher> dispatcher;dispatcher = DBus::StandaloneDispatcher::create();//Create a connection to the D-Bus session busstd::shared_ptr<DBus::Connection> connection;connection = dispatcher->create_connection( DBus::BusType::SESSION );//create an object proxy, which stands in for a real object.(stand in:替身)//a proxy exists over the dbusstd::shared_ptr<DBus::ObjectProxy> object;object = connection->create_object_proxy("dbuscxx.quickstart_0.server", "/dbuscxx/quickstart_0");//a method proxy acts like a real method, but will go over the dbus//to do its work.DBus::MethodProxy<double(double,double)>& add_proxy= *(object->create_method<double(double,double)>("dbuscxx.Quickstart","add"));DBus::MethodProxy<double(double,double)>& sub_proxy= *(object->create_method<double(double,double)>("dbuscxx.Quickstart","sub"));DBus::MethodProxy<double(double,double)>& mul_proxy= *(object->create_method<double(double,double)>("dbuscxx.Quickstart","mul"));DBus::MethodProxy<double(double,double)>& div_proxy= *(object->create_method<double(double,double)>("dbuscxx.Quickstart","divv"));double answer;answer = add_proxy( 1.1, 2.2 );std::cout << "1.1 + 2.2 = " << answer << std::endl;answer = sub_proxy( 1.1, 2.2 );std::cout << "1.1 - 2.2 = " << answer << std::endl;answer = mul_proxy( 12300.1, 36.2 );std::cout << "12300.1 * 36.2 = " << answer << std::endl;answer = div_proxy( 9.1, 3.2 );std::cout << "9.1 / 3.2 = " << answer << std::endl;return 0;
}

Makefile


.PHONY:clean
LD_LIBRARY_PATH:= /home/minshilzm/dbux-cxx/dbus-cxx-2.3.0/build:/home/minshilzm/libsigc++-3.0.0/__install/libCXXFLAG         = -std=c++17 -O3
PKGFLAG         = `pkg-config --cflags --libs dbus-cxx-2.0`SERVER_SRC      = dbus_cxx_server.cpp
CLIENT_SRC      = dbus_cxx_client.cppSERVER_TARGET   = dbus_cxx_server
CLIENT_TARGET   = dbus_cxx_clientall: $(SERVER_TARGET) $(CLIENT_TARGET)$(SERVER_TARGET):$(SERVER_SRC)g++ $(CXXFLAG)  $(SERVER_SRC) -o $(SERVER_TARGET) $(PKGFLAG)
$(CLIENT_TARGET):$(CLIENT_SRC)g++ $(CXXFLAG) $(CLIENT_SRC) -o $(CLIENT_TARGET) $(PKGFLAG)
clean:rm $(SERVER_TARGET) $(CLIENT_TARGET)

make完之后要在环境变量配置动态库路径,我用的是bash,所以有:
(1)vi ~/.bashrc
(2)添加
export LD_LIBRARY_PATH=/home/minshilzm/dbux-cxx/dbus-cxx-2.3.0/build:/home/minshilzm/libsigc+±3.0.0/__install/lib:$LD_LIBRARY_PATH

export PKG_CONFIG_PATH=/home/minshilzm/libsigc+±3.0.0/__install/lib/pkgconfig:$PKG_CONFIG_PATH

运行结果

最后这个div咋回事还在找原因…

【dbux-cxx】简介及例程相关推荐

  1. 【fmt】fmt简介及例程

    简介 {fmt}是一个开源的文本格式库,用以替代C的stdio和C++的iostreams. {fmt}是在benchmarked排行中最快的方法, 比printf还快20%. Speed tests ...

  2. 从零开始学C++之标准库类型(一):string 类简介和例程

    一.标准库string类型 string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作 ,在VC中直接F1查看 template <class Ch ...

  3. Halcon例程(基于GMM模型的分类)详解 —— classify_citrus_fruits.hdev

    一.例程简介 该例程比较有代表性,属于Halcon里的分类方法之一,直接调用Halcon封装好的GMM分类器(高斯混合模型)对橘子和柠檬进行分类.GMM属于概率分类方法,属于P(Y|X),通过对样本的 ...

  4. C51单片机-串行口2-蓝牙模块-应用例程

    一.例程简介 本例程51单片机与蓝牙模块连接,可通过蓝牙模块接收和发送字符串,从而控制测试灯的亮灭.其中使用51单片机的串行口2的工作方式1,即8位UART,波特率可变.波特率设为9600.缺省UAR ...

  5. Oracle后台进程

    后台进程简介 启动例程时,Oracle不仅会分配SGA,还会启动后台进程:关闭例程时,Oracle不仅会释放SGA所占用的内存空间,而且还会释放后台进程所占用的Cpu和内存资源.Oracle提供了很多 ...

  6. STM32CubeMx开发之路—使用SysTick实现微妙延时函数delay_us()

    运行环境 Windows10 STM32CubeMX Version 5.4.0 Keil5(MDK5) Version 5.28.0.0 硬件开发板 F103RB-NUCLEO 简介 本例程主要讲解 ...

  7. STM32CubeMx开发之路—LTDC驱动STM32F429I-Discover上的显示屏

    附件 源码已放到码云 ! ! ! ( 请点击文首链接进入仓库 ) 运行环境 Windows10 STM32CubeMX Version 5.4.0 Keil5(MDK5) Version 5.28.0 ...

  8. arduino 多个超声波模块HC-SR04 Newping.h库的使用——摆脱万恶的阻塞等待

    目录 前言(一点废话) 问题的发现 Newping.h库 简介 安装 例程1:定时器示例TimerExample 例程2:超声波模块的读取NewPingExample 例程3:中断读取超声波NewPi ...

  9. 复旦微FM33LE0x单片机之多通道ADC DMA

    一.简介   本例程以FM33LE026为例,已经过验证,其他型号不保证适用.   工欲善其事必先利其器,官方手册就是利器,在接触新开发平台时,仔细阅读手册能大幅提升开发进度,避免很多掉头发事件,光头 ...

最新文章

  1. 东大19春在线作业计算机应用基础,东大19春学期《计算机应用基础》在线作业2...
  2. SystemTap工具的使用基础
  3. 一个高效的内存池实现
  4. C语言 字符串和指针
  5. 前松后紧和前紧后松——想起PM的点滴
  6. HDFS、MR、Kafka、Storm、Spark、Hbase、Redis原理图
  7. 第一篇JavaScript基础
  8. Flutter根据偏移量转换角度 Offset 的使用实例
  9. 02 | 日志系统:一条SQL更新语句是如何执行的? 笔记(转)
  10. PHP框架 CI与TP之MVC比较
  11. ASP.NET AJAX入门系列(7):使用客户端脚本对UpdateProgress编程
  12. 买基金你们都亏了多少钱?
  13. Android怎样实现毛玻璃效果之Android高级模糊技术
  14. linux 安装萍方字体,苹方字体大全-苹果苹方字体全套打包下载【windows完整免费版】-西西软件下载...
  15. html5shiv_深入探讨:HTML5 Shiv和Polyfills
  16. Mac 使用 扫描 仪
  17. 引用 好文共赏:hao123站长李兴平的成功史
  18. Unity-黑暗之魂复刻-角色攻击
  19. Java 在PPT中添加文本水印的简易方法(单一/平铺水印)
  20. 【Love2d从青铜到王者】第十三篇:Love2d之游戏:射击敌人(Game: Shoot the enemy)

热门文章

  1. 前馈神经网络_BP算法+R语言程序运行实例
  2. Pony.ai成立AI研究所
  3. 马云说今年他要读100本书,你呢?
  4. 开放式创新的未来:趋势与预测
  5. window.parent
  6. 新年贺词新年祝福短信
  7. 1104 Sum of Number Segments
  8. hibernate-jpa中criteriaBuilde[or]用法
  9. Python中读取索引图像
  10. 在华为云服务器安装Anaconda搭建环境体验深度学习——你值得了解一下