static void on_hangup(void *closure, struct afb_wsj1 *wsj1);
static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure);
static void emit(const char *api, const char *verb, const char *object);
static void on_hangup(void *closure, struct afb_wsj1 *wsj1);
static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg);
static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure);
static void emit(const char *api, const char *verb, const char *object);
static void usage(int status, char *arg0)
{
char *name = strrchr(arg0, '/');
name = name ? name + 1 : arg0;
static void usage(int status, char *arg0)
{
char *name = strrchr(arg0, '/');
name = name ? name + 1 : arg0;
- wsj1 = afb_ws_client_connect_wsj1(av[1], &itf, NULL);
+ /* get the default event loop */
+ rc = sd_event_default(&loop);
+ if (rc < 0) {
+ fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc));
+ return 1;
+ }
+
+ /* connect the websocket wsj1 to the uri given by the first argument */
+ wsj1 = afb_ws_client_connect_wsj1(loop, av[1], &itf, NULL);
- sd_event_add_io(afb_common_get_event_loop(), &evsrc, 0, EPOLLIN, io_event_callback, NULL);
+ sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
int rc;
printf("ON-CALL %s/%s(%s)\n", api, verb, afb_wsj1_msg_object_s(msg));
static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
int rc;
printf("ON-CALL %s/%s(%s)\n", api, verb, afb_wsj1_msg_object_s(msg));
rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
if (rc < 0)
fprintf(stderr, "replying failed: %m\n");
}
rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
if (rc < 0)
fprintf(stderr, "replying failed: %m\n");
}
static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
{
printf("ON-EVENT %s(%s)\n", event, afb_wsj1_msg_object_s(msg));
static void on_event(void *closure, const char *event, struct afb_wsj1_msg *msg)
{
printf("ON-EVENT %s(%s)\n", event, afb_wsj1_msg_object_s(msg));
-static void event(const char *event, const char *object)
-{
- int rc;
-
- rc = afb_wsj1_send_event_s(wsj1, event, object);
- if (rc < 0)
- fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
-}
-
+/* called when wsj1 receives a reply */
static void on_reply(void *closure, struct afb_wsj1_msg *msg)
{
printf("ON-REPLY %s: %s\n", (char*)closure, afb_wsj1_msg_object_s(msg));
static void on_reply(void *closure, struct afb_wsj1_msg *msg)
{
printf("ON-REPLY %s: %s\n", (char*)closure, afb_wsj1_msg_object_s(msg));
rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
callcount++;
rc = afb_wsj1_call_s(wsj1, api, verb, object, on_reply, key);
if (rc < 0) {
callcount++;
rc = afb_wsj1_call_s(wsj1, api, verb, object, on_reply, key);
if (rc < 0) {
+/* sends an event */
+static void event(const char *event, const char *object)
+{
+ int rc;
+
+ rc = afb_wsj1_send_event_s(wsj1, event, object);
+ if (rc < 0)
+ fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
+}
+
+/* emits either a call (when api!='!') or an event */
static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
{
static size_t count = 0;
static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
{
static size_t count = 0;
- if (api[0] == api[1] || verb[0] == verb[1] || rest[0] == rest[1])
- fprintf(stderr, "bad line: %s\n", line+pos);
- else {
+ if (api[0] == api[1]) {
+ /* empty line */
+ } else if (line[api[0]] == '#') {
+ /* comment */
+ } else if (verb[0] == verb[1]) {
+ fprintf(stderr, "verb missing, bad line: %s\n", line+pos);
+ } else {
line[api[1]] = line[verb[1]] = 0;
emit(line + api[0], line + verb[0], line + rest[0]);
}
line[api[1]] = line[verb[1]] = 0;
emit(line + api[0], line + verb[0], line + rest[0]);
}