]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blobdiff - mt_server.c
Do not generate "empty" messages
[coffee/mt-apps.git] / mt_server.c
index 567c3af6fe08d2dd2c53bf59aa0d27e48e09b637..ee80d87a7f73a9ac251653369b02339963be2b76 100644 (file)
@@ -1,9 +1,13 @@
+#define _GNU_SOURCE         /* See feature_test_macros(7) */
+#include <errno.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <syslog.h>
 
 #include "mt_server.h"
 #include "signal_exit.h"
+#include "mt_blank.h"
 
 char *new_line()
 {
@@ -86,23 +90,23 @@ void list_deinit(list *in)
 }
 
 static const struct lws_http_mount mount = {
-    /* .mount_next */            NULL,         /* linked-list "next" */
-    /* .mountpoint */            HTTP_MOUNTPOINT,
-    /* .origin */                HTTP_ORIGIN,  /* serve from dir */
-    /* .def */                   HTTP_DEFAULT, /* default filename */
-    /* .protocol */              NULL,
-    /* .cgienv */                NULL,
-    /* .extra_mimetypes */       NULL,
-    /* .interpret */             NULL,
-    /* .cgi_timeout */           0,
-    /* .cache_max_age */         0,
-    /* .auth_mask */             0,
-    /* .cache_reusable */        0,
-    /* .cache_revalidate */      0,
-    /* .cache_intermediaries */  0,
-    /* .origin_protocol */       LWSMPRO_FILE, /* files in a dir */
-    /* .mountpoint_len */        1,            /* char count */
-    /* .basic_auth_login_file */ NULL,
+    .mount_next =            NULL,         /* linked-list "next" */
+    .mountpoint =            HTTP_MOUNTPOINT,
+    .origin =                HTTP_ORIGIN,  /* serve from dir */
+    .def =                   HTTP_DEFAULT, /* default filename */
+    .protocol =              NULL,
+    .cgienv =                NULL,
+    .extra_mimetypes =       NULL,
+    .interpret =             NULL,
+    .cgi_timeout =           0,
+    .cache_max_age =         0,
+    .auth_mask =             0,
+    .cache_reusable =        0,
+    .cache_revalidate =      0,
+    .cache_intermediaries =  0,
+    .origin_protocol =       LWSMPRO_FILE, /* files in a dir */
+    .mountpoint_len =        1,            /* char count */
+    .basic_auth_login_file = NULL,
 };
 
 typedef struct per_vhost_data__merica_terminal {
@@ -189,34 +193,15 @@ static int callback_merica_terminal(struct lws *wsi,
                     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 {
+                if (m < n) {
+                    fprintf(stderr, "ERROR %d writing to di socket\n", n);
                     return -1;
                 }
             }
-            if (m < n) {
-                fprintf(stderr, "ERROR %d writing to di socket\n", n);
-                return -1;
-            }
             break;
 
         case LWS_CALLBACK_RECEIVE:
-            if (strcmp((const char *)in, "reset") == 0) {
-                line = copy_line(JSON_EMPTY);
-                if (line) {
-                    if (list_add(lines, line) == 0) {
-                        lws_callback_on_writable_all_protocol(context,
-                            &protocols[PROTOCOL_MERICA_TERMINAL]);
-                    } else {
-                        free_line(line);
-                    }
-                }
-            } else if (strcmp((const char *)in, "close") == 0) {
+            if (strcmp((const char *)in, "close") == 0) {
                 fprintf(stderr, "closing websocket\n");
                 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
                                  (unsigned char *)"seeya", 5);
@@ -240,7 +225,11 @@ static void fd_cb(EV_P_ ev_io *w_, int revents)
     read(w->w.fd, pos, 1);
 
     if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
+#ifdef NO_MAIN
+       mt_blank_wake();
+#endif
         *pos = 0;
+       syslog(LOG_INFO, w->text);
         char *line = new_line();
         if (line) {
             if (list_add(w->lines, w->text) == 0) {
@@ -265,6 +254,8 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
         return -1;
     }
 
+    openlog(program_invocation_short_name, LOG_PID | LOG_PERROR, LOG_DAEMON);
+
     memset(&info, 0, sizeof(info));
     info.port = HTTP_PORT;
     info.mounts = &mount;
@@ -304,6 +295,7 @@ void mt_server_deinit(mt_server_t *self)
     free_line(self->fd_watcher.text);
     list_deinit(self->fd_watcher.lines);
     lws_context_destroy(self->context);
+    closelog();
 }
 
 #ifndef NO_MAIN