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);
if (wsj1 == NULL) {
fprintf(stderr, "connection to %s failed: %m\n", av[1]);
return 1;
}
wsj1 = afb_ws_client_connect_wsj1(av[1], &itf, NULL);
if (wsj1 == NULL) {
fprintf(stderr, "connection to %s failed: %m\n", av[1]);
return 1;
}
fcntl(0, F_SETFL, O_NONBLOCK);
sd_event_add_io(afb_common_get_event_loop(), &evsrc, 0, EPOLLIN, io_event_callback, NULL);
} else {
fcntl(0, F_SETFL, O_NONBLOCK);
sd_event_add_io(afb_common_get_event_loop(), &evsrc, 0, EPOLLIN, io_event_callback, NULL);
} else {
static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
int rc;
static void on_call(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
{
int rc;
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;
fprintf(stderr, "bad line: %s\n", line+pos);
else {
line[api[1]] = line[verb[1]] = 0;
fprintf(stderr, "bad line: %s\n", line+pos);
else {
line[api[1]] = line[verb[1]] = 0;