]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/c-apps/osrf_math.c
9cb22aa640dc6a5dc27a934eef749fa9f980386c
[Evergreen.git] / OpenSRF / src / c-apps / osrf_math.c
1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4 #include "opensrf/osrf_log.h"
5
6 int osrfAppInitialize();
7 int osrfAppChildInit();
8 int osrfMathRun( osrfMethodContext* );
9
10
11 int osrfAppInitialize() {
12         osrfLogInit("opensrf.math");
13         /* tell the server about the methods we handle */
14         osrfAppRegisterMethod( "opensrf.math", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
15         osrfAppRegisterMethod( "opensrf.math", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
16         osrfAppRegisterMethod( "opensrf.math", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
17         osrfAppRegisterMethod( "opensrf.math", "div", "osrfMathRun", "send 2 numbers and I'll subtract them", 2 );
18         return 0;
19 }
20
21 int osrfAppChildInit() {
22         return 0;
23 }
24
25 int osrfMathRun( osrfMethodContext* ctx ) {
26
27         OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
28
29         osrfLog( OSRF_DEBUG, "Running opensrf.math %s", ctx->method->name );
30
31         /* collect the request params */
32         jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
33         jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
34
35         if( x && y ) {
36
37                 /* pull out the params as strings since they may be either
38                         strings or numbers depending on the client */
39                 char* a = jsonObjectToSimpleString(x);
40                 char* b = jsonObjectToSimpleString(y);
41
42                 if( a && b ) {
43
44                         /* construct a new params object to send to dbmath */
45                         jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
46                         free(a); free(b);
47
48                         /* connect to db math */
49                         osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
50
51                         /* dbmath uses the same method names that math does */
52                         int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
53                         osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
54
55                         if(omsg) {
56                                 /* return dbmath's response to the user */
57                                 osrfAppRequestRespondComplete( ctx->session, ctx->request, osrfMessageGetResult(omsg) ); 
58                                 osrfMessageFree(omsg);
59                                 osrfAppSessionFree(ses);
60                                 return 0;
61                         }
62
63                         osrfAppSessionFree(ses);
64                 }
65         }
66
67         return -1;
68 }
69
70
71