|
|
@@ -36,48 +36,39 @@
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
|
-struct mg_mgr g_mgr;
|
|
|
-
|
|
|
-//char s_web_directory[] = ".";
|
|
|
-char s_web_directory[] = "/home/bus/config/html";
|
|
|
+#define PIUMSGD_VERSION "0.1.0"
|
|
|
+#define PIUMSGD_DEFAULT_WEBDIR "/home/bus/config/html"
|
|
|
+#define PIUMSGD_DEFAULT_ADDR "0.0.0.0"
|
|
|
+#define PIUMSGD_DEFAULT_PORT 8001
|
|
|
+
|
|
|
+static struct option long_opt[] = {
|
|
|
+ {"webdir", required_argument, 0, 0},
|
|
|
+ {"host", required_argument, 0, 0},
|
|
|
+ {"port", required_argument, 0, 0},
|
|
|
+ {"help", no_argument, 0, 0},
|
|
|
+ {"verbose", no_argument, 0, 0},
|
|
|
+ {"version", no_argument, 0, 0},
|
|
|
+ {0, 0, 0, 0}
|
|
|
+};
|
|
|
+
|
|
|
+int g_verbose = 0;
|
|
|
+
|
|
|
+//char s_web_directory[] = "/home/bus/config/html";
|
|
|
+char *s_web_directory = NULL;
|
|
|
|
|
|
-/*
|
|
|
-void ws_send(struct mg_mgr *mgr, char *msg) {
|
|
|
- struct mg_connection *conn;
|
|
|
- for (conn = mg_next(mgr, NULL); conn; conn = mg_next(mgr, conn)) {
|
|
|
- if (!is_websocket(conn)) { continue; }
|
|
|
- mg_send_websocket_frame(conn, WEBSOCKET_OP_TEXT, msg, strlen(msg));
|
|
|
- }
|
|
|
-}
|
|
|
-*/
|
|
|
+struct mg_mgr g_mgr;
|
|
|
|
|
|
void _ws_send(struct mg_mgr *mgr, char *msg, size_t msg_n) {
|
|
|
int i;
|
|
|
struct mg_connection *con;
|
|
|
|
|
|
- printf(" ... ");
|
|
|
- for (i=0; i<msg_n; i++) { printf("%c", msg[i]); }
|
|
|
- printf("\n");
|
|
|
-
|
|
|
for (con = mgr->conns; con; con = con->next) {
|
|
|
- printf(" ...con %i %i\n", con->is_websocket, con->is_client);
|
|
|
- //if (con->is_websocket && con->is_client) {
|
|
|
if (con->is_websocket && con->is_accepted) {
|
|
|
- printf(" !!\n");
|
|
|
-
|
|
|
mg_ws_send(con, msg, msg_n, WEBSOCKET_OP_TEXT);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-void _ws_send_str(struct mg_connection *mgc, char *msg) {
|
|
|
- size_t msg_n;
|
|
|
- msg_n = strlen(msg);
|
|
|
- _ws_send(mgc, msg, msg_n);
|
|
|
-}
|
|
|
-*/
|
|
|
-
|
|
|
|
|
|
static void _cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|
|
int i;
|
|
|
@@ -86,70 +77,40 @@ static void _cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|
|
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
|
|
|
|
|
- printf("bang\n"); fflush(stdout);
|
|
|
-
|
|
|
hm = (struct mg_http_message *) ev_data;
|
|
|
|
|
|
- //DEBUG
|
|
|
- //ptr len
|
|
|
- /*
|
|
|
- printf("body:\n");
|
|
|
- for (i=0; i<hm->body.len; i++) {
|
|
|
- printf("%i (%c)\n", hm->body.ptr[i], hm->body.ptr[i]);
|
|
|
- }
|
|
|
- printf("message:\n");
|
|
|
- for (i=0; i<hm->message.len; i++) {
|
|
|
- printf("%c", hm->message.ptr[i], hm->message.ptr[i]);
|
|
|
- }
|
|
|
- printf("\n\n");
|
|
|
- */
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
if (mg_http_match_uri(hm, "/ws")) {
|
|
|
-
|
|
|
// Upgrade to websocket. From now on, a connection is a full-duplex
|
|
|
// Websocket connection, which will receive MG_EV_WS_MSG events.
|
|
|
//
|
|
|
- printf("ws request?\n"); fflush(stdout);
|
|
|
-
|
|
|
mg_ws_upgrade(c, hm, NULL);
|
|
|
}
|
|
|
- else if (mg_http_match_uri(hm, "/")) {
|
|
|
|
|
|
- printf("html request? (1)\n"); fflush(stdout);
|
|
|
+ else if (mg_http_match_uri(hm, "/")) {
|
|
|
struct mg_http_serve_opts opts = {.root_dir = s_web_directory};
|
|
|
mg_http_serve_dir(c, hm, &opts);
|
|
|
}
|
|
|
- else if (mg_http_match_uri(hm, "/rest")) {
|
|
|
-
|
|
|
- printf("other req?\n");
|
|
|
|
|
|
+ else if (mg_http_match_uri(hm, "/rest")) {
|
|
|
// Serve REST response
|
|
|
//
|
|
|
mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
|
|
|
}
|
|
|
- else {
|
|
|
-
|
|
|
- printf("cp x\n");
|
|
|
|
|
|
+ else {
|
|
|
// Serve static files
|
|
|
//
|
|
|
struct mg_http_serve_opts opts = {.root_dir = s_web_directory};
|
|
|
mg_http_serve_dir(c, (mg_http_message *)ev_data, &opts);
|
|
|
}
|
|
|
- } else if (ev == MG_EV_WS_MSG) {
|
|
|
+ }
|
|
|
+
|
|
|
+ else if (ev == MG_EV_WS_MSG) {
|
|
|
|
|
|
// Got websocket frame. Received data is wm->data. Echo it back!
|
|
|
//
|
|
|
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
|
|
|
|
|
|
- printf(">>> ");
|
|
|
- for (int i=0; i<wm->data.len; i++) {
|
|
|
- printf("%c", *(wm->data.ptr + i));
|
|
|
- }
|
|
|
- printf("\n"); fflush(stdout);
|
|
|
-
|
|
|
n = strlen("relay ");
|
|
|
if (wm->data.len < n) {
|
|
|
n = wm->data.len;
|
|
|
@@ -164,17 +125,37 @@ static void _cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|
|
mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
|
|
|
mg_iobuf_delete(&c->recv, c->recv.len);
|
|
|
|
|
|
- printf("broadcasting...\n");
|
|
|
n = strlen("broadcast");
|
|
|
_ws_send(&g_mgr, (char *)"broadcast", n);
|
|
|
|
|
|
}
|
|
|
+
|
|
|
(void) fn_data;
|
|
|
}
|
|
|
|
|
|
-int main(int argc, char **argv) {
|
|
|
+void show_version(FILE *ofp) {
|
|
|
+ fprintf(ofp, "piumsgd version %s\n", PIUMSGD_VERSION);
|
|
|
+}
|
|
|
+
|
|
|
+void show_help(FILE *ofp) {
|
|
|
+
|
|
|
+ show_version(ofp);
|
|
|
+
|
|
|
+ fprintf(ofp, "\npiusmgd usage:\n\n");
|
|
|
+ fprintf(ofp, " piumsgd [-h] [-v] [-p port] [-H host] [-D webdir] [-V]\n");
|
|
|
+ fprintf(ofp, "\n");
|
|
|
+ fprintf(ofp, " -D web directory (default %s)\n", PIUMSGD_DEFAULT_WEBDIR);
|
|
|
+ fprintf(ofp, " -H host (default %s)\n", PIUMSGD_DEFAULT_ADDR);
|
|
|
+ fprintf(ofp, " -p port (default %i)\n", PIUMSGD_DEFAULT_PORT);
|
|
|
+ fprintf(ofp, " -V verbose\n");
|
|
|
+ fprintf(ofp, " -v show version\n");
|
|
|
+ fprintf(ofp, " -h help (this screen)\n");
|
|
|
+ fprintf(ofp, "\n");
|
|
|
+}
|
|
|
|
|
|
- //struct pollfd fds[32];
|
|
|
+
|
|
|
+int main(int argc, char **argv) {
|
|
|
+ int ch, opt_index;
|
|
|
int nfd=0;
|
|
|
|
|
|
int poll_return;
|
|
|
@@ -182,13 +163,66 @@ int main(int argc, char **argv) {
|
|
|
|
|
|
time_t now;
|
|
|
|
|
|
+ int port = -1;
|
|
|
+ char *host = NULL, *webdir = NULL;
|
|
|
struct mg_conneciton *nc;
|
|
|
|
|
|
+ char *listen_host = NULL;
|
|
|
+
|
|
|
+ while ((ch = getopt_long(argc, argv, "hvVH:D:p:", long_opt, &opt_index)) >= 0) {
|
|
|
+ switch (ch) {
|
|
|
+ case 0:
|
|
|
+ break;
|
|
|
+ case 'h':
|
|
|
+ show_help(stdout);
|
|
|
+ exit(0);
|
|
|
+ break;
|
|
|
+ case 'v':
|
|
|
+ show_version(stdout);
|
|
|
+ exit(0);
|
|
|
+ break;
|
|
|
+ case 'V':
|
|
|
+ g_verbose++;
|
|
|
+ break;
|
|
|
+ case 'p':
|
|
|
+ port = atoi(optarg);
|
|
|
+ break;
|
|
|
+ case 'H':
|
|
|
+ host = strdup(optarg);
|
|
|
+ break;
|
|
|
+ case 'D':
|
|
|
+ s_web_directory = strdup(optarg);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ fprintf(stderr, "unknown option %c\n", ch);
|
|
|
+ show_help(stderr);
|
|
|
+ exit(-1);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!s_web_directory) {
|
|
|
+ s_web_directory = strdup(PIUMSGD_DEFAULT_WEBDIR);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (port < 0) {
|
|
|
+ port = PIUMSGD_DEFAULT_PORT;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!host) {
|
|
|
+ host = strdup(PIUMSGD_DEFAULT_ADDR);
|
|
|
+ }
|
|
|
+
|
|
|
+ listen_host = (char *)malloc(sizeof(char)*2*strlen(host));
|
|
|
+ sprintf(listen_host, "http://%s:%i", host, port);
|
|
|
+
|
|
|
// setup mongoose web server
|
|
|
//
|
|
|
mg_mgr_init(&g_mgr);
|
|
|
+
|
|
|
//mg_http_listen(&g_mgr, "http://127.0.0.1:8001", _cb, NULL);
|
|
|
- mg_http_listen(&g_mgr, "http://0.0.0.0:8001", _cb, NULL);
|
|
|
+ //mg_http_listen(&g_mgr, "http://0.0.0.0:8001", _cb, NULL);
|
|
|
+ mg_http_listen(&g_mgr, listen_host, _cb, NULL);
|
|
|
for (;;) {
|
|
|
mg_mgr_poll(&g_mgr, 200);
|
|
|
}
|