DBus:使用直接dbus API测量数据交换的时间

时间:2014-12-07 06:22:43

标签: c dbus c-api

我正在寻找一个示例c代码,用于衡量在不使用任何glib绑定的情况下在两个简单应用程序之间发送数据所花费的时间,我在很多帖子中看到http://www.matthew.ath.cx/misc/dbus有一个示例,但链接确实如此现在不存在。

我的代码发送时没有给出任何错误,但接收应用程序没有收到任何内容,也附加了service.conf文件。从其他一些网站获得此代码:

////////////////////////////Sender Code

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <dbus/dbus.h>
#define false 0
#define true 1
#define bool char

#define DERR_SHOW_CLEAN(error)                                \
    if (dbus_error_is_set(&(error)))                          \
    {                                                       \
        printf("error [%s:%d] %s\n    %s\n",                  \
               __FILE__, __LINE__, error.name, error.message);  \
        dbus_error_free(&(error));                            \
    }

int main() {
    dbus_bool_t rc;
    DBusError error;
  DBusConnection* conn;
  int ret;
  char* sigvalue = "Test";
  // initialise the errors
  dbus_error_init(&error); //TODO
  conn = dbus_connection_open("unix:path=/dev/shmem/dbus_service_socket", &error);
  DERR_SHOW_CLEAN(error);

  dbus_uint32_t serial = 0; // unique number to associate replies with requests
  DBusMessage* msg;
  DBusMessageIter args;

  rc = dbus_bus_register(conn, &error);
  DERR_SHOW_CLEAN(error);

  // create a signal and check for errors 
  msg = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
    "org.freedesktop.DBus", // interface name of the signal
    "Test"); // name of the signal
  if (NULL == msg) 
  { 
    fprintf(stderr, "Message Null\n"); 
  }

  // append arguments onto signal
  dbus_message_iter_init_append(msg, &args);
  if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) { 
    fprintf(stderr, "Out Of Memory!\n"); 
  }
  while(1)
  {
      // send the message and flush the connection
      if (!dbus_connection_send(conn, msg, &serial)) {
        fprintf(stderr, "Out Of Memory!\n");
        break;
      }
      else
      {
          sleep(5);
      }
  }
  dbus_connection_flush(conn);

  // free the message 
  dbus_message_unref(msg);
  return 0;
}

////////////////////////////Receiver Code
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <dbus/dbus.h>
#define false 0
#define true 1
#define bool char

#define DERR_SHOW_CLEAN(error)                                \
    if (dbus_error_is_set(&(error)))                          \
    {                                                       \
        printf("error [%s:%d] %s\n    %s\n",                  \
               __FILE__, __LINE__, error.name, error.message);  \
        dbus_error_free(&(error));                            \
    }

int main()
{
    DBusError err;
    DBusConnection* conn;
    DBusMessage* msg;
    DBusMessageIter args;
    int ret;
    char* sigvalue;
    dbus_bool_t rc;
    // initialise the errors
    dbus_error_init(&err); //TODO
    conn = dbus_connection_open("unix:path=/dev/shmem/dbus_service_socket", &err);
    DERR_SHOW_CLEAN(err);
    rc = dbus_bus_register(conn, &err);
    DERR_SHOW_CLEAN(err);

  while (true) {
        // non blocking read of the next available message
        dbus_connection_read_write(conn, 0);
        msg = dbus_connection_pop_message(conn);
        printf("Got Si3\n");
        // loop again if we haven't read a message
        if (NULL == msg) {
            sleep(5);
            continue;
        }

        // check if the message is a signal from the correct interface
        // and with the correct name
        if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "Test")) {
            // read the parameters
            printf("Got Si4\n");
            if (!dbus_message_iter_init(msg, &args))
                fprintf(stderr, "Message has no arguments!\n");
            else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
                fprintf(stderr, "Argument is not string!\n");
            else {
                dbus_message_iter_get_basic(&args, &sigvalue);
                printf("Got Signal with value %s\n", sigvalue);
            }
        }
        else
        {
            printf("Got Signal with type %s\n", dbus_message_get_sender(msg));
        }
        // free the message
        dbus_message_unref(msg);
   }

}


/////service.conf file contents which is used while dbus-daemon starts
<!-- This configuration file controls the systemwide message bus.
     Add a system-local.conf and edit that rather than changing this 
     file directly. -->

<!-- Note that there are any number of ways you can hose yourself
     security-wise by screwing up this file; in particular, you
     probably don't want to listen on any more addresses, add any more
     auth mechanisms, run as a different user, etc. -->

<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>

  <!-- Our well-known bus type, do not change this -->
  <type>system</type>

  <!-- Run as special user -->
  <user>root</user>

  <!-- Fork into daemon mode -->
  <fork/>

  <!-- Write a pid file -->
  <pidfile>/dev/shmem/dbus_service.pid</pidfile>

  <!-- Enable logging to syslog -->
  <syslog/>

  <!-- allow everyone -->
  <allow_anonymous />

  <!-- Only allow socket-credentials-based authentication -->
  <!-- <auth>EXTERNAL</auth>-->

  <!-- Only listen on a local socket. (abstract=/path/to/socket 
       means use abstract namespace, don't really create filesystem 
       file; only Linux supports this. Use path=/whatever on other 
       systems.) -->
  <listen>unix:path=/dev/shmem/dbus_service_socket</listen>

   <policy context="default">
    <!-- All users can connect to system bus -->
    <allow user="*"/>
    <allow group="*"/>
    <allow own="*"/>

    <!-- Signals and reply messages (method returns, errors) are allowed
         by default -->
    <allow send_type="signal"/>
    <allow send_type="method_call" log="true"/>
    <allow send_requested_reply="true" send_type="method_return"/>
    <allow send_requested_reply="true" send_type="error"/>

    <!-- All messages may be received by default -->
    <allow receive_type="method_call"/>
    <allow receive_type="method_return"/>
    <allow receive_type="error"/>
    <allow receive_type="signal"/>

    <allow receive_interface="org.freedesktop.DBus.Introspectable"/>
    <allow send_destination="*"/>
    <allow receive_sender="*" />
    <allow receive_path="/org/freedesktop/DBus"/>

    <!-- Allow anyone to talk to the message bus -->
    <allow send_destination="org.freedesktop.DBus"/>
  </policy>

  <!-- Config files are placed here that among other things, punch 
       holes in the above policy for specific services. -->
  <includedir>service.d</includedir>    
</busconfig>

1 个答案:

答案 0 :(得分:1)

在接收主函数中输入while循环之前调用dbus_bus_add_match,它可以正常工作 参考示例:在https://github.com/Jeshwanth/dbus-1/blob/master/dbus-1/dbus-example.c