2003-04-10 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / test / test-service.c
index 9e06b60..9d5ecee 100644 (file)
@@ -2,6 +2,17 @@
 #include "test-utils.h"
 
 static DBusLoop *loop;
+static dbus_bool_t already_quit = FALSE;
+
+static void
+quit (void)
+{
+  if (!already_quit)
+    {
+      _dbus_loop_quit (loop);
+      already_quit = TRUE;
+    }
+}
 
 static void
 die (const char *message)
@@ -11,13 +22,12 @@ die (const char *message)
 }
 
 static DBusHandlerResult
-echo_handler (DBusMessageHandler *handler,
-              DBusConnection     *connection,
-              DBusMessage        *message,
-              void               *user_data)
+handle_echo (DBusConnection     *connection,
+             DBusMessage        *message)
 {
   DBusError error;
   DBusMessage *reply;
+  DBusMessageIter iter;
   char *s;
   
   dbus_error_init (&error);
@@ -46,7 +56,9 @@ echo_handler (DBusMessageHandler *handler,
   if (reply == NULL)
     die ("No memory\n");
 
-  if (!dbus_message_append_string (reply, s))
+  dbus_message_append_iter_init (message, &iter);
+  
+  if (!dbus_message_iter_append_string (&iter, s))
     die ("No memory");
 
   if (!dbus_connection_send (connection, reply, NULL))
@@ -59,6 +71,27 @@ echo_handler (DBusMessageHandler *handler,
   return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
 }
 
+static DBusHandlerResult
+filter_func (DBusMessageHandler *handler,
+             DBusConnection     *connection,
+             DBusMessage        *message,
+             void               *user_data)
+{  
+  if (dbus_message_name_is (message, "org.freedesktop.DBus.TestSuiteEcho"))
+    return handle_echo (connection, message);
+  else if (dbus_message_name_is (message, "org.freedesktop.DBus.TestSuiteExit") ||
+           dbus_message_name_is (message, DBUS_MESSAGE_LOCAL_DISCONNECT))
+    {
+      dbus_connection_disconnect (connection);
+      quit ();
+      return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+    }
+  else
+    {
+      return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+    }
+}
+
 int
 main (int    argc,
       char **argv)
@@ -66,7 +99,11 @@ main (int    argc,
   DBusConnection *connection;
   DBusError error;
   DBusMessageHandler *handler;
-  const char *to_handle[] = { "org.freedesktop.DBus.TestSuiteEcho" };
+  const char *to_handle[] = {
+    "org.freedesktop.DBus.TestSuiteEcho",
+    "org.freedesktop.DBus.TestSuiteExit",
+    DBUS_MESSAGE_LOCAL_DISCONNECT,
+  };
   int result;
   
   dbus_error_init (&error);
@@ -86,11 +123,12 @@ main (int    argc,
   if (!test_connection_setup (loop, connection))
     die ("No memory\n");
 
-  handler = dbus_message_handler_new (echo_handler, NULL, NULL);
+  handler = dbus_message_handler_new (filter_func, NULL, NULL);
   if (handler == NULL)
     die ("No memory");
   
-  if (!dbus_connection_register_handler (connection, handler, to_handle, 1))
+  if (!dbus_connection_register_handler (connection, handler, to_handle,
+                                         _DBUS_N_ELEMENTS (to_handle)))
     die ("No memory");
 
   result = dbus_bus_acquire_service (connection, "org.freedesktop.DBus.TestSuiteEchoService",
@@ -100,19 +138,24 @@ main (int    argc,
       fprintf (stderr, "Failed to acquire service: %s\n",
                error.message);
       dbus_error_free (&error);
-      return 1;
+      exit (1);
     }
-  
+
+  _dbus_verbose ("*** Test service entering main loop\n");
   _dbus_loop_run (loop);
 
+  test_connection_shutdown (loop, connection);
+  
   dbus_connection_unref (connection);
-
+  
   dbus_message_handler_unref (handler);
 
   _dbus_loop_unref (loop);
   loop = NULL;
   
   dbus_shutdown ();
+
+  _dbus_verbose ("*** Test service exiting\n");
   
   return 0;
 }