afb-client-demo: Add option to keep running 09/14609/1
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 22 Jun 2018 12:00:43 +0000 (14:00 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 22 Jun 2018 12:00:43 +0000 (14:00 +0200)
This option allows to keep the client running
even when its input is closed. This will be used for
tests and for reporting because it allows to write:

  echo 'monitor trace {"add":{"api":"*","request":"*","event":"*","session":"*","global":"*"}}' |
  afb-client -k $WSURL > logfile

to capture all events.

Change-Id: If1c7804556190fd8ec9388260fe014c50b498b1d
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/main-afb-client-demo.c

index 4e86587..fab56f1 100644 (file)
@@ -80,6 +80,7 @@ static int exonrep;
 static int callcount;
 static int human;
 static int raw;
+static int keeprun;
 static int direct;
 static int echo;
 static sd_event_source *evsrc;
@@ -100,6 +101,7 @@ static void usage(int status, char *arg0)
                "  --help, -h          Display this help\n"
                "  --human, -H         Display human readable JSON\n"
                "  --raw, -r           Raw output (default)\n"
+               "  --keep-running, -k  Keep running until disconnect, even if input closed\n"
                "Example:\n"
                " %s --human 'localhost:1234/api?token=HELLO&uuid=magic' hello ping\n"
                "\n", name
@@ -135,6 +137,9 @@ int main(int ac, char **av, char **env)
                        else if (!strcmp(av[1], "--break")) /* request to break connection */
                                breakcon = 1;
 
+                       else if (!strcmp(av[1], "--keep-running")) /* request to break connection */
+                               keeprun = 1;
+
                        else if (!strcmp(av[1], "--echo")) /* request to echo inputs */
                                echo = 1;
 
@@ -149,6 +154,7 @@ int main(int ac, char **av, char **env)
                                case 'r': raw = 1; break;
                                case 'd': direct = 1; break;
                                case 'b': breakcon = 1; break;
+                               case 'k': keeprun = 1; break;
                                case 'e': echo = 1; break;
                                default: usage(av[1][rc] != 'h', a0);
                                }
@@ -195,7 +201,7 @@ int main(int ac, char **av, char **env)
                sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
        } else {
                /* the request is defined by the arguments */
-               exonrep = 1;
+               exonrep = !keeprun;
                if (direct)
                        pws_call(av[2], av[3]);
                else
@@ -336,9 +342,11 @@ static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, voi
                exit(1);
        }
        if (rc == 0) {
-               if (!callcount)
-                       exit(0);
-               exonrep = 1;
+               if (!keeprun) {
+                       if (!callcount)
+                               exit(0);
+                       exonrep = 1;
+               }
                sd_event_source_unref(evsrc);
        }
        count += (size_t)rc;