summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/muniacli.cc185
1 files changed, 182 insertions, 3 deletions
diff --git a/src/muniacli.cc b/src/muniacli.cc
index 5c6b207..4a95b2a 100644
--- a/src/muniacli.cc
+++ b/src/muniacli.cc
@@ -34,12 +34,190 @@
#include <list>
#include <map>
-#include "message.h"
-#include "messageparser.h"
+//#include "message.h"
+//#include "messageparser.h"
#include <libwebsockets.h>
+static bool run;
+static std::string msgs;
+
+struct per_session_data__lws_task {
+ struct libwebsocket *wsi;
+};
+
+static int callback_lws_task(struct libwebsocket_context *me,
+ struct libwebsocket *wsi,
+ enum libwebsocket_callback_reasons reason,
+ void *user, void *in, size_t len)
+{
+ unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
+ LWS_SEND_BUFFER_POST_PADDING];
+ int l;
+ char msg[4096];
+ std::string r;
+
+ switch (reason) {
+
+ case LWS_CALLBACK_CLOSED:
+// fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED\n");
+ break;
+
+ case LWS_CALLBACK_CLIENT_ESTABLISHED:
+ libwebsocket_callback_on_writable(me, wsi);
+ break;
+
+ case LWS_CALLBACK_CLIENT_RECEIVE:
+// pretty_print((char*)in);
+ printf("%s\n", (char*)in);
+ libwebsocket_callback_on_writable(me, wsi);
+
+// if(!interactive) run = false;
+ run = false;
+ break;
+
+ case LWS_CALLBACK_CLIENT_WRITEABLE:
+
+// if(interactive) {
+// fgets(msg, sizeof(msg), stdin);
+// msgs += msg;
+// printf("send...\n");
+// }
+
+ l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING], "%s", msgs.c_str());
+
+ libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
+ l, LWS_WRITE_TEXT);
+ break;
+
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+static struct libwebsocket_protocols protocols[] = {
+ { "lws-task-protocol", callback_lws_task,
+ sizeof(struct per_session_data__lws_task)
+ },
+ { NULL, NULL, 0 } // End of list
+};
+
+int client(const char *address, int port)
+{
+// printf("Connection to %s on port %d\n", address, port);
+ struct libwebsocket_context *context;
+ struct libwebsocket *wsi_task = NULL;
+ int ietf_version = -1; // latest
+
+ lws_set_log_level(0, NULL);
+ struct lws_context_creation_info info;
+ memset(&info, 0, sizeof info);
+ info.port = port;
+ info.iface = NULL; // What is this?
+ info.protocols = protocols;
+ info.extensions = libwebsocket_get_internal_extensions();
+
+ context = libwebsocket_create_context(&info);
+
+ if (context == NULL) {
+ fprintf(stderr, "Creating libwebsocket context failed\n");
+ return 1;
+ }
+
+
+ /*
+ * sit there servicing the websocket context to handle incoming
+ * packets, and drawing random circles on the mirror protocol websocket
+ */
+ int n = 1;
+ while(n >= 0) {
+ n = libwebsocket_service(context, 1000);
+
+ if (wsi_task == NULL) {
+ wsi_task = libwebsocket_client_connect(context, address, port,
+ 0, "/", address, address,
+ "lws-task-protocol",
+ ietf_version);
+
+ if (wsi_task == NULL) {
+ fprintf(stderr, "libwebsocket task connect failed\n");
+ return -1;
+ }
+ else {
+ }
+
+ } else {
+// fprintf(stderr, "closing mirror session\n");
+// libwebsocket_close_and_free_session(context,
+// wsi_mirror, LWS_CLOSE_STATUS_GOINGAWAY);
+ }
+
+ if(!run) break;
+ }
+
+ libwebsocket_context_destroy(context);
+
+ return 0;
+}
+
+static struct option options[] = {
+ { "command", required_argument, NULL, 'c'},
+ { "help", no_argument, NULL, 'h' },
+ { "host", required_argument, NULL, 'H'},
+ { "port", required_argument, NULL, 'p' },
+ { "raw", no_argument, NULL, 'r'},
+ { NULL, 0, 0, 0 }
+};
+
+static const char* USAGE=
+ "Usage: muniacli [OPTION]... TASK COMMAND\n"
+ "Commandline client to munia."
+ "Options:\n"
+ " -p, --port Port on host\n"
+ " -H, --host Host\n"
+ " -c, --command Command\n";
+
+int main(int argc, char** argv)
+{
+ int port = 7681;
+ std::string host = "localhost";
+ std::string cmd;
+ bool raw = true;
+
+ run = true;
+
+ int n = 0;
+ while(n >= 0) {
+ n = getopt_long(argc, argv, "c:H:p:r", options, NULL);
+ if(n < 0) continue;
+ switch(n) {
+ case 'c':
+ cmd = optarg;
+ break;
+ case 'p':
+ port = atoi(optarg);
+ break;
+ case 'H':
+ host = optarg;
+ break;
+ case 'r':
+ raw = true;
+ break;
+ case 'h':
+ fprintf(stderr, "%s\n", USAGE);
+ exit(1);
+ break;
+ }
+ }
+ msgs = cmd;
+
+ client(host.c_str(), port);
+ return 0;
+}
+#if 0
enum demo_protocols {
PROTOCOL_TASK
};
@@ -212,7 +390,7 @@ int client(const char *address, int port)
if (wsi_task == NULL) {
wsi_task = libwebsocket_client_connect(context, address, port,
0, "/", address, address,
- protocols[PROTOCOL_TASK].name,
+ protocols[lws-task-protocol].name,
ietf_version);
if (wsi_task == NULL) {
@@ -381,3 +559,4 @@ int main(int argc, char** argv)
client(host.c_str(), port);
return 0;
}
+#endif/*0*/