Git init
[external/dbus-glib.git] / test / core / test-thread-server.c
1 #include <glib.h>
2 #include <dbus/dbus-glib-lowlevel.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 DBusHandlerResult
29 filter_test_message (DBusConnection     *connection,
30                      DBusMessage        *message,
31                      void               *user_data)
32 {
33   ThreadTestData *data = user_data;
34   DBusMessageIter iter;
35   gint32 threadnr;
36   guint32 counter;
37   const char *str;
38   char *expected_str;
39   GString *counter_str;
40   int i;
41
42   if (!dbus_message_is_method_call (message, "org.freedesktop.DBus.GLib.ThreadTest",
43                                     "TestMethod"))
44     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
45   
46   dbus_message_iter_init (message, &iter);
47   
48   if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
49     {
50       g_print ("First arg not right type\n");
51       goto out;
52     }
53    dbus_message_iter_get_basic (&iter, &threadnr);
54   if (threadnr < 0 || threadnr >= N_TEST_THREADS)
55     {
56       g_print ("Invalid thread nr\n");
57       goto out;
58     }
59
60   if (! dbus_message_iter_next (&iter))
61     {
62       g_print ("Couldn't get second arg\n");
63       goto out;
64     }
65
66   if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32)
67     {
68       g_print ("Second arg not right type\n");
69       goto out;
70     }
71   
72    dbus_message_iter_get_basic (&iter, &counter);
73
74   if (counter != data->counters[threadnr])
75     {
76       g_print ("Thread %d, counter %d, expected %d\n", threadnr, counter, data->counters[threadnr]);
77       goto out;
78     }
79   data->counters[threadnr]++;
80   
81   if (! dbus_message_iter_next (&iter))
82     {
83       g_print ("Couldn't get third arg\n");
84       goto out;
85     }
86
87   if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
88     {
89       g_print ("Third arg not right type\n");
90       goto out;
91     }
92
93   dbus_message_iter_get_basic (&iter, &str);
94
95   if (str == NULL)
96     {
97       g_print ("No third arg\n");
98       goto out;
99     }
100
101   expected_str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter);
102   if (strcmp (expected_str, str) != 0)
103     {
104       g_print ("Wrong string '%s', expected '%s'\n", str, expected_str);
105       goto out;
106     }
107   g_free (expected_str);
108
109   if (dbus_message_iter_next (&iter))
110     {
111       g_print ("Extra args on end of message\n");
112       goto out;
113     }
114   
115   dbus_connection_flush (connection);
116
117   counter_str = g_string_new ("");
118   for (i = 0; i < N_TEST_THREADS; i++)
119     {
120       g_string_append_printf (counter_str, "%d ", data->counters[i]);
121     }
122   g_print ("%s\r", counter_str->str);
123   g_string_free (counter_str, TRUE);
124   
125  out:
126   return DBUS_HANDLER_RESULT_HANDLED;
127 }
128
129 static DBusHandlerResult
130 filter_disconnect (DBusConnection     *connection,
131                    DBusMessage        *message,
132                    void               *user_data)
133 {
134   if (!dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL,
135                                "Disconnected"))
136     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
137
138   g_print ("connection disconnected\n");
139   dbus_connection_unref (connection);
140   
141   return DBUS_HANDLER_RESULT_HANDLED;
142 }
143
144 static void
145 new_connection_callback (DBusServer     *server,
146                          DBusConnection *new_connection,
147                          void           *user_data)
148 {
149   ThreadTestData * data;
150
151   g_print ("new_connection_callback\n");
152   
153   dbus_connection_ref (new_connection);
154   dbus_connection_setup_with_g_main (new_connection, NULL);
155
156   data = thread_test_data_new ();
157   
158   if (!dbus_connection_add_filter (new_connection,
159                                    filter_test_message, data,
160                                    (DBusFreeFunction) thread_test_data_free))
161     goto nomem;
162   
163   if (!dbus_connection_add_filter (new_connection,
164                                    filter_disconnect, NULL, NULL))
165     goto nomem;
166
167   return;
168   
169  nomem:
170   g_error ("no memory to setup new connection");
171 }
172
173 int
174 main (int argc, char *argv[])
175 {
176   GMainLoop *loop;
177   DBusServer *server;
178   DBusError error;
179
180   g_thread_init (NULL);
181   dbus_g_thread_init ();
182   
183   if (argc < 2)
184     {
185       fprintf (stderr, "Give the server address as an argument\n");
186       return 1;
187     }
188
189   dbus_error_init (&error);
190   server = dbus_server_listen (argv[1], &error);
191   if (server == NULL)
192     {
193       fprintf (stderr, "Failed to start server on %s: %s\n",
194                argv[1], error.message);
195       dbus_error_free (&error);
196       return 1;
197     }
198   
199   dbus_server_set_new_connection_function (server,
200                                            new_connection_callback,
201                                            NULL, NULL);
202
203   dbus_server_setup_with_g_main (server, NULL);
204   
205   loop = g_main_loop_new (NULL, FALSE);
206   g_main_loop_run (loop);  
207
208   return 0;
209 }