providing option to connect to memcache at general connect time
[OpenSRF.git] / src / c-apps / osrf_version.c
1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "opensrf/osrf_json.h"
4 #include "opensrf/utils.h"
5 #include "opensrf/log.h"
6
7 #define OSRF_VERSION_CACHE_TIME 300
8
9 int osrfAppInitialize();
10 int osrfAppChildInit();
11 int osrfVersion( osrfMethodContext* );
12
13
14 int osrfAppInitialize() {
15
16         osrfAppRegisterMethod( 
17                         "opensrf.version", 
18                         "opensrf.version.verify", 
19                         "osrfVersion", 
20                         "The data for a service/method/params combination will be retrieved "
21                         "from the necessary server and the MD5 sum of the total values received "
22                         "will be returned. PARAMS( serviceName, methodName, [param1, ...] )", 
23                         2, 0 );
24         
25         return 0;
26 }
27
28 int osrfAppChildInit() {
29         return 0;
30 }
31
32 int osrfVersion( osrfMethodContext* ctx ) {
33
34         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
35
36         /* First, see if the data is in the cache */
37         char* json = jsonObjectToJSON(ctx->params);
38         char* paramsmd5 = md5sum(json);
39         char* cachedmd5 = osrfCacheGetString(paramsmd5);
40         free(json); 
41
42         if( cachedmd5 ) {
43                 osrfLogDebug(OSRF_LOG_MARK,  "Found %s object in cache, returning....", cachedmd5 );
44                 jsonObject* resp = jsonNewObject(cachedmd5);
45                 osrfAppRespondComplete( ctx, resp  );
46                 jsonObjectFree(resp);
47                 free(paramsmd5);
48                 free(cachedmd5);
49                 return 0;
50         }
51
52         const jsonObject* serv = jsonObjectGetIndex(ctx->params, 0);
53         const jsonObject* meth = jsonObjectGetIndex(ctx->params, 1);
54         const char* service = jsonObjectGetString(serv);
55         const char* methd = jsonObjectGetString(meth);
56
57         if( service && methd ) {
58                 /* shove the additional params into an array */
59                 jsonObject* tmpArray = jsonNewObject(NULL);
60                 int i;
61                 for( i = 2; i != ctx->params->size; i++ ) 
62                         jsonObjectPush( tmpArray, jsonObjectClone(jsonObjectGetIndex(ctx->params, i)));
63
64                 osrfAppSession* ses = osrfAppSessionClientInit(service);
65                 int reqid = osrfAppSessionMakeRequest( ses, tmpArray, methd, 1, NULL );
66                 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, reqid, 60 );
67                 jsonObjectFree(tmpArray);
68
69                 if( omsg ) {
70
71                         jsonObject* result = osrfMessageGetResult( omsg );
72                         char* resultjson = jsonObjectToJSON(result);
73                         char* resultmd5 = md5sum(resultjson);
74                         free(resultjson);
75                         osrfMessageFree(omsg);
76
77                         if( resultmd5 ) {
78                                 jsonObject* resp = jsonNewObject(resultmd5);
79                                 osrfAppRespondComplete( ctx, resp );
80                                 jsonObjectFree(resp);
81                                 osrfAppSessionFree(ses);
82                                 osrfLogDebug(OSRF_LOG_MARK, "Found version string %s, caching and returning...", resultmd5 );
83                                 osrfCachePutString( paramsmd5, resultmd5, OSRF_VERSION_CACHE_TIME );
84                                 free(resultmd5);
85                                 free(paramsmd5);
86                                 return 0;
87                         } 
88                 }
89                 osrfAppSessionFree(ses);
90         }
91
92         free(paramsmd5);
93
94         return -1;
95 }
96
97
98