afb-hreq.c: fix etag size (fixes stack smashing detected by stack protector)
[src/app-framework-binder.git] / src / afb-client-demo.c
1 /* 
2  * Copyright (C) 2015, 2016 "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-common.h"  /* TODO: remove dependency to afb-common.h */
34 #include "afb-wsj1.h"
35 #include "afb-ws-client.h"
36
37 /* declaration of functions */
38 static void on_hangup(void *closure, struct afb_wsj1 *wsj1);
39 static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
40 static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
41 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure);
42 static void emit(const char *api, const char *verb, const char *object);
43
44 /* the callback interface for wsj1 */
45 static struct afb_wsj1_itf itf = {
46         .on_hangup = on_hangup,
47         .on_call = on_call,
48         .on_event = on_event
49 };
50
51 /* global variables */
52 static struct afb_wsj1 *wsj1;
53 static int exonrep;
54 static int callcount;
55 static sd_event_source *evsrc;
56
57 /* print usage of the program */
58 static void usage(int status, char *arg0)
59 {
60         char *name = strrchr(arg0, '/');
61         name = name ? name + 1 : arg0;
62         fprintf(status ? stderr : stdout, "usage: %s uri [api verb [data]]\n", name);
63         exit(status);
64 }
65
66 /* entry function */
67 int main(int ac, char **av, char **env)
68 {
69         /* check the argument count */
70         if (ac != 2 && ac != 4 && ac != 5)
71                 usage(1, av[0]);
72
73         /* emit error and exit if requested */
74         if (!strcmp(av[1], "-h") || !strcmp(av[1], "--help"))
75                 usage(0, av[0]);
76
77         /* connect the websocket wsj1 to the uri given by the first argument */
78         wsj1 = afb_ws_client_connect_wsj1(av[1], &itf, NULL);
79         if (wsj1 == NULL) {
80                 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
81                 return 1;
82         }
83
84         /* test the behaviour */
85         if (ac == 2) {
86                 /* get requests from stdin */
87                 fcntl(0, F_SETFL, O_NONBLOCK);
88                 sd_event_add_io(afb_common_get_event_loop(), &evsrc, 0, EPOLLIN, io_event_callback, NULL);
89         } else {
90                 /* the request is defined by the arguments */
91                 exonrep = 1;
92                 emit(av[2], av[3], av[4]);
93         }
94
95         /* loop until end */
96         for(;;)
97                 sd_event_run(afb_common_get_event_loop(), 30000000);
98         return 0;
99 }
100
101 /* called when wsj1 hangsup */
102 static void on_hangup(void *closure, struct afb_wsj1 *wsj1)
103 {
104         printf("ON-HANGUP\n");
105         exit(0);
106 }
107
108 /* called when wsj1 receives a method invocation */
109 static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
110 {
111         int rc;
112         printf("ON-CALL %s/%s(%s)\n", api, verb, afb_wsj1_msg_object_s(msg));
113         rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
114         if (rc < 0)
115                 fprintf(stderr, "replying failed: %m\n");
116 }
117
118 /* called when wsj1 receives an event */
119 static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
120 {
121         printf("ON-EVENT %s(%s)\n", event, afb_wsj1_msg_object_s(msg));
122 }
123
124 /* called when wsj1 receives a reply */
125 static void on_reply(void *closure, struct afb_wsj1_msg *msg)
126 {
127         printf("ON-REPLY %s: %s\n", (char*)closure, afb_wsj1_msg_object_s(msg));
128         free(closure);
129         callcount--;
130         if (exonrep && !callcount)
131                 //afb_wsj1_hangup(afb_wsj1_msg_wsj1(msg));
132                 exit(0);
133 }
134
135 /* makes a call */
136 static void call(const char *api, const char *verb, const char *object)
137 {
138         static int num = 0;
139         char *key;
140         int rc;
141
142         /* allocates an id for the request */
143         rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
144
145         /* send the request */
146         callcount++;
147         rc = afb_wsj1_call_s(wsj1, api, verb, object, on_reply, key);
148         if (rc < 0) {
149                 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
150                 callcount--;
151         }
152 }
153
154 /* sends an event */
155 static void event(const char *event, const char *object)
156 {
157         int rc;
158
159         rc = afb_wsj1_send_event_s(wsj1, event, object);
160         if (rc < 0)
161                 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
162 }
163
164 /* emits either a call (when api!='!') or an event */
165 static void emit(const char *api, const char *verb, const char *object)
166 {
167         if (object == NULL || object[0] == 0)
168                 object = "null";
169         if (api[0] == '!' && api[1] == 0)
170                 event(verb, object);
171         else
172                 call(api, verb, object);
173 }
174
175 /* called when something happens on stdin */
176 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
177 {
178         static size_t count = 0;
179         static char line[16384];
180         static char sep[] = " \t";
181         static char sepnl[] = " \t\n";
182
183         ssize_t rc;
184         size_t pos;
185
186         /* read the buffer */
187         do { rc = read(0, line + count, sizeof line - count); } while (rc < 0 && errno == EINTR);
188         if (rc < 0) {
189                 fprintf(stderr, "read error: %m\n");
190                 exit(1);
191         }
192         if (rc == 0) {
193                 if (!callcount)
194                         exit(0);
195                 exonrep = 1;
196                 sd_event_source_unref(evsrc);
197         }
198         count += (size_t)rc;
199
200         /* normalise the buffer content */
201         /* TODO: handle backspace \x7f ? */
202
203         /* process the lines */
204         pos = 0;
205         for(;;) {
206                 size_t i, api[2], verb[2], rest[2];
207                 i = pos;
208                 while(i < count && strchr(sep, line[i])) i++;
209                 api[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; api[1] = i;
210                 while(i < count && strchr(sep, line[i])) i++;
211                 verb[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; verb[1] = i;
212                 while(i < count && strchr(sep, line[i])) i++;
213                 rest[0] = i; while(i < count && line[i] != '\n') i++; rest[1] = i;
214                 if (i == count) break;
215                 line[i++] = 0;
216                 if (api[0] == api[1] || verb[0] == verb[1])
217                         fprintf(stderr, "bad line: %s\n", line+pos);
218                 else {
219                         line[api[1]] = line[verb[1]] = 0;
220                         emit(line + api[0], line + verb[0], line + rest[0]);
221                 }
222                 pos = i;
223         }
224         count -= pos;
225         if (count == sizeof line) {
226                 fprintf(stderr, "overflow\n");
227                 exit(1);
228         }
229         if (count)
230                 memmove(line, line + pos, count);
231         return 1;
232 }
233