从ObServer发出的选择性D-BUS信号(单播信号) [英] selective D-BUS signal emitting from ObServer (unicast signal)

查看:135
本文介绍了从ObServer发出的选择性D-BUS信号(单播信号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是我只有一个ObServer对象和一组客户端. ObServer和客户端通过D-BUS(IPC)连接. ObServer具有适用于所有客户端的通用接口.但是,ObServer有时需要将某些事件通知客户端.客户端侦听ObServer通用接口信号OnNotify().

I have a situation where I have a single ObServer object and a set of Clients. ObServer and clients connected through D-BUS (IPC). ObServer has generic interface for all clients. But, time to time ObServer needs to notify clients about some events. Clients listen for a ObServer Generic interface signal OnNotify().

问题: 如何从ObServer向指定客户端发出D-BUS信号(OnNotify())(而不是向所有人广播)?

Question: How to emit D-BUS signal (OnNotify()) from ObServer to specified client (instead of broadcast to all) ?

p.s

Qt D-BUS用于包装,但欢迎使用任何方法.

Qt D-BUS used for wrapping but any approach are welcome.

推荐答案

您不能.在DBus接口上已为该信号注册的所有客户端都会接收到信号.

You can't. Signals are received by all clients that have registered for that signal on a DBus interface.

您可以在OnNotify信号中添加参数,并在客户端中对其进行处理,也可以为每个客户端创建单独的信号.但是,如果您希望该组件是动态的(在运行时添加客户端),则必须采用第一种方法(OnNotify的参数).

You can either add a parameter to the OnNotify signal, and handle it in the client, or create separate signals for each client. However, if you want this component to be dynamic (add clients at runtime), you have to go with the first approach (parameter to OnNotify).

有关信号的更多信息

信号定义如下:

DBus中的信号包含一个消息,该消息由一个进程发送到任意数量的其他进程.即,信号是单向广播.该信号可能包含自变量(数据有效负载),但是由于它是广播,因此它永远不会有返回值".将此与方法调用(请参见调用方法-幕后"一节)进行对比,其中方法调用消息具有匹配的方法回复消息.

A signal in DBus consists of a single message, sent by one process to any number of other processes. That is, a signal is a unidirectional broadcast. The signal may contain arguments (a data payload), but because it is a broadcast, it never has a "return value." Contrast this with a method call (see the section called "Calling a Method - Behind the Scenes") where the method call message has a matching method reply message.

信号的发射器(即发送者)不知道信号的接收者.收件人在总线守护程序中注册,以根据匹配规则"接收信号-这些规则通常包括发送方和信号名称.总线守护程序仅将每个信号发送给已对该信号表示兴趣的收件人.

The emitter (aka sender) of a signal has no knowledge of the signal recipients. Recipients register with the bus daemon to receive signals based on "match rules" - these rules would typically include the sender and the signal name. The bus daemon sends each signal only to recipients who have expressed interest in that signal.

原始来源.

根据德米特里(Dmitry)的评论更新了答案.

updated answer in light of Dmitry's comments.

过滤dbus信号不适用于任何当前可用的绑定(未检查所有绑定,仅检查了2个(dbus-cpp和qt),因此任何人都可以进行后续操作).

Filtering dbus signals will not work with any of the current available bindings (didn't check all of them, only 2 (dbus-cpp and qt), so anyone can follow up on this).

但是,可以使用dbus界面(dbus-message.h)中可用的功能在dbus消息的标题中设置DESTINATION字段:

However it is possible to set the DESTINATION field in the header of the dbus message, using a function that is available in the dbus interface (dbus-message.h):

dbus_bool_t dbus_message_set_destination (DBusMessage  *message, const char *destination)

对于QT绑定,您必须按以下方式修改绑定:在方法中的qdbusmessage.cpp

In case of QT bindings, you have to modify the bindings as follows: in qdbusmessage.cpp in the method

DBusMessage *QDBusMessagePrivate::toDBusMessage(const QDBusMessage &message, QDBusError *error)

在案例分支DBUS_MESSAGE_TYPE_SIGNAL上,您需要调用q_dbus_message_set_destination.

on the case branch DBUS_MESSAGE_TYPE_SIGNAL you need to make a call to q_dbus_message_set_destination.

目的地也必须在上层可用.最简单的方法是扩展QDBusMessage类以保留目标,然后将其在下面传递给dbus层.

Also the destination must be available from the upper layers. Easiest way would be to extend the QDBusMessage class in order to retain the destination, and then pass it below to the dbus layer.

如果您能够修改项目中的QT绑定,则可以进行这样的操作:).

If you are able to modify the QT bindings in your project, then you might do such a maneuver :).

这篇关于从ObServer发出的选择性D-BUS信号(单播信号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