Logo Search packages:      
Sourcecode: libnih version File versions  Download package

test_node_proxy_functions_no_properties.c

DBusPendingCall *
my_test_poke (NihDBusProxy *      proxy,
              uint32_t            address,
              const char *        value,
              MyTestPokeReply     handler,
              NihDBusErrorHandler error_handler,
              void *              data,
              int                 timeout)
{
      DBusMessage *       method_call;
      DBusMessageIter     iter;
      DBusPendingCall *   pending_call;
      NihDBusPendingData *pending_data;

      nih_assert (proxy != NULL);
      nih_assert (value != NULL);
      nih_assert ((handler == NULL) || (error_handler != NULL));

      /* Construct the method call message. */
      method_call = dbus_message_new_method_call (proxy->name, proxy->path, "com.netsplit.Nih.Test", "Poke");
      if (! method_call)
            nih_return_no_memory_error (NULL);

      dbus_message_set_auto_start (method_call, proxy->auto_start);

      dbus_message_iter_init_append (method_call, &iter);

      /* Marshal a uint32_t onto the message */
      if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &address)) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (NULL);
      }

      /* Marshal a char * onto the message */
      if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &value)) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (NULL);
      }

      /* Handle a fire-and-forget message */
      if (! error_handler) {
            dbus_message_set_no_reply (method_call, TRUE);
            if (! dbus_connection_send (proxy->connection, method_call, NULL)) {
                  dbus_message_unref (method_call);
                  nih_return_no_memory_error (NULL);
            }

            dbus_message_unref (method_call);
            return (DBusPendingCall *)TRUE;
      }

      /* Send the message and set up the reply notification. */
      pending_data = nih_dbus_pending_data_new (NULL, proxy->connection,
                                                (NihDBusReplyHandler)handler,
                                                error_handler, data);
      if (! pending_data) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (NULL);
      }

      pending_call = NULL;
      if (! dbus_connection_send_with_reply (proxy->connection, method_call,
                                             &pending_call, timeout)) {
            dbus_message_unref (method_call);
            nih_free (pending_data);
            nih_return_no_memory_error (NULL);
      }

      dbus_message_unref (method_call);

      if (! pending_call) {
            nih_dbus_error_raise (DBUS_ERROR_DISCONNECTED,
                                  "Connection is closed");
            nih_free (pending_data);
            return NULL;
      }

      NIH_MUST (dbus_pending_call_set_notify (pending_call, (DBusPendingCallNotifyFunction)my_com_netsplit_Nih_Test_Poke_notify,
                                              pending_data, (DBusFreeFunction)nih_discard));

      return pending_call;
}

static void
my_com_netsplit_Nih_Test_Poke_notify (DBusPendingCall *   pending_call,
                                      NihDBusPendingData *pending_data)
{
      DBusMessage *   reply;
      DBusMessageIter iter;
      NihDBusMessage *message;
      DBusError       error;

      nih_assert (pending_call != NULL);
      nih_assert (pending_data != NULL);

      nih_assert (dbus_pending_call_get_completed (pending_call));

      /* Steal the reply from the pending call. */
      reply = dbus_pending_call_steal_reply (pending_call);
      nih_assert (reply != NULL);

      /* Handle error replies */
      if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR) {
            message = NIH_MUST (nih_dbus_message_new (pending_data, pending_data->connection, reply));

            dbus_error_init (&error);
            dbus_set_error_from_message (&error, message->message);

            nih_error_push_context ();
            nih_dbus_error_raise (error.name, error.message);
            pending_data->error_handler (pending_data->data, message);
            nih_error_pop_context ();

            dbus_error_free (&error);
            nih_free (message);
            dbus_message_unref (reply);
            return;
      }

      nih_assert (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN);

      do {
            __label__ enomem;

            /* Create a message context for the reply, and iterate
             * over its arguments.
             */
            message = nih_dbus_message_new (pending_data, pending_data->connection, reply);
            if (! message)
                  goto enomem;

            dbus_message_iter_init (message->message, &iter);

            if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
                  nih_error_push_context ();
                  nih_error_raise (NIH_DBUS_INVALID_ARGS,
                                   _(NIH_DBUS_INVALID_ARGS_STR));
                  pending_data->error_handler (pending_data->data, message);
                  nih_error_pop_context ();

                  nih_free (message);
                  dbus_message_unref (reply);
                  return;
            }

      enomem: __attribute__ ((unused));
      } while (! message);

      /* Call the handler function */
      if (pending_data->handler) {
            nih_error_push_context ();
            ((MyTestPokeReply)pending_data->handler) (pending_data->data, message);
            nih_error_pop_context ();
      }

      nih_free (message);
      dbus_message_unref (reply);
}

