6 #include "signal_exit.h"
10 char *line = (char *)malloc((LWS_PRE + INPUT_LINE_LENGTH)*sizeof(char));
14 return line + LWS_PRE;
17 char *copy_line(char *line) {
18 char *copy = new_line();
25 void free_line(char *line)
30 node *node_init(char *line)
32 node *res = (node *)malloc(sizeof(node));
44 list *res = (list *)calloc(1, sizeof(list));
51 int list_add(list *in, char *line)
53 node *new = node_init(line);
66 void list_remove(list *in)
68 node *tmp = in->first;
70 in->first = tmp->next;
79 void list_deinit(list *in)
86 static const struct lws_http_mount mount = {
87 /* .mount_next */ NULL, /* linked-list "next" */
88 /* .mountpoint */ HTTP_MOUNTPOINT,
89 /* .origin */ HTTP_ORIGIN, /* serve from dir */
90 /* .def */ HTTP_DEFAULT, /* default filename */
93 /* .extra_mimetypes */ NULL,
94 /* .interpret */ NULL,
96 /* .cache_max_age */ 0,
98 /* .cache_reusable */ 0,
99 /* .cache_revalidate */ 0,
100 /* .cache_intermediaries */ 0,
101 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
102 /* .mountpoint_len */ 1, /* char count */
103 /* .basic_auth_login_file */ NULL,
106 typedef struct per_vhost_data__merica_terminal {
107 struct lws_context *context;
108 struct lws_vhost *vhost;
109 const struct lws_protocols *protocol;
110 } per_vhost_data__merica_terminal;
113 typedef struct per_session_data__merica_terminal {
115 } per_session_data__merica_terminal;
119 PROTOCOL_HTTP = 0, // always first
120 PROTOCOL_MERICA_TERMINAL
123 static int callback_merica_terminal(struct lws *wsi,
124 enum lws_callback_reasons reason,
125 void *user, void *in, size_t len);
127 // list of supported protocols and callbacks
128 static struct lws_protocols protocols[] = {
129 // first protocol must always be HTTP handler
130 {"http", lws_callback_http_dummy, 0, 0},
132 "merica-terminal-protocol",
133 callback_merica_terminal,
134 0, //sizeof(struct per_session_data__merica_terminal),
135 MT_PROTOCOL_RX_BUFFER_SIZE
137 {NULL, NULL, 0, 0} // terminator
140 static int callback_merica_terminal(struct lws *wsi,
141 enum lws_callback_reasons reason,
142 void *user, void *in, size_t len)
144 struct lws_vhost *vhost = lws_get_vhost(wsi);
145 const struct lws_protocols *prot = lws_get_protocol(wsi);
147 per_session_data__merica_terminal *pss =
148 (per_session_data__merica_terminal *)user;
150 per_vhost_data__merica_terminal *vhd =
151 (per_vhost_data__merica_terminal *)
152 lws_protocol_vh_priv_get(vhost, prot);
156 list *lines = (list *)prot->user;
160 case LWS_CALLBACK_PROTOCOL_INIT:
161 vhd = lws_protocol_vh_priv_zalloc(
163 sizeof(struct per_vhost_data__merica_terminal)
165 vhd->context = lws_get_context(wsi);
166 vhd->protocol = prot;
170 case LWS_CALLBACK_PROTOCOL_DESTROY:
176 case LWS_CALLBACK_ESTABLISHED:
179 case LWS_CALLBACK_SERVER_WRITEABLE:
180 line = lines->first->line;
182 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
183 if (lines->first->next != NULL) {
185 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
186 &protocols[PROTOCOL_MERICA_TERMINAL]);
189 fprintf(stderr, "ERROR %d writing to di socket\n", n);
194 case LWS_CALLBACK_RECEIVE:
195 if (strcmp((const char *)in, "reset") == 0) {
196 line = copy_line(JSON_EMPTY);
198 if (list_add(lines, line) == 0) {
199 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
200 &protocols[PROTOCOL_MERICA_TERMINAL]);
203 } else if (strcmp((const char *)in, "close") == 0) {
204 fprintf(stderr, "closing websocket\n");
205 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
206 (unsigned char *)"seeya", 5);
218 static void fd_cb(EV_P_ ev_io *w_, int revents)
220 ev_io_ws *w = (ev_io_ws *)w_;
222 char *pos = w->pos++;
224 read(w->w.fd, pos, 1);
226 if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
228 if (list_add(w->lines, w->text) == 0) {
229 char *line = new_line();
233 lws_callback_on_writable_all_protocol(w->context,
234 &protocols[PROTOCOL_MERICA_TERMINAL]);
238 fprintf(stderr, "cannot malloc new line\n");
242 int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
244 struct lws_context_creation_info info;
246 memset(&info, 0, sizeof(info));
247 info.port = HTTP_PORT;
248 info.mounts = &mount;
249 info.protocols = protocols;
250 info.max_http_header_pool = 1;
251 info.options |= LWS_SERVER_OPTION_LIBEV;
253 struct lws_context *context = lws_create_context(&info);
255 fprintf(stderr, "lws_create_context failed\n");
259 self->context = context;
261 ev_io_ws *w = &(self->fd_watcher);
262 w->context = context;
263 char *line = copy_line(JSON_EMPTY);
267 w->lines = list_init();
272 if (list_add(w->lines, line) != 0) {
273 list_deinit(w->lines);
276 protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->lines;
279 list_deinit(w->lines);
284 ev_io_init(&(w->w), fd_cb, fd, EV_READ);
285 ev_io_start(loop, (ev_io *)w);
287 return lws_ev_initloop(context, loop, 0);
290 void mt_server_deinit(mt_server_t *self)
292 list_deinit((list *)protocols[PROTOCOL_MERICA_TERMINAL].user);
293 lws_context_destroy(self->context);
297 int main(int argc, const char **argv)
299 struct ev_loop *loop = EV_DEFAULT;
302 set_signal_exit(loop);
304 if (mt_server_init(&server, loop, STDIN_FILENO) != 0) {
310 mt_server_deinit(&server);