3

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().

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

p.s

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

Dmitry
  • 906
  • 1
  • 13
  • 32

1 Answers1

3

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

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).

EDIT: more information on signals

A signal is defined as follows:

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.

Original source.

EDIT: updated answer in light of Dmitry's comments.

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).

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)

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)

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

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.

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

Community
  • 1
  • 1
Alexandru C.
  • 3,337
  • 1
  • 25
  • 27
  • In case of parameter in OnNotify I need to handle it in each client. For example, I waiting for OnNotify for some file in file system (this functionality provides ObServer), but, each client wants to receive notification only for specified directory. In this approach each client receive all notification and need to filter it regarding specified directory. Not convenient approach for performance reason. – Dmitry Jun 14 '12 at 04:39
  • 2
    Revision 0.18 http://dbus.freedesktop.org/doc/dbus-specification.html Applications may send unicast messages to a specific recipient or to the message bus itself, or broadcast messages to all interested recipients. See the section called “Message Bus Message Routing” for details. – Dmitry Jun 14 '12 at 07:31
  • Oh, didn't know that. This is interesting. I wonder if the bindings support such a thing. You're using QT bindings, right? – Alexandru C. Jun 14 '12 at 07:43
  • Yes, I'm using Qt and searching for possibility to use unicast signal in it or patch it ;) – Dmitry Jun 14 '12 at 07:45
  • 1
    Ok, I did a little research on the QT bindings and it is possible to do this. Just that you have to modify the source code. Thanks for the hint in the specification, I didn't read that part, or it didn't stick to me. I can now modify my own bindings to include this nifty feature :). – Alexandru C. Jun 14 '12 at 09:47
  • 2
    Thank you for this help and investigation, I will come back to this question after some day and report about results. – Dmitry Jun 14 '12 at 09:57