summaryrefslogtreecommitdiff
path: root/src/examples/edbus/complex-types-server.c
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@gmail.com>2012-12-28 17:53:25 +0000
committerGustavo Sverzut Barbieri <barbieri@gmail.com>2012-12-28 17:53:25 +0000
commit331488d1ce3db2d07533ee276dabdc23f153fa61 (patch)
treea091fe83bfcf0c9d8f9f34ebfe44a5347537bff8 /src/examples/edbus/complex-types-server.c
parent6800b178dc918633ede220c572e5b99fa0046e7e (diff)
efl: merge edbus (v2).
SVN revision: 81825
Diffstat (limited to 'src/examples/edbus/complex-types-server.c')
-rw-r--r--src/examples/edbus/complex-types-server.c390
1 files changed, 390 insertions, 0 deletions
diff --git a/src/examples/edbus/complex-types-server.c b/src/examples/edbus/complex-types-server.c
new file mode 100644
index 0000000000..5e8742236c
--- /dev/null
+++ b/src/examples/edbus/complex-types-server.c
@@ -0,0 +1,390 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4#define BUS "com.profusion"
5#define PATH "/com/profusion/Test"
6#define IFACE "com.profusion.Test"
7
8static char *resp2;
9/* dummy, incremented each time DBus.Properties.Get() is called */
10static int int32 = 35;
11static Ecore_Timer *timer;
12
13static EDBus_Message *
14_receive_array(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
15{
16 EDBus_Message *reply = edbus_message_method_return_new(msg);
17 EDBus_Message_Iter *array;
18 const char *txt;
19
20 printf("- receiveArray\n");
21 if (!edbus_message_arguments_get(msg, "as", &array))
22 {
23 printf("Error on edbus_message_arguments_get()\n");
24 return reply;
25 }
26
27 while (edbus_message_iter_get_and_next(array, 's', &txt))
28 printf("%s\n", txt);
29 printf("}\n\n");
30
31 return reply;
32}
33
34static EDBus_Message *
35_receive_array_of_string_int_with_size(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
36{
37 EDBus_Message *reply = edbus_message_method_return_new(msg);
38 EDBus_Message_Iter *array, *struct_si;
39 int size, i = 0;
40
41 printf("- receiveArrayOfStringIntWithSize\n{\n");
42 if (!edbus_message_arguments_get(msg, "ia(si)", &size, &array))
43 {
44 printf("Error on edbus_message_arguments_get()\n");
45 return reply;
46 }
47
48 while (edbus_message_iter_get_and_next(array, 'r', &struct_si))
49 {
50 const char *txt;
51 int num;
52 if (!edbus_message_iter_arguments_get(struct_si, "si", &txt, &num))
53 {
54 printf("Error on edbus_message_arguments_get()\n");
55 return reply;
56 }
57 printf("%s | %d\n", txt, num);
58 i++;
59 }
60 printf("size in msg %d | size read %d\n", size, i);
61 printf("}\n\n");
62
63 return reply;
64}
65
66static EDBus_Message *
67_receive_variant(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
68{
69 EDBus_Message *reply = edbus_message_method_return_new(msg);
70 EDBus_Message_Iter *var, *array, *main_iter;
71
72 main_iter = edbus_message_iter_get(reply);
73 var = edbus_message_iter_container_new(main_iter, 'v', "as");
74 edbus_message_iter_arguments_append(var, "as", &array);
75
76 edbus_message_iter_arguments_append(array, "s", "item1");
77 edbus_message_iter_arguments_append(array, "s", "item2");
78 edbus_message_iter_arguments_append(array, "s", "item3");
79
80 edbus_message_iter_container_close(var, array);
81 edbus_message_iter_container_close(main_iter, var);
82
83 return reply;
84}
85
86static EDBus_Message *
87_send_variant(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
88{
89 EDBus_Message *reply = edbus_message_method_return_new(msg);
90 EDBus_Message_Iter *variant;
91 char *type;
92
93 printf("- sendVariantData\n{\n");
94 if (!edbus_message_arguments_get(msg, "v", &variant))
95 {
96 printf("Error on edbus_message_arguments_get()\n");
97 return reply;
98 }
99
100 type = edbus_message_iter_signature_get(variant);
101 if (type[1])
102 {
103 printf("It is a complex type, not handle yet.\n");
104 free(type);
105 return reply;
106 }
107
108 switch (type[0])
109 {
110 case 's':
111 case 'o':
112 {
113 char *txt;
114 edbus_message_iter_arguments_get(variant, type, &txt);
115 printf("type = %c value = %s\n", type[0], txt);
116 break;
117 }
118 case 'i':
119 {
120 int num;
121 edbus_message_iter_arguments_get(variant, type, &num);
122 printf("type = %c value = %d\n", type[0], num);
123 break;
124 }
125 default:
126 {
127 printf("Unhandled type\n");
128 }
129 }
130
131 printf("}\n\n");
132
133 free(type);
134 return reply;
135}
136
137static EDBus_Message *
138_send_array_int(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
139{
140 EDBus_Message *reply = edbus_message_method_return_new(msg);
141 EDBus_Message_Iter *iter, *array;
142 int i;
143
144 printf("- sendArrayInt\n\n");
145
146 iter = edbus_message_iter_get(reply);
147 array = edbus_message_iter_container_new(iter, 'a', "i");
148 for (i = 0; i < 5; i++)
149 edbus_message_iter_arguments_append(array, "i", i);
150 edbus_message_iter_container_close(iter, array);
151
152 return reply;
153}
154
155static EDBus_Message *
156_send_array(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
157{
158 EDBus_Message *reply = edbus_message_method_return_new(msg);
159 EDBus_Message_Iter *iter, *array;
160 const char *array_string[5] = {"qqqq", "wwwww", "eeeeee", "rrrrr", "ttttt"};
161 int i;
162
163 printf("sendArray\n\n");
164
165 iter = edbus_message_iter_get(reply);
166 array = edbus_message_iter_container_new(iter, 'a', "s");
167 for (i = 0; i < 5; i++)
168 edbus_message_iter_arguments_append(array, "s", array_string[i]);
169 edbus_message_iter_container_close(iter, array);
170
171 return reply;
172}
173
174static EDBus_Message *
175_plus_one(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
176{
177 EDBus_Message *reply = edbus_message_method_return_new(msg);
178 int num;
179
180 printf("- plusOne\n\n");
181 if (!edbus_message_arguments_get(msg, "i", &num))
182 {
183 printf("Error on edbus_message_arguments_get()\n");
184 return reply;
185 }
186 num++;
187 edbus_message_arguments_append(reply, "i", num);
188
189 return reply;
190}
191
192static EDBus_Message *
193_double_container(const EDBus_Service_Interface *iface, const EDBus_Message *msg)
194{
195 EDBus_Message_Iter *array1, *array2, *structure;
196 int num1, num2;
197 EDBus_Message *reply = edbus_message_method_return_new(msg);
198
199 if (!edbus_message_arguments_get(msg, "a(ii)a(ii)", &array1, &array2))
200 {
201 printf("Error on edbus_message_arguments_get()\n");
202 return NULL;
203 }
204
205 printf("DoubleCountainer\n{\nArray1:\n");
206 while (edbus_message_iter_get_and_next(array1, 'r', &structure))
207 {
208 edbus_message_iter_arguments_get(structure, "ii", &num1, &num2);
209 printf("1 %d - 2 %d\n", num1, num2);
210 }
211
212 printf("Array2:\n");
213 while (edbus_message_iter_get_and_next(array2, 'r', &structure))
214 {
215 edbus_message_iter_arguments_get(structure, "ii", &num1, &num2);
216 printf("1 %d - 2 %d\n", num1, num2);
217 }
218 printf("}\n\n");
219 return reply;
220}
221
222static Eina_Bool
223_properties_get(const EDBus_Service_Interface *iface, const char *propname, EDBus_Message_Iter *iter, const EDBus_Message *request_msg, EDBus_Message **error)
224{
225 printf("Properties_get - %s\n", propname);
226 if (!strcmp(propname, "Resp2"))
227 edbus_message_iter_basic_append(iter, 's', resp2);
228 else if (!strcmp(propname, "text"))
229 edbus_message_iter_basic_append(iter, 's', "lalalala");
230 else if (!strcmp(propname, "int32"))
231 {
232 edbus_message_iter_arguments_append(iter, "i", int32);
233 int32++;
234 }
235 else if (!strcmp(propname, "st"))
236 {
237 EDBus_Message_Iter *st;
238 edbus_message_iter_arguments_append(iter, "(ss)", &st);
239 edbus_message_iter_arguments_append(st, "ss", "string1", "string2");
240 edbus_message_iter_container_close(iter, st);
241 }
242 return EINA_TRUE;
243}
244
245static EDBus_Message *
246_properties_set(const EDBus_Service_Interface *iface, const char *propname, EDBus_Message_Iter *iter, const EDBus_Message *msg)
247{
248 char *type;
249
250 type = edbus_message_iter_signature_get(iter);
251
252 if (!strcmp(propname, "int32"))
253 {
254 int num;
255 if (type[0] != 'i')
256 goto invalid_signature;
257 edbus_message_iter_arguments_get(iter, "i", &num);
258 printf("int32 was set to: %d, previously was: %d\n", num, int32);
259 int32 = num;
260 }
261 else if (!strcmp(propname, "Resp2"))
262 {
263 const char *txt;
264 if (type[0] != 's')
265 goto invalid_signature;
266 edbus_message_iter_arguments_get(iter, "s", &txt);
267 printf("Resp2 was set to: %s, previously was: %s\n", txt, resp2);
268 free(resp2);
269 resp2 = strdup(txt);
270 }
271 free(type);
272 return edbus_message_method_return_new(msg);
273
274invalid_signature:
275 free(type);
276 return edbus_message_error_new(msg, "org.freedesktop.DBus.Error.InvalidSignature",
277 "Invalid type.");
278}
279
280static const EDBus_Method methods[] = {
281 {
282 "ReceiveArray", EDBUS_ARGS({"as", "array_of_strings"}),
283 NULL, _receive_array
284 },
285 {
286 "ReceiveArrayOfStringIntWithSize",
287 EDBUS_ARGS({"i", "size_of_array"}, {"a(si)", "array"}),
288 NULL, _receive_array_of_string_int_with_size, 0
289 },
290 {
291 "SendVariantData", EDBUS_ARGS({"v", "variant_data"}),
292 NULL, _send_variant
293 },
294 {
295 "ReceiveVariantData", NULL, EDBUS_ARGS({"v", "variant_data"}),
296 _receive_variant
297 },
298 {
299 "SendArrayInt", NULL,
300 EDBUS_ARGS({"ai", "array_of_int"}), _send_array_int, 0
301 },
302 {
303 "SendArray", NULL, EDBUS_ARGS({"as", "array_string"}),
304 _send_array
305 },
306 {
307 "PlusOne", EDBUS_ARGS({"i", "integer"}),
308 EDBUS_ARGS({"i", "integer_plus_one"}), _plus_one
309 },
310 {
311 "DoubleContainner", EDBUS_ARGS({"a(ii)", "array1"}, {"a(ii)", "array2"}),
312 NULL, _double_container
313 },
314 { }
315};
316
317static const EDBus_Property properties[] = {
318 { "Resp2", "s", NULL, _properties_set },
319 { "text", "s" },
320 { "int32", "i", NULL, _properties_set },
321 { "st", "(ss)" },
322 { }
323};
324
325static const EDBus_Service_Interface_Desc iface_desc = {
326 IFACE, methods, NULL, properties, _properties_get
327};
328
329static Eina_Bool _emit_changed(void *data)
330{
331 EDBus_Service_Interface *iface = data;
332 edbus_service_property_changed(iface, "int32");
333 edbus_service_property_invalidate_set(iface, "Resp2", EINA_TRUE);
334 return ECORE_CALLBACK_RENEW;
335}
336
337static void
338on_name_request(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
339{
340 unsigned int reply;
341 EDBus_Service_Interface *iface = data;
342
343 if (edbus_message_error_get(msg, NULL, NULL))
344 {
345 printf("error on on_name_request\n");
346 return;
347 }
348
349 if (!edbus_message_arguments_get(msg, "u", &reply))
350 {
351 printf("error geting arguments on on_name_request\n");
352 return;
353 }
354
355 if (reply != EDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER)
356 {
357 printf("error name already in use\n");
358 return;
359 }
360
361 timer = ecore_timer_add(3, _emit_changed, iface);
362}
363
364int
365main(void)
366{
367 EDBus_Connection *conn;
368 EDBus_Service_Interface *iface;
369
370 ecore_init();
371 edbus_init();
372
373 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
374
375 resp2 = malloc(sizeof(char) * 5);
376 strcpy(resp2, "test");
377 iface = edbus_service_interface_register(conn, PATH, &iface_desc);
378 edbus_name_request(conn, BUS, EDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
379 on_name_request, iface);
380
381 ecore_main_loop_begin();
382
383 free(resp2);
384 ecore_timer_del(timer);
385 edbus_connection_unref(conn);
386
387 edbus_shutdown();
388 ecore_shutdown();
389 return 0;
390}