| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- /*
- * 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/>.
- *
- */
- /*
- * A simple client to inject websocket messages to piumsgd
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <sysexits.h>
- #include "mongoose.h"
- #define PIUMSG "piumsg"
- #define PIUMSG_VERSION "0.1.0"
- #define PIUMSG_HOST "localhost"
- //#define PIUMSG_HOST "ws://192.168.0.26"
- #define PIUMSG_PORT 8001
- #define _BUFLEN 1024
- char *g_host = NULL;
- int g_port = PIUMSG_PORT;
- char *g_msg = NULL;
- //static const char *s_url = "ws://" PIUMSG_HOST ":" PIUMSG_PORT;
- //
- char *s_url=NULL;
- int g_verbose = 0;
- struct option g_longopt[] = {
- { "verbose", no_argument, 0, 'V'},
- { "version", no_argument, 0, 'v'},
- { "help", no_argument, 0, 'h'},
- {0,0,0,0}
- };
- // Print websocket response and signal that we're done
- //
- static void _ws(struct mg_connection *c, int ev, void *ev_data, void *_data) {
- size_t n;
- int *idat=NULL;
- idat = (int *)_data;
- if (ev == MG_EV_ERROR) {
- // On error, log error message
- //
- LOG(LL_ERROR, ("%p %s", c->fd, (char *) ev_data));
- } else if (ev == MG_EV_WS_OPEN) {
- // When websocket handshake is successful, send message
- //
- n = strlen(g_msg);
- printf("# sending: "); fflush(stdout);
- write(1, g_msg, n);
- printf("\n"); fflush(stdout);
- mg_ws_send(c, g_msg, n, WEBSOCKET_OP_TEXT);
- } else if (ev == MG_EV_WS_MSG) {
- // When we get echo response, print it
- //
- struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
- printf("# got: %.*s\n", (int) wm->data.len, wm->data.ptr);
- }
- if ((ev == MG_EV_ERROR) ||
- (ev == MG_EV_CLOSE) ||
- (ev == MG_EV_WS_MSG)) {
- printf("# ev %i (err%i, close%i, msg%i)\n",
- ev, MG_EV_ERROR, MG_EV_CLOSE, MG_EV_WS_MSG);
- // Signal that we're done
- //
- //*(bool *) fn_data = true;
- *idat = 1;
- }
- }
- void show_version(FILE *fp) {
- fprintf(fp, "version %s\n", PIUMSG_VERSION);
- }
- void show_help(FILE *fp) {
- fprintf(fp, "%s - connect to PIU display manager\n", PIUMSG);
- fprintf(fp, "\n");
- fprintf(fp, " usage: %s [-h] [-v] [-V] [msg]\n", PIUMSG);
- fprintf(fp, "\n");
- fprintf(fp, " msg push message\n");
- fprintf(fp, " [-H host] host (default %s)\n", PIUMSG_HOST);
- fprintf(fp, " [-p port] port (default %i)\n", PIUMSG_PORT);
- fprintf(fp, " [-V] verbose\n");
- fprintf(fp, " [-h] help (this screen)\n");
- fprintf(fp, " [-v] print version\n");
- }
- int main(int argc, char **argv) {
- struct mg_mgr mgr;
- //bool done = false;
- int done = 0;
- struct mg_connection *c;
- int ch, option_index;
- s_url = (char *)malloc(sizeof(char)*_BUFLEN);
- while ((ch=getopt_long(argc, argv, "hvVm:H:p:", g_longopt, &option_index)) >= 0) {
- switch (ch) {
- case 0:
- break;
- case 'H':
- g_host = strdup(optarg);
- break;
- case 'p':
- g_port = atoi(optarg);
- break;
- case 'V':
- g_verbose++;
- break;
- case 'v':
- show_version(stdout);
- break;
- case 'h':
- show_help(stdout);
- exit(-1);
- default:
- break;
- }
- }
- if (optind < argc) {
- g_msg = strdup(argv[optind]);
- }
- if (!g_msg) {
- fprintf(stderr, "provide message\n");
- show_help(stderr);
- exit(EX_NOINPUT);
- }
- if (!g_host) {
- g_host = strdup(PIUMSG_HOST);
- }
- snprintf(s_url, _BUFLEN-1, "ws://%s:%i", g_host, g_port);
- s_url[_BUFLEN-1]=0;
- mg_mgr_init(&mgr);
- c = mg_ws_connect(&mgr, s_url, _ws, &done, NULL);
- while (c && (done == 0)) {
- mg_mgr_poll(&mgr, 1000);
- }
- mg_mgr_free(&mgr);
- free(g_msg);
- free(g_host);
- return 0;
- }
|