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