]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blobdiff - mt_server.c
doc, cleaning, no per session data
[coffee/mt-apps.git] / mt_server.c
index 76485d708dc8447ab0059a24f66d98580d28daac..d294cfa44f82058ca7ff61fc712b9d8e6c15ae47 100644 (file)
@@ -3,15 +3,13 @@
 #include <string.h>
 
 #include "mt_server.h"
-#include "ev_signal_exit.h"
-
-#define JSON_EMPTY "{\"type\":\"empty\"}"
+#include "signal_exit.h"
 
 static const struct lws_http_mount mount = {
     /* .mount_next */            NULL,         /* linked-list "next" */
-    /* .mountpoint */            "/",          /* mountpoint URL */
-    /* .origin */                "/usr/share/mt-server", /* serve from dir */
-    /* .def */                   "index.html", /* default filename */
+    /* .mountpoint */            HTTP_MOUNTPOINT,
+    /* .origin */                HTTP_ORIGIN,  /* serve from dir */
+    /* .def */                   HTTP_DEFAULT, /* default filename */
     /* .protocol */              NULL,
     /* .cgienv */                NULL,
     /* .extra_mimetypes */       NULL,
@@ -27,19 +25,17 @@ static const struct lws_http_mount mount = {
     /* .basic_auth_login_file */ NULL,
 };
 
-typedef struct per_vhost_data__dumb_increment {
+typedef struct per_vhost_data__merica_terminal {
     struct lws_context *context;
     struct lws_vhost *vhost;
     const struct lws_protocols *protocol;
-} per_vhost_data__dumb_increment;
+} per_vhost_data__merica_terminal;
 
-typedef struct per_session_data__dumb_increment {
-    uint8_t card_type;
-    uint8_t sak;
-    uint8_t uid_size;
-    char *uid;
+/*
+typedef struct per_session_data__merica_terminal {
     int number;
-} per_session_data__dumb_increment;
+} per_session_data__merica_terminal;
+*/
 
 enum protocols {
     PROTOCOL_HTTP = 0, // always first
@@ -52,18 +48,15 @@ static int callback_merica_terminal(struct lws *wsi,
 
 // list of supported protocols and callbacks
 static struct lws_protocols protocols[] = {
-    /* first protocol must always be HTTP handler */
+    // first protocol must always be HTTP handler
     {"http", lws_callback_http_dummy, 0, 0},
     {
-        "dumb-increment-protocol",
+        "merica-terminal-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 */
+        0, //sizeof(struct per_session_data__merica_terminal),
+        MT_PROTOCOL_RX_BUFFER_SIZE
     },
-    {NULL, NULL, 0, 0} /* terminator */
+    {NULL, NULL, 0, 0} // terminator
 };
 
 static int callback_merica_terminal(struct lws *wsi,
@@ -72,10 +65,12 @@ static int callback_merica_terminal(struct lws *wsi,
 {
     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 *)
+    /*
+    per_session_data__merica_terminal *pss =
+        (per_session_data__merica_terminal *)user;
+    */
+    per_vhost_data__merica_terminal *vhd =
+        (per_vhost_data__merica_terminal *)
         lws_protocol_vh_priv_get(vhost, prot);
 
     int n, m;
@@ -86,7 +81,7 @@ static int callback_merica_terminal(struct lws *wsi,
         case LWS_CALLBACK_PROTOCOL_INIT:
             vhd = lws_protocol_vh_priv_zalloc(
                       vhost, prot,
-                      sizeof(struct per_vhost_data__dumb_increment)
+                      sizeof(struct per_vhost_data__merica_terminal)
                   );
             vhd->context = lws_get_context(wsi);
             vhd->protocol = prot;
@@ -106,7 +101,7 @@ static int callback_merica_terminal(struct lws *wsi,
             n = strlen(line);
             m = lws_write(wsi, line, n, LWS_WRITE_TEXT);
             if (m < n) {
-                printf("ERROR %d writing to di socket\n", n);
+                fprintf(stderr, "ERROR %d writing to di socket\n", n);
                 return -1;
             }
             break;
@@ -117,7 +112,7 @@ static int callback_merica_terminal(struct lws *wsi,
                 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");
+                fprintf(stderr, "closing as WebSocket\n");
                 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
                                  (unsigned char *)"seeya", 5);
                 return -1;
@@ -139,7 +134,7 @@ static void fd_cb(EV_P_ ev_io *w_, int revents)
 
     read(w->w.fd, pos, 1);
 
-    if (*pos == '\n') {
+    if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
         *pos = 0;
         w->pos = w->text;
         lws_callback_on_writable_all_protocol(w->context,
@@ -152,7 +147,7 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
     struct lws_context_creation_info info;
 
     memset(&info, 0, sizeof(info));
-    info.port = 80;
+    info.port = HTTP_PORT;
     info.mounts = &mount;
     info.protocols = protocols;
     info.max_http_header_pool = 1;
@@ -168,7 +163,7 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
 
     ev_io_ws *w = &(self->fd_watcher);
     w->context = context;
-    w->text = (char *)malloc(512*sizeof(char));
+    w->text = (char *)malloc(INPUT_LINE_LENGTH*sizeof(char));
     if (!w->text) {
         perror("malloc");
         return -1;
@@ -182,7 +177,7 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
     return lws_ev_initloop(context, loop, 0);
 }
 
-void mt_server_deinit(mt_server_t *self)
+void mt_server_deinit(mt_server_t *self) //TODO wtf
 {
     //free(self->fd_watcher.text);
     //protocols[PROTOCOL_MERICA_TERMINAL].user = NULL;