33652f8ce0c26ce9ad584c18f373de2c9f67c4bf
[platform/upstream/dbus.git] / glib / test-thread-server.c
1 #include <glib.h>
2 #include "dbus-glib.h"
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "test-thread.h"
7
8 typedef struct {
9   guint32 counters[N_TEST_THREADS];
10 } ThreadTestData;
11
12 static ThreadTestData *
13 thread_test_data_new (void)
14 {
15   ThreadTestData *data;
16
17   data = g_new0 (ThreadTestData, 1);
18   
19   return data;
20 }
21
22 static void
23 thread_test_data_free (ThreadTestData *data)
24 {
25   g_free (data);
26 }
27
28 static DBusMessageHandler *disconnect_handler;
29 static DBusMessageHandler *filter_handler;
30 static dbus_int32_t handler_slot = -1;
31
32 static DBusHandlerResult
33 handle_test_message (DBusMessageHandler *handler,
34                      DBusConnection     *connection,
35                      DBusMessage        *message,
36                      void               *user_data)
37 {
38   ThreadTestData *data = user_data;
39   DBusMessageIter iter;
40   gint32 threadnr;
41   guint32 counter;
42   char *str, *expected_str;
43   GString *counter_str;
44   int i;
45
46   if (!dbus_message_is_method_call (message, "org.freedesktop.ThreadTest",
47                                     "TestMethod"))
48     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
49   
50   dbus_message_iter_init (message, &iter);
51   
52   if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
53     {
54       g_print ("First arg not right type\n");
55       goto out;
56     }
57   threadnr = dbus_message_iter_get_int32 (&iter);
58   if (threadnr < 0 || threadnr >= N_TEST_THREADS)
59     {
60       g_print ("Invalid thread nr\n");
61       goto out;
62     }
63
64   if (! dbus_message_iter_next (&iter))
65     {
66       g_print ("Couldn't get second arg\n");
67       goto out;
68     }
69
70   if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_UINT32)
71     {
72       g_print ("Second arg not right type\n");
73       goto out;
74     }
75   
76   counter = dbus_message_iter_get_uint32 (&iter);
77
78   if (counter != data->counters[threadnr])
79     {
80       g_print ("Thread %d, counter %d, expected %d\n", threadnr, counter, data->counters[threadnr]);
81       goto out;
82     }
83   data->counters[threadnr]++;
84   
85   if (! dbus_message_iter_next (&iter))
86     {
87       g_print ("Couldn't get third arg\n");
88       goto out;
89     }
90
91   if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
92     {
93       g_print ("Third arg not right type\n");
94       goto out;
95     }
96
97   str = dbus_message_iter_get_string (&iter);
98
99   if (str == NULL)
100     {
101       g_print ("No third arg\n");
102       goto out;
103     }
104
105   expected_str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter);
106   if (strcmp (expected_str, str) != 0)
107     {
108       g_print ("Wrong string '%s', expected '%s'\n", str, expected_str);
109       goto out;
110     }
111   g_free (str);
112   g_free (expected_str);
113
114   if (dbus_message_iter_next (&iter))
115     {
116       g_print ("Extra args on end of message\n");
117       goto out;
118     }
119   
120   dbus_connection_flush (connection);
121
122   counter_str = g_string_new ("");
123   for (i = 0; i < N_TEST_THREADS; i++)
124     {
125       g_string_append_printf (counter_str, "%d ", data->counters[i]);
126     }
127   g_print ("%s\r", counter_str->str);
128   g_string_free (counter_str, TRUE);
129   
130  out:
131   return DBUS_HANDLER_RESULT_HANDLED;
132 }
133
134 static DBusHandlerResult
135 handle_filter (DBusMessageHandler *handler,
136                DBusConnection     *connection,
137                DBusMessage        *message,
138                void               *user_data)
139 {  
140   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
141 }
142
143 static DBusHandlerResult
144 handle_disconnect (DBusMessageHandler *handler,
145                    DBusConnection     *connection,
146                    DBusMessage        *message,
147                    void               *user_data)
148 {
149   if (!dbus_message_is_signal (message, DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
150                                "Disconnected"))
151     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
152
153   g_print ("connection disconnected\n");
154   dbus_connection_unref (connection);
155   
156   return DBUS_HANDLER_RESULT_HANDLED;
157 }
158
159 static void
160 new_connection_callback (DBusServer     *server,
161                          DBusConnection *new_connection,
162                          void           *user_data)
163 {
164   DBusMessageHandler *test_message_handler;
165   ThreadTestData * data;
166
167   g_print ("new_connection_callback\n");
168   
169   dbus_connection_ref (new_connection);
170   dbus_connection_setup_with_g_main (new_connection, NULL);
171
172   data = thread_test_data_new ();
173   
174   test_message_handler =
175     dbus_message_handler_new (handle_test_message,
176                               data, (DBusFreeFunction)thread_test_data_free);
177   
178   if (!dbus_connection_add_filter (new_connection,
179                                    test_message_handler))
180     goto nomem;
181
182   if (!dbus_connection_set_data (new_connection,
183                                  handler_slot,
184                                  test_message_handler,
185                                  (DBusFreeFunction)dbus_message_handler_unref))
186     goto nomem;
187   
188   if (!dbus_connection_add_filter (new_connection,
189                                    disconnect_handler))
190     goto nomem;
191   
192   if (!dbus_connection_add_filter (new_connection,
193                                    filter_handler))
194     goto nomem;
195
196   return;
197   
198  nomem:
199   g_error ("no memory to setup new connection");
200 }
201
202 int
203 main (int argc, char *argv[])
204 {
205   GMainLoop *loop;
206   DBusServer *server;
207   DBusError error;
208
209   g_thread_init (NULL);
210   dbus_gthread_init ();
211   
212   if (argc < 2)
213     {
214       fprintf (stderr, "Give the server address as an argument\n");
215       return 1;
216     }
217
218   dbus_error_init (&error);
219   server = dbus_server_listen (argv[1], &error);
220   if (server == NULL)
221     {
222       fprintf (stderr, "Failed to start server on %s: %s\n",
223                argv[1], error.message);
224       dbus_error_free (&error);
225       return 1;
226     }
227
228   if (!dbus_connection_allocate_data_slot (&handler_slot))
229     g_error ("no memory for data slot");
230   
231   filter_handler =
232     dbus_message_handler_new (handle_filter, NULL, NULL);
233   if (filter_handler == NULL)
234     g_error ("no memory for handler");
235   
236   disconnect_handler =
237     dbus_message_handler_new (handle_disconnect, NULL, NULL);
238   if (disconnect_handler == NULL)
239     g_error ("no memory for handler");
240   
241   dbus_server_set_new_connection_function (server,
242                                            new_connection_callback,
243                                            NULL, NULL);
244
245   dbus_server_setup_with_g_main (server, NULL);
246   
247   loop = g_main_loop_new (NULL, FALSE);
248   g_main_run (loop);  
249
250   return 0;
251 }