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 list *list_init(char *line)
32 list *res = (list *)malloc(sizeof(list));
42 int list_add(list **in, char *line)
44 list *new = list_init(line);
53 void list_remove(list **in)
61 void list_deinit(list *in)
68 static const struct lws_http_mount mount = {
69 /* .mount_next */ NULL, /* linked-list "next" */
70 /* .mountpoint */ HTTP_MOUNTPOINT,
71 /* .origin */ HTTP_ORIGIN, /* serve from dir */
72 /* .def */ HTTP_DEFAULT, /* default filename */
75 /* .extra_mimetypes */ NULL,
76 /* .interpret */ NULL,
78 /* .cache_max_age */ 0,
80 /* .cache_reusable */ 0,
81 /* .cache_revalidate */ 0,
82 /* .cache_intermediaries */ 0,
83 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
84 /* .mountpoint_len */ 1, /* char count */
85 /* .basic_auth_login_file */ NULL,
88 typedef struct per_vhost_data__merica_terminal {
89 struct lws_context *context;
90 struct lws_vhost *vhost;
91 const struct lws_protocols *protocol;
92 } per_vhost_data__merica_terminal;
95 typedef struct per_session_data__merica_terminal {
97 } per_session_data__merica_terminal;
101 PROTOCOL_HTTP = 0, // always first
102 PROTOCOL_MERICA_TERMINAL
105 static int callback_merica_terminal(struct lws *wsi,
106 enum lws_callback_reasons reason,
107 void *user, void *in, size_t len);
109 // list of supported protocols and callbacks
110 static struct lws_protocols protocols[] = {
111 // first protocol must always be HTTP handler
112 {"http", lws_callback_http_dummy, 0, 0},
114 "merica-terminal-protocol",
115 callback_merica_terminal,
116 0, //sizeof(struct per_session_data__merica_terminal),
117 MT_PROTOCOL_RX_BUFFER_SIZE
119 {NULL, NULL, 0, 0} // terminator
122 static int callback_merica_terminal(struct lws *wsi,
123 enum lws_callback_reasons reason,
124 void *user, void *in, size_t len)
126 struct lws_vhost *vhost = lws_get_vhost(wsi);
127 const struct lws_protocols *prot = lws_get_protocol(wsi);
129 per_session_data__merica_terminal *pss =
130 (per_session_data__merica_terminal *)user;
132 per_vhost_data__merica_terminal *vhd =
133 (per_vhost_data__merica_terminal *)
134 lws_protocol_vh_priv_get(vhost, prot);
138 list **lines = (list **)&(prot->user);
142 case LWS_CALLBACK_PROTOCOL_INIT:
143 vhd = lws_protocol_vh_priv_zalloc(
145 sizeof(struct per_vhost_data__merica_terminal)
147 vhd->context = lws_get_context(wsi);
148 vhd->protocol = prot;
152 case LWS_CALLBACK_PROTOCOL_DESTROY:
158 case LWS_CALLBACK_ESTABLISHED:
161 case LWS_CALLBACK_SERVER_WRITEABLE:
162 line = (*lines)->line;
164 m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
165 if ((*lines)->next != NULL) {
167 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
168 &protocols[PROTOCOL_MERICA_TERMINAL]);
171 fprintf(stderr, "ERROR %d writing to di socket\n", n);
176 case LWS_CALLBACK_RECEIVE:
177 if (strcmp((const char *)in, "reset") == 0) {
178 strcpy((*lines)->line, JSON_EMPTY);
179 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
180 &protocols[PROTOCOL_MERICA_TERMINAL]);
181 } else if (strcmp((const char *)in, "close") == 0) {
182 fprintf(stderr, "closing websocket\n");
183 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
184 (unsigned char *)"seeya", 5);
196 static void fd_cb(EV_P_ ev_io *w_, int revents)
198 ev_io_ws *w = (ev_io_ws *)w_;
200 char *pos = w->pos++;
202 read(w->w.fd, pos, 1);
204 if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) {
206 if (list_add(&(w->lines), w->text) == 0) {
207 char *line = new_line();
211 lws_callback_on_writable_all_protocol(w->context,
212 &protocols[PROTOCOL_MERICA_TERMINAL]);
216 fprintf(stderr, "cannot malloc new line\n");
220 int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd)
222 struct lws_context_creation_info info;
224 memset(&info, 0, sizeof(info));
225 info.port = HTTP_PORT;
226 info.mounts = &mount;
227 info.protocols = protocols;
228 info.max_http_header_pool = 1;
229 info.options |= LWS_SERVER_OPTION_LIBEV;
231 struct lws_context *context = lws_create_context(&info);
233 fprintf(stderr, "lws_create_context failed\n");
237 self->context = context;
239 ev_io_ws *w = &(self->fd_watcher);
240 w->context = context;
241 char *line = copy_line(JSON_EMPTY);
245 w->lines = list_init(line);
250 protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->lines;
253 list_deinit(w->lines);
258 ev_io_init(&(w->w), fd_cb, fd, EV_READ);
259 ev_io_start(loop, (ev_io *)w);
261 return lws_ev_initloop(context, loop, 0);
264 void mt_server_deinit(mt_server_t *self)
266 list_deinit((list *)protocols[PROTOCOL_MERICA_TERMINAL].user);
267 lws_context_destroy(self->context);
271 int main(int argc, const char **argv)
273 struct ev_loop *loop = EV_DEFAULT;
276 set_signal_exit(loop);
278 if (mt_server_init(&server, loop, STDIN_FILENO) != 0) {
284 mt_server_deinit(&server);