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 DBusHandlerResult
29 filter_test_message (DBusConnection *connection,
33 ThreadTestData *data = user_data;
37 char *str, *expected_str;
41 if (!dbus_message_is_method_call (message, "org.freedesktop.ThreadTest",
43 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
45 dbus_message_iter_init (message, &iter);
47 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
49 g_print ("First arg not right type\n");
52 threadnr = dbus_message_iter_get_int32 (&iter);
53 if (threadnr < 0 || threadnr >= N_TEST_THREADS)
55 g_print ("Invalid thread nr\n");
59 if (! dbus_message_iter_next (&iter))
61 g_print ("Couldn't get second arg\n");
65 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
67 g_print ("Second arg not right type\n");
71 counter = dbus_message_iter_get_uint32 (&iter);
73 if (counter != data->counters[threadnr])
75 g_print ("Thread %d, counter %d, expected %d\n", threadnr, counter, data->counters[threadnr]);
78 data->counters[threadnr]++;
80 if (! dbus_message_iter_next (&iter))
82 g_print ("Couldn't get third arg\n");
86 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
88 g_print ("Third arg not right type\n");
92 str = dbus_message_iter_get_string (&iter);
96 g_print ("No third arg\n");
100 expected_str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter);
101 if (strcmp (expected_str, str) != 0)
103 g_print ("Wrong string '%s', expected '%s'\n", str, expected_str);
107 g_free (expected_str);
109 if (dbus_message_iter_next (&iter))
111 g_print ("Extra args on end of message\n");
115 dbus_connection_flush (connection);
117 counter_str = g_string_new ("");
118 for (i = 0; i < N_TEST_THREADS; i++)
120 g_string_append_printf (counter_str, "%d ", data->counters[i]);
122 g_print ("%s\r", counter_str->str);
123 g_string_free (counter_str, TRUE);
126 return DBUS_HANDLER_RESULT_HANDLED;
129 static DBusHandlerResult
130 filter_disconnect (DBusConnection *connection,
131 DBusMessage *message,
134 if (!dbus_message_is_signal (message, DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
136 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
138 g_print ("connection disconnected\n");
139 dbus_connection_unref (connection);
141 return DBUS_HANDLER_RESULT_HANDLED;
145 new_connection_callback (DBusServer *server,
146 DBusConnection *new_connection,
149 ThreadTestData * data;
151 g_print ("new_connection_callback\n");
153 dbus_connection_ref (new_connection);
154 dbus_connection_setup_with_g_main (new_connection, NULL);
156 data = thread_test_data_new ();
158 if (!dbus_connection_add_filter (new_connection,
159 filter_test_message, data,
160 (DBusFreeFunction) thread_test_data_free))
163 if (!dbus_connection_add_filter (new_connection,
164 filter_disconnect, NULL, NULL))
170 g_error ("no memory to setup new connection");
174 main (int argc, char *argv[])
180 g_thread_init (NULL);
181 dbus_gthread_init ();
185 fprintf (stderr, "Give the server address as an argument\n");
189 dbus_error_init (&error);
190 server = dbus_server_listen (argv[1], &error);
193 fprintf (stderr, "Failed to start server on %s: %s\n",
194 argv[1], error.message);
195 dbus_error_free (&error);
199 dbus_server_set_new_connection_function (server,
200 new_connection_callback,
203 dbus_server_setup_with_g_main (server, NULL);
205 loop = g_main_loop_new (NULL, FALSE);