6 #include "signal_exit.h"
8 static const struct lws_http_mount mount = {
9 /* .mount_next */ NULL, /* linked-list "next" */
10 /* .mountpoint */ HTTP_MOUNTPOINT,
11 /* .origin */ HTTP_ORIGIN, /* serve from dir */
12 /* .def */ HTTP_DEFAULT, /* default filename */
15 /* .extra_mimetypes */ NULL,
16 /* .interpret */ NULL,
18 /* .cache_max_age */ 0,
20 /* .cache_reusable */ 0,
21 /* .cache_revalidate */ 0,
22 /* .cache_intermediaries */ 0,
23 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
24 /* .mountpoint_len */ 1, /* char count */
25 /* .basic_auth_login_file */ NULL,
28 typedef struct per_vhost_data__merica_terminal {
29 struct lws_context *context;
30 struct lws_vhost *vhost;
31 const struct lws_protocols *protocol;
32 } per_vhost_data__merica_terminal;
35 typedef struct per_session_data__merica_terminal {
37 } per_session_data__merica_terminal;
41 PROTOCOL_HTTP = 0, // always first
42 PROTOCOL_MERICA_TERMINAL
45 static int callback_merica_terminal(struct lws *wsi,
46 enum lws_callback_reasons reason,
47 void *user, void *in, size_t len);
49 // list of supported protocols and callbacks
50 static struct lws_protocols protocols[] = {
51 // first protocol must always be HTTP handler
52 {"http", lws_callback_http_dummy, 0, 0},
54 "merica-terminal-protocol",
55 callback_merica_terminal,
56 0, //sizeof(struct per_session_data__merica_terminal),
57 MT_PROTOCOL_RX_BUFFER_SIZE
59 {NULL, NULL, 0, 0} // terminator
62 static int callback_merica_terminal(struct lws *wsi,
63 enum lws_callback_reasons reason,
64 void *user, void *in, size_t len)
66 struct lws_vhost *vhost = lws_get_vhost(wsi);
67 const struct lws_protocols *prot = lws_get_protocol(wsi);
69 per_session_data__merica_terminal *pss =
70 (per_session_data__merica_terminal *)user;
72 per_vhost_data__merica_terminal *vhd =
73 (per_vhost_data__merica_terminal *)
74 lws_protocol_vh_priv_get(vhost, prot);
78 char *line = (char *)prot->user;
81 case LWS_CALLBACK_PROTOCOL_INIT:
82 vhd = lws_protocol_vh_priv_zalloc(
84 sizeof(struct per_vhost_data__merica_terminal)
86 vhd->context = lws_get_context(wsi);
91 case LWS_CALLBACK_PROTOCOL_DESTROY:
97 case LWS_CALLBACK_ESTABLISHED:
100 case LWS_CALLBACK_SERVER_WRITEABLE:
102 m = lws_write(wsi, line, n, LWS_WRITE_TEXT);
104 fprintf(stderr, "ERROR %d writing to di socket\n", n);
109 case LWS_CALLBACK_RECEIVE:
110 if (strcmp((const char *)in, "reset") == 0) {
111 strcpy(line, JSON_EMPTY);
112 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
113 &protocols[PROTOCOL_MERICA_TERMINAL]);
114 } else if (strcmp((const char *)in, "close") == 0) {
115 fprintf(stderr, "closing websocket\n");
116 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
117 (unsigned char *)"seeya", 5);
129 static void fd_cb(EV_P_ ev_io *w_, int revents)
131 ev_io_ws *w = (ev_io_ws *)w_;
133 char *pos = w->pos++;
135 read(w->w.fd, pos, 1);
137 if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
140 lws_callback_on_writable_all_protocol(w->context,
141 &protocols[PROTOCOL_MERICA_TERMINAL]);
145 int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
147 struct lws_context_creation_info info;
149 memset(&info, 0, sizeof(info));
150 info.port = HTTP_PORT;
151 info.mounts = &mount;
152 info.protocols = protocols;
153 info.max_http_header_pool = 1;
154 info.options |= LWS_SERVER_OPTION_LIBEV;
156 struct lws_context *context = lws_create_context(&info);
158 fprintf(stderr, "lws_create_context failed\n");
162 self->context = context;
164 ev_io_ws *w = &(self->fd_watcher);
165 w->context = context;
166 w->text = (char *)malloc(INPUT_LINE_LENGTH*sizeof(char));
171 strcpy(w->text, JSON_EMPTY);
173 protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->text;
174 ev_io_init(&(w->w), fd_cb, fd, EV_READ);
175 ev_io_start(loop, (ev_io *)w);
177 return lws_ev_initloop(context, loop, 0);
180 void mt_server_deinit(mt_server_t *self) //TODO wtf
182 //free(self->fd_watcher.text);
183 //protocols[PROTOCOL_MERICA_TERMINAL].user = NULL;
184 lws_context_destroy(self->context);
188 int main(int argc, const char **argv)
190 struct ev_loop *loop = EV_DEFAULT;
193 set_signal_exit(loop);
195 if (mt_server_init(&server, loop, STDIN_FILENO) != 0) {
201 mt_server_deinit(&server);