]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blobdiff - mt_server.c
Make error message more useful
[coffee/mt-apps.git] / mt_server.c
index fa099bbd59e90d0f4630f6ad34b6214a2c976532..567c3af6fe08d2dd2c53bf59aa0d27e48e09b637 100644 (file)
@@ -10,6 +10,7 @@ char *new_line()
     char *line = (char *)malloc((LWS_PRE + INPUT_LINE_LENGTH)*sizeof(char));
     if (!line) {
         perror("malloc");
+        return NULL;
     }
     return line + LWS_PRE;
 }
@@ -19,7 +20,7 @@ char *copy_line(char *line) {
     if (!copy) {
         return NULL;
     }
-    strcpy(copy, line);
+    strncpy(copy, line, INPUT_LINE_LENGTH);
 }
 
 void free_line(char *line)
@@ -81,6 +82,7 @@ void list_deinit(list *in)
     while (in->first) {
         list_remove(in);
     }
+    free(in);
 }
 
 static const struct lws_http_mount mount = {
@@ -141,6 +143,7 @@ static int callback_merica_terminal(struct lws *wsi,
                                     enum lws_callback_reasons reason,
                                     void *user, void *in, size_t len)
 {
+    struct lws_context *context = lws_get_context(wsi);
     struct lws_vhost *vhost = lws_get_vhost(wsi);
     const struct lws_protocols *prot = lws_get_protocol(wsi);
     /*
@@ -153,7 +156,7 @@ static int callback_merica_terminal(struct lws *wsi,
 
     int n, m;
 
-    list *lines = (list *)prot->user;
+    list *lines = (list *)lws_context_user(context);
     char *line;
 
     switch (reason) {
@@ -162,7 +165,7 @@ static int callback_merica_terminal(struct lws *wsi,
                       vhost, prot,
                       sizeof(struct per_vhost_data__merica_terminal)
                   );
-            vhd->context = lws_get_context(wsi);
+            vhd->context = context;
             vhd->protocol = prot;
             vhd->vhost = vhost;
             break;
@@ -177,13 +180,24 @@ static int callback_merica_terminal(struct lws *wsi,
             break;
 
         case LWS_CALLBACK_SERVER_WRITEABLE:
-            line = lines->first->line;
-            n = strlen(line);
-            m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
-            if (lines->first->next != NULL) {
+            if (lines->first) {
+                line = lines->first->line;
+                n = strlen(line);
+                m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
                 list_remove(lines);
-                lws_callback_on_writable_all_protocol(lws_get_context(wsi),
-                    &protocols[PROTOCOL_MERICA_TERMINAL]);
+                if (lines->first) {
+                    lws_callback_on_writable_all_protocol(context,
+                        &protocols[PROTOCOL_MERICA_TERMINAL]);
+                }
+            } else {
+                line = copy_line(JSON_EMPTY);
+                if (line) {
+                    n = strlen(line);
+                    m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
+                    free_line(line);
+                } else {
+                    return -1;
+                }
             }
             if (m < n) {
                 fprintf(stderr, "ERROR %d writing to di socket\n", n);
@@ -196,7 +210,7 @@ static int callback_merica_terminal(struct lws *wsi,
                 line = copy_line(JSON_EMPTY);
                 if (line) {
                     if (list_add(lines, line) == 0) {
-                        lws_callback_on_writable_all_protocol(lws_get_context(wsi),
+                        lws_callback_on_writable_all_protocol(context,
                             &protocols[PROTOCOL_MERICA_TERMINAL]);
                     } else {
                         free_line(line);
@@ -246,16 +260,23 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
 {
     struct lws_context_creation_info info;
 
+    list *l = list_init();
+    if (!l) {
+        return -1;
+    }
+
     memset(&info, 0, sizeof(info));
     info.port = HTTP_PORT;
     info.mounts = &mount;
     info.protocols = protocols;
     info.max_http_header_pool = 1;
     info.options |= LWS_SERVER_OPTION_LIBEV;
+    info.user = l;
 
     struct lws_context *context = lws_create_context(&info);
     if (!context) {
         fprintf(stderr, "lws_create_context failed\n");
+        list_deinit(l);
         return -1;
     }
 
@@ -263,23 +284,11 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
 
     ev_io_ws *w = &(self->fd_watcher);
     w->context = context;
-    char *line = copy_line(JSON_EMPTY);
-    if (!line) {
-        return -1;
-    }
-    w->lines = list_init();
-    if (!w->lines) {
-        free_line(line);
-        return -1;
-    }
-    if (list_add(w->lines, line) != 0) {
-        list_deinit(w->lines);
-        return -1;
-    }
-    protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->lines;
-    line = new_line();
+    w->lines = l;
+
+    char *line = new_line();
     if (!line) {
-        list_deinit(w->lines);
+        list_deinit(l);
         return -1;
     }
     w->text = line;
@@ -292,7 +301,8 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
 
 void mt_server_deinit(mt_server_t *self)
 {
-    list_deinit((list *)protocols[PROTOCOL_MERICA_TERMINAL].user);
+    free_line(self->fd_watcher.text);
+    list_deinit(self->fd_watcher.lines);
     lws_context_destroy(self->context);
 }
 
@@ -311,6 +321,7 @@ int main(int argc, const char **argv)
     ev_run(loop, 0);
 
     mt_server_deinit(&server);
+    ev_loop_destroy(loop);
 
     return 0;
 }