summaryrefslogtreecommitdiff
path: root/src/bin/eeze
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2016-11-28 12:14:48 -0200
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>2016-11-28 12:15:38 -0200
commitb132e1e1524f77d9c17e6672927970cee75ed16c (patch)
tree42e55a0eccc89498c6945b9ccf6b6c0be387abad /src/bin/eeze
parent211eb3f00a214200cebd295c7ab84402874c5c76 (diff)
eeze_scanner: now using efl_net.
Rewrite eeze_scanner on top of efl_net_server_simple.
Diffstat (limited to 'src/bin/eeze')
-rw-r--r--src/bin/eeze/eeze_scanner.c299
1 files changed, 195 insertions, 104 deletions
diff --git a/src/bin/eeze/eeze_scanner.c b/src/bin/eeze/eeze_scanner.c
index 8fefe30ff4..adc06c0db2 100644
--- a/src/bin/eeze/eeze_scanner.c
+++ b/src/bin/eeze/eeze_scanner.c
@@ -25,9 +25,9 @@
25#endif 25#endif
26 26
27static int es_log_dom = -1; 27static int es_log_dom = -1;
28static Ecore_Con_Server *svr = NULL; 28static int retval = EXIT_SUCCESS;
29static Eet_Data_Descriptor *es_edd = NULL; 29static Eet_Data_Descriptor *es_edd = NULL;
30static Eina_Hash *clients = NULL; 30static Eina_List *clients = NULL;
31 31
32static Eina_List *storage_devices = NULL; 32static Eina_List *storage_devices = NULL;
33static Eina_List *storage_cdrom = NULL; 33static Eina_List *storage_cdrom = NULL;
@@ -39,32 +39,30 @@ static void
39event_send(const char *device, Eeze_Scanner_Event_Type type, Eina_Bool volume) 39event_send(const char *device, Eeze_Scanner_Event_Type type, Eina_Bool volume)
40{ 40{
41 Eeze_Scanner_Event ev; 41 Eeze_Scanner_Event ev;
42 const Eina_List *l; 42 Eet_Connection *ec;
43 Ecore_Con_Client *cl; 43 const Eina_List *n;
44
44 45
45 ev.device = device; 46 ev.device = device;
46 ev.type = type; 47 ev.type = type;
47 ev.volume = volume; 48 ev.volume = volume;
48 EINA_LIST_FOREACH(ecore_con_server_clients_get(svr), l, cl) 49 EINA_LIST_FOREACH(clients, n, ec)
49 { 50 {
50 Eet_Connection *ec; 51 const char *ts;
51 52 const char *vol = volume ? " (volume)" : "";
52 ec = eina_hash_find(clients, &cl); 53 switch (type)
53 if (!ec) continue; 54 {
54 INF("Serializing event..."); 55 case EEZE_SCANNER_EVENT_TYPE_ADD: ts = "ADD"; break;
56 case EEZE_SCANNER_EVENT_TYPE_REMOVE: ts = "REMOVE"; break;
57 case EEZE_SCANNER_EVENT_TYPE_CHANGE: ts = "CHANGE"; break;
58 default: ts = "?";
59 }
60 INF("Serializing event %s '%s'%s for client %p...", ts, device, vol, ec);
55 eet_connection_send(ec, es_edd, &ev, NULL); 61 eet_connection_send(ec, es_edd, &ev, NULL);
56 } 62 }
57} 63}
58 64
59static Eina_Bool 65static Eina_Bool
60event_write(const void *data, size_t size, Ecore_Con_Client *cl)
61{
62 INF("Event sent!");
63 ecore_con_client_send(cl, data, size);
64 return EINA_TRUE;
65}
66
67static Eina_Bool
68disk_mount(void *data EINA_UNUSED, int type EINA_UNUSED, Eeze_Disk *disk) 66disk_mount(void *data EINA_UNUSED, int type EINA_UNUSED, Eeze_Disk *disk)
69{ 67{
70 Eina_List *l; 68 Eina_List *l;
@@ -83,14 +81,14 @@ disk_mount(void *data EINA_UNUSED, int type EINA_UNUSED, Eeze_Disk *disk)
83} 81}
84 82
85static void 83static void
86cl_setup(Ecore_Con_Client *cl EINA_UNUSED, Eet_Connection *ec) 84cl_setup(Eo *cl, Eet_Connection *ec)
87{ 85{
88 Eina_List *l; 86 Eina_List *l;
89 Eeze_Scanner_Device *dev; 87 Eeze_Scanner_Device *dev;
90 Eeze_Scanner_Event ev; 88 Eeze_Scanner_Event ev;
91 const char *sys; 89 const char *sys;
92 90
93 INF("Sending initial events to new client"); 91 INF("Sending initial events to new client %p (ec=%p)", cl, ec);
94 EINA_LIST_FOREACH(storage_devices, l, sys) 92 EINA_LIST_FOREACH(storage_devices, l, sys)
95 { 93 {
96 ev.device = sys; 94 ev.device = sys;
@@ -122,47 +120,95 @@ cl_setup(Ecore_Con_Client *cl EINA_UNUSED, Eet_Connection *ec)
122} 120}
123 121
124static Eina_Bool 122static Eina_Bool
125es_read(const void *eet_data EINA_UNUSED, size_t size EINA_UNUSED, void *user_data EINA_UNUSED) 123ec_write(const void *data, size_t size, void *user_data)
126{ 124{
125 Eo *client = user_data;
126 Eina_Slice slice = { .mem = data, .len = size };
127 Eina_Error err;
128 DBG("Write " EINA_SLICE_FMT " to %p", EINA_SLICE_PRINT(slice), client);
129 err = efl_io_writer_write(client, &slice, NULL);
130 if (err)
131 {
132 DBG("Failed write " EINA_SLICE_FMT " to %p: %s", EINA_SLICE_PRINT(slice), client, eina_error_msg_get(err));
133 if (!efl_io_closer_closed_get(client))
134 efl_io_closer_close(client);
135 return EINA_FALSE;
136 }
127 return EINA_TRUE; 137 return EINA_TRUE;
128} 138}
129 139
130static Eina_Bool 140static Eina_Bool
131cl_add(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Add *ev) 141ec_read(const void *eet_data EINA_UNUSED, size_t size EINA_UNUSED, void *user_data EINA_UNUSED)
132{ 142{
133 Eet_Connection *ec; 143 return EINA_TRUE;
134 INF("Added client"); 144}
135 145
136 ec = eet_connection_new(es_read, (Eet_Write_Cb*)event_write, ev->client); 146static void
137 if (!ec) 147cl_finished(void *data, const Efl_Event *event)
138 { 148{
139 ERR("Could not create eet serializer! Lost client!"); 149 Eo *client = event->object;
140 ecore_con_client_del(ev->client); 150 Eet_Connection *ec = data;
141 return ECORE_CALLBACK_RENEW;
142 }
143 151
144 eina_hash_add(clients, &ev->client, ec); 152 DBG("Finished %p (ec=%p)", client, ec);
145 cl_setup(ev->client, ec); 153
146 return ECORE_CALLBACK_RENEW; 154 if (!efl_io_closer_closed_get(client))
155 efl_io_closer_close(client);
147} 156}
148 157
149static Eina_Bool 158static void
150cl_del(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Del *ev) 159cl_error(void *data, const Efl_Event *event)
160{
161 Eo *client = event->object;
162 Eina_Error *perr = event->info;
163 Eet_Connection *ec = data;
164
165 WRN("client %p (ec=%p) error: %s", client, ec, eina_error_msg_get(*perr));
166
167 if (!efl_io_closer_closed_get(client))
168 efl_io_closer_close(client);
169}
170
171static Efl_Callback_Array_Item *cl_cbs(void);
172
173static void
174cl_closed(void *data, const Efl_Event *event)
151{ 175{
176 Eo *client = event->object;
177 Eet_Connection *ec = data;
178
179 INF("Removed client %p (ec=%p)", client, ec);
180 clients = eina_list_remove(clients, ec);
181 efl_event_callback_array_del(client, cl_cbs(), ec);
182 efl_unref(client);
183 eet_connection_close(ec, NULL);
184}
185
186EFL_CALLBACKS_ARRAY_DEFINE(cl_cbs,
187 { EFL_IO_BUFFERED_STREAM_EVENT_READ_FINISHED, cl_finished },
188 { EFL_IO_BUFFERED_STREAM_EVENT_ERROR, cl_error },
189 { EFL_IO_CLOSER_EVENT_CLOSED, cl_closed });
190
191static void
192cl_add(void *data EINA_UNUSED, const Efl_Event *event)
193{
194 Eo *client = event->info;
152 Eet_Connection *ec; 195 Eet_Connection *ec;
153 Eina_Bool d; 196
154 INF("Removed client"); 197 INF("Added client %p", client);
155 ec = eina_hash_find(clients, &ev->client); 198 ec = eet_connection_new(ec_read, ec_write, client);
156 if (ec) 199 if (!ec)
157 { 200 {
158 eina_hash_del_by_data(clients, &ec); 201 ERR("Could not create eet serializer! Lost client %p!", client);
159 eet_connection_close(ec, &d); 202 return;
160 } 203 }
161 204
162 return ECORE_CALLBACK_RENEW; 205 efl_ref(client);
206 efl_event_callback_array_add(client, cl_cbs(), ec);
207 clients = eina_list_append(clients, ec);
208 cl_setup(client, ec);
163} 209}
164 210
165static void 211static Eina_Bool
166eet_setup(void) 212eet_setup(void)
167{ 213{
168 Eet_Data_Descriptor_Class eddc; 214 Eet_Data_Descriptor_Class eddc;
@@ -170,7 +216,7 @@ eet_setup(void)
170 if (!eet_eina_stream_data_descriptor_class_set(&eddc, sizeof(eddc), "eeze_scanner_event", sizeof(Eeze_Scanner_Event))) 216 if (!eet_eina_stream_data_descriptor_class_set(&eddc, sizeof(eddc), "eeze_scanner_event", sizeof(Eeze_Scanner_Event)))
171 { 217 {
172 CRI("Could not create eet data descriptor!"); 218 CRI("Could not create eet data descriptor!");
173 exit(1); 219 return EINA_FALSE;
174 } 220 }
175 221
176 es_edd = eet_data_descriptor_stream_new(&eddc); 222 es_edd = eet_data_descriptor_stream_new(&eddc);
@@ -179,6 +225,7 @@ eet_setup(void)
179 DAT(type, UINT); 225 DAT(type, UINT);
180 DAT(volume, UCHAR); 226 DAT(volume, UCHAR);
181#undef DAT 227#undef DAT
228 return EINA_TRUE;
182} 229}
183 230
184static Eina_Bool 231static Eina_Bool
@@ -220,7 +267,7 @@ cdrom_timer(Eeze_Scanner_Device *dev)
220 return EINA_TRUE; 267 return EINA_TRUE;
221} 268}
222 269
223static void 270static Eina_Bool
224storage_setup(void) 271storage_setup(void)
225{ 272{
226 Eina_List *l, *ll; 273 Eina_List *l, *ll;
@@ -230,15 +277,12 @@ storage_setup(void)
230 if (!storage_devices) 277 if (!storage_devices)
231 { 278 {
232 ERR("No storage devices found! This is not supposed to happen!"); 279 ERR("No storage devices found! This is not supposed to happen!");
233 exit(1); 280 return EINA_FALSE;
234 } 281 }
235 EINA_LIST_FOREACH(storage_devices, l, sys)
236 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_FALSE);
237 282
238 ll = eeze_udev_find_by_type(EEZE_UDEV_TYPE_DRIVE_REMOVABLE, NULL); 283 ll = eeze_udev_find_by_type(EEZE_UDEV_TYPE_DRIVE_REMOVABLE, NULL);
239 EINA_LIST_FREE(ll, sys) 284 EINA_LIST_FREE(ll, sys)
240 { 285 {
241 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_FALSE);
242 storage_devices = eina_list_append(storage_devices, sys); 286 storage_devices = eina_list_append(storage_devices, sys);
243 } 287 }
244 288
@@ -266,7 +310,6 @@ storage_setup(void)
266 dev->device = sys; 310 dev->device = sys;
267 dev->mounted = eeze_disk_mounted_get(disk); 311 dev->mounted = eeze_disk_mounted_get(disk);
268 eeze_disk_free(disk); 312 eeze_disk_free(disk);
269 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_FALSE);
270 dev->poller = ecore_poller_add(ECORE_POLLER_CORE, 32, (Ecore_Task_Cb)cdrom_timer, dev); 313 dev->poller = ecore_poller_add(ECORE_POLLER_CORE, 32, (Ecore_Task_Cb)cdrom_timer, dev);
271 storage_cdrom = eina_list_append(storage_cdrom, dev); 314 storage_cdrom = eina_list_append(storage_cdrom, dev);
272 } 315 }
@@ -282,13 +325,12 @@ storage_setup(void)
282 eina_stringshare_del(sys); 325 eina_stringshare_del(sys);
283 volume_devices = eina_list_remove_list(volume_devices, l); 326 volume_devices = eina_list_remove_list(volume_devices, l);
284 volume_cdrom = eina_list_append(volume_cdrom, dev); 327 volume_cdrom = eina_list_append(volume_cdrom, dev);
285 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_TRUE);
286 l = NULL; 328 l = NULL;
287 break; 329 break;
288 } 330 }
289 if (!l) continue;
290 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_TRUE);
291 } 331 }
332
333 return EINA_TRUE;
292} 334}
293 335
294static void 336static void
@@ -392,72 +434,121 @@ cb_stor_chg(const char *device, Eeze_Udev_Event ev, void *data EINA_UNUSED, Eeze
392} 434}
393 435
394static void 436static void
395es_exit(int sig) 437server_error(void *data EINA_UNUSED, const Efl_Event *event)
396{ 438{
397 ecore_con_server_del(svr); 439 Eina_Error *perr = event->info;
398 exit(sig);
399}
400 440
401static void 441 ERR("server error: %s", eina_error_msg_get(*perr));
402sigs_setup(void) 442 retval = EXIT_FAILURE;
403{ 443 ecore_main_loop_quit();
404 sigset_t sigs = {{0}};
405 struct sigaction s;
406
407 memset(&s, 0, sizeof(s));
408
409 sigfillset(&sigs);
410 sigdelset(&sigs, SIGSEGV);
411 sigdelset(&sigs, SIGTERM);
412 sigdelset(&sigs, SIGINT);
413 sigdelset(&sigs, SIGQUIT);
414
415 s.sa_handler = es_exit;
416 s.sa_flags = 0;
417 sigaction(SIGTERM, &s, NULL);
418 sigaction(SIGSEGV, &s, NULL);
419 sigaction(SIGINT, &s, NULL);
420} 444}
421 445
422int 446int
423main(void) 447main(void)
424{ 448{
425 int ret; 449 Eo *server, *loop;
450 char *path = NULL;
451 Ecore_Event_Handler *eh;
452 Eina_List *ehl = NULL;
453 Eeze_Udev_Watch *uw;
454 Eina_List *uwl = NULL;
455 Eina_Error err;
456
457 ecore_app_no_system_modules();
458
426 eina_init(); 459 eina_init();
427 ecore_init(); 460 ecore_init();
461 eet_init();
428 ecore_con_init(); 462 ecore_con_init();
429 eeze_init(); 463 eeze_init();
430 eeze_disk_function(); 464 eeze_disk_function();
431 eeze_mount_tabs_watch(); 465 eeze_mount_tabs_watch();
432 466
433 sigs_setup();
434 es_log_dom = eina_log_domain_register("eeze_scanner", EINA_COLOR_CYAN); 467 es_log_dom = eina_log_domain_register("eeze_scanner", EINA_COLOR_CYAN);
435 468
436 eet_setup(); 469 if (!eet_setup())
437 clients = eina_hash_pointer_new(NULL); 470 goto error_setup;
438 ret = 1; 471
439 EINA_SAFETY_ON_NULL_GOTO(clients, error); 472 if (!storage_setup())
440 473 goto error_setup;
441 ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb)cl_add, NULL); 474
442 ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, (Ecore_Event_Handler_Cb)cl_del, NULL); 475#define ADD_HANDLER(type, cb) \
443 ecore_event_handler_add(EEZE_EVENT_DISK_UNMOUNT, (Ecore_Event_Handler_Cb)disk_mount, NULL); 476 ehl = eina_list_append(ehl, ecore_event_handler_add(type, (Ecore_Event_Handler_Cb)cb, NULL))
444 ecore_event_handler_add(EEZE_EVENT_DISK_MOUNT, (Ecore_Event_Handler_Cb)disk_mount, NULL); 477 ADD_HANDLER(EEZE_EVENT_DISK_UNMOUNT, disk_mount);
445 478 ADD_HANDLER(EEZE_EVENT_DISK_MOUNT, disk_mount);
446 eeze_udev_watch_add(EEZE_UDEV_TYPE_DRIVE_INTERNAL, EEZE_UDEV_EVENT_NONE, cb_stor_chg, NULL); 479#undef ADD_HANDLER
447 eeze_udev_watch_add(EEZE_UDEV_TYPE_DRIVE_REMOVABLE, EEZE_UDEV_EVENT_NONE, cb_stor_chg, NULL); 480
448 eeze_udev_watch_add(EEZE_UDEV_TYPE_DRIVE_CDROM, EEZE_UDEV_EVENT_NONE, cb_stor_chg, NULL); 481#define ADD_WATCH(type, cb) \
449 eeze_udev_watch_add(EEZE_UDEV_TYPE_DRIVE_MOUNTABLE, EEZE_UDEV_EVENT_NONE, cb_vol_chg, NULL); 482 uwl = eina_list_append(uwl, eeze_udev_watch_add(type, EEZE_UDEV_EVENT_NONE, cb, NULL))
450 483 ADD_WATCH(EEZE_UDEV_TYPE_DRIVE_INTERNAL, cb_stor_chg);
451 svr = ecore_con_server_add(ECORE_CON_LOCAL_SYSTEM, "eeze_scanner", 0, NULL); 484 ADD_WATCH(EEZE_UDEV_TYPE_DRIVE_REMOVABLE, cb_stor_chg);
452 ret = 2; 485 ADD_WATCH(EEZE_UDEV_TYPE_DRIVE_CDROM, cb_stor_chg);
453 EINA_SAFETY_ON_NULL_GOTO(svr, error); 486 ADD_WATCH(EEZE_UDEV_TYPE_DRIVE_MOUNTABLE, cb_vol_chg);
454 487#undef ADD_WATCH
455 storage_setup(); 488
489 path = ecore_con_local_path_new(EINA_TRUE, "eeze_scanner", 0);
490 if (!path)
491 {
492 fprintf(stderr, "ERROR: could not get local communication path\n");
493 retval = EXIT_FAILURE;
494 goto end;
495 }
496
497 loop = ecore_main_loop_get();
498
499#ifdef EFL_NET_SERVER_UNIX_CLASS
500 server = efl_add(EFL_NET_SERVER_SIMPLE_CLASS, loop,
501 efl_net_server_simple_inner_class_set(efl_added, EFL_NET_SERVER_UNIX_CLASS));
502#else
503 /* TODO: maybe start a TCP using locahost:12345?
504 * Right now eina_debug_monitor is only for AF_UNIX, so not an issue.
505 */
506 fprintf(stderr, "ERROR: your platform doesn't support Efl.Net.Server.Unix\n");
507#endif
508 if (!server)
509 {
510 fprintf(stderr, "ERROR: could not create communication server\n");
511 retval = EXIT_FAILURE;
512 goto end;
513 }
514
515 efl_event_callback_add(server, EFL_NET_SERVER_EVENT_CLIENT_ADD, cl_add, NULL);
516 efl_event_callback_add(server, EFL_NET_SERVER_EVENT_ERROR, server_error, NULL);
517
518#ifdef EFL_NET_SERVER_UNIX_CLASS
519 {
520 Eo *inner_server = efl_net_server_simple_inner_server_get(server);
521 efl_net_server_unix_unlink_before_bind_set(inner_server, EINA_TRUE);
522 efl_net_server_unix_leading_directories_create_set(inner_server, EINA_TRUE, 0700);
523 }
524#endif
525
526 err = efl_net_server_serve(server, path);
527 if (err)
528 {
529 fprintf(stderr, "ERROR: could not serve '%s': %s\n", path, eina_error_msg_get(err));
530 retval = EXIT_FAILURE;
531 goto end;
532 }
533
456 ecore_main_loop_begin(); 534 ecore_main_loop_begin();
457 535
458 ecore_con_server_del(svr); 536 end:
459 return 0; 537 efl_del(server);
460error: 538 server = NULL;
461 ERR("Could not start up!"); 539
462 exit(ret); 540 free(path);
541 path = NULL;
542
543 EINA_LIST_FREE(ehl, eh) ecore_event_handler_del(eh);
544 EINA_LIST_FREE(uwl, uw) eeze_udev_watch_del(uw);
545
546 error_setup:
547 eeze_shutdown();
548 ecore_con_shutdown();
549 eet_shutdown();
550 ecore_shutdown();
551 eina_shutdown();
552
553 return retval;
463} 554}