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
--- /dev/null
+#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;
+}
+
+
+
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);
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;
+ }
}
}
/** 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));
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 ) {
_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)
// --------------------------------------------------------------------------
/** 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
* 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);
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 );