#include "mt_server.h"
#include "signal_exit.h"
+char *new_line()
+{
+ char *line = (char *)malloc((LWS_PRE + INPUT_LINE_LENGTH)*sizeof(char));
+ if (!line) {
+ perror("malloc");
+ }
+ return line + LWS_PRE;
+}
+
+char *copy_line(char *line) {
+ char *copy = new_line();
+ if (!copy) {
+ return NULL;
+ }
+ strcpy(copy, line);
+}
+
+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);
+ }
+}
+
static const struct lws_http_mount mount = {
/* .mount_next */ NULL, /* linked-list "next" */
/* .mountpoint */ HTTP_MOUNTPOINT,
int n, m;
- char *line = (char *)prot->user;
+ list *lines = (list *)prot->user;
+ char *line;
switch (reason) {
case LWS_CALLBACK_PROTOCOL_INIT:
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
+ line = lines->first->line;
n = strlen(line);
- m = lws_write(wsi, line, n, LWS_WRITE_TEXT);
+ m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
+ if (lines->first->next != NULL) {
+ list_remove(lines);
+ lws_callback_on_writable_all_protocol(lws_get_context(wsi),
+ &protocols[PROTOCOL_MERICA_TERMINAL]);
+ }
if (m < n) {
fprintf(stderr, "ERROR %d writing to di socket\n", n);
return -1;
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]);
+ 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,
if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
*pos = 0;
- w->pos = w->text;
- lws_callback_on_writable_all_protocol(w->context,
- &protocols[PROTOCOL_MERICA_TERMINAL]);
+ if (list_add(w->lines, w->text) == 0) {
+ char *line = new_line();
+ if (line) {
+ w->text = line;
+ w->pos = w->text;
+ lws_callback_on_writable_all_protocol(w->context,
+ &protocols[PROTOCOL_MERICA_TERMINAL]);
+ return;
+ }
+ }
+ fprintf(stderr, "cannot malloc new line\n");
}
}
ev_io_ws *w = &(self->fd_watcher);
w->context = context;
- w->text = (char *)malloc(INPUT_LINE_LENGTH*sizeof(char));
- if (!w->text) {
- perror("malloc");
+ 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();
+ if (!line) {
+ list_deinit(w->lines);
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);
return lws_ev_initloop(context, loop, 0);
}
-void mt_server_deinit(mt_server_t *self) //TODO wtf
+void mt_server_deinit(mt_server_t *self)
{
- //free(self->fd_watcher.text);
- //protocols[PROTOCOL_MERICA_TERMINAL].user = NULL;
+ list_deinit((list *)protocols[PROTOCOL_MERICA_TERMINAL].user);
lws_context_destroy(self->context);
}
-#ifdef IS_MAIN
+#ifndef NO_MAIN
int main(int argc, const char **argv)
{
struct ev_loop *loop = EV_DEFAULT;
set_signal_exit(loop);
- if (mt_server_init(&server, loop, STDIN_FILENO) == -1) {
+ if (mt_server_init(&server, loop, STDIN_FILENO) != 0) {
return -1;
}