Websocket stdio / websocketd experiment
authorBill Erickson <berickxx@gmail.com>
Sat, 9 Jun 2018 23:05:25 +0000 (19:05 -0400)
committerBill Erickson <berickxx@gmail.com>
Sat, 9 Jun 2018 23:05:25 +0000 (19:05 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
configure.ac
src/Makefile.am
src/websocket-stdio/Makefile.am [new file with mode: 0644]
src/websocket-stdio/osrf-websocket-stdio.c [new file with mode: 0644]

index 073d6f8..48ffab8 100644 (file)
@@ -387,6 +387,7 @@ if test "x$OSRF_INSTALL_CORE" = "xtrue"; then
                         src/python/opensrf.py
                         src/router/Makefile
                         src/srfsh/Makefile
+                        src/websocket-stdio/Makefile
              tests/Makefile
                         bin/opensrf-perl.pl
                         bin/osrf_config])
index a86281f..c8b6107 100644 (file)
@@ -40,7 +40,7 @@ js_SCRIPTS = javascript/DojoSRF.js javascript/JSON_v1.js javascript/md5.js javas
 endif
 
 if BUILDCORE
-MAYBE_CORE = libopensrf c-apps router srfsh gateway perl
+MAYBE_CORE = libopensrf c-apps router srfsh gateway perl websocket-stdio
 if BUILDPYTHON
 dist_bin_SCRIPTS = @top_srcdir@/bin/opensrf-perl.pl @top_srcdir@/src/python/opensrf.py @top_srcdir@/src/python/srfsh.py
 else
diff --git a/src/websocket-stdio/Makefile.am b/src/websocket-stdio/Makefile.am
new file mode 100644 (file)
index 0000000..0bb6c99
--- /dev/null
@@ -0,0 +1,22 @@
+# Copyright (C) 2008 Equinox Software, Inc.
+# Kevin Beswick <kevinbeswick00@gmail.com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+
+
+LDADD = $(DEF_LDLIBS) 
+AM_CFLAGS = $(DEF_CFLAGS) -DEXEC_DEFAULT -L@top_builddir@/src/libopensrf
+AM_LDFLAGS = $(DEF_LDFLAGS)
+
+DISTCLEANFILES = Makefile.in Makefile
+
+bin_PROGRAMS = osrf-websocket-stdio
+osrf_websocket_stdio_SOURCES = osrf-websocket-stdio.c
diff --git a/src/websocket-stdio/osrf-websocket-stdio.c b/src/websocket-stdio/osrf-websocket-stdio.c
new file mode 100644 (file)
index 0000000..a237c06
--- /dev/null
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <opensrf/utils.h>
+#include <opensrf/socket_bundle.h>
+#include <opensrf/transport_client.h>
+#include <opensrf/osrf_message.h>
+#include <opensrf/osrf_app_session.h>
+#include <opensrf/utils.h>
+#include <opensrf/log.h>
+
+static void child_init();
+static void read_from_stdin();
+static void read_from_osrf();
+static growing_buffer* stdin_buf = NULL;
+static transport_client* osrf_handle = NULL; 
+char* osrf_config = "/openils/conf/opensrf_core.xml"; // TODO
+
+int main() {
+    // Reusable stdin reading buffer.
+    stdin_buf = buffer_init(512);
+
+    child_init(); // exits on error
+
+    // Disable output buffering.
+    setbuf(stdout, NULL);
+
+    fd_set fds;
+    int stdin_no = fileno(stdin);
+    int osrf_no = osrf_handle->session->sock_id;
+    int maxfd = osrf_no > stdin_no ? osrf_no : stdin_no;
+
+    while (1) {
+
+        FD_ZERO(&fds);
+        FD_SET(osrf_no, &fds); 
+        FD_SET(stdin_no, &fds); 
+
+        select(maxfd + 1, &fds, NULL, NULL, NULL); 
+
+        if (FD_ISSET(stdin_no, &fds)) {
+            read_from_stdin();
+        }
+
+        if (FD_ISSET(osrf_no, &fds)) {
+            read_from_osrf();
+        }
+    }
+
+    buffer_free(stdin_buf);
+    return 0;
+}
+
+
+// Connect to opensrf
+static void child_init() {
+
+    if (!osrf_system_bootstrap_client(osrf_config, "gateway") ) {
+        fprintf(stderr, "Cannot boostrap OSRF\n");
+        exit(1);
+    }
+
+       osrf_handle = osrfSystemGetTransportClient();
+       osrfAppSessionSetIngress("osrf-websocket-stdio");
+}
+
+// Relay messages from STDIN to OpenSRF
+// Reads one message then exits
+static void read_from_stdin() {
+    char c;
+
+    while ( (c = getchar()) ) {
+
+        if (c == '\n') { // end of current message
+
+            if (stdin_buf->n_used > 0) {
+                printf("Echoing: %s\n", stdin_buf->buf);
+                buffer_reset(stdin_buf);
+            }
+
+            return;
+
+        } else {
+
+            buffer_add_char(stdin_buf, c);
+        }
+    }
+}
+
+
+// Relay messages from OpenSRF to STDIN
+// Relays all available messages
+static void read_from_osrf() {
+
+}
+
+
+