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