X-Git-Url: https://rtime.felk.cvut.cz/gitweb/coffee/mt-apps.git/blobdiff_plain/ebfa3df4ad6998e3ee96a403609bfd2a599090e8..HEAD:/mt_server.c diff --git a/mt_server.c b/mt_server.c index 76485d7..3b617bd 100644 --- a/mt_server.c +++ b/mt_server.c @@ -1,45 +1,125 @@ +#define _GNU_SOURCE /* See feature_test_macros(7) */ +#include #include #include #include +#include #include "mt_server.h" -#include "ev_signal_exit.h" +#include "signal_exit.h" +#include "mt_blank.h" -#define JSON_EMPTY "{\"type\":\"empty\"}" +char *new_line() +{ + char *line = (char *)malloc((LWS_PRE + INPUT_LINE_LENGTH)*sizeof(char)); + if (!line) { + perror("malloc"); + return NULL; + } + return line + LWS_PRE; +} + +char *copy_line(char *line) { + char *copy = new_line(); + if (!copy) { + return NULL; + } + strncpy(copy, line, INPUT_LINE_LENGTH); +} + +void free_line(char *line) +{ + free(line - LWS_PRE); +} + +node *node_init(char *line) +{ + node *res = (node *)malloc(sizeof(node)); + if (!res) { + perror("malloc"); + return res; + } + res->line = line;; + res->next = NULL; + return res; +} + +list *list_init() +{ + 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; + } + if (!in->first) { + in->first = new; + } else { + in->last->next = new; + } + in->last = new; + return 0; +} + +void list_remove(list *in) +{ + 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->first) { + list_remove(in); + } + free(in); +} 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 */ - /* .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__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,43 +132,44 @@ 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, 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); - 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; - char *line = (char *)prot->user; + list *lines = (list *)lws_context_user(context); + char *line; switch (reason) { 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->context = context; vhd->protocol = prot; vhd->vhost = vhost; break; @@ -103,21 +184,25 @@ static int callback_merica_terminal(struct lws *wsi, break; case LWS_CALLBACK_SERVER_WRITEABLE: - n = strlen(line); - m = lws_write(wsi, line, n, LWS_WRITE_TEXT); - if (m < n) { - printf("ERROR %d writing to di socket\n", n); - return -1; + if (lines->first) { + line = lines->first->line; + n = strlen(line); + m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT); + list_remove(lines); + if (lines->first) { + lws_callback_on_writable_all_protocol(context, + &protocols[PROTOCOL_MERICA_TERMINAL]); + } + 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) { - 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"); + if (strcmp((const char *)in, "close") == 0) { + fprintf(stderr, "closing websocket\n"); lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY, (unsigned char *)"seeya", 5); return -1; @@ -139,11 +224,24 @@ 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) { +#ifdef NO_MAIN + mt_blank_wake(); +#endif *pos = 0; - w->pos = w->text; - lws_callback_on_writable_all_protocol(w->context, - &protocols[PROTOCOL_MERICA_TERMINAL]); + syslog(LOG_INFO, "%s", w->text); + char *line = new_line(); + if (line) { + if (list_add(w->lines, w->text) == 0) { + w->text = line; + w->pos = w->text; + lws_callback_on_writable_all_protocol(w->context, + &protocols[PROTOCOL_MERICA_TERMINAL]); + return; + } + free_line(line); + } + fprintf(stderr, "cannot malloc new line\n"); } } @@ -151,16 +249,25 @@ 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; + } + + openlog(program_invocation_short_name, LOG_PID | LOG_PERROR, LOG_DAEMON); + memset(&info, 0, sizeof(info)); - info.port = 80; + 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; } @@ -168,14 +275,15 @@ 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)); - if (!w->text) { - perror("malloc"); + w->lines = l; + + char *line = new_line(); + if (!line) { + list_deinit(l); return -1; } - strcpy(w->text, JSON_EMPTY); + w->text = line; 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); @@ -184,12 +292,13 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd) void mt_server_deinit(mt_server_t *self) { - //free(self->fd_watcher.text); - //protocols[PROTOCOL_MERICA_TERMINAL].user = NULL; + free_line(self->fd_watcher.text); + list_deinit(self->fd_watcher.lines); lws_context_destroy(self->context); + closelog(); } -#ifdef IS_MAIN +#ifndef NO_MAIN int main(int argc, const char **argv) { struct ev_loop *loop = EV_DEFAULT; @@ -197,13 +306,14 @@ int main(int argc, const char **argv) set_signal_exit(loop); - if (mt_server_init(&server, loop, STDIN_FILENO) == -1) { + if (mt_server_init(&server, loop, STDIN_FILENO) != 0) { return -1; } ev_run(loop, 0); mt_server_deinit(&server); + ev_loop_destroy(loop); return 0; }