piumsg.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * A simple client to inject websocket messages to piumsgd
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <unistd.h>
  29. #include <getopt.h>
  30. #include <sysexits.h>
  31. #include "mongoose.h"
  32. #define PIUMSG "piumsg"
  33. #define PIUMSG_VERSION "0.1.0"
  34. #define PIUMSG_HOST "127.0.0.1"
  35. #define PIUMSG_PATH "ws"
  36. //#define PIUMSG_HOST "ws://192.168.0.26"
  37. #define PIUMSG_PORT 8001
  38. #define _BUFLEN 1024
  39. char *g_host = NULL;
  40. char *g_path= NULL;
  41. int g_port = PIUMSG_PORT;
  42. char *g_msg = NULL;
  43. //static const char *s_url = "ws://" PIUMSG_HOST ":" PIUMSG_PORT;
  44. //
  45. char *s_url=NULL;
  46. int g_verbose = 0;
  47. struct option g_longopt[] = {
  48. { "verbose", no_argument, 0, 'V'},
  49. { "version", no_argument, 0, 'v'},
  50. { "help", no_argument, 0, 'h'},
  51. {0,0,0,0}
  52. };
  53. // Print websocket response and signal that we're done
  54. //
  55. static void _ws(struct mg_connection *c, int ev, void *ev_data, void *_data) {
  56. size_t n;
  57. int *idat=NULL;
  58. idat = (int *)_data;
  59. if (ev == MG_EV_ERROR) {
  60. // On error, log error message
  61. //
  62. LOG(LL_ERROR, ("%p %s", c->fd, (char *) ev_data));
  63. } else if (ev == MG_EV_WS_OPEN) {
  64. // When websocket handshake is successful, send message
  65. //
  66. n = strlen(g_msg);
  67. //printf("# sending: "); fflush(stdout);
  68. //write(1, g_msg, n);
  69. //printf("\n"); fflush(stdout);
  70. mg_ws_send(c, g_msg, n, WEBSOCKET_OP_TEXT);
  71. } else if (ev == MG_EV_WS_MSG) {
  72. // When we get echo response, print it
  73. //
  74. struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
  75. //printf("# got: %.*s\n", (int) wm->data.len, wm->data.ptr);
  76. }
  77. if ((ev == MG_EV_ERROR) ||
  78. (ev == MG_EV_CLOSE) ||
  79. (ev == MG_EV_WS_MSG)) {
  80. //printf("# ev %i (err%i, close%i, msg%i)\n",
  81. // ev, MG_EV_ERROR, MG_EV_CLOSE, MG_EV_WS_MSG);
  82. // Signal that we're done
  83. //
  84. //*(bool *) fn_data = true;
  85. *idat = 1;
  86. }
  87. }
  88. void show_version(FILE *fp) {
  89. fprintf(fp, "version %s\n", PIUMSG_VERSION);
  90. }
  91. void show_help(FILE *fp) {
  92. fprintf(fp, "%s - connect to PIU display manager\n", PIUMSG);
  93. fprintf(fp, "\n");
  94. fprintf(fp, " usage: %s [-h] [-v] [-V] [msg]\n", PIUMSG);
  95. fprintf(fp, "\n");
  96. fprintf(fp, " msg push message\n");
  97. fprintf(fp, " [-H host] host (default %s)\n", PIUMSG_HOST);
  98. fprintf(fp, " [-P path] path on host (default %s)\n", PIUMSG_PATH);
  99. fprintf(fp, " [-p port] port (default %i)\n", PIUMSG_PORT);
  100. fprintf(fp, " [-V] verbose\n");
  101. fprintf(fp, " [-h] help (this screen)\n");
  102. fprintf(fp, " [-v] print version\n");
  103. }
  104. int main(int argc, char **argv) {
  105. struct mg_mgr mgr;
  106. //bool done = false;
  107. int done = 0;
  108. struct mg_connection *c;
  109. int ch, option_index;
  110. s_url = (char *)malloc(sizeof(char)*_BUFLEN);
  111. while ((ch=getopt_long(argc, argv, "hvVm:H:p:", g_longopt, &option_index)) >= 0) {
  112. switch (ch) {
  113. case 0:
  114. break;
  115. case 'H':
  116. g_host = strdup(optarg);
  117. break;
  118. case 'P':
  119. g_path = strdup(optarg);
  120. break;
  121. case 'p':
  122. g_port = atoi(optarg);
  123. break;
  124. case 'V':
  125. g_verbose++;
  126. break;
  127. case 'v':
  128. show_version(stdout);
  129. break;
  130. case 'h':
  131. show_help(stdout);
  132. exit(-1);
  133. default:
  134. break;
  135. }
  136. }
  137. if (optind < argc) {
  138. g_msg = strdup(argv[optind]);
  139. }
  140. if (!g_msg) {
  141. fprintf(stderr, "provide message\n");
  142. show_help(stderr);
  143. exit(EX_NOINPUT);
  144. }
  145. if (!g_host) {
  146. g_host = strdup(PIUMSG_HOST);
  147. }
  148. if (!g_path) {
  149. g_path = strdup(PIUMSG_PATH);
  150. }
  151. snprintf(s_url, _BUFLEN-1, "ws://%s:%i/%s", g_host, g_port, g_path);
  152. s_url[_BUFLEN-1]=0;
  153. mg_mgr_init(&mgr);
  154. c = mg_ws_connect(&mgr, s_url, _ws, &done, NULL);
  155. while (c && (done == 0)) {
  156. mg_mgr_poll(&mgr, 1000);
  157. }
  158. mg_mgr_free(&mgr);
  159. free(g_msg);
  160. free(g_host);
  161. free(g_path);
  162. return 0;
  163. }