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