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
|
/* -*- mode: C; c-file-style: "gnu" -*- */
#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static GMainLoop *loop = NULL;
static int n_times_foo_received = 0;
static gboolean
timed_exit (gpointer loop)
{
g_main_loop_quit (loop);
return TRUE;
}
static void
foo_signal_handler (DBusGProxy *proxy,
void *user_data)
{
#if 0
double d;
/* FIXME - need to fix up dbus_gproxy_signal_connect() to be able to
* get signal args
*/
DBusError derror;
if (!dbus_message_is_signal (signal,
"org.freedesktop.TestSuite",
"Foo"))
{
g_printerr ("Signal handler received the wrong message\n");
exit (1);
}
dbus_error_init (&derror);
if (!dbus_message_get_args (signal, &derror, DBUS_TYPE_DOUBLE,
&d, DBUS_TYPE_INVALID))
{
g_printerr ("failed to get signal args: %s\n", derror.message);
dbus_error_free (&derror);
exit (1);
}
#endif
n_times_foo_received += 1;
g_main_loop_quit (loop);
}
int
main (int argc, char **argv)
{
DBusGConnection *connection;
GError *error;
DBusGProxy *driver;
DBusGProxy *proxy;
DBusGPendingCall *call;
char **name_list;
int name_list_len;
int i;
guint32 result;
const char *v_STRING;
guint32 v_UINT32;
g_type_init ();
loop = g_main_loop_new (NULL, FALSE);
error = NULL;
connection = dbus_g_bus_get (DBUS_BUS_SESSION,
&error);
if (connection == NULL)
{
g_printerr ("Failed to open connection to bus: %s\n",
error->message);
g_error_free (error);
exit (1);
}
/* Create a proxy object for the "bus driver" */
driver = dbus_g_proxy_new_for_name (connection,
DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
DBUS_PATH_ORG_FREEDESKTOP_DBUS,
DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
/* Call ListNames method */
call = dbus_g_proxy_begin_call (driver, "ListNames", DBUS_TYPE_INVALID);
error = NULL;
if (!dbus_g_proxy_end_call (driver, call, &error,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
&name_list, &name_list_len,
DBUS_TYPE_INVALID))
{
g_printerr ("Failed to complete ListNames call: %s\n",
error->message);
g_error_free (error);
exit (1);
}
g_print ("Names on the message bus:\n");
i = 0;
while (i < name_list_len)
{
g_assert (name_list[i] != NULL);
g_print (" %s\n", name_list[i]);
++i;
}
g_assert (name_list[i] == NULL);
g_strfreev (name_list);
/* Test handling of unknown method */
v_STRING = "blah blah blah blah blah";
v_UINT32 = 10;
call = dbus_g_proxy_begin_call (driver, "ThisMethodDoesNotExist",
DBUS_TYPE_STRING,
&v_STRING,
DBUS_TYPE_INT32,
&v_UINT32,
DBUS_TYPE_INVALID);
error = NULL;
if (dbus_g_proxy_end_call (driver, call, &error,
DBUS_TYPE_INVALID))
{
g_printerr ("Calling nonexistent method succeeded!\n");
exit (1);
}
g_print ("Got EXPECTED error from calling unknown method: %s\n",
error->message);
g_error_free (error);
/* Activate a service */
v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
v_UINT32 = 0;
call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
DBUS_TYPE_STRING,
&v_STRING,
DBUS_TYPE_UINT32,
&v_UINT32,
DBUS_TYPE_INVALID);
error = NULL;
if (!dbus_g_proxy_end_call (driver, call, &error,
DBUS_TYPE_UINT32, &result,
DBUS_TYPE_INVALID))
{
g_printerr ("Failed to complete Activate call: %s\n",
error->message);
g_error_free (error);
exit (1);
}
g_print ("Starting echo service result = 0x%x\n", result);
/* Activate a service again */
v_STRING = "org.freedesktop.DBus.TestSuiteEchoService";
v_UINT32 = 0;
call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
DBUS_TYPE_STRING,
&v_STRING,
DBUS_TYPE_UINT32,
&v_UINT32,
DBUS_TYPE_INVALID);
error = NULL;
if (!dbus_g_proxy_end_call (driver, call, &error,
DBUS_TYPE_UINT32, &result,
DBUS_TYPE_INVALID))
{
g_printerr ("Failed to complete Activate call: %s\n",
error->message);
g_error_free (error);
exit (1);
}
g_print ("Duplicate start of echo service = 0x%x\n", result);
/* Talk to the new service */
proxy = dbus_g_proxy_new_for_name_owner (connection,
"org.freedesktop.DBus.TestSuiteEchoService",
"/org/freedesktop/TestSuite",
"org.freedesktop.TestSuite",
&error);
if (proxy == NULL)
{
g_printerr ("Failed to create proxy for name owner: %s\n",
error->message);
g_error_free (error);
exit (1);
}
v_STRING = "my string hello";
call = dbus_g_proxy_begin_call (proxy, "Echo",
DBUS_TYPE_STRING,
&v_STRING,
DBUS_TYPE_INVALID);
error = NULL;
if (!dbus_g_proxy_end_call (proxy, call, &error,
DBUS_TYPE_STRING, &v_STRING,
DBUS_TYPE_INVALID))
{
g_printerr ("Failed to complete Echo call: %s\n",
error->message);
g_error_free (error);
exit (1);
}
g_print ("String echoed = \"%s\"\n", v_STRING);
/* Test oneway call and signal handling */
dbus_g_proxy_connect_signal (proxy, "Foo",
G_CALLBACK (foo_signal_handler),
NULL, NULL);
dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
DBUS_TYPE_INVALID);
dbus_g_connection_flush (connection);
g_timeout_add (5000, timed_exit, loop);
g_main_loop_run (loop);
if (n_times_foo_received != 1)
{
g_printerr ("Foo signal received %d times, should have been 1\n",
n_times_foo_received);
exit (1);
}
g_object_unref (G_OBJECT (driver));
g_object_unref (G_OBJECT (proxy));
g_print ("Successfully completed %s\n", argv[0]);
return 0;
}
|