| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /*
- * Copyright (c) 2021 Clementine Computing LLC.
- *
- * This file is part of PopuFare.
- *
- * PopuFare is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * PopuFare is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
- *
- */
- /*
- * PIU interface server.
- * piumsgd is in charge of passing messages from the underlying system
- * to the passenger via a websocket connection.
- *
- * piumsgd also acts as the web server to serve the local passenger
- * interface HTML/CSS/JS files.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <getopt.h>
- #include <string.h>
- #include "mongoose.h"
- struct mg_mgr g_mgr;
- char s_web_directory[] = ".";
- /*
- void ws_send(struct mg_mgr *mgr, char *msg) {
- struct mg_connection *conn;
- for (conn = mg_next(mgr, NULL); conn; conn = mg_next(mgr, conn)) {
- if (!is_websocket(conn)) { continue; }
- mg_send_websocket_frame(conn, WEBSOCKET_OP_TEXT, msg, strlen(msg));
- }
- }
- */
- void _ws_send(struct mg_mgr *mgr, char *msg, size_t msg_n) {
- int i;
- struct mg_connection *con;
- printf(" ... ");
- for (i=0; i<msg_n; i++) { printf("%c", msg[i]); }
- printf("\n");
- for (con = mgr->conns; con; con = con->next) {
- printf(" ...con %i %i\n", con->is_websocket, con->is_client);
- //if (con->is_websocket && con->is_client) {
- if (con->is_websocket && con->is_accepted) {
- printf(" !!\n");
- mg_ws_send(con, msg, msg_n, WEBSOCKET_OP_TEXT);
- }
- }
- }
- /*
- void _ws_send_str(struct mg_connection *mgc, char *msg) {
- size_t msg_n;
- msg_n = strlen(msg);
- _ws_send(mgc, msg, msg_n);
- }
- */
- static void _cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
- size_t n;
- if (ev == MG_EV_HTTP_MSG) {
- printf("bang\n"); fflush(stdout);
- struct mg_http_message *hm = (struct mg_http_message *) ev_data;
- if (mg_http_match_uri(hm, "/websocket")) {
- // Upgrade to websocket. From now on, a connection is a full-duplex
- // Websocket connection, which will receive MG_EV_WS_MSG events.
- //
- printf("ws request?\n"); fflush(stdout);
- mg_ws_upgrade(c, hm, NULL);
- }
- else if (mg_http_match_uri(hm, "/")) {
- printf("ws request? (1)\n"); fflush(stdout);
- mg_ws_upgrade(c, hm, NULL);
- }
- else if (mg_http_match_uri(hm, "/rest")) {
- // Serve REST response
- //
- mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
- }
- else {
- // Serve static files
- //
- struct mg_http_serve_opts opts = {.root_dir = s_web_directory};
- mg_http_serve_dir(c, (mg_http_message *)ev_data, &opts);
- }
- } else if (ev == MG_EV_WS_MSG) {
- // Got websocket frame. Received data is wm->data. Echo it back!
- //
- struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
- printf(">>> ");
- for (int i=0; i<wm->data.len; i++) {
- printf("%c", *(wm->data.ptr + i));
- }
- printf("\n"); fflush(stdout);
- n = strlen("relay ");
- if (wm->data.len < n) {
- n = wm->data.len;
- }
- if (n < wm->data.len) {
- if (strncmp("relay ", (char *)(wm->data.ptr), n)==0) {
- _ws_send(&g_mgr, ((char *)(wm->data.ptr)) + n, wm->data.len - n);
- }
- }
- mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
- mg_iobuf_delete(&c->recv, c->recv.len);
- printf("broadcasting...\n");
- n = strlen("broadcast");
- _ws_send(&g_mgr, (char *)"broadcast", n);
- }
- (void) fn_data;
- }
- int main(int argc, char **argv) {
- //struct pollfd fds[32];
- int nfd=0;
- int poll_return;
- int read_return;
- time_t now;
- struct mg_conneciton *nc;
- // setup mongoose web server
- //
- mg_mgr_init(&g_mgr);
- mg_http_listen(&g_mgr, "http://127.0.0.1:8001", _cb, NULL);
- for (;;) {
- mg_mgr_poll(&g_mgr, 200);
- }
- mg_mgr_free(&g_mgr);
- exit(0);
- /*
- //loop until we get asked to exit.../
- //
- while (1) {
- // Add our websocket socket to the list of file descriptors
- // to select on.
- //
- for (nc = g_mgr.active_connections; nc != NULL; nc = nc->next) {
- if (nc->sock != INVALID_SOCKET) {
- if (nfd < 32) {
- fds[nfd].fd = nc->sock;
- fds[nfd].events = POLLIN | POLLOUT;
- fds[nfd].revents = 0;
- nfd++;
- }
- }
- }
- }
- */
- }
|