6 #include "signal_exit.h"
10 char *line = (char *)malloc((LWS_PRE + INPUT_LINE_LENGTH)*sizeof(char));
15 return line + LWS_PRE;
18 char *copy_line(char *line) {
19 char *copy = new_line();
23 strncpy(copy, line, INPUT_LINE_LENGTH);
26 void free_line(char *line)
31 node *node_init(char *line)
33 node *res = (node *)malloc(sizeof(node));
45 list *res = (list *)calloc(1, sizeof(list));
52 int list_add(list *in, char *line)
54 node *new = node_init(line);
67 void list_remove(list *in)
69 node *tmp = in->first;
71 in->first = tmp->next;
80 void list_deinit(list *in)
88 static const struct lws_http_mount mount = {
89 /* .mount_next */ NULL, /* linked-list "next" */
90 /* .mountpoint */ HTTP_MOUNTPOINT,
91 /* .origin */ HTTP_ORIGIN, /* serve from dir */
92 /* .def */ HTTP_DEFAULT, /* default filename */
95 /* .extra_mimetypes */ NULL,
96 /* .interpret */ NULL,
98 /* .cache_max_age */ 0,
100 /* .cache_reusable */ 0,
101 /* .cache_revalidate */ 0,
102 /* .cache_intermediaries */ 0,
103 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
104 /* .mountpoint_len */ 1, /* char count */
105 /* .basic_auth_login_file */ NULL,
108 typedef struct per_vhost_data__merica_terminal {
109 struct lws_context *context;
110 struct lws_vhost *vhost;
111 const struct lws_protocols *protocol;
112 } per_vhost_data__merica_terminal;
115 typedef struct per_session_data__merica_terminal {
117 } per_session_data__merica_terminal;
121 PROTOCOL_HTTP = 0, // always first
122 PROTOCOL_MERICA_TERMINAL
125 static int callback_merica_terminal(struct lws *wsi,
126 enum lws_callback_reasons reason,
127 void *user, void *in, size_t len);
129 // list of supported protocols and callbacks
130 static struct lws_protocols protocols[] = {
131 // first protocol must always be HTTP handler
132 {"http", lws_callback_http_dummy, 0, 0},
134 "merica-terminal-protocol",
135 callback_merica_terminal,
136 0, //sizeof(struct per_session_data__merica_terminal),
137 MT_PROTOCOL_RX_BUFFER_SIZE
139 {NULL, NULL, 0, 0} // terminator
142 static int callback_merica_terminal(struct lws *wsi,
143 enum lws_callback_reasons reason,
144 void *user, void *in, size_t len)
146 struct lws_vhost *vhost = lws_get_vhost(wsi);
147 const struct lws_protocols *prot = lws_get_protocol(wsi);
149 per_session_data__merica_terminal *pss =
150 (per_session_data__merica_terminal *)user;
152 per_vhost_data__merica_terminal *vhd =
153 (per_vhost_data__merica_terminal *)
154 lws_protocol_vh_priv_get(vhost, prot);
158 list *lines = (list *)prot->user;
162 case LWS_CALLBACK_PROTOCOL_INIT:
163 vhd = lws_protocol_vh_priv_zalloc(
165 sizeof(struct per_vhost_data__merica_terminal)
167 vhd->context = lws_get_context(wsi);
168 vhd->protocol = prot;
172 case LWS_CALLBACK_PROTOCOL_DESTROY:
178 case LWS_CALLBACK_ESTABLISHED:
181 case LWS_CALLBACK_SERVER_WRITEABLE:
183 line = lines->first->line;
185 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
188 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
189 &protocols[PROTOCOL_MERICA_TERMINAL]);
192 line = copy_line(JSON_EMPTY);
195 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
202 fprintf(stderr, "ERROR %d writing to di socket\n", n);
207 case LWS_CALLBACK_RECEIVE:
208 if (strcmp((const char *)in, "reset") == 0) {
209 line = copy_line(JSON_EMPTY);
211 if (list_add(lines, line) == 0) {
212 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
213 &protocols[PROTOCOL_MERICA_TERMINAL]);
218 } else if (strcmp((const char *)in, "close") == 0) {
219 fprintf(stderr, "closing websocket\n");
220 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
221 (unsigned char *)"seeya", 5);
233 static void fd_cb(EV_P_ ev_io *w_, int revents)
235 ev_io_ws *w = (ev_io_ws *)w_;
237 char *pos = w->pos++;
239 read(w->w.fd, pos, 1);
241 if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
243 char *line = new_line();
245 if (list_add(w->lines, w->text) == 0) {
248 lws_callback_on_writable_all_protocol(w->context,
249 &protocols[PROTOCOL_MERICA_TERMINAL]);
254 fprintf(stderr, "cannot malloc new line\n");
258 int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
260 struct lws_context_creation_info info;
262 memset(&info, 0, sizeof(info));
263 info.port = HTTP_PORT;
264 info.mounts = &mount;
265 info.protocols = protocols;
266 info.max_http_header_pool = 1;
267 info.options |= LWS_SERVER_OPTION_LIBEV;
269 struct lws_context *context = lws_create_context(&info);
271 fprintf(stderr, "lws_create_context failed\n");
275 self->context = context;
277 ev_io_ws *w = &(self->fd_watcher);
278 w->context = context;
279 w->lines = list_init();
283 protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->lines;
284 char *line = new_line();
286 list_deinit(w->lines);
291 ev_io_init(&(w->w), fd_cb, fd, EV_READ);
292 ev_io_start(loop, (ev_io *)w);
294 return lws_ev_initloop(context, loop, 0);
297 void mt_server_deinit(mt_server_t *self)
299 free_line(self->fd_watcher.text);
300 list_deinit(self->fd_watcher.lines);
301 lws_context_destroy(self->context);
305 int main(int argc, const char **argv)
307 struct ev_loop *loop = EV_DEFAULT;
310 set_signal_exit(loop);
312 if (mt_server_init(&server, loop, STDIN_FILENO) != 0) {
318 mt_server_deinit(&server);
319 ev_loop_destroy(loop);