int
my_test_poke_sync (const void *  parent,
                   NihDBusProxy *proxy,
                   uint32_t      address,
                   const char *  value)
{
      DBusMessage *   method_call;
      DBusMessageIter iter;
      DBusError       error;
      DBusMessage *   reply;

      nih_assert (proxy != NULL);
      nih_assert (value != NULL);

      /* Construct the method call message. */
      method_call = dbus_message_new_method_call (proxy->name, proxy->path, "com.netsplit.Nih.Test", "Poke");
      if (! method_call)
            nih_return_no_memory_error (-1);

      dbus_message_set_auto_start (method_call, proxy->auto_start);

      dbus_message_iter_init_append (method_call, &iter);

      /* Marshal a uint32_t onto the message */
      if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &address)) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (-1);
      }

      /* Marshal a char * onto the message */
      if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &value)) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (-1);
      }

      /* Send the message, and wait for the reply. */
      dbus_error_init (&error);

      reply = dbus_connection_send_with_reply_and_block (proxy->connection, method_call, -1, &error);
      if (! reply) {
            dbus_message_unref (method_call);

            if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) {
                  nih_error_raise_no_memory ();
            } else {
                  nih_dbus_error_raise (error.name, error.message);
            }

            dbus_error_free (&error);
            return -1;
      }

      dbus_message_unref (method_call);

      /* Iterate the arguments of the reply */
      dbus_message_iter_init (reply, &iter);

      if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
            dbus_message_unref (reply);
            nih_return_error (-1, NIH_DBUS_INVALID_ARGS,
                              _(NIH_DBUS_INVALID_ARGS_STR));
      }

      dbus_message_unref (reply);

      return 0;
}


DBusPendingCall *
my_test_peek (NihDBusProxy *      proxy,
              uint32_t            address,
              MyTestPeekReply     handler,
              NihDBusErrorHandler error_handler,
              void *              data,
              int                 timeout)
{
      DBusMessage *       method_call;
      DBusMessageIter     iter;
      DBusPendingCall *   pending_call;
      NihDBusPendingData *pending_data;

      nih_assert (proxy != NULL);
      nih_assert ((handler == NULL) || (error_handler != NULL));

      /* Construct the method call message. */
      method_call = dbus_message_new_method_call (proxy->name, proxy->path, "com.netsplit.Nih.Test", "Peek");
      if (! method_call)
            nih_return_no_memory_error (NULL);

      dbus_message_set_auto_start (method_call, proxy->auto_start);

      dbus_message_iter_init_append (method_call, &iter);

      /* Marshal a uint32_t onto the message */
      if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &address)) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (NULL);
      }

      /* Handle a fire-and-forget message */
      if (! error_handler) {
            dbus_message_set_no_reply (method_call, TRUE);
            if (! dbus_connection_send (proxy->connection, method_call, NULL)) {
                  dbus_message_unref (method_call);
                  nih_return_no_memory_error (NULL);
            }

            dbus_message_unref (method_call);
            return (DBusPendingCall *)TRUE;
      }

      /* Send the message and set up the reply notification. */
      pending_data = nih_dbus_pending_data_new (NULL, proxy->connection,
                                                (NihDBusReplyHandler)handler,
                                                error_handler, data);
      if (! pending_data) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (NULL);
      }

      pending_call = NULL;
      if (! dbus_connection_send_with_reply (proxy->connection, method_call,
                                             &pending_call, timeout)) {
            dbus_message_unref (method_call);
            nih_free (pending_data);
            nih_return_no_memory_error (NULL);
      }

      dbus_message_unref (method_call);

      if (! pending_call) {
            nih_dbus_error_raise (DBUS_ERROR_DISCONNECTED,
                                  "Connection is closed");
            nih_free (pending_data);
            return NULL;
      }

      NIH_MUST (dbus_pending_call_set_notify (pending_call, (DBusPendingCallNotifyFunction)my_com_netsplit_Nih_Test_Peek_notify,
                                              pending_data, (DBusFreeFunction)nih_discard));

      return pending_call;
}

