6 #include "test-thread.h"
9 guint32 counters[N_TEST_THREADS];
12 static ThreadTestData *
13 thread_test_data_new (void)
17 data = g_new0 (ThreadTestData, 1);
23 thread_test_data_free (ThreadTestData *data)
28 static DBusMessageHandler *disconnect_handler;
29 static DBusMessageHandler *filter_handler;
30 static dbus_int32_t handler_slot = -1;
32 static DBusHandlerResult
33 handle_test_message (DBusMessageHandler *handler,
34 DBusConnection *connection,
38 ThreadTestData *data = user_data;
42 char *str, *expected_str;
46 dbus_message_iter_init (message, &iter);
48 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
50 g_print ("First arg not right type\n");
53 threadnr = dbus_message_iter_get_int32 (&iter);
54 if (threadnr < 0 || threadnr >= N_TEST_THREADS)
56 g_print ("Invalid thread nr\n");
60 if (! dbus_message_iter_next (&iter))
62 g_print ("Couldn't get second arg\n");
66 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
68 g_print ("Second arg not right type\n");
72 counter = dbus_message_iter_get_uint32 (&iter);
74 if (counter != data->counters[threadnr])
76 g_print ("Thread %d, counter %d, expected %d\n", threadnr, counter, data->counters[threadnr]);
79 data->counters[threadnr]++;
81 if (! dbus_message_iter_next (&iter))
83 g_print ("Couldn't get third arg\n");
87 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
89 g_print ("Third arg not right type\n");
93 str = dbus_message_iter_get_string (&iter);
97 g_print ("No third arg\n");
101 expected_str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter);
102 if (strcmp (expected_str, str) != 0)
104 g_print ("Wrong string '%s', expected '%s'\n", str, expected_str);
108 g_free (expected_str);
110 if (dbus_message_iter_next (&iter))
112 g_print ("Extra args on end of message\n");
116 dbus_connection_flush (connection);
118 counter_str = g_string_new ("");
119 for (i = 0; i < N_TEST_THREADS; i++)
121 g_string_append_printf (counter_str, "%d ", data->counters[i]);
123 g_print ("%s\r", counter_str->str);
124 g_string_free (counter_str, TRUE);
127 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
130 static DBusHandlerResult
131 handle_filter (DBusMessageHandler *handler,
132 DBusConnection *connection,
133 DBusMessage *message,
136 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
139 static DBusHandlerResult
140 handle_disconnect (DBusMessageHandler *handler,
141 DBusConnection *connection,
142 DBusMessage *message,
145 g_print ("connection disconnected\n");
146 dbus_connection_unref (connection);
148 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
153 new_connection_callback (DBusServer *server,
154 DBusConnection *new_connection,
157 const char *test_messages[] = { "org.freedesktop.ThreadTest" };
158 const char *disconnect_messages[] = { "org.freedesktop.Local.Disconnect" };
159 DBusMessageHandler *test_message_handler;
160 ThreadTestData * data;
162 g_print ("new_connection_callback\n");
164 dbus_connection_ref (new_connection);
165 dbus_connection_setup_with_g_main (new_connection, NULL);
167 data = thread_test_data_new ();
169 test_message_handler =
170 dbus_message_handler_new (handle_test_message,
171 data, (DBusFreeFunction)thread_test_data_free);
173 if (!dbus_connection_register_handler (new_connection,
174 test_message_handler,
178 if (!dbus_connection_set_data (new_connection,
180 test_message_handler,
181 (DBusFreeFunction)dbus_message_handler_unref))
184 if (!dbus_connection_register_handler (new_connection,
186 disconnect_messages, 1))
189 if (!dbus_connection_add_filter (new_connection,
196 g_error ("no memory to setup new connection");
200 main (int argc, char *argv[])
206 g_thread_init (NULL);
207 dbus_gthread_init ();
211 fprintf (stderr, "Give the server address as an argument\n");
215 dbus_error_init (&error);
216 server = dbus_server_listen (argv[1], &error);
219 fprintf (stderr, "Failed to start server on %s: %s\n",
220 argv[1], error.message);
221 dbus_error_free (&error);
225 if (!dbus_connection_allocate_data_slot (&handler_slot))
226 g_error ("no memory for data slot");
229 dbus_message_handler_new (handle_filter, NULL, NULL);
230 if (filter_handler == NULL)
231 g_error ("no memory for handler");
234 dbus_message_handler_new (handle_disconnect, NULL, NULL);
235 if (disconnect_handler == NULL)
236 g_error ("no memory for handler");
238 dbus_server_set_new_connection_function (server,
239 new_connection_callback,
242 dbus_server_setup_with_g_main (server, NULL);
244 loop = g_main_loop_new (NULL, FALSE);