Adds the library libafbwsc.so
[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 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 static struct afb_wsj1_itf itf = {
44         .on_hangup = on_hangup,
45         .on_call = on_call,
46         .on_event = on_event
47 };
48
49 static struct afb_wsj1 *wsj1;
50 static int exonrep;
51 static int callcount;
52 static sd_event_source *evsrc;
53
54 static void usage(int status, char *arg0)
55 {
56         char *name = strrchr(arg0, '/');
57         name = name ? name + 1 : arg0;
58         fprintf(status ? stderr : stdin, "usage: %s uri [api verb data]\n", name);
59         exit(status);
60 }
61
62 int main(int ac, char **av, char **env)
63 {
64         if (ac != 2 && ac != 5)
65                 usage(1, av[0]);
66         if (!strcmp(av[1], "-h") || !strcmp(av[1], "--help"))
67                 usage(0, av[0]);
68
69         wsj1 = afb_ws_client_connect_wsj1(av[1], &itf, NULL);
70         if (wsj1 == NULL) {
71                 fprintf(stderr, "connection to %s failed: %m\n", av[1]);
72                 return 1;
73         }
74
75         if (ac == 2) {
76                 fcntl(0, F_SETFL, O_NONBLOCK);
77                 sd_event_add_io(afb_common_get_event_loop(), &evsrc, 0, EPOLLIN, io_event_callback, NULL);
78         } else {
79                 exonrep = 1;
80                 emit(av[2], av[3], av[4]);
81         }
82         for(;;)
83                 sd_event_run(afb_common_get_event_loop(), 30000000);
84         return 0;
85 }
86
87 static void on_hangup(void *closure, struct afb_wsj1 *wsj1)
88 {
89         printf("ON-HANGUP\n");
90         exit(0);
91 }
92
93 static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
94 {
95         int rc;
96         printf("ON-CALL %s/%s(%s)\n", api, verb, afb_wsj1_msg_object_s(msg));
97         rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
98         if (rc < 0)
99                 fprintf(stderr, "replying failed: %m\n");
100 }
101
102 static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
103 {
104         printf("ON-EVENT %s(%s)\n", event, afb_wsj1_msg_object_s(msg));
105 }
106
107 static void event(const char *event, const char *object)
108 {
109         int rc;
110
111         rc = afb_wsj1_send_event_s(wsj1, event, object);
112         if (rc < 0)
113                 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
114 }
115
116 static void on_reply(void *closure, struct afb_wsj1_msg *msg)
117 {
118         printf("ON-REPLY %s: %s\n", (char*)closure, afb_wsj1_msg_object_s(msg));
119         free(closure);
120         callcount--;
121         if (exonrep && !callcount)
122                 //afb_wsj1_hangup(afb_wsj1_msg_wsj1(msg));
123                 exit(0);
124 }
125
126 static void call(const char *api, const char *verb, const char *object)
127 {
128         static int num = 0;
129         char *key;
130         int rc;
131
132         rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
133         callcount++;
134         rc = afb_wsj1_call_s(wsj1, api, verb, object, on_reply, key);
135         if (rc < 0) {
136                 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
137                 callcount--;
138         }
139 }
140
141 static void emit(const char *api, const char *verb, const char *object)
142 {
143         if (api[0] == '!' && api[1] == 0)
144                 event(verb, object);
145         else
146                 call(api, verb, object);
147 }
148
149 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
150 {
151         static size_t count = 0;
152         static char line[16384];
153         static char sep[] = " \t";
154         static char sepnl[] = " \t\n";
155
156         ssize_t rc;
157         size_t pos;
158
159         /* read the buffer */
160         do { rc = read(0, line + count, sizeof line - count); } while (rc < 0 && errno == EINTR);
161         if (rc < 0) {
162                 fprintf(stderr, "read error: %m\n");
163                 exit(1);
164         }
165         if (rc == 0) {
166                 if (!callcount)
167                         exit(0);
168                 exonrep = 1;
169                 sd_event_source_unref(evsrc);
170         }
171         count += (size_t)rc;
172
173         /* normalise the buffer content */
174         /* TODO: handle backspace \x7f */
175
176         /* process the lines */
177         pos = 0;
178         for(;;) {
179                 size_t i, api[2], verb[2], rest[2];
180                 i = pos;
181                 while(i < count && strchr(sep, line[i])) i++;
182                 api[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; api[1] = i;
183                 while(i < count && strchr(sep, line[i])) i++;
184                 verb[0] = i; while(i < count && !strchr(sepnl, line[i])) i++; verb[1] = i;
185                 while(i < count && strchr(sep, line[i])) i++;
186                 rest[0] = i; while(i < count && line[i] != '\n') i++; rest[1] = i;
187                 if (i == count) break;
188                 line[i++] = 0;
189                 if (api[0] == api[1] || verb[0] == verb[1] || rest[0] == rest[1])
190                         fprintf(stderr, "bad line: %s\n", line+pos);
191                 else {
192                         line[api[1]] = line[verb[1]] = 0;
193                         emit(line + api[0], line + verb[0], line + rest[0]);
194                 }
195                 pos = i;
196         }
197         count -= pos;
198         if (count == sizeof line) {
199                 fprintf(stderr, "overflow\n");
200                 exit(1);
201         }
202         if (count)
203                 memmove(line, line + pos, count);
204         return 1;
205 }
206