static void
my_com_netsplit_Nih_Test_Peek_notify (DBusPendingCall *   pending_call,
                                      NihDBusPendingData *pending_data)
{
      DBusMessage *   reply;
      DBusMessageIter iter;
      NihDBusMessage *message;
      DBusError       error;
      char *          value;
      const char *    value_dbus;

      nih_assert (pending_call != NULL);
      nih_assert (pending_data != NULL);

      nih_assert (dbus_pending_call_get_completed (pending_call));

      /* Steal the reply from the pending call. */
      reply = dbus_pending_call_steal_reply (pending_call);
      nih_assert (reply != NULL);

      /* Handle error replies */
      if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR) {
            message = NIH_MUST (nih_dbus_message_new (pending_data, pending_data->connection, reply));

            dbus_error_init (&error);
            dbus_set_error_from_message (&error, message->message);

            nih_error_push_context ();
            nih_dbus_error_raise (error.name, error.message);
            pending_data->error_handler (pending_data->data, message);
            nih_error_pop_context ();

            dbus_error_free (&error);
            nih_free (message);
            dbus_message_unref (reply);
            return;
      }

      nih_assert (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN);

      do {
            __label__ enomem;

            /* Create a message context for the reply, and iterate
             * over its arguments.
             */
            message = nih_dbus_message_new (pending_data, pending_data->connection, reply);
            if (! message)
                  goto enomem;

            dbus_message_iter_init (message->message, &iter);

            /* Demarshal a char * from the message */
            if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING) {
                  nih_error_push_context ();
                  nih_error_raise (NIH_DBUS_INVALID_ARGS,
                                   _(NIH_DBUS_INVALID_ARGS_STR));
                  pending_data->error_handler (pending_data->data, message);
                  nih_error_pop_context ();

                  nih_free (message);
                  dbus_message_unref (reply);
                  return;
            }

            dbus_message_iter_get_basic (&iter, &value_dbus);

            value = nih_strdup (message, value_dbus);
            if (! value) {
                  nih_free (message);
                  message = NULL;
                  goto enomem;
            }

            dbus_message_iter_next (&iter);

            if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
                  nih_error_push_context ();
                  nih_error_raise (NIH_DBUS_INVALID_ARGS,
                                   _(NIH_DBUS_INVALID_ARGS_STR));
                  pending_data->error_handler (pending_data->data, message);
                  nih_error_pop_context ();

                  nih_free (message);
                  dbus_message_unref (reply);
                  return;
            }

      enomem: __attribute__ ((unused));
      } while (! message);

      /* Call the handler function */
      if (pending_data->handler) {
            nih_error_push_context ();
            ((MyTestPeekReply)pending_data->handler) (pending_data->data, message, value);
            nih_error_pop_context ();
      }

      nih_free (message);
      dbus_message_unref (reply);
}

int
my_test_peek_sync (const void *  parent,
                   NihDBusProxy *proxy,
                   uint32_t      address,
                   char **       value)
{
      DBusMessage *   method_call;
      DBusMessageIter iter;
      DBusError       error;
      DBusMessage *   reply;
      char *          value_local;
      const char *    value_local_dbus;

      nih_assert (proxy != NULL);
      nih_assert (value != NULL);

      /* Construct the method call message. */
      method_call = dbus_message_new_method_call (proxy->name, proxy->path, "com.netsplit.Nih.Test", "Peek");
      if (! method_call)
            nih_return_no_memory_error (-1);

      dbus_message_set_auto_start (method_call, proxy->auto_start);

      dbus_message_iter_init_append (method_call, &iter);

      /* Marshal a uint32_t onto the message */
      if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &address)) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (-1);
      }

      /* Send the message, and wait for the reply. */
      dbus_error_init (&error);

      reply = dbus_connection_send_with_reply_and_block (proxy->connection, method_call, -1, &error);
      if (! reply) {
            dbus_message_unref (method_call);

            if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) {
                  nih_error_raise_no_memory ();
            } else {
                  nih_dbus_error_raise (error.name, error.message);
            }

            dbus_error_free (&error);
            return -1;
      }

      dbus_message_unref (method_call);

      /* Iterate the arguments of the reply */
      dbus_message_iter_init (reply, &iter);

      do {
            __label__ enomem;

            /* Demarshal a char * from the message */
            if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING) {
                  dbus_message_unref (reply);
                  nih_return_error (-1, NIH_DBUS_INVALID_ARGS,
                                    _(NIH_DBUS_INVALID_ARGS_STR));
            }

            dbus_message_iter_get_basic (&iter, &value_local_dbus);

            value_local = nih_strdup (parent, value_local_dbus);
            if (! value_local) {
                  *value = NULL;
                  goto enomem;
            }

            dbus_message_iter_next (&iter);

            *value = value_local;
      enomem: __attribute__ ((unused));
      } while (! *value);

      if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
            nih_free (value_local);
            *value = NULL;
            dbus_message_unref (reply);
            nih_return_error (-1, NIH_DBUS_INVALID_ARGS,
                              _(NIH_DBUS_INVALID_ARGS_STR));
      }

      dbus_message_unref (reply);

      return 0;
}


