]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/c-apps/osrf_math.c
f30780485fc89a901d130403c36375a3c8029a7d
[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/log.h"
5
6 #define MODULENAME "opensrf.math"
7
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 int osrfMathRun( osrfMethodContext* );
11
12
13 int osrfAppInitialize() {
14
15         osrfAppRegisterMethod( 
16                         MODULENAME,                             /* which application has this method */
17                         "add",                                  /* the name of the method */
18                         "osrfMathRun",                  /* the symbol that runs the method */
19                         "Adds two numbers",     /* description of the method */
20                         2,                                                      /* the minimum number of params required to run the method */
21                         0 );                                            /* method options, 0 for not special options */
22
23         osrfAppRegisterMethod( 
24                         MODULENAME, 
25                         "sub", 
26                         "osrfMathRun", 
27                         "Subtracts two numbers", 2, 0 );
28
29         osrfAppRegisterMethod( 
30                         MODULENAME, 
31                         "mult", 
32                         "osrfMathRun", 
33                         "Multiplies two numbers", 2, 0 );
34
35         osrfAppRegisterMethod( 
36                         MODULENAME, 
37                         "div", 
38                         "osrfMathRun", 
39                         "Divides two numbers", 2, 0 );
40
41         return 0;
42 }
43
44 int osrfAppChildInit() {
45         return 0;
46 }
47
48 int osrfMathRun( osrfMethodContext* ctx ) {
49
50         OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
51
52         /* collect the request params */
53         jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
54         jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
55
56         if( x && y ) {
57
58                 /* pull out the params as strings since they may be either
59                         strings or numbers depending on the client */
60                 char* a = jsonObjectToSimpleString(x);
61                 char* b = jsonObjectToSimpleString(y);
62
63                 if( a && b ) {
64
65                         osrfLogActivity( OSRF_LOG_MARK, "Running opensrf.math %s [ %s : %s ]", 
66                                         ctx->method->name, a, b );
67
68                         /* construct a new params object to send to dbmath */
69                         jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
70                         free(a); free(b);
71
72                         /* connect to db math */
73                         osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
74
75                         /* dbmath uses the same method names that math does */
76                         int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
77                         osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
78
79                         if(omsg) {
80                                 /* return dbmath's response to the user */
81                                 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) ); 
82                                 osrfMessageFree(omsg);
83                                 osrfAppSessionFree(ses);
84                                 return 0;
85                         }
86
87                         osrfAppSessionFree(ses);
88                 }
89         }
90
91         return -1;
92 }
93
94
95