afb-client-demo: Add option -e/--echo
[src/app-framework-binder.git] / src / afb-client-demo.c
index ae914a4..6a0fef5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ * Copyright (C) 2015-2018 "IoT.bzh"
  * Author "Fulup Ar Foll"
  * Author José Bollo <jose.bollo@iot.bzh>
  *
@@ -79,11 +79,13 @@ static struct afb_proto_ws_client_itf pws_itf = {
 /* global variables */
 static struct afb_wsj1 *wsj1;
 static struct afb_proto_ws *pws;
+static int breakcon;
 static int exonrep;
 static int callcount;
 static int human;
 static int raw;
 static int direct;
+static int echo;
 static sd_event_source *evsrc;
 static char *sessionid = "afb-client-demo";
 
@@ -92,8 +94,8 @@ static void usage(int status, char *arg0)
 {
        char *name = strrchr(arg0, '/');
        name = name ? name + 1 : arg0;
-       fprintf(status ? stderr : stdout, "usage: %s [-H [-r]] uri [api verb [data]]\n", name);
-       fprintf(status ? stderr : stdout, "       %s -d [-H [-r]] uri [verb [data]]\n", name);
+       fprintf(status ? stderr : stdout, "usage: %s [-H [-r]] [-b] [-e] uri [api verb [data]]\n", name);
+       fprintf(status ? stderr : stdout, "       %s -d [-H [-r]] [-b] [-e] uri [verb [data]]\n", name);
        exit(status);
 }
 
@@ -121,6 +123,12 @@ int main(int ac, char **av, char **env)
                        else if (!strcmp(av[1], "--direct")) /* request for direct api */
                                direct = 1;
 
+                       else if (!strcmp(av[1], "--break")) /* request to break connection */
+                               breakcon = 1;
+
+                       else if (!strcmp(av[1], "--echo")) /* request to echo inputs */
+                               echo = 1;
+
                        /* emit usage and exit */
                        else
                                usage(!!strcmp(av[1], "--help"), a0);
@@ -131,6 +139,8 @@ int main(int ac, char **av, char **env)
                                case 'H': human = 1; break;
                                case 'r': raw = 1; break;
                                case 'd': direct = 1; break;
+                               case 'b': breakcon = 1; break;
+                               case 'e': echo = 1; break;
                                default: usage(av[1][rc] != 'h', a0);
                                }
                }
@@ -258,6 +268,10 @@ static void wsj1_call(const char *api, const char *verb, const char *object)
        /* allocates an id for the request */
        rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
 
+       /* echo the command if asked */
+       if (echo)
+               printf("SEND-CALL %s/%s %s\n", api, verb, object?:"null");
+
        /* send the request */
        callcount++;
        rc = afb_wsj1_call_s(wsj1, api, verb, object, on_wsj1_reply, key);
@@ -272,6 +286,10 @@ static void wsj1_event(const char *event, const char *object)
 {
        int rc;
 
+       /* echo the command if asked */
+       if (echo)
+               printf("SEND-EVENT: %s %s\n", event, object?:"null");
+
        rc = afb_wsj1_send_event_s(wsj1, event, object);
        if (rc < 0)
                fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
@@ -282,10 +300,13 @@ static void wsj1_emit(const char *api, const char *verb, const char *object)
 {
        if (object == NULL || object[0] == 0)
                object = "null";
+
        if (api[0] == '!' && api[1] == 0)
                wsj1_event(verb, object);
        else
                wsj1_call(api, verb, object);
+       if (breakcon)
+               exit(0);
 }
 
 /* called when something happens on stdin */
@@ -441,6 +462,10 @@ static void pws_call(const char *verb, const char *object)
        /* allocates an id for the request */
        rc = asprintf(&key, "%d:%s", ++num, verb);
 
+       /* echo the command if asked */
+       if (echo)
+               printf("SEND-CALL: %s %s\n", verb, object?:"null");
+
        /* send the request */
        callcount++;
        if (object == NULL || object[0] == 0 || !strcmp(object, "null"))
@@ -451,10 +476,13 @@ static void pws_call(const char *verb, const char *object)
                        o = json_object_new_string(object);
        }
        rc = afb_proto_ws_client_call(pws, verb, o, sessionid, key);
+       json_object_put(o);
        if (rc < 0) {
                fprintf(stderr, "calling %s(%s) failed: %m\n", verb, object?:"");
                dec_callcount();
        }
+       if (breakcon)
+               exit(0);
 }
 
 /* called when pws hangsup */