forked from inrazhtet/Client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrams-test-server.c
More file actions
422 lines (374 loc) · 13.3 KB
/
Copy pathrams-test-server.c
File metadata and controls
422 lines (374 loc) · 13.3 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* rams-test-server.c
* A very simple DBus server used to help the RAMS test their code.
*/
// +---------------------+--------------------------------------------
// | Notes and Questions |
// +---------------------+
/*
We create a lot of new GVariants. Should we also explicitly free them?
Since this is a test program, I'm not going to worry about it. However,
for a production program, we need to think more about the issue and
check the documentation for g_dbus_method_invocation_return_value and
g_dbus_new.
*/
// +---------+--------------------------------------------------------
// | Headers |
// +---------+
#include <gio/gio.h> // For all the DBus communcation
#include <stdlib.h> // For rand, among other things
#include <stdio.h> // For printf and the ilk
#include <string.h> // For strcmp
// +--------------------+---------------------------------------------
// | Introspection Data |
// +--------------------+
static GDBusNodeInfo *introspection_data = NULL;
/* Introspection data for the service we are exporting */
static const gchar introspection_xml[] =
"<node>"
" <interface name='edu.grinnell.cs.glimmer.RAMStest'>"
" <method name='v2v'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='void -> void'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Prints Hello'/>"
" </method>"
" <method name='i2v'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='int -> void'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Prints the parameter'/>"
" <arg type='i' name='x' direction='in'/>"
" </method>"
" <method name='v2i'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='void -> int'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Returns the answer'/>"
" <arg type='i' name='answer' direction='out'/>"
" </method>"
" <method name='i2i'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='int -> int'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Squares the parameter'/>"
" <arg type='i' name='x' direction='in'/>"
" <arg type='i' name='result' direction='out'/>"
" </method>"
" <method name='v2ii'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='void -> int x int'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Two random numbers'/>"
" <arg type='i' name='x' direction='out'/>"
" <arg type='i' name='y' direction='out'/>"
" </method>"
" <method name='ii2v'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='int x int -> void'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Prints both parameters'/>"
" <arg type='i' name='x' direction='in'/>"
" <arg type='i' name='y' direction='in'/>"
" </method>"
" <method name='ii2i'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='int x int -> int'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Some funky function of two params'/>"
" <arg type='i' name='x' direction='in'/>"
" <arg type='i' name='y' direction='in'/>"
" <arg type='i' name='result' direction='out'/>"
" </method>"
" <method name='s2v'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='string -> void'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Prints the parameter'/>"
" <arg type='s' name='x' direction='in'/>"
" </method>"
" <method name='v2s'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='void -> string'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Returns the answer'/>"
" <arg type='s' name='answer' direction='out'/>"
" </method>"
" <method name='i2s'>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='int -> string'/>"
" <annotation name='org.gtk.GDBus.Annotation'"
" value='Converts int to string'/>"
" <arg type='i' name='x' direction='in'/>"
" <arg type='s' name='answer' direction='out'/>"
" </method>"
" </interface>"
"</node>";
// +-------------------+----------------------------------------------
// | Standard Handlers |
// +-------------------+
static void
handle_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
// Note: Handlers are in alphabetical order, rather than order of
// increasing complexity
// This stupid test is mostly a way for me to put everything else in
// an "else if" clause.
if (0)
;
// Handle i2i
else if (g_strcmp0 (method_name, "i2i") == 0)
{
int x;
g_variant_get (parameters, "(i)", &x);
#ifdef VERBOSE
printf ("i2i(%d)\n", x);
#endif
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(i)", x*x));
} // i2i
// Handle i2s
else if (g_strcmp0 (method_name, "i2s") == 0)
{
int x; // Our input value
gchar *response; // Our output value
g_variant_get (parameters, "(i)", &x);
#ifdef VERBOSE
printf ("i2s(%d)\n", x);
#endif
// g_strdup_printf is an awesome function that builds a new string
// using the structure of printf. You will need to free that string
// at some point, using g_free.
response = g_strdup_printf ("%d", x);
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(s)", response));
// Ooh! We can do things after we return a value. The wonder of
// asynchronous code.
g_free (response);
} // i2s
// Handle i2v
else if (g_strcmp0 (method_name, "i2v") == 0)
{
int x;
g_variant_get (parameters, "(i)", &x);
#ifdef VERBOSE
printf ("i2v(%d)\n", x);
#endif
printf ("%d\n", x);
g_dbus_method_invocation_return_value (invocation, NULL);
} // i2v
// Handle ii2i
else if (g_strcmp0 (method_name, "ii2i") == 0)
{
int x, y;
g_variant_get (parameters, "(ii)", &x, &y);
int result = 10*x + y;
#ifdef VERBOSE
printf ("ii2v(%d,%d)\n", x,y);
#endif
printf ("(%d,%d)\n", x, y);
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(i)", result));
} // ii2i
// Handle ii2v
else if (g_strcmp0 (method_name, "ii2v") == 0)
{
int x, y;
g_variant_get (parameters, "(ii)", &x, &y);
#ifdef VERBOSE
printf ("ii2v(%d,%d)\n", x,y);
#endif
printf ("(%d,%d)\n", x, y);
g_dbus_method_invocation_return_value (invocation, NULL);
} // ii2v
// Handle s2i
else if (g_strcmp0 (method_name, "s2i") == 0)
{
const gchar *str; // The parameter
int len; // The result
g_variant_get (parameters, "(&s)", &str);
#ifdef VERBOSE
printf ("s2i(%s)\n", str);
#endif
len = strlen (str);
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(i)", len));
} // s2i
// Handle s2v
else if (g_strcmp0 (method_name, "s2v") == 0)
{
const gchar *x;
g_variant_get (parameters, "(&s)", &x);
#ifdef VERBOSE
printf ("s2v(%s)\n", x);
#endif
printf ("%s\n", x);
g_dbus_method_invocation_return_value (invocation, NULL);
} // s2v
// Handle v2i
else if (g_strcmp0 (method_name, "v2i") == 0)
{
#ifdef VERBOSE
printf ("v2i()\n");
#endif
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(i)", 42));
} // v2i
// Handle v2ii
else if (g_strcmp0 (method_name, "v2ii") == 0)
{
#ifdef VERBOSE
printf ("v2ii()\n");
#endif
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(ii)",
rand (),
rand ()));
} // v2ii
// Handle v2s
else if (g_strcmp0 (method_name, "v2s") == 0)
{
#ifdef VERBOSE
printf ("v2s()\n");
#endif
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(s)", "Two"));
} // v2i
// Handle v2v.
else if (g_strcmp0 (method_name, "v2v") == 0)
{
#ifdef VERBOSE
printf ("v2v()\n")
#endif
printf ("Hello world.\n");
// Note: We use NULL as the second actual parameter of the
// return_value function to say "return nothing, other than
// a note that you've returned."
g_dbus_method_invocation_return_value (invocation, NULL);
} // v2v
// Handle all other method calls
else
{
#ifdef VERBOSE
printf ("handle_method (%s ...): Unknown method\n" method_name);
#endif
gchar *response = g_strdup_printf ("Unknown method: %s", method_name);
g_dbus_method_invocation_return_error (invocation,
G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
response);
g_free (response);
} // if the name is not known
} // handle_method_call
static GVariant *
handle_get_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GError **error,
gpointer user_data)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"No property %s found in interface %s.",
property_name,
interface_name);
// NULL signals failure.
return NULL;
} // handle_set_property
static gboolean
handle_set_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GVariant *value,
GError **error,
gpointer user_data)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"No property %s found in interface %s.",
property_name,
interface_name);
// false signals failure.
return FALSE;
} // handle_get_property
/* Our three handlers */
static const GDBusInterfaceVTable interface_vtable =
{
handle_method_call,
handle_get_property,
handle_set_property
};
// +----------------------------+-------------------------------------
// | Standard DBus Server Setup |
// +----------------------------+
static void
on_bus_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
guint registration_id;
#ifdef VERBOSE
fprintf (stderr, "Yay, I got name %s on the bus.\n", name);
#endif
registration_id = g_dbus_connection_register_object (connection,
"/edu/grinnell/cs/glimmer/RAMStest",
introspection_data->interfaces[0],
&interface_vtable,
NULL, /* user_data */
NULL, /* user_data_free_func */
NULL); /* GERROR */
} // on_bus_acquired
static void
on_name_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
#ifdef VERBOSE
fprintf (stderr, "Acquired name %s\n", name);
#endif
} // on_name_acquired
static void
on_name_lost (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
#ifdef VERBOSE
fprintf (stderr, "Lost name %s\n", name);
#endif
exit (1);
} // on_name_lost
// +------+-----------------------------------------------------
// | Main |
// +------+
int
main (int argc, char *argv[])
{
guint owner_id;
GMainLoop *loop;
g_type_init ();
introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
g_assert (introspection_data != NULL);
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
"edu.grinnell.cs.glimmer.RAMStest",
G_BUS_NAME_OWNER_FLAGS_NONE,
on_bus_acquired,
on_name_acquired,
on_name_lost,
NULL,
NULL);
// Event loop. Wait for functions to get called asyn h.
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
g_bus_unown_name (owner_id);
g_dbus_node_info_unref (introspection_data);
return 0;
} // main