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