dbus基础-qt版

DBus基础

  1. 概述
    D-Bus 是针对桌面环境优化的 IPC(interprocess communication )机制,用于进程间的通信或进程与内核的通信。最基本的 D-Bus 协议是一对一的通信协议。但在很多情况下,通信的一方是消息总线。消息总线是一个特殊的应用,它同时与多个应用通信,并在应用之间传递消息。消息总线的角色有点类似于X 系统中的窗口管理器,窗口管理器既是 X 客户,又负责管理窗口。
    支持 DBUS的系统都有两个标准的消息总线:系统总线会话总线系统总线用于系统与应用的通信,会话总线用于应用之间的通信。
  2. 使用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();
}

  1. 客户端通过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();
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值