1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 #include <dbus/dbus-glib.h>
7 static GMainLoop *loop = NULL;
8 static int n_times_foo_received = 0;
11 timed_exit (gpointer loop)
13 g_main_loop_quit (loop);
18 foo_signal_handler (DBusGProxy *proxy,
24 /* FIXME - need to fix up dbus_gproxy_signal_connect() to be able to
30 if (!dbus_message_is_signal (signal,
31 "org.freedesktop.TestSuite",
34 g_printerr ("Signal handler received the wrong message\n");
38 dbus_error_init (&derror);
39 if (!dbus_message_get_args (signal, &derror, DBUS_TYPE_DOUBLE,
40 &d, DBUS_TYPE_INVALID))
42 g_printerr ("failed to get signal args: %s\n", derror.message);
43 dbus_error_free (&derror);
48 n_times_foo_received += 1;
50 g_main_loop_quit (loop);
54 main (int argc, char **argv)
56 DBusGConnection *connection;
60 DBusGPendingCall *call;
69 loop = g_main_loop_new (NULL, FALSE);
72 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
74 if (connection == NULL)
76 g_printerr ("Failed to open connection to bus: %s\n",
82 /* Create a proxy object for the "bus driver" */
84 driver = dbus_g_proxy_new_for_service (connection,
85 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
86 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
87 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
89 /* Call ListServices method */
91 call = dbus_g_proxy_begin_call (driver, "ListServices", DBUS_TYPE_INVALID);
94 if (!dbus_g_proxy_end_call (driver, call, &error,
95 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
96 &service_list, &service_list_len,
99 g_printerr ("Failed to complete ListServices call: %s\n",
101 g_error_free (error);
105 g_print ("Services on the message bus:\n");
107 while (i < service_list_len)
109 g_assert (service_list[i] != NULL);
110 g_print (" %s\n", service_list[i]);
113 g_assert (service_list[i] == NULL);
115 g_strfreev (service_list);
117 /* Test handling of unknown method */
118 call = dbus_g_proxy_begin_call (driver, "ThisMethodDoesNotExist",
120 "blah blah blah blah blah",
126 if (dbus_g_proxy_end_call (driver, call, &error,
129 g_printerr ("Calling nonexistent method succeeded!\n");
133 g_print ("Got EXPECTED error from calling unknown method: %s\n",
135 g_error_free (error);
137 /* Activate a service */
138 call = dbus_g_proxy_begin_call (driver, "ActivateService",
140 "org.freedesktop.DBus.TestSuiteEchoService",
146 if (!dbus_g_proxy_end_call (driver, call, &error,
147 DBUS_TYPE_UINT32, &result,
150 g_printerr ("Failed to complete Activate call: %s\n",
152 g_error_free (error);
156 g_print ("Activation of echo service = 0x%x\n", result);
158 /* Activate a service again */
159 call = dbus_g_proxy_begin_call (driver, "ActivateService",
161 "org.freedesktop.DBus.TestSuiteEchoService",
167 if (!dbus_g_proxy_end_call (driver, call, &error,
168 DBUS_TYPE_UINT32, &result,
171 g_printerr ("Failed to complete Activate call: %s\n",
173 g_error_free (error);
177 g_print ("Duplicate activation of echo service = 0x%x\n", result);
179 /* Talk to the new service */
181 proxy = dbus_g_proxy_new_for_service_owner (connection,
182 "org.freedesktop.DBus.TestSuiteEchoService",
183 "/org/freedesktop/TestSuite",
184 "org.freedesktop.TestSuite",
189 g_printerr ("Failed to create proxy for service owner: %s\n",
191 g_error_free (error);
195 call = dbus_g_proxy_begin_call (proxy, "Echo",
201 if (!dbus_g_proxy_end_call (proxy, call, &error,
202 DBUS_TYPE_STRING, &str,
205 g_printerr ("Failed to complete Echo call: %s\n",
207 g_error_free (error);
211 g_print ("String echoed = \"%s\"\n", str);
214 /* Test oneway call and signal handling */
216 dbus_g_proxy_connect_signal (proxy, "Foo",
217 G_CALLBACK (foo_signal_handler),
220 dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
223 dbus_g_connection_flush (connection);
225 g_timeout_add (5000, timed_exit, loop);
227 g_main_loop_run (loop);
229 if (n_times_foo_received != 1)
231 g_printerr ("Foo signal received %d times, should have been 1\n",
232 n_times_foo_received);
236 g_object_unref (G_OBJECT (driver));
237 g_object_unref (G_OBJECT (proxy));
239 g_print ("Successfully completed %s\n", argv[0]);