4 #include <dbus/dbus-glib-lowlevel.h>
8 #include "test-thread.h"
11 guint32 counters[N_TEST_THREADS];
14 static ThreadTestData *
15 thread_test_data_new (void)
19 data = g_new0 (ThreadTestData, 1);
25 thread_test_data_free (ThreadTestData *data)
30 static DBusHandlerResult
31 filter_test_message (DBusConnection *connection,
35 ThreadTestData *data = user_data;
44 if (!dbus_message_is_method_call (message, "org.freedesktop.DBus.GLib.ThreadTest",
46 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
48 dbus_message_iter_init (message, &iter);
50 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
52 g_print ("First arg not right type\n");
55 dbus_message_iter_get_basic (&iter, &threadnr);
56 if (threadnr < 0 || threadnr >= N_TEST_THREADS)
58 g_print ("Invalid thread nr\n");
62 if (! dbus_message_iter_next (&iter))
64 g_print ("Couldn't get second arg\n");
68 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
70 g_print ("Second arg not right type\n");
74 dbus_message_iter_get_basic (&iter, &counter);
76 if (counter != data->counters[threadnr])
78 g_print ("Thread %d, counter %d, expected %d\n", threadnr, counter, data->counters[threadnr]);
81 data->counters[threadnr]++;
83 if (! dbus_message_iter_next (&iter))
85 g_print ("Couldn't get third arg\n");
89 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
91 g_print ("Third arg not right type\n");
95 dbus_message_iter_get_basic (&iter, &str);
99 g_print ("No third arg\n");
103 expected_str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter);
104 if (strcmp (expected_str, str) != 0)
106 g_print ("Wrong string '%s', expected '%s'\n", str, expected_str);
107 g_free (expected_str);
110 g_free (expected_str);
112 if (dbus_message_iter_next (&iter))
114 g_print ("Extra args on end of message\n");
118 dbus_connection_flush (connection);
120 counter_str = g_string_new ("");
121 for (i = 0; i < N_TEST_THREADS; i++)
123 g_string_append_printf (counter_str, "%d ", data->counters[i]);
125 g_print ("%s\r", counter_str->str);
126 g_string_free (counter_str, TRUE);
129 return DBUS_HANDLER_RESULT_HANDLED;
132 static DBusHandlerResult
133 filter_disconnect (DBusConnection *connection,
134 DBusMessage *message,
137 if (!dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL,
139 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
141 g_print ("connection disconnected\n");
142 dbus_connection_unref (connection);
144 return DBUS_HANDLER_RESULT_HANDLED;
148 new_connection_callback (DBusServer *server,
149 DBusConnection *new_connection,
152 ThreadTestData * data;
154 g_print ("new_connection_callback\n");
156 dbus_connection_ref (new_connection);
157 dbus_connection_setup_with_g_main (new_connection, NULL);
159 data = thread_test_data_new ();
161 if (!dbus_connection_add_filter (new_connection,
162 filter_test_message, data,
163 (DBusFreeFunction) thread_test_data_free))
166 if (!dbus_connection_add_filter (new_connection,
167 filter_disconnect, NULL, NULL))
173 g_error ("no memory to setup new connection");
177 main (int argc, char *argv[])
183 g_thread_init (NULL);
184 dbus_g_thread_init ();
188 fprintf (stderr, "Give the server address as an argument\n");
192 dbus_error_init (&error);
193 server = dbus_server_listen (argv[1], &error);
196 fprintf (stderr, "Failed to start server on %s: %s\n",
197 argv[1], error.message);
198 dbus_error_free (&error);
202 dbus_server_set_new_connection_function (server,
203 new_connection_callback,
206 dbus_server_setup_with_g_main (server, NULL);
208 loop = g_main_loop_new (NULL, FALSE);
209 g_main_loop_run (loop);