]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blobdiff - mt_server.c
almost ready
[coffee/mt-apps.git] / mt_server.c
index 2df7568c07b66af70456b772da3843c9003ebfa5..fb5eb1b22410382748a95519bdf1bc88ff7619c6 100644 (file)
@@ -1,27 +1,9 @@
 #include <stdlib.h>
 #include <stdio.h>
-#include <libwebsockets.h>
 #include <string.h>
-#include <ev.h>
-
-char *uids[10] = {
-    "evelyn",
-    "sera",
-    "solas",
-    "iron bull",
-    "cassandra",
-    "varric",
-    "dorian",
-    "vivienne",
-    "blackwall",
-    "cole"
-};
 
-#define JSON_STRING "{" \
-    "\"type\": \"rfid\"," \
-    "\"uid\": \"%s\"," \
-    "\"size\": %d" \
-    "}\n"
+#include "mt_server.h"
+#include "ev_signal_exit.h"
 
 static const struct lws_http_mount mount = {
     /* .mount_next */            NULL,         /* linked-list "next" */
@@ -57,40 +39,59 @@ typedef struct per_session_data__dumb_increment {
     int number;
 } per_session_data__dumb_increment;
 
-typedef struct user_data {
-    //ev_ufr_io *ufr;
-} user_data;
+#define JSON_EMPTY "{\"type\":\"empty\"}"
+
+enum protocols {
+    PROTOCOL_HTTP = 0, // always first
+    PROTOCOL_MERICA_TERMINAL
+};
 
-    static char line[512];
+static int callback_merica_terminal(struct lws *wsi,
+                                    enum lws_callback_reasons reason,
+                                    void *user, void *in, size_t len);
+
+/* list of supported protocols and callbacks */
+
+static struct lws_protocols protocols[] = {
+    /* first protocol must always be HTTP handler */
+    {"http", lws_callback_http_dummy, 0, 0},
+    {
+        "dumb-increment-protocol",
+        callback_merica_terminal,
+        sizeof(struct per_session_data__dumb_increment),
+        128, /* rx buf size must be >= permessage-deflate rx size
+             * dumb-increment only sends very small packets, so we set
+             * this accordingly.  If your protocol will send bigger
+             * things, adjust this to match */
+    },
+    {NULL, NULL, 0, 0} /* terminator */
+};
 
-static int callback_dumb_increment(struct lws *wsi,
-                                   enum lws_callback_reasons reason,
-                                   void *user, void *in, size_t len)
+static int callback_merica_terminal(struct lws *wsi,
+                                    enum lws_callback_reasons reason,
+                                    void *user, void *in, size_t len)
 {
+    struct lws_vhost *vhost = lws_get_vhost(wsi);
+    const struct lws_protocols *prot = lws_get_protocol(wsi);
     per_session_data__dumb_increment *pss =
         (per_session_data__dumb_increment *)user;
     per_vhost_data__dumb_increment *vhd =
         (per_vhost_data__dumb_increment *)
-        lws_protocol_vh_priv_get(lws_get_vhost(wsi), lws_get_protocol(wsi));
-
-    struct lws_context *context = lws_get_context(wsi);
-    user_data *data = (user_data *)lws_context_user(context);
+        lws_protocol_vh_priv_get(vhost, prot);
 
-
-    unsigned char buf[LWS_PRE + 512];
-    unsigned char *p = &buf[LWS_PRE];
     int n, m;
 
+    char *line = (char *)prot->user;
+
     switch (reason) {
         case LWS_CALLBACK_PROTOCOL_INIT:
             vhd = lws_protocol_vh_priv_zalloc(
-                      lws_get_vhost(wsi),
-                      lws_get_protocol(wsi),
+                      vhost, prot,
                       sizeof(struct per_vhost_data__dumb_increment)
                   );
             vhd->context = lws_get_context(wsi);
-            vhd->protocol = lws_get_protocol(wsi);
-            vhd->vhost = lws_get_vhost(wsi);
+            vhd->protocol = prot;
+            vhd->vhost = vhost;
             break;
 
         case LWS_CALLBACK_PROTOCOL_DESTROY:
@@ -100,13 +101,10 @@ static int callback_dumb_increment(struct lws *wsi,
             break;
 
         case LWS_CALLBACK_ESTABLISHED:
-            pss->number = 0;
             break;
 
         case LWS_CALLBACK_SERVER_WRITEABLE:
-            n = sprintf((char *)p, JSON_STRING, uids[pss->number % 10], pss->number);
-            pss->number++;
-                       n = strlen(line);
+            n = strlen(line);
             m = lws_write(wsi, line, n, LWS_WRITE_TEXT);
             if (m < n) {
                 printf("ERROR %d writing to di socket\n", n);
@@ -115,13 +113,11 @@ static int callback_dumb_increment(struct lws *wsi,
             break;
 
         case LWS_CALLBACK_RECEIVE:
-            if (len < 6) {
-                break;
-            }
-            if (strcmp((const char *)in, "reset\n") == 0) {
-                pss->number = 0;
-            }
-            if (strcmp((const char *)in, "close\n") == 0) {
+            if (strcmp((const char *)in, "reset") == 0) {
+                strcpy(line, JSON_EMPTY);
+                lws_callback_on_writable_all_protocol(lws_get_context(wsi),
+                                                      &protocols[PROTOCOL_MERICA_TERMINAL]);
+            } else if (strcmp((const char *)in, "close") == 0) {
                 printf("dumb_inc: closing as requested\n");
                 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
                                  (unsigned char *)"seeya", 5);
@@ -136,129 +132,92 @@ static int callback_dumb_increment(struct lws *wsi,
     return 0;
 }
 
-enum demo_protocols {
-    /* always first */
-    PROTOCOL_HTTP = 0,
-
-    PROTOCOL_DUMB_INCREMENT,
-    PROTOCOL_LWS_MIRROR,
-    PROTOCOL_LWS_STATUS,
-
-    /* always last */
-    DEMO_PROTOCOL_COUNT
-};
+/*static void ev_timeout_cb(EV_P_ ev_timer *w, int revents)
+{
+    lws_callback_on_writable_all_protocol(context,
+                                          &protocols[PROTOCOL_MERICA_TERMINAL]);
+}*/
 
-/* list of supported protocols and callbacks */
 
-static struct lws_protocols protocols[] = {
-    /* first protocol must always be HTTP handler */
-    {"http", lws_callback_http_dummy, 0, 0},
-    {
-        "dumb-increment-protocol",
-        callback_dumb_increment,
-        sizeof(struct per_session_data__dumb_increment),
-        128, /* rx buf size must be >= permessage-deflate rx size
-             * dumb-increment only sends very small packets, so we set
-             * this accordingly.  If your protocol will send bigger
-             * things, adjust this to match */
-    },
-    {NULL, NULL, 0, 0} /* terminator */
-};
+static void fd_cb(EV_P_ ev_io *w_, int revents)
+{
+    ev_io_ws *w = (ev_io_ws *)w_;
 
-struct lws_context *context;
+    char *pos = w->pos++;
 
-static void ev_timeout_cb(EV_P_ ev_timer *w, int revents)
-{
-    lws_callback_on_writable_all_protocol(context,
-                                          &protocols[PROTOCOL_DUMB_INCREMENT]);
-}
+    read(w->w.fd, pos, 1);
 
-static void signal_cb(EV_P_ ev_signal *w, int revents)
-{
-    fprintf(stderr, "signal caught, terminating\n");
-    switch (w->signum) {
-        case SIGTERM:
-        case SIGINT:
-            ev_break(loop, EVBREAK_ALL);
-            break;
-        default:
-            signal(SIGABRT, SIG_DFL);
-            abort();
-            break;
+    if (*pos == '\n') {
+        *pos = 0;
+        w->pos = w->text;
+        lws_callback_on_writable_all_protocol(w->context,
+                                              &protocols[PROTOCOL_MERICA_TERMINAL]);
     }
-}
-
 
+}
 
-int server_init(void *user)
+int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
 {
     struct lws_context_creation_info info;
 
-       protocols[PROTOCOL_DUMB_INCREMENT].user = (void *)line;
-
-    memset(&info, 0, sizeof info);
+    memset(&info, 0, sizeof(info));
     info.port = 80;
     info.mounts = &mount;
     info.protocols = protocols;
     info.max_http_header_pool = 1;
     info.options |= LWS_SERVER_OPTION_LIBEV;
-    info.user = user;
 
-    context = lws_create_context(&info);
+    struct lws_context *context = lws_create_context(&info);
     if (!context) {
         fprintf(stderr, "lws_create_context failed\n");
         return -1;
     }
 
-    return 0;
-}
+    self->context = context;
 
+    ev_io_ws *w = &(self->fd_watcher);
+    w->context = context;
+    w->text = (char *)malloc(512*sizeof(char));
+    if (!w->text) {
+        perror("malloc");
+        return -1;
+    }
+    strcpy(w->text, JSON_EMPTY);
+    w->pos = w->text;
+    protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->text;
+    ev_io_init(&(w->w), fd_cb, fd, EV_READ);
+    ev_io_start(loop, (ev_io *)w);
 
+    return lws_ev_initloop(context, loop, 0);
+}
 
-static void stdin_cb(EV_P_ ev_io *w, int revents)
+void mt_server_deinit(mt_server_t *self)
 {
-    static int pos = 0;
-
-    read(w->fd, &line[pos++], 1);
-
-    if (line[pos-1] == '\n') {
-        line[pos] = 0;
-        pos = 0;
-//        puts(line);
-        lws_callback_on_writable_all_protocol(context,
-                                              &protocols[PROTOCOL_DUMB_INCREMENT]);
-    }
-
+    //free(self->fd_watcher.text);
+    //protocols[PROTOCOL_MERICA_TERMINAL].user = NULL;
+    lws_context_destroy(self->context);
 }
 
+#ifdef IS_MAIN
 int main(int argc, const char **argv)
 {
-    int sigs[] = { SIGINT, SIGKILL, SIGTERM, SIGSEGV, SIGFPE };
-    struct ev_signal signals[ARRAY_SIZE(sigs)];
-    ev_io stdin_watcher;
     struct ev_loop *loop = ev_default_loop(0);
-    ev_timer timeout_watcher;
+    mt_server_t server;
 
-    for (int n = 0; n < ARRAY_SIZE(sigs); n++) {
-        ev_init(&signals[n], signal_cb);
-        ev_signal_set(&signals[n], sigs[n]);
-        ev_signal_start(loop, &signals[n]);
-    }
+    set_signal_exit(loop);
 
-    if (server_init(NULL) == -1) {
+    if (mt_server_init(&server, loop, STDIN_FILENO) == -1) {
         return -1;
     }
-    lws_ev_initloop(context, loop, 0);
-
-    ev_io_init(&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
-    ev_io_start(loop, &stdin_watcher);
 
-    ev_timer_init(&timeout_watcher, ev_timeout_cb, 1, 1);
+    //ev_timer timeout_watcher;
+    //ev_timer_init(&timeout_watcher, ev_timeout_cb, 1, 1);
     //ev_timer_start(loop, &timeout_watcher);
 
     ev_run(loop, 0);
 
-    lws_context_destroy(context);
+    mt_server_deinit(&server);
 
     return 0;
 }
+#endif