added dbmath
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 2 Sep 2005 20:38:27 +0000 (20:38 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 2 Sep 2005 20:38:27 +0000 (20:38 +0000)
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
OpenSRF/src/c-apps/osrf_dbmath.c [new file with mode: 0644]
OpenSRF/src/c-apps/osrf_math.c
OpenSRF/src/libstack/osrf_app_session.c
OpenSRF/src/libstack/osrf_app_session.h

index 592b0ca..62301e7 100644 (file)
@@ -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 (file)
index 0000000..84feba2
--- /dev/null
@@ -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;
+}
+
+
+
index 0d548ba..844a7c5 100644 (file)
@@ -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;
+                       }
                }
        }
 
index 60b767f..35036ce 100644 (file)
@@ -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)
index 3b57880..8eb4f85 100644 (file)
@@ -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 );