All pastes #151313 Raw Edit

Anonymous

public cpp v1 · immutable
#151313 ·published 2006-08-27 22:45 UTC
rendered paste body
#define DBUS_API_SUBJECT_TO_CHANGE#include <dbus/dbus.h>#include <cstdio>#include <cstdlib>static DBusConnection* conn;static const char* service = "vandenoever.strigi";static const char* interface = "vandenoever.strigi";static const char* object = "/search";static DBusHandlerResultmessage_function (DBusConnection  *connection,                  DBusMessage     *message,                  void            *user_data);static const DBusObjectPathVTable vtable = {    NULL,    message_function,    NULL};voidreply_to_method_call(DBusMessage* msg, DBusConnection* conn) {   DBusMessage* reply;   DBusMessageIter args;   bool stat = true;   dbus_uint32_t level = 21614;   dbus_uint32_t serial = 0;   char* param = "";   // read the arguments   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, &param);   printf("Method called with %s\n", param);   // create a reply from the message   reply = dbus_message_new_method_return(msg);   // add the arguments to the reply   dbus_message_iter_init_append(reply, &args);   if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &stat)) {      fprintf(stderr, "Out Of Memory!\n");      exit(1);   }   if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &level)) {      fprintf(stderr, "Out Of Memory!\n");      exit(1);   }   // send the reply && flush the connection   if (!dbus_connection_send(conn, reply, &serial)) {      fprintf(stderr, "Out Of Memory!\n");      exit(1);   }   dbus_connection_flush(conn);   // free the reply   dbus_message_unref(reply);}static DBusHandlerResultmessage_function (DBusConnection  *connection,                  DBusMessage     *msg,                  void            *user_data){  if (dbus_message_is_method_call(msg, "test.method.Type", "Method")) {      reply_to_method_call(msg, conn);      return DBUS_HANDLER_RESULT_HANDLED;  }  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;}/** * Server that exposes a method call and waits for it to be called */void listen(){   DBusMessage* msg;   DBusMessage* reply;   DBusMessageIter args;   DBusError err;   int ret;   char* param;   printf("Listening for method calls\n");   // initialise the error   dbus_error_init(&err);   // connect to the bus and check for errors   conn = dbus_bus_get(DBUS_BUS_SESSION, &err);   if (dbus_error_is_set(&err)) {      fprintf(stderr, "Connection Error (%s)\n", err.message);      dbus_error_free(&err);   }   if (NULL == conn) {      fprintf(stderr, "Connection Null\n");      exit(1);   }   // request our name on the bus and check for errors   ret = dbus_bus_request_name(conn, service,      DBUS_NAME_FLAG_REPLACE_EXISTING , &err);   if (dbus_error_is_set(&err)) {      fprintf(stderr, "Name Error (%s)\n", err.message);      dbus_error_free(&err);   }   if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {      fprintf(stderr, "Not Primary Owner (%d)\n", ret);      exit(1);   }   // register the introspection interface   // TODO   dbus_connection_register_object_path (conn, object, &vtable, NULL);    // loop, testing for new messages   while (true) {      // non blocking read of the next available message      dbus_connection_read_write_dispatch(conn, 1);      dbus_connection_flush(conn);   }   // close the connection   dbus_connection_close(conn);}intmain(int argc, char** argv) {    listen();    return 0;}