DBus基础
- 概述
D-Bus 是针对桌面环境优化的 IPC(interprocess communication )机制,用于进程间的通信或进程与内核的通信。最基本的 D-Bus 协议是一对一的通信协议。但在很多情况下,通信的一方是消息总线。消息总线是一个特殊的应用,它同时与多个应用通信,并在应用之间传递消息。消息总线的角色有点类似于X 系统中的窗口管理器,窗口管理器既是 X 客户,又负责管理窗口。
支持 DBUS的系统都有两个标准的消息总线:系统总线和会话总线。系统总线用于系统与应用的通信,会话总线用于应用之间的通信。 - 使用qt建立qtdbus步骤
1)建立连接
QDBusConnection connection = QDBusConnection::sessionBus();
2)在session上注册服务
connection.registerService("com.kylin.test");
这里注册了com.kylin.test的服务
3)注册对象名为/test/object的对象,把类Object所有槽函数导出为object的method
TestServer testServer(60);
connection.registerObject("/test/objects", &testServer,QDBusConnection::ExportAllSlots);
registerObject(const QString &path, const QString &interface, QObject *object, RegisterOptions options = ExportAdaptors);
testserver.h
#ifndef TESTSERVER_H
#define TESTSERVER_H
#include <QObject>
class TestServer : public QObject
{
Q_OBJECT
//定义Interface名称为com.kylin.test.value
Q_CLASSINFO("D-BUS Interfacce","com.kylin.test.value")
public:
TestServer(int value);
public slots:
int maxValue(int a,int b);
int minValue(int a,int b);
int value(int a);
private:
int m_value;
signals:
};
#endif // TESTSERVER_H
testserver.cpp
#include "testserver.h"
TestServer::TestServer(int value)
{
m_value = value;
}
int TestServer::maxValue(int a,int b)
{
//return 100;
if(a>b)
return a;
else
return b;
}
int TestServer::minValue(int a,int b)
{
//return 0;
if(a<b)
return a;
else
return b;
}
int TestServer::value(int a)
{
return a;
}
main.cpp
#include "widget.h"
#include <QApplication>
#include <QDBusConnection>
#include <QDBusError>
#include <QDBusMessage>
#include <QDebug>
#include "testserver.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
// w.show();
//建立到session bus的连接
QDBusConnection connection = QDBusConnection::sessionBus();
//在session bus上注册名为com.scorpio.test的服务
if(!connection.registerService("com.kylin.test"))
{
qDebug() << "error:" << connection.lastError().message() <<endl;
exit(-1);
}
TestServer testServer(60);
//注册名为/test/objects的对象,把类Object所有槽函数导出为object的method
connection.registerObject("/test/objects", &testServer,QDBusConnection::ExportAllSlots);
return a.exec();
}
- 客户端通过dbus使用该服务的方法
1) 创建QDBusInterface接口
QDBusInterface interface("com.kylin.test", "/test/objects",
"local.TestDBus.TestServer",
QDBusConnection::sessionBus());
2)调用远程的value方法
QDBusReply<int> reply = interface.call("value");
4.客户端使用QDBusMessage调用远端方法
QCoreApplication a(argc, argv);
//构造一个method_call消息,服务名称为:com.kylin.test,对象路径为:/test/objects
//接口名称为com.kylin.test.value,method名称为value
QDBusMessage message = QDBusMessage::createMethodCall(
"com.kylin.test",
"/test/objects",
"local.TestDBus.TestServer",
"maxValue");
//设置参数
QList<QVariant> args;
args.append(111);
args.append(212);
message.setArguments(args);
//发送消息
QDBusMessage response = QDBusConnection::sessionBus().call(message);
//判断method是否被正确返回
if (response.type() == QDBusMessage::ReplyMessage)
{
//从返回参数获取返回值
int value = response.arguments().takeFirst().toInt();
qDebug() << QString("value = %1").arg(value);
}
else
{
qDebug() << "value method called failed!" << response.errorMessage()<<endl;
}
return a.exec();