math server (really dbmath code)
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 2 Sep 2005 19:39:22 +0000 (19:39 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 2 Sep 2005 19:39:22 +0000 (19:39 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@519 9efc2488-bf62-4759-914b-345cdb29e865

src/c-apps/Makefile [new file with mode: 0644]
src/c-apps/osrf_math.c [new file with mode: 0644]

diff --git a/src/c-apps/Makefile b/src/c-apps/Makefile
new file mode 100644 (file)
index 0000000..592b0ca
--- /dev/null
@@ -0,0 +1,15 @@
+LDLIBS += -lobjson -lopensrf
+
+
+all:   osrf_math.so
+
+osrf_math.o: osrf_math.c
+
+osrf_math.so: osrf_math.o
+       $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) osrf_math.o -o $(TMPDIR)/osrf_math.so
+
+install:
+       cp $(TMPDIR)/osrf_math.so $(LIBDIR)/
+
+clean:
+       rm -f *.o *.so
diff --git a/src/c-apps/osrf_math.c b/src/c-apps/osrf_math.c
new file mode 100644 (file)
index 0000000..0d548ba
--- /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.math", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
+       osrfAppRegisterMethod( "opensrf.math", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
+       osrfAppRegisterMethod( "opensrf.math", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
+       osrfAppRegisterMethod( "opensrf.math", "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;
+}
+
+
+