X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-client-demo.c;h=4cbc9c7a865beb6d1796fe6df4f019303caec933;hb=69d7968b9713dafa23268ee34516e50133ca8b36;hp=1ead5f749b6738609a319112ab43cfd71675d562;hpb=404f24c13cb45bd3e9ca837bc033fd6840cbf669;p=src%2Fapp-framework-binder.git diff --git a/src/afb-client-demo.c b/src/afb-client-demo.c index 1ead5f74..4cbc9c7a 100644 --- a/src/afb-client-demo.c +++ b/src/afb-client-demo.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015, 2016 "IoT.bzh" + * Copyright (C) 2015, 2016, 2017 "IoT.bzh" * Author "Fulup Ar Foll" * Author José Bollo * @@ -65,6 +65,9 @@ static void usage(int status, char *arg0) /* entry function */ int main(int ac, char **av, char **env) { + int rc; + sd_event *loop; + /* check the argument count */ if (ac != 2 && ac != 4 && ac != 5) usage(1, av[0]); @@ -73,8 +76,15 @@ int main(int ac, char **av, char **env) if (!strcmp(av[1], "-h") || !strcmp(av[1], "--help")) usage(0, av[0]); + /* get the default event loop */ + rc = sd_event_default(&loop); + if (rc < 0) { + fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc)); + return 1; + } + /* connect the websocket wsj1 to the uri given by the first argument */ - wsj1 = afb_ws_client_connect_wsj1(av[1], &itf, NULL); + wsj1 = afb_ws_client_connect_wsj1(loop, av[1], &itf, NULL); if (wsj1 == NULL) { fprintf(stderr, "connection to %s failed: %m\n", av[1]); return 1; @@ -84,7 +94,7 @@ int main(int ac, char **av, char **env) if (ac == 2) { /* get requests from stdin */ fcntl(0, F_SETFL, O_NONBLOCK); - sd_event_add_io(afb_ws_client_get_event_loop(), &evsrc, 0, EPOLLIN, io_event_callback, NULL); + sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL); } else { /* the request is defined by the arguments */ exonrep = 1; @@ -93,7 +103,7 @@ int main(int ac, char **av, char **env) /* loop until end */ for(;;) - sd_event_run(afb_ws_client_get_event_loop(), 30000000); + sd_event_run(loop, 30000000); return 0; } @@ -101,6 +111,7 @@ int main(int ac, char **av, char **env) static void on_hangup(void *closure, struct afb_wsj1 *wsj1) { printf("ON-HANGUP\n"); + fflush(stdout); exit(0); } @@ -109,6 +120,7 @@ static void on_call(void *closure, const char *api, const char *verb, struct afb { int rc; printf("ON-CALL %s/%s(%s)\n", api, verb, afb_wsj1_msg_object_s(msg)); + fflush(stdout); rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL); if (rc < 0) fprintf(stderr, "replying failed: %m\n"); @@ -118,12 +130,14 @@ static void on_call(void *closure, const char *api, const char *verb, struct afb static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg) { printf("ON-EVENT %s(%s)\n", event, afb_wsj1_msg_object_s(msg)); + fflush(stdout); } /* called when wsj1 receives a reply */ static void on_reply(void *closure, struct afb_wsj1_msg *msg) { printf("ON-REPLY %s: %s\n", (char*)closure, afb_wsj1_msg_object_s(msg)); + fflush(stdout); free(closure); callcount--; if (exonrep && !callcount) @@ -212,9 +226,10 @@ static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, voi rest[0] = i; while(i < count && line[i] != '\n') i++; rest[1] = i; if (i == count) break; line[i++] = 0; - if (api[0] == api[1] || verb[0] == verb[1]) - fprintf(stderr, "bad line: %s\n", line+pos); - else { + if (api[0] == api[1] || verb[0] == verb[1]) { + if (api[0] != api[1] || verb[0] != verb[1]) + fprintf(stderr, "bad line: %s\n", line+pos); + } else { line[api[1]] = line[verb[1]] = 0; emit(line + api[0], line + verb[0], line + rest[0]); }