From: Bill Erickson Date: Sat, 9 Jun 2018 23:05:25 +0000 (-0400) Subject: Websocket stdio / websocketd experiment X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=6729ff777d9555d6d5c2cff62b4b5ba23ed72f6d;p=working%2FOpenSRF.git Websocket stdio / websocketd experiment Signed-off-by: Bill Erickson --- diff --git a/configure.ac b/configure.ac index 073d6f8..48ffab8 100644 --- a/configure.ac +++ b/configure.ac @@ -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]) diff --git a/src/Makefile.am b/src/Makefile.am index a86281f..c8b6107 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 0000000..0bb6c99 --- /dev/null +++ b/src/websocket-stdio/Makefile.am @@ -0,0 +1,22 @@ +# Copyright (C) 2008 Equinox Software, Inc. +# Kevin Beswick +# +# 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 index 0000000..a237c06 --- /dev/null +++ b/src/websocket-stdio/osrf-websocket-stdio.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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() { + +} + + +