From b16852503f3798bde7496d60664f852247589901 Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 2 Sep 2005 20:38:27 +0000 Subject: [PATCH] added dbmath made some api changes to session, more to come git-svn-id: svn://svn.open-ils.org/ILS/trunk@1793 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- OpenSRF/src/c-apps/Makefile | 7 ++- OpenSRF/src/c-apps/osrf_dbmath.c | 68 +++++++++++++++++++++++++ OpenSRF/src/c-apps/osrf_math.c | 34 +++++-------- OpenSRF/src/libstack/osrf_app_session.c | 16 ++++++ OpenSRF/src/libstack/osrf_app_session.h | 7 +++ 5 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 OpenSRF/src/c-apps/osrf_dbmath.c diff --git a/OpenSRF/src/c-apps/Makefile b/OpenSRF/src/c-apps/Makefile index 592b0cada2..62301e78b6 100644 --- a/OpenSRF/src/c-apps/Makefile +++ b/OpenSRF/src/c-apps/Makefile @@ -1,15 +1,20 @@ LDLIBS += -lobjson -lopensrf -all: osrf_math.so +all: osrf_math.so osrf_dbmath.so osrf_math.o: osrf_math.c +osrf_dbmath.o: osrf_dbmath.c osrf_math.so: osrf_math.o $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) osrf_math.o -o $(TMPDIR)/osrf_math.so +osrf_dbmath.so: osrf_dbmath.o + $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) osrf_dbmath.o -o $(TMPDIR)/osrf_dbmath.so + install: cp $(TMPDIR)/osrf_math.so $(LIBDIR)/ + cp $(TMPDIR)/osrf_dbmath.so $(LIBDIR)/ clean: rm -f *.o *.so diff --git a/OpenSRF/src/c-apps/osrf_dbmath.c b/OpenSRF/src/c-apps/osrf_dbmath.c new file mode 100644 index 0000000000..84feba258c --- /dev/null +++ b/OpenSRF/src/c-apps/osrf_dbmath.c @@ -0,0 +1,68 @@ +#include "opensrf/osrf_app_session.h" +#include "opensrf/osrf_application.h" +#include "objson/object.h" + +int initialize(); +int childInit(); +int osrfMathRun( osrfMethodDispatcher* ); + + +int initialize() { + osrfAppRegisterMethod( "opensrf.dbmath", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 ); + osrfAppRegisterMethod( "opensrf.dbmath", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 ); + osrfAppRegisterMethod( "opensrf.dbmath", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 ); + osrfAppRegisterMethod( "opensrf.dbmath", "div", "osrfMathRun", "send 2 numbers and I'll subtract them", 2 ); + return 0; +} + +int childInit() { + return 0; +} + +int osrfMathRun( osrfMethodDispatcher* d ) { + + /* + OSRF_METHOD_VERIFY_DISPATCHER(d) + Verifies viability of the dispatcher components. + Checks for NULLness of key components. + Creates local variables : + session - the app session ( osrfAppSession* ) + method - the method ( osrfMethod* ) + params - the methd parameters ( jsonObject* ) + request - the request id ( int ) */ + + OSRF_METHOD_VERIFY_DISPATCHER(d); + + jsonObject* x = jsonObjectGetIndex(params, 0); + jsonObject* y = jsonObjectGetIndex(params, 1); + + if( x && y ) { + + char* a = jsonObjectToSimpleString(x); + char* b = jsonObjectToSimpleString(y); + + if( a && b ) { + + double i = strtod(a, NULL); + double j = strtod(b, NULL); + double r = 0; + + if(!strcmp(method->name, "add")) r = i + j; + if(!strcmp(method->name, "sub")) r = i - j; + if(!strcmp(method->name, "mult")) r = i * j; + if(!strcmp(method->name, "div")) r = i / j; + + jsonObject* resp = jsonNewNumberObject(r); + osrfAppRequestRespond( session, request, resp ); + jsonObjectFree(resp); + + free(a); free(b); + return 0; + } + } + + return -1; +} + + + diff --git a/OpenSRF/src/c-apps/osrf_math.c b/OpenSRF/src/c-apps/osrf_math.c index 0d548ba507..844a7c5b55 100644 --- a/OpenSRF/src/c-apps/osrf_math.c +++ b/OpenSRF/src/c-apps/osrf_math.c @@ -21,16 +21,6 @@ int childInit() { int osrfMathRun( osrfMethodDispatcher* d ) { - /* - OSRF_METHOD_VERIFY_DISPATCHER(d) - Verifies viability of the dispatcher components. - Checks for NULLness of key components. - Creates local variables : - session - the app session ( osrfAppSession* ) - method - the method ( osrfMethod* ) - params - the methd parameters ( jsonObject* ) - request - the request id ( int ) */ - OSRF_METHOD_VERIFY_DISPATCHER(d); jsonObject* x = jsonObjectGetIndex(params, 0); @@ -43,21 +33,21 @@ int osrfMathRun( osrfMethodDispatcher* d ) { if( a && b ) { - double i = strtod(a, NULL); - double j = strtod(b, NULL); - double r = 0; + jsonObject* new_params = jsonParseString("[]"); + jsonObjectPush(new_params, jsonNewObject(a)); + jsonObjectPush(new_params, jsonNewObject(b)); - if(!strcmp(method->name, "add")) r = i + j; - if(!strcmp(method->name, "sub")) r = i - j; - if(!strcmp(method->name, "mult")) r = i * j; - if(!strcmp(method->name, "div")) r = i / j; + free(a); free(b); - jsonObject* resp = jsonNewNumberObject(r); - osrfAppRequestRespond( session, request, resp ); - jsonObjectFree(resp); + osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath"); + int req_id = osrfAppSessionMakeRequest( ses, new_params, method->name, 1, NULL ); + osrf_message* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 ); - free(a); free(b); - return 0; + if(omsg) { + osrfAppRequestRespond( session, request, omsg->_result_content ); + osrf_message_free(omsg); + return 0; + } } } diff --git a/OpenSRF/src/libstack/osrf_app_session.c b/OpenSRF/src/libstack/osrf_app_session.c index 60b767f73a..35036ce6c4 100644 --- a/OpenSRF/src/libstack/osrf_app_session.c +++ b/OpenSRF/src/libstack/osrf_app_session.c @@ -235,6 +235,10 @@ void _osrf_app_session_remove_session( char* session_id ) { /** Allocates a initializes a new app_session */ +osrf_app_session* osrfAppSessionClientInit( char* remote_service ) { + return osrf_app_client_session_init( remote_service ); +} + osrf_app_session* osrf_app_client_session_init( char* remote_service ) { osrf_app_session* session = safe_malloc(sizeof(osrf_app_session)); @@ -343,6 +347,14 @@ void _osrf_app_session_free( osrf_app_session* session ){ free(session); } +int osrfAppSessionMakeRequest( + osrf_app_session* session, jsonObject* params, + char* method_name, int protocol, string_array* param_strings ) { + + return osrf_app_session_make_req( session, params, + method_name, protocol, param_strings ); +} + int osrf_app_session_make_req( osrf_app_session* session, jsonObject* params, char* method_name, int protocol, string_array* param_strings ) { @@ -673,6 +685,10 @@ void osrf_app_session_destroy ( osrf_app_session* session ){ _osrf_app_session_free( session ); } +osrf_message* osrfAppSessionRequestRecv( + osrf_app_session* session, int req_id, int timeout ) { + return osrf_app_session_request_recv( session, req_id, timeout ); +} osrf_message* osrf_app_session_request_recv( osrf_app_session* session, int req_id, int timeout ) { if(req_id < 0 || session == NULL) diff --git a/OpenSRF/src/libstack/osrf_app_session.h b/OpenSRF/src/libstack/osrf_app_session.h index 3b57880f0d..8eb4f85432 100644 --- a/OpenSRF/src/libstack/osrf_app_session.h +++ b/OpenSRF/src/libstack/osrf_app_session.h @@ -89,6 +89,7 @@ typedef struct osrf_app_session_struct osrfAppSession; // -------------------------------------------------------------------------- /** Allocates a initializes a new app_session */ +osrf_app_session* osrfAppSessionClientInit( char* remote_service ); osrf_app_session* osrf_app_client_session_init( char* remote_service ); /** Allocates and initializes a new server session. The global session cache @@ -104,6 +105,10 @@ osrf_app_session* osrf_app_session_find_session( char* session_id ); * the id of the request. This id is then used to perform work on the * requeset. */ +int osrfAppSessionMakeRequest( + osrf_app_session* session, jsonObject* params, + char* method_name, int protocol, string_array* param_strings); + int osrf_app_session_make_req( osrf_app_session* session, jsonObject* params, char* method_name, int protocol, string_array* param_strings); @@ -115,6 +120,8 @@ void osrf_app_session_set_complete( osrf_app_session* session, int request_id ); int osrf_app_session_request_complete( osrf_app_session* session, int request_id ); /** Does a recv call on the given request */ +osrf_message* osrfAppSessionRequestRecv( + osrf_app_session* session, int request_id, int timeout ); osrf_message* osrf_app_session_request_recv( osrf_app_session* session, int request_id, int timeout ); -- 2.43.2