DBusPendingCall *
my_test_is_valid_address (NihDBusProxy *            proxy,
                          uint32_t                  address,
                          MyTestIsValidAddressReply handler,
                          NihDBusErrorHandler       error_handler,
                          void *                    data,
                          int                       timeout)
{
      DBusMessage *       method_call;
      DBusMessageIter     iter;
      DBusPendingCall *   pending_call;
      NihDBusPendingData *pending_data;

      nih_assert (proxy != NULL);
      nih_assert ((handler == NULL) || (error_handler != NULL));

      /* Construct the method call message. */
      method_call = dbus_message_new_method_call (proxy->name, proxy->path, "com.netsplit.Nih.Test", "IsValidAddress");
      if (! method_call)
            nih_return_no_memory_error (NULL);

      dbus_message_set_auto_start (method_call, proxy->auto_start);

      dbus_message_iter_init_append (method_call, &iter);

      /* Marshal a uint32_t onto the message */
      if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &address)) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (NULL);
      }

      /* Handle a fire-and-forget message */
      if (! error_handler) {
            dbus_message_set_no_reply (method_call, TRUE);
            if (! dbus_connection_send (proxy->connection, method_call, NULL)) {
                  dbus_message_unref (method_call);
                  nih_return_no_memory_error (NULL);
            }

            dbus_message_unref (method_call);
            return (DBusPendingCall *)TRUE;
      }

      /* Send the message and set up the reply notification. */
      pending_data = nih_dbus_pending_data_new (NULL, proxy->connection,
                                                (NihDBusReplyHandler)handler,
                                                error_handler, data);
      if (! pending_data) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (NULL);
      }

      pending_call = NULL;
      if (! dbus_connection_send_with_reply (proxy->connection, method_call,
                                             &pending_call, timeout)) {
            dbus_message_unref (method_call);
            nih_free (pending_data);
            nih_return_no_memory_error (NULL);
      }

      dbus_message_unref (method_call);

      if (! pending_call) {
            nih_dbus_error_raise (DBUS_ERROR_DISCONNECTED,
                                  "Connection is closed");
            nih_free (pending_data);
            return NULL;
      }

      NIH_MUST (dbus_pending_call_set_notify (pending_call, (DBusPendingCallNotifyFunction)my_com_netsplit_Nih_Test_IsValidAddress_notify,
                                              pending_data, (DBusFreeFunction)nih_discard));

      return pending_call;
}

