cleaning copyrights
[src/app-framework-binder.git] / src / afb-ws.c
index da248c8..3479681 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 IoT.bzh
+ * Copyright (C) 2016 "IoT.bzh"
  * Author: José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
 #include <errno.h>
 #include <sys/uio.h>
 #include <string.h>
+#include <stdarg.h>
+
+#include <systemd/sd-event.h>
 
 #include "websock.h"
 #include "afb-ws.h"
 
-#include "utils-upoll.h"
+#include "afb-common.h"
 
 /*
  * declaration of the websock interface for afb-ws
@@ -85,7 +88,7 @@ struct afb_ws
        const struct afb_ws_itf *itf; /* the callback interface */
        void *closure;          /* closure when calling the callbacks */
        struct websock *ws;     /* the websock handler */
-       struct upoll *up;       /* the upoll handler for the socket */
+       sd_event_source *evsrc; /* the event source for the socket */
        struct buf buffer;      /* the last read fragment */
 };
 
@@ -109,8 +112,8 @@ static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
        struct websock *wsi = ws->ws;
        if (wsi != NULL) {
                ws->ws = NULL;
-               upoll_close(ws->up);
-               ws->up = NULL;
+               sd_event_source_unref(ws->evsrc);
+               ws->evsrc = NULL;
                websock_destroy(wsi);
                free(aws_pick_buffer(ws).buffer);
                ws->state = waiting;
@@ -119,6 +122,15 @@ static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
        }
 }
 
+static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *ws)
+{
+       if ((revents & EPOLLIN) != 0)
+               aws_on_readable(ws);
+       if ((revents & EPOLLHUP) != 0)
+               afb_ws_hangup(ws);
+       return 0;
+}
+
 /*
  * Creates the afb_ws structure for the file descritor
  * 'fd' and the callbacks described by the interface 'itf'
@@ -128,6 +140,7 @@ static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
  */
 struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure)
 {
+       int rc;
        struct afb_ws *result;
 
        assert(fd >= 0);
@@ -150,15 +163,12 @@ struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure
        if (result->ws == NULL)
                goto error2;
 
-       /* creates the upoll */
-       result->up = upoll_open(result->fd, result);
-       if (result->up == NULL)
+       /* creates the evsrc */
+       rc = sd_event_add_io(afb_common_get_event_loop(), &result->evsrc, result->fd, EPOLLIN, io_event_callback, result);
+       if (rc < 0) {
+               errno = -rc;
                goto error3;
-
-       /* init the upoll */
-       upoll_on_readable(result->up, (void*)aws_on_readable);
-       upoll_on_hangup(result->up, (void*)afb_ws_hangup);
-
+       }
        return result;
 
 error3:
@@ -235,6 +245,40 @@ int afb_ws_text(struct afb_ws *ws, const char *text, size_t length)
        return websock_text(ws->ws, 1, text, length);
 }
 
+/*
+ * Sends a variable list of texts to the endpoint of 'ws'.
+ * Returns 0 on success or -1 in case of error.
+ */
+int afb_ws_texts(struct afb_ws *ws, ...)
+{
+       va_list args;
+       struct iovec ios[32];
+       int count;
+       const char *s;
+
+       if (ws->ws == NULL) {
+               /* disconnected */
+               errno = EPIPE;
+               return -1;
+       }
+
+       count = 0;
+       va_start(args, ws);
+       s = va_arg(args, const char *);
+       while (s != NULL) {
+               if (count == 32) {
+                       errno = EINVAL;
+                       return -1;
+               }
+               ios[count].iov_base = (void*)s;
+               ios[count].iov_len = strlen(s);
+               count++;
+               s = va_arg(args, const char *);
+       }
+       va_end(args);
+       return websock_text_v(ws->ws, 1, ios, count);
+}
+
 /*
  * Sends a binary 'data' of 'length' to the endpoint of 'ws'.
  * Returns 0 on success or -1 in case of error.
@@ -416,4 +460,3 @@ static void aws_on_error(struct afb_ws *ws, uint16_t code, const void *data, siz
 }
 
 
-