piumsgd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2021 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. /*
  21. * PIU interface server.
  22. * piumsgd is in charge of passing messages from the underlying system
  23. * to the passenger via a websocket connection.
  24. *
  25. * piumsgd also acts as the web server to serve the local passenger
  26. * interface HTML/CSS/JS files.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <errno.h>
  31. #include <getopt.h>
  32. #include <string.h>
  33. #include "mongoose.h"
  34. struct mg_mgr g_mgr;
  35. char s_web_directory[] = ".";
  36. /*
  37. void ws_send(struct mg_mgr *mgr, char *msg) {
  38. struct mg_connection *conn;
  39. for (conn = mg_next(mgr, NULL); conn; conn = mg_next(mgr, conn)) {
  40. if (!is_websocket(conn)) { continue; }
  41. mg_send_websocket_frame(conn, WEBSOCKET_OP_TEXT, msg, strlen(msg));
  42. }
  43. }
  44. */
  45. void _ws_send(struct mg_mgr *mgr, char *msg, size_t msg_n) {
  46. int i;
  47. struct mg_connection *con;
  48. printf(" ... ");
  49. for (i=0; i<msg_n; i++) { printf("%c", msg[i]); }
  50. printf("\n");
  51. for (con = mgr->conns; con; con = con->next) {
  52. printf(" ...con %i %i\n", con->is_websocket, con->is_client);
  53. //if (con->is_websocket && con->is_client) {
  54. if (con->is_websocket && con->is_accepted) {
  55. printf(" !!\n");
  56. mg_ws_send(con, msg, msg_n, WEBSOCKET_OP_TEXT);
  57. }
  58. }
  59. }
  60. /*
  61. void _ws_send_str(struct mg_connection *mgc, char *msg) {
  62. size_t msg_n;
  63. msg_n = strlen(msg);
  64. _ws_send(mgc, msg, msg_n);
  65. }
  66. */
  67. static void _cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
  68. size_t n;
  69. if (ev == MG_EV_HTTP_MSG) {
  70. printf("bang\n"); fflush(stdout);
  71. struct mg_http_message *hm = (struct mg_http_message *) ev_data;
  72. if (mg_http_match_uri(hm, "/ws")) {
  73. // Upgrade to websocket. From now on, a connection is a full-duplex
  74. // Websocket connection, which will receive MG_EV_WS_MSG events.
  75. //
  76. printf("ws request?\n"); fflush(stdout);
  77. mg_ws_upgrade(c, hm, NULL);
  78. }
  79. else if (mg_http_match_uri(hm, "/")) {
  80. printf("html request? (1)\n"); fflush(stdout);
  81. struct mg_http_serve_opts opts = {.root_dir = s_web_directory};
  82. mg_http_serve_dir(c, hm, &opts);
  83. }
  84. else if (mg_http_match_uri(hm, "/rest")) {
  85. // Serve REST response
  86. //
  87. mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
  88. }
  89. else {
  90. // Serve static files
  91. //
  92. struct mg_http_serve_opts opts = {.root_dir = s_web_directory};
  93. mg_http_serve_dir(c, (mg_http_message *)ev_data, &opts);
  94. }
  95. } else if (ev == MG_EV_WS_MSG) {
  96. // Got websocket frame. Received data is wm->data. Echo it back!
  97. //
  98. struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
  99. printf(">>> ");
  100. for (int i=0; i<wm->data.len; i++) {
  101. printf("%c", *(wm->data.ptr + i));
  102. }
  103. printf("\n"); fflush(stdout);
  104. n = strlen("relay ");
  105. if (wm->data.len < n) {
  106. n = wm->data.len;
  107. }
  108. if (n < wm->data.len) {
  109. if (strncmp("relay ", (char *)(wm->data.ptr), n)==0) {
  110. _ws_send(&g_mgr, ((char *)(wm->data.ptr)) + n, wm->data.len - n);
  111. }
  112. }
  113. mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
  114. mg_iobuf_delete(&c->recv, c->recv.len);
  115. printf("broadcasting...\n");
  116. n = strlen("broadcast");
  117. _ws_send(&g_mgr, (char *)"broadcast", n);
  118. }
  119. (void) fn_data;
  120. }
  121. int main(int argc, char **argv) {
  122. //struct pollfd fds[32];
  123. int nfd=0;
  124. int poll_return;
  125. int read_return;
  126. time_t now;
  127. struct mg_conneciton *nc;
  128. // setup mongoose web server
  129. //
  130. mg_mgr_init(&g_mgr);
  131. mg_http_listen(&g_mgr, "http://127.0.0.1:8001", _cb, NULL);
  132. for (;;) {
  133. mg_mgr_poll(&g_mgr, 200);
  134. }
  135. mg_mgr_free(&g_mgr);
  136. return 0;
  137. }