piumsgd.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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, "/websocket")) {
  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("ws request? (1)\n"); fflush(stdout);
  81. mg_ws_upgrade(c, hm, NULL);
  82. }
  83. else if (mg_http_match_uri(hm, "/rest")) {
  84. // Serve REST response
  85. //
  86. mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
  87. }
  88. else {
  89. // Serve static files
  90. //
  91. struct mg_http_serve_opts opts = {.root_dir = s_web_directory};
  92. mg_http_serve_dir(c, (mg_http_message *)ev_data, &opts);
  93. }
  94. } else if (ev == MG_EV_WS_MSG) {
  95. // Got websocket frame. Received data is wm->data. Echo it back!
  96. //
  97. struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
  98. printf(">>> ");
  99. for (int i=0; i<wm->data.len; i++) {
  100. printf("%c", *(wm->data.ptr + i));
  101. }
  102. printf("\n"); fflush(stdout);
  103. n = strlen("relay ");
  104. if (wm->data.len < n) {
  105. n = wm->data.len;
  106. }
  107. if (n < wm->data.len) {
  108. if (strncmp("relay ", (char *)(wm->data.ptr), n)==0) {
  109. _ws_send(&g_mgr, ((char *)(wm->data.ptr)) + n, wm->data.len - n);
  110. }
  111. }
  112. mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
  113. mg_iobuf_delete(&c->recv, c->recv.len);
  114. printf("broadcasting...\n");
  115. n = strlen("broadcast");
  116. _ws_send(&g_mgr, (char *)"broadcast", n);
  117. }
  118. (void) fn_data;
  119. }
  120. int main(int argc, char **argv) {
  121. //struct pollfd fds[32];
  122. int nfd=0;
  123. int poll_return;
  124. int read_return;
  125. time_t now;
  126. struct mg_conneciton *nc;
  127. // setup mongoose web server
  128. //
  129. mg_mgr_init(&g_mgr);
  130. mg_http_listen(&g_mgr, "http://127.0.0.1:8001", _cb, NULL);
  131. for (;;) {
  132. mg_mgr_poll(&g_mgr, 200);
  133. }
  134. mg_mgr_free(&g_mgr);
  135. exit(0);
  136. /*
  137. //loop until we get asked to exit.../
  138. //
  139. while (1) {
  140. // Add our websocket socket to the list of file descriptors
  141. // to select on.
  142. //
  143. for (nc = g_mgr.active_connections; nc != NULL; nc = nc->next) {
  144. if (nc->sock != INVALID_SOCKET) {
  145. if (nfd < 32) {
  146. fds[nfd].fd = nc->sock;
  147. fds[nfd].events = POLLIN | POLLOUT;
  148. fds[nfd].revents = 0;
  149. nfd++;
  150. }
  151. }
  152. }
  153. }
  154. */
  155. }