]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/commitdiff
queue improved
authorJiří Matěják <jiri.matejak@fel.cvut.cz>
Fri, 18 May 2018 02:32:58 +0000 (04:32 +0200)
committerJiří Matěják <jiri.matejak@fel.cvut.cz>
Fri, 18 May 2018 02:32:58 +0000 (04:32 +0200)
mt_server.c
mt_server.h

index 7575fedfca15ce0853374af2d1f619fad5c14b00..dc70aab33fd6d65e5e2ab271c7409b7d25e300a9 100644 (file)
@@ -27,9 +27,9 @@ void free_line(char *line)
     free(line - LWS_PRE);
 }
 
-list *list_init(char *line)
+node *node_init(char *line)
 {
-    list *res = (list *)malloc(sizeof(list));
+    node *res = (node *)malloc(sizeof(node));
     if (!res) {
         perror("malloc");
         return res;
@@ -39,29 +39,47 @@ list *list_init(char *line)
     return res;
 }
 
-int list_add(list **in, char *line)
+list *list_init()
 {
-    list *new = list_init(line);
+    list *res = (list *)calloc(1, sizeof(list));
+    if (!res) {
+        perror("malloc");
+    }
+    return res;
+}
+
+int list_add(list *in, char *line)
+{
+    node *new = node_init(line);
     if (!new) {
         return -1;
     }
-    (*in)->next = new;
-    *in = new;
+    if (!in->first) {
+        in->first = new;
+    } else {
+        in->last->next = new;
+    }
+    in->last = new;
     return 0;
 }
 
-void list_remove(list **in)
+void list_remove(list *in)
 {
-    list *tmp = *in;
-    *in = (*in)->next;
-    free_line(tmp->line);
-    free(tmp);
+    node *tmp = in->first;
+    if (tmp) {
+        in->first = tmp->next;
+        if (!tmp->next) {
+            in->last = NULL;
+        }
+        free_line(tmp->line);
+        free(tmp);
+    }
 }
 
 void list_deinit(list *in)
 {
-    while (in) {
-        list_remove(&in);
+    while (in->first) {
+        list_remove(in);
     }
 }
 
@@ -135,7 +153,7 @@ static int callback_merica_terminal(struct lws *wsi,
 
     int n, m;
 
-    list **lines = (list **)&(prot->user);
+    list *lines = (list *)prot->user;
     char *line;
 
     switch (reason) {
@@ -159,13 +177,13 @@ static int callback_merica_terminal(struct lws *wsi,
             break;
 
         case LWS_CALLBACK_SERVER_WRITEABLE:
-            line = (*lines)->line;
+            line = lines->first->line;
             n = strlen(line);
             m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
-            if ((*lines)->next != NULL) {
+            if (lines->first->next != NULL) {
                 list_remove(lines);
                 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
-                                                      &protocols[PROTOCOL_MERICA_TERMINAL]);
+                    &protocols[PROTOCOL_MERICA_TERMINAL]);
             }
             if (m < n) {
                 fprintf(stderr, "ERROR %d writing to di socket\n", n);
@@ -175,9 +193,13 @@ static int callback_merica_terminal(struct lws *wsi,
 
         case LWS_CALLBACK_RECEIVE:
             if (strcmp((const char *)in, "reset") == 0) {
-                strcpy((*lines)->line, JSON_EMPTY);
-                lws_callback_on_writable_all_protocol(lws_get_context(wsi),
-                                                      &protocols[PROTOCOL_MERICA_TERMINAL]);
+                line = copy_line(JSON_EMPTY);
+                if (line) {
+                    if (list_add(lines, line) == 0) {
+                        lws_callback_on_writable_all_protocol(lws_get_context(wsi),
+                            &protocols[PROTOCOL_MERICA_TERMINAL]);
+                    }
+                }
             } else if (strcmp((const char *)in, "close") == 0) {
                 fprintf(stderr, "closing websocket\n");
                 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
@@ -203,7 +225,7 @@ static void fd_cb(EV_P_ ev_io *w_, int revents)
 
     if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
         *pos = 0;
-        if (list_add(&(w->lines), w->text) == 0) {
+        if (list_add(w->lines, w->text) == 0) {
             char *line = new_line();
             if (line) {
                 w->text = line;
@@ -242,11 +264,15 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
     if (!line) {
         return -1;
     }
-    w->lines = list_init(line);
+    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();
     if (!line) {
index f08dfbd5ccaf823f01174cbb887e30337fee765d..ead05f160e0940707d519a4efc0771466ead2ad6 100644 (file)
 
 #define JSON_EMPTY "{\"type\":\"empty\"}" // default message
 
-typedef struct list {
+typedef struct node {
     char *line;
-    struct list *next;
+    struct node *next;
+} node;
+
+typedef struct list {
+    struct node *first;
+    struct node *last;
 } list;
 
 typedef struct ev_io_ws {