static void
my_com_netsplit_Nih_Test_IsValidAddress_notify (DBusPendingCall *   pending_call,
                                                NihDBusPendingData *pending_data)
{
      DBusMessage *   reply;
      DBusMessageIter iter;
      NihDBusMessage *message;
      DBusError       error;

      nih_assert (pending_call != NULL);
      nih_assert (pending_data != NULL);

      nih_assert (dbus_pending_call_get_completed (pending_call));

      /* Steal the reply from the pending call. */
      reply = dbus_pending_call_steal_reply (pending_call);
      nih_assert (reply != NULL);

      /* Handle error replies */
      if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR) {
            message = NIH_MUST (nih_dbus_message_new (pending_data, pending_data->connection, reply));

            dbus_error_init (&error);
            dbus_set_error_from_message (&error, message->message);

            nih_error_push_context ();
            nih_dbus_error_raise (error.name, error.message);
            pending_data->error_handler (pending_data->data, message);
            nih_error_pop_context ();

            dbus_error_free (&error);
            nih_free (message);
            dbus_message_unref (reply);
            return;
      }

      nih_assert (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN);

      do {
            __label__ enomem;

            /* Create a message context for the reply, and iterate
             * over its arguments.
             */
            message = nih_dbus_message_new (pending_data, pending_data->connection, reply);
            if (! message)
                  goto enomem;

            dbus_message_iter_init (message->message, &iter);

            if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
                  nih_error_push_context ();
                  nih_error_raise (NIH_DBUS_INVALID_ARGS,
                                   _(NIH_DBUS_INVALID_ARGS_STR));
                  pending_data->error_handler (pending_data->data, message);
                  nih_error_pop_context ();

                  nih_free (message);
                  dbus_message_unref (reply);
                  return;
            }

      enomem: __attribute__ ((unused));
      } while (! message);

      /* Call the handler function */
      if (pending_data->handler) {
            nih_error_push_context ();
            ((MyTestIsValidAddressReply)pending_data->handler) (pending_data->data, message);
            nih_error_pop_context ();
      }

      nih_free (message);
      dbus_message_unref (reply);
}

int
my_test_is_valid_address_sync (const void *  parent,
                               NihDBusProxy *proxy,
                               uint32_t      address)
{
      DBusMessage *   method_call;
      DBusMessageIter iter;
      DBusError       error;
      DBusMessage *   reply;

      nih_assert (proxy != NULL);

      /* Construct the method call message. */
      method_call = dbus_message_new_method_call (proxy->name, proxy->path, "com.netsplit.Nih.Test", "IsValidAddress");
      if (! method_call)
            nih_return_no_memory_error (-1);

      dbus_message_set_auto_start (method_call, proxy->auto_start);

      dbus_message_iter_init_append (method_call, &iter);

      /* Marshal a uint32_t onto the message */
      if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &address)) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (-1);
      }

      /* Send the message, and wait for the reply. */
      dbus_error_init (&error);

      reply = dbus_connection_send_with_reply_and_block (proxy->connection, method_call, -1, &error);
      if (! reply) {
            dbus_message_unref (method_call);

            if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) {
                  nih_error_raise_no_memory ();
            } else {
                  nih_dbus_error_raise (error.name, error.message);
            }

            dbus_error_free (&error);
            return -1;
      }

      dbus_message_unref (method_call);

      /* Iterate the arguments of the reply */
      dbus_message_iter_init (reply, &iter);

      if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
            dbus_message_unref (reply);
            nih_return_error (-1, NIH_DBUS_INVALID_ARGS,
                              _(NIH_DBUS_INVALID_ARGS_STR));
      }

      dbus_message_unref (reply);

      return 0;
}


static DBusHandlerResult
my_com_netsplit_Nih_Test_Bounce_signal (DBusConnection *    connection,
                                        DBusMessage *       signal,
                                        NihDBusProxySignal *proxied)
{
      DBusMessageIter iter;
      NihDBusMessage *message;
      uint32_t        height;
      int32_t         velocity;

      nih_assert (connection != NULL);
      nih_assert (signal != NULL);
      nih_assert (proxied != NULL);
      nih_assert (connection == proxied->proxy->connection);

      if (! dbus_message_is_signal (signal, proxied->interface->name, proxied->signal->name))
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

      if (! dbus_message_has_path (signal, proxied->proxy->path))
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

      if (proxied->proxy->name)
            if (! dbus_message_has_sender (signal, proxied->proxy->owner))
                  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

      message = nih_dbus_message_new (NULL, connection, signal);
      if (! message)
            return DBUS_HANDLER_RESULT_NEED_MEMORY;

      /* Iterate the arguments to the signal and demarshal into arguments
       * for our own function call.
       */
      dbus_message_iter_init (message->message, &iter);

      /* Demarshal a uint32_t from the message */
      if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32) {
            nih_free (message);
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
      }

      dbus_message_iter_get_basic (&iter, &height);

      dbus_message_iter_next (&iter);

      /* Demarshal a int32_t from the message */
      if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32) {
            nih_free (message);
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
      }

      dbus_message_iter_get_basic (&iter, &velocity);

      dbus_message_iter_next (&iter);

      if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
            nih_free (message);
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
      }

      /* Call the handler function */
      nih_error_push_context ();
      ((MyTestBounceHandler)proxied->handler) (proxied->data, message, height, velocity);
      nih_error_pop_context ();
      nih_free (message);

      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}


