Enforce flush of incoming data
[src/app-framework-binder.git] / src / afb-client-demo.c
1 /*
2  * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <errno.h>
30
31 #include <systemd/sd-event.h>
32
33 #include "afb-wsj1.h"
34 #include "afb-ws-client.h"
35
36 /* declaration of functions */
37 static void on_hangup(void *closure, struct afb_wsj1 *wsj1);
38 static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
39 static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
40 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure);
41 static void emit(const char *api, const char *verb, const char *object);
42
43 /* the callback interface for wsj1 */
44 static struct afb_wsj1_itf itf = {
45         .on_hangup = on_hangup,
46         .on_call = on_call,
47         .on_event = on_event
48 };
49
50 /* global variables */
51 static struct afb_wsj1 *wsj1;
52 static int exonrep;
53 static int callcount;
54 static sd_event_source *evsrc;
55
56 /* print usage of the program */
57 static void usage(int status, char *arg0)
58 {
59         char *name = strrchr(arg0, '/');
60         name = name ? name + 1 : arg0;
61         fprintf(status ? stderr : stdout, "usage: %s uri [api verb [data]]\n", name);
62         exit(status);
63 }
64
65 /* entry function */
66 int main(int ac, char **av, char **env)
67 {
68         int rc;
69         sd_event *loop;
70
71         /* check the argument count */
72         if (ac != 2 && ac != 4 && ac != 5)
73                 usage(1, av[0]);
74
75         /* emit error and exit if requested */
76         if (!strcmp(av[1], "-h") || !strcmp(av[1], "--help"))
77                 usage(0, av[0]);
78
79         /* get the default event loop */
80         rc = sd_event_default(&loop);
81         if (rc < 0) {
82                 fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc));
83                 return 1;
84         }
85
86         /* connect the websocket wsj1 to the uri given by the first argument */
87         wsj1 = afb_ws_client_connect_wsj1(loop, av[1], &itf, NULL);
88         if (wsj1 == NULL) {
89                 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
90                 return 1;
91         }
92
93         /* test the behaviour */
94         if (ac == 2) {
95                 /* get requests from stdin */
96                 fcntl(0, F_SETFL, O_NONBLOCK);
97                 sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
98         } else {
99                 /* the request is defined by the arguments */
100                 exonrep = 1;
101                 emit(av[2], av[3], av[4]);
102         }
103
104         /* loop until end */
105         for(;;)
106                 sd_event_run(loop, 30000000);
107         return 0;
108 }
109
110 /* called when wsj1 hangsup */
111 static void on_hangup(void *closure, struct afb_wsj1 *wsj1)
112 {
113         printf("ON-HANGUP\n");
114         fflush(stdout);
115         exit(0);
116 }
117
118 /* called when wsj1 receives a method invocation */
119 static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
120 {
121         int rc;
122         printf("ON-CALL %s/%s(%s)\n", api, verb, afb_wsj1_msg_object_s(msg));
123         fflush(stdout);
124         rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
125         if (rc < 0)
126                 fprintf(stderr, "replying failed: %m\n");
127 }
128
129 /* called when wsj1 receives an event */
130 static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
131 {
132         printf("ON-EVENT %s(%s)\n", event, afb_wsj1_msg_object_s(msg));
133         fflush(stdout);
134 }
135
136 /* called when wsj1 receives a reply */
137 static void on_reply(void *closure, struct afb_wsj1_msg *msg)
138 {
139         printf("ON-REPLY %s: %s\n", (char*)closure, afb_wsj1_msg_object_s(msg));
140         fflush(stdout);
141         free(closure);
142         callcount--;
143         if (exonrep && !callcount)
144                 //afb_wsj1_hangup(afb_wsj1_msg_wsj1(msg));
145                 exit(0);
146 }
147
148 /* makes a call */
149 static void call(const char *api, const char *verb, const char *object)
150 {
151         static int num = 0;
152         char *key;
153         int rc;
154
155         /* allocates an id for the request */
156         rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
157
158         /* send the request */
159         callcount++;
160         rc = afb_wsj1_call_s(wsj1, api, verb, object, on_reply, key);
161         if (rc < 0) {
162                 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
163                 callcount--;
164         }
165 }
166
167 /* sends an event */
168 static void event(const char *event, const char *object)
169 {
170         int rc;
171
172         rc = afb_wsj1_send_event_s(wsj1, event, object);
173         if (rc < 0)
174                 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
175 }
176
177 /* emits either a call (when api!='!') or an event */
178 static void emit(const char *api, const char *verb, const char *object)
179 {
180         if (object == NULL || object[0] == 0)
181                 object = "null";
182         if (api[0] == '!' && api[1] == 0)
183                 event(verb, object);
184         else
185                 call(api, verb, object);
186 }
187
188 /* called when something happens on stdin */
189 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
190 {
191         static size_t count = 0;
192         static char line[16384];
193         static char sep[] = " \t";
194         static char sepnl[] = " \t\n";
195
196         ssize_t rc;
197         size_t pos;
198
199         /* read the buffer */
200         do { rc = read(0, line + count, sizeof line - count); } while (rc < 0 && errno == EINTR);
201         if (rc < 0) {
202                 fprintf(stderr, "read error: %m\n");
203                 exit(1);
204         }
205         if (rc == 0) {
206                 if (!callcount)
207                         exit(0);
208                 exonrep = 1;
209                 sd_event_source_unref(evsrc);
210         }
211         count += (size_t)rc;
212
213         /* normalise the buffer content */
214         /* TODO: handle backspace \x7f ? */
215
216         /* process the lines */
217         pos = 0;
218         for(;;) {
219                 size_t i, api[2], verb[2], rest[2];
220                 i = pos;
221                 while(i < count && strchr(sep, line[i])) i++;
222                 api[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; api[1] = i;
223                 while(i < count && strchr(sep, line[i])) i++;
224                 verb[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; verb[1] = i;
225                 while(i < count && strchr(sep, line[i])) i++;
226                 rest[0] = i; while(i < count && line[i] != '\n') i++; rest[1] = i;
227                 if (i == count) break;
228                 line[i++] = 0;
229                 if (api[0] == api[1] || verb[0] == verb[1]) {
230                         if (api[0] != api[1] || verb[0] != verb[1])
231                                 fprintf(stderr, "bad line: %s\n", line+pos);
232                 } else {
233                         line[api[1]] = line[verb[1]] = 0;
234                         emit(line + api[0], line + verb[0], line + rest[0]);
235                 }
236                 pos = i;
237         }
238         count -= pos;
239         if (count == sizeof line) {
240                 fprintf(stderr, "overflow\n");
241                 exit(1);
242         }
243         if (count)
244                 memmove(line, line + pos, count);
245         return 1;
246 }
247