piumsg.c 4.3 KB

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