static DBusHandlerResult
my_com_netsplit_Nih_Test_Exploded_signal (DBusConnection *    connection,
                                          DBusMessage *       signal,
                                          NihDBusProxySignal *proxied)
{
      DBusMessageIter iter;
      NihDBusMessage *message;

      nih_assert (connection != NULL);
      nih_assert (signal != NULL);
      nih_assert (proxied != NULL);
      nih_assert (connection == proxied->proxy->connection);

      if (! dbus_message_is_signal (signal, proxied->interface->name, proxied->signal->name))
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

      if (! dbus_message_has_path (signal, proxied->proxy->path))
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

      if (proxied->proxy->name)
            if (! dbus_message_has_sender (signal, proxied->proxy->owner))
                  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

      message = nih_dbus_message_new (NULL, connection, signal);
      if (! message)
            return DBUS_HANDLER_RESULT_NEED_MEMORY;

      /* Iterate the arguments to the signal and demarshal into arguments
       * for our own function call.
       */
      dbus_message_iter_init (message->message, &iter);

      if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
            nih_free (message);
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
      }

      /* Call the handler function */
      nih_error_push_context ();
      ((MyTestExplodedHandler)proxied->handler) (proxied->data, message);
      nih_error_pop_context ();
      nih_free (message);

      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}


DBusPendingCall *
my_foo_bing (NihDBusProxy *      proxy,
             MyFooBingReply      handler,
             NihDBusErrorHandler error_handler,
             void *              data,
             int                 timeout)
{
      DBusMessage *       method_call;
      DBusMessageIter     iter;
      DBusPendingCall *   pending_call;
      NihDBusPendingData *pending_data;

      nih_assert (proxy != NULL);
      nih_assert ((handler == NULL) || (error_handler != NULL));

      /* Construct the method call message. */
      method_call = dbus_message_new_method_call (proxy->name, proxy->path, "com.netsplit.Nih.Foo", "Bing");
      if (! method_call)
            nih_return_no_memory_error (NULL);

      dbus_message_set_auto_start (method_call, proxy->auto_start);

      dbus_message_iter_init_append (method_call, &iter);

      /* Handle a fire-and-forget message */
      if (! error_handler) {
            dbus_message_set_no_reply (method_call, TRUE);
            if (! dbus_connection_send (proxy->connection, method_call, NULL)) {
                  dbus_message_unref (method_call);
                  nih_return_no_memory_error (NULL);
            }

            dbus_message_unref (method_call);
            return (DBusPendingCall *)TRUE;
      }

      /* Send the message and set up the reply notification. */
      pending_data = nih_dbus_pending_data_new (NULL, proxy->connection,
                                                (NihDBusReplyHandler)handler,
                                                error_handler, data);
      if (! pending_data) {
            dbus_message_unref (method_call);
            nih_return_no_memory_error (NULL);
      }

      pending_call = NULL;
      if (! dbus_connection_send_with_reply (proxy->connection, method_call,
                                             &pending_call, timeout)) {
            dbus_message_unref (method_call);
            nih_free (pending_data);
            nih_return_no_memory_error (NULL);
      }

      dbus_message_unref (method_call);

      if (! pending_call) {
            nih_dbus_error_raise (DBUS_ERROR_DISCONNECTED,
                                  "Connection is closed");
            nih_free (pending_data);
            return NULL;
      }

      NIH_MUST (dbus_pending_call_set_notify (pending_call, (DBusPendingCallNotifyFunction)my_com_netsplit_Nih_Foo_Bing_notify,
                                              pending_data, (DBusFreeFunction)nih_discard));

      return pending_call;
}

