DBUS的 hello 消息

DBUS connection 在建立之前,需要给 dbus daemon 发一个 hello消息, 以便获得 connection 的 unique name.

下面是 DBUS specification 里的一段话:

Each connection has at least one name, assigned at connection time and returned in response to theorg.freedesktop.DBus.Hello method call. This automatically-assigned name is called the connection'sunique name. Unique names are never reused for two different connections to the same bus.

org.freedesktop.DBus.Hello

As a method:

            STRING Hello ()
          

Reply arguments:

ArgumentTypeDescription
0STRINGUnique name assigned to the connection

Before an application is able to send messages to other applications it must send theorg.freedesktop.DBus.Hello message to the message bus to obtain a unique name. If an application without a unique name tries to send a message to another application, or a message to the message bus itself that isn't theorg.freedesktop.DBus.Hello message, it will be disconnected from the bus.

hello 消息和相应的处理函数定义在 dbus/driver.c 里:

static const MessageHandler dbus_message_handlers[] = {
  { "Hello",
    "",
    DBUS_TYPE_STRING_AS_STRING,
    bus_driver_handle_hello },
typedef struct
{
  const char *name;
  const char *in_args;
  const char *out_args;
  dbus_bool_t (* handler) (DBusConnection *connection,
                           BusTransaction *transaction,
                           DBusMessage    *message,
                           DBusError      *error);
} MessageHandler;
static dbus_bool_t
bus_driver_handle_hello (DBusConnection *connection,
                         BusTransaction *transaction,
                         DBusMessage    *message,
                         DBusError      *error)
{

调用 bus_driver_handle_hello的 stack 如下:

(gdb) where
#0  bus_driver_handle_hello (connection=0x813dbc0, transaction=0x81462e8, message=0x813de30, error=0xbfffeaa0)
    at /home/charles/code/dbus/bus/driver.c:300
#1  0x080797b1 in bus_driver_handle_message (connection=0x813dbc0, transaction=0x81462e8, message=0x813de30, 
    error=0xbfffeaa0) at /home/charles/code/dbus/bus/driver.c:2038
#2  0x0806df77 in bus_dispatch (connection=0x813dbc0, message=0x813de30) at /home/charles/code/dbus/bus/dispatch.c:284
#3  0x0806e302 in bus_dispatch_message_filter (connection=0x813dbc0, message=0x813de30, user_data=0x0)
    at /home/charles/code/dbus/bus/dispatch.c:407
#4  0x08092672 in dbus_connection_dispatch (connection=0x813dbc0) at /home/charles/code/dbus/dbus/dbus-connection.c:4677
#5  0x080de738 in _dbus_loop_dispatch (loop=0x8137320) at /home/charles/code/dbus/dbus/dbus-mainloop.c:533
#6  0x080deef4 in _dbus_loop_iterate (loop=0x8137320, block=1) at /home/charles/code/dbus/dbus/dbus-mainloop.c:859
#7  0x080defb9 in _dbus_loop_run (loop=0x8137320) at /home/charles/code/dbus/dbus/dbus-mainloop.c:885
#8  0x080872b8 in main (argc=3, argv=0xbffff004) at /home/charles/code/dbus/bus/main.c:647

和dbus daemon 连接的stack如下:

(gdb) where
#0  dbus_bus_register (connection=0x804e8d0, error=0xbfffefa0) at /home/charles/code/dbus/dbus/dbus-bus.c:684
#1  0xb7f48ed4 in internal_bus_get (type=DBUS_BUS_SESSION, private=0, error=0xbfffefa0)
    at /home/charles/code/dbus/dbus/dbus-bus.c:483
#2  0xb7f49041 in dbus_bus_get (type=DBUS_BUS_SESSION, error=0xbfffefa0) at /home/charles/code/dbus/dbus/dbus-bus.c:561
#3  0x08049946 in main (argc=1, argv=0xbffff054) at /home/charles/code/dbus/tools/dbus-monitor.c:341

dbus_bus_register()里会发送 hello 消息:

  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
                                          DBUS_PATH_DBUS,
                                          DBUS_INTERFACE_DBUS,
                                          "Hello");

在  peer-to-peer通信里面, hello 消息是不需要发送的。GDBUS里面, g_bus_get()会发送 hello消息,但是g_dbus_connection_new()只有在G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION被设置的时候踩会发送 hello 消息。

在 libdbus里面,dbus_connection_open()不会发送 hello 消息,dbus_bus_get()会发送。所以,我们在 dbus monitor的代码里能看到下面的代码:

  if (address != NULL)
    {   
      connection = dbus_connection_open (address, &error);
      if (connection)
        {
          if (!dbus_bus_register (connection, &error))
            {
              fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
                       address, error.message);
              dbus_error_free (&error);
              exit (1);
            }
        }
    }   
  else
    connection = dbus_bus_get (type, &error);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DBus是一种用于进程间通信的消息总线系统而Java是一种面向对象的编程语言。在Java中,可以使用DBus-Java库来实现与DBus的通信。 DBus-Java是一个Java库,它提供了与DBus进行通信的API。使用DBus-Java,可以在Java应用程序中创建DBus服务和客户端,并通过DBus进行消息传递。 要使用DBus-Java进行通信,首先需要在Java应用程序中引入DBus-Java库。然后,可以使用DBusConnection类来连接到DBus,并通过DBusConnection发送和接收消息。 在DBus中,每个对象都有一个唯一的对象路径和一个接口名称。通过DBusConnection的方法,可以发送消息到指定的对象路径和接口,并接收来自其他对象的消息。 以下是一个简单的示例代码,演示了如何使用DBus-Java进行通信: ```java import org.freedesktop.dbus.DBusConnection; import org.freedesktop.dbus.exceptions.DBusException; public class DBusJavaExample { public static void main(String[] args) { try { // 连接到DBus DBusConnection connection = DBusConnection.getConnection(DBusConnection.SYSTEM); // 发送消息 String destination = "org.example.service"; String path = "/org/example/object"; String iface = "org.example.interface"; String method = "HelloWorld"; String message = "Hello from Java!"; connection.callMethod(destination, path, iface, method, message); // 接收消息 connection.addSigHandler(iface, method, (String msg) -> { System.out.println("Received message: " + msg); }); // 断开与DBus的连接 connection.disconnect(); } catch (DBusException e) { e.printStackTrace(); } } } ``` 上述示例代码中,首先通过DBusConnection.getConnection()方法连接到DBus。然后,使用connection.callMethod()方法发送消息到指定的对象路径和接口。接着,使用connection.addSigHandler()方法注册一个信号处理器,用于接收来自其他对象的消息。最后,使用connection.disconnect()方法断开与DBus的连接。 这只是一个简单的示例,DBus-Java还提供了更多的功能和API,可以根据具体需求进行使用和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值