6 #include "signal_exit.h"
11 char *line = (char *)malloc((LWS_PRE + INPUT_LINE_LENGTH)*sizeof(char));
16 return line + LWS_PRE;
19 char *copy_line(char *line) {
20 char *copy = new_line();
24 strncpy(copy, line, INPUT_LINE_LENGTH);
27 void free_line(char *line)
32 node *node_init(char *line)
34 node *res = (node *)malloc(sizeof(node));
46 list *res = (list *)calloc(1, sizeof(list));
53 int list_add(list *in, char *line)
55 node *new = node_init(line);
68 void list_remove(list *in)
70 node *tmp = in->first;
72 in->first = tmp->next;
81 void list_deinit(list *in)
89 static const struct lws_http_mount mount = {
90 .mount_next = NULL, /* linked-list "next" */
91 .mountpoint = HTTP_MOUNTPOINT,
92 .origin = HTTP_ORIGIN, /* serve from dir */
93 .def = HTTP_DEFAULT, /* default filename */
96 .extra_mimetypes = NULL,
102 .cache_revalidate = 0,
103 .cache_intermediaries = 0,
104 .origin_protocol = LWSMPRO_FILE, /* files in a dir */
105 .mountpoint_len = 1, /* char count */
106 .basic_auth_login_file = NULL,
109 typedef struct per_vhost_data__merica_terminal {
110 struct lws_context *context;
111 struct lws_vhost *vhost;
112 const struct lws_protocols *protocol;
113 } per_vhost_data__merica_terminal;
116 typedef struct per_session_data__merica_terminal {
118 } per_session_data__merica_terminal;
122 PROTOCOL_HTTP = 0, // always first
123 PROTOCOL_MERICA_TERMINAL
126 static int callback_merica_terminal(struct lws *wsi,
127 enum lws_callback_reasons reason,
128 void *user, void *in, size_t len);
130 // list of supported protocols and callbacks
131 static struct lws_protocols protocols[] = {
132 // first protocol must always be HTTP handler
133 {"http", lws_callback_http_dummy, 0, 0},
135 "merica-terminal-protocol",
136 callback_merica_terminal,
137 0, //sizeof(struct per_session_data__merica_terminal),
138 MT_PROTOCOL_RX_BUFFER_SIZE
140 {NULL, NULL, 0, 0} // terminator
143 static int callback_merica_terminal(struct lws *wsi,
144 enum lws_callback_reasons reason,
145 void *user, void *in, size_t len)
147 struct lws_context *context = lws_get_context(wsi);
148 struct lws_vhost *vhost = lws_get_vhost(wsi);
149 const struct lws_protocols *prot = lws_get_protocol(wsi);
151 per_session_data__merica_terminal *pss =
152 (per_session_data__merica_terminal *)user;
154 per_vhost_data__merica_terminal *vhd =
155 (per_vhost_data__merica_terminal *)
156 lws_protocol_vh_priv_get(vhost, prot);
160 list *lines = (list *)lws_context_user(context);
164 case LWS_CALLBACK_PROTOCOL_INIT:
165 vhd = lws_protocol_vh_priv_zalloc(
167 sizeof(struct per_vhost_data__merica_terminal)
169 vhd->context = context;
170 vhd->protocol = prot;
174 case LWS_CALLBACK_PROTOCOL_DESTROY:
180 case LWS_CALLBACK_ESTABLISHED:
183 case LWS_CALLBACK_SERVER_WRITEABLE:
185 line = lines->first->line;
187 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
190 lws_callback_on_writable_all_protocol(context,
191 &protocols[PROTOCOL_MERICA_TERMINAL]);
194 line = copy_line(JSON_EMPTY);
197 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
204 fprintf(stderr, "ERROR %d writing to di socket\n", n);
209 case LWS_CALLBACK_RECEIVE:
210 if (strcmp((const char *)in, "reset") == 0) {
211 line = copy_line(JSON_EMPTY);
213 if (list_add(lines, line) == 0) {
214 lws_callback_on_writable_all_protocol(context,
215 &protocols[PROTOCOL_MERICA_TERMINAL]);
220 } else if (strcmp((const char *)in, "close") == 0) {
221 fprintf(stderr, "closing websocket\n");
222 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
223 (unsigned char *)"seeya", 5);
235 static void fd_cb(EV_P_ ev_io *w_, int revents)
237 ev_io_ws *w = (ev_io_ws *)w_;
239 char *pos = w->pos++;
241 read(w->w.fd, pos, 1);
243 if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
248 char *line = new_line();
250 if (list_add(w->lines, w->text) == 0) {
253 lws_callback_on_writable_all_protocol(w->context,
254 &protocols[PROTOCOL_MERICA_TERMINAL]);
259 fprintf(stderr, "cannot malloc new line\n");
263 int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
265 struct lws_context_creation_info info;
267 list *l = list_init();
272 memset(&info, 0, sizeof(info));
273 info.port = HTTP_PORT;
274 info.mounts = &mount;
275 info.protocols = protocols;
276 info.max_http_header_pool = 1;
277 info.options |= LWS_SERVER_OPTION_LIBEV;
280 struct lws_context *context = lws_create_context(&info);
282 fprintf(stderr, "lws_create_context failed\n");
287 self->context = context;
289 ev_io_ws *w = &(self->fd_watcher);
290 w->context = context;
293 char *line = new_line();
300 ev_io_init(&(w->w), fd_cb, fd, EV_READ);
301 ev_io_start(loop, (ev_io *)w);
303 return lws_ev_initloop(context, loop, 0);
306 void mt_server_deinit(mt_server_t *self)
308 free_line(self->fd_watcher.text);
309 list_deinit(self->fd_watcher.lines);
310 lws_context_destroy(self->context);
314 int main(int argc, const char **argv)
316 struct ev_loop *loop = EV_DEFAULT;
319 set_signal_exit(loop);
321 if (mt_server_init(&server, loop, STDIN_FILENO) != 0) {
327 mt_server_deinit(&server);
328 ev_loop_destroy(loop);