static void
my_com_netsplit_Nih_Foo_Bing_notify (DBusPendingCall *   pending_call,
                                     NihDBusPendingData *pending_data)
{
      DBusMessage *   reply;
      DBusMessageIter iter;
      NihDBusMessage *message;
      DBusError       error;

      nih_assert (pending_call != NULL);
      nih_assert (pending_data != NULL);

      nih_assert (dbus_pending_call_get_completed (pending_call));

      /* Steal the reply from the pending call. */
      reply = dbus_pending_call_steal_reply (pending_call);
      nih_assert (reply != NULL);

      /* Handle error replies */
      if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR) {
            message = NIH_MUST (nih_dbus_message_new (pending_data, pending_data->connection, reply));

            dbus_error_init (&error);
            dbus_set_error_from_message (&error, message->message);

            nih_error_push_context ();
            nih_dbus_error_raise (error.name, error.message);
            pending_data->error_handler (pending_data->data, message);
            nih_error_pop_context ();

            dbus_error_free (&error);
            nih_free (message);
            dbus_message_unref (reply);
            return;
      }

      nih_assert (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN);

      do {
            __label__ enomem;

            /* Create a message context for the reply, and iterate
             * over its arguments.
             */
            message = nih_dbus_message_new (pending_data, pending_data->connection, reply);
            if (! message)
                  goto enomem;

            dbus_message_iter_init (message->message, &iter);

            if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
                  nih_error_push_context ();
                  nih_error_raise (NIH_DBUS_INVALID_ARGS,
                                   _(NIH_DBUS_INVALID_ARGS_STR));
                  pending_data->error_handler (pending_data->data, message);
                  nih_error_pop_context ();

                  nih_free (message);
                  dbus_message_unref (reply);
                  return;
            }

      enomem: __attribute__ ((unused));
      } while (! message);

      /* Call the handler function */
      if (pending_data->handler) {
            nih_error_push_context ();
            ((MyFooBingReply)pending_data->handler) (pending_data->data, message);
            nih_error_pop_context ();
      }

      nih_free (message);
      dbus_message_unref (reply);
}

int
my_foo_bing_sync (const void *  parent,
                  NihDBusProxy *proxy)
{
      DBusMessage *   method_call;
      DBusMessageIter iter;
      DBusError       error;
      DBusMessage *   reply;

      nih_assert (proxy != NULL);

      /* Construct the method call message. */
      method_call = dbus_message_new_method_call (proxy->name, proxy->path, "com.netsplit.Nih.Foo", "Bing");
      if (! method_call)
            nih_return_no_memory_error (-1);

      dbus_message_set_auto_start (method_call, proxy->auto_start);

      dbus_message_iter_init_append (method_call, &iter);

      /* Send the message, and wait for the reply. */
      dbus_error_init (&error);

      reply = dbus_connection_send_with_reply_and_block (proxy->connection, method_call, -1, &error);
      if (! reply) {
            dbus_message_unref (method_call);

            if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) {
                  nih_error_raise_no_memory ();
            } else {
                  nih_dbus_error_raise (error.name, error.message);
            }

            dbus_error_free (&error);
            return -1;
      }

      dbus_message_unref (method_call);

      /* Iterate the arguments of the reply */
      dbus_message_iter_init (reply, &iter);

      if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
            dbus_message_unref (reply);
            nih_return_error (-1, NIH_DBUS_INVALID_ARGS,
                              _(NIH_DBUS_INVALID_ARGS_STR));
      }

      dbus_message_unref (reply);

      return 0;
}


static DBusHandlerResult
my_com_netsplit_Nih_Foo_NewResult_signal (DBusConnection *    connection,
                                          DBusMessage *       signal,
                                          NihDBusProxySignal *proxied)
{
      DBusMessageIter iter;
      NihDBusMessage *message;

      nih_assert (connection != NULL);
      nih_assert (signal != NULL);
      nih_assert (proxied != NULL);
      nih_assert (connection == proxied->proxy->connection);

      if (! dbus_message_is_signal (signal, proxied->interface->name, proxied->signal->name))
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

      if (! dbus_message_has_path (signal, proxied->proxy->path))
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

      if (proxied->proxy->name)
            if (! dbus_message_has_sender (signal, proxied->proxy->owner))
                  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

      message = nih_dbus_message_new (NULL, connection, signal);
      if (! message)
            return DBUS_HANDLER_RESULT_NEED_MEMORY;

      /* Iterate the arguments to the signal and demarshal into arguments
       * for our own function call.
       */
      dbus_message_iter_init (message->message, &iter);

      if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
            nih_free (message);
            return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
      }

      /* Call the handler function */
      nih_error_push_context ();
      ((MyFooNewResultHandler)proxied->handler) (proxied->data, message);
      nih_error_pop_context ();
      nih_free (message);

      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

Generated by  Doxygen 1.6.0   Back to index