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_context *context = lws_get_context(wsi);
147 struct lws_vhost *vhost = lws_get_vhost(wsi);
148 const struct lws_protocols *prot = lws_get_protocol(wsi);
150 per_session_data__merica_terminal *pss =
151 (per_session_data__merica_terminal *)user;
153 per_vhost_data__merica_terminal *vhd =
154 (per_vhost_data__merica_terminal *)
155 lws_protocol_vh_priv_get(vhost, prot);
159 list *lines = (list *)lws_context_user(context);
163 case LWS_CALLBACK_PROTOCOL_INIT:
164 vhd = lws_protocol_vh_priv_zalloc(
166 sizeof(struct per_vhost_data__merica_terminal)
168 vhd->context = context;
169 vhd->protocol = prot;
173 case LWS_CALLBACK_PROTOCOL_DESTROY:
179 case LWS_CALLBACK_ESTABLISHED:
182 case LWS_CALLBACK_SERVER_WRITEABLE:
184 line = lines->first->line;
186 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
189 lws_callback_on_writable_all_protocol(context,
190 &protocols[PROTOCOL_MERICA_TERMINAL]);
193 line = copy_line(JSON_EMPTY);
196 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
203 fprintf(stderr, "ERROR %d writing to di socket\n", n);
208 case LWS_CALLBACK_RECEIVE:
209 if (strcmp((const char *)in, "reset") == 0) {
210 line = copy_line(JSON_EMPTY);
212 if (list_add(lines, line) == 0) {
213 lws_callback_on_writable_all_protocol(context,
214 &protocols[PROTOCOL_MERICA_TERMINAL]);
219 } else if (strcmp((const char *)in, "close") == 0) {
220 fprintf(stderr, "closing websocket\n");
221 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
222 (unsigned char *)"seeya", 5);
234 static void fd_cb(EV_P_ ev_io *w_, int revents)
236 ev_io_ws *w = (ev_io_ws *)w_;
238 char *pos = w->pos++;
240 read(w->w.fd, pos, 1);
242 if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
244 char *line = new_line();
246 if (list_add(w->lines, w->text) == 0) {
249 lws_callback_on_writable_all_protocol(w->context,
250 &protocols[PROTOCOL_MERICA_TERMINAL]);
255 fprintf(stderr, "cannot malloc new line\n");
259 int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
261 struct lws_context_creation_info info;
263 list *l = list_init();
268 memset(&info, 0, sizeof(info));
269 info.port = HTTP_PORT;
270 info.mounts = &mount;
271 info.protocols = protocols;
272 info.max_http_header_pool = 1;
273 info.options |= LWS_SERVER_OPTION_LIBEV;
276 struct lws_context *context = lws_create_context(&info);
278 fprintf(stderr, "lws_create_context failed\n");
283 self->context = context;
285 ev_io_ws *w = &(self->fd_watcher);
286 w->context = context;
289 char *line = new_line();
296 ev_io_init(&(w->w), fd_cb, fd, EV_READ);
297 ev_io_start(loop, (ev_io *)w);
299 return lws_ev_initloop(context, loop, 0);
302 void mt_server_deinit(mt_server_t *self)
304 free_line(self->fd_watcher.text);
305 list_deinit(self->fd_watcher.lines);
306 lws_context_destroy(self->context);
310 int main(int argc, const char **argv)
312 struct ev_loop *loop = EV_DEFAULT;
315 set_signal_exit(loop);
317 if (mt_server_init(&server, loop, STDIN_FILENO) != 0) {
323 mt_server_deinit(&server);
324 ev_loop_destroy(loop);