1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4 #include "opensrf/utils.h"
5 #include "opensrf/osrf_log.h"
7 #define OSRF_VERSION_CACHE_TIME 300
9 int osrfAppInitialize();
10 int osrfAppChildInit();
11 int osrfVersion( osrfMethodContext* );
14 int osrfAppInitialize() {
15 osrfLogInit("opensrf.version");
16 osrfAppRegisterMethod(
18 "opensrf.version.verify",
20 "Send the service, method, and params as paramters to this method "
21 "The data for this service/method/params combination will be retrieved "
22 "from the necessary server and the MD5 sum of the total values received "
23 "will be returned", 2 );
28 int osrfAppChildInit() {
32 int osrfVersion( osrfMethodContext* ctx ) {
34 OSRF_METHOD_VERIFY_CONTEXT(ctx);
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);
43 osrfLog( OSRF_DEBUG, "Found %s object in cache, returning....", cachedmd5 );
44 jsonObject* resp = jsonNewObject(cachedmd5);
45 osrfAppRequestRespondComplete( ctx->session, ctx->request, resp );
52 jsonObject* serv = jsonObjectGetIndex(ctx->params, 0);
53 jsonObject* meth = jsonObjectGetIndex(ctx->params, 1);
54 char* service = jsonObjectGetString(serv);
55 char* methd = jsonObjectGetString(meth);
57 if( service && methd ) {
58 /* shove the additional params into an array */
59 jsonObject* tmpArray = jsonNewObject(NULL);
61 for( i = 2; i != ctx->params->size; i++ )
62 jsonObjectPush( tmpArray, jsonObjectClone(jsonObjectGetIndex(ctx->params, i)));
64 osrfAppSession* ses = osrfAppSessionClientInit(service);
65 int reqid = osrfAppSessionMakeRequest( ses, tmpArray, methd, 1, NULL );
66 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, reqid, 60 );
67 jsonObjectFree(tmpArray);
71 jsonObject* result = osrfMessageGetResult( omsg );
72 char* resultjson = jsonObjectToJSON(result);
73 char* resultmd5 = md5sum(resultjson);
75 osrfMessageFree(omsg);
78 jsonObject* resp = jsonNewObject(resultmd5);
79 osrfAppRequestRespondComplete( ctx->session, ctx->request, resp );
81 osrfAppSessionFree(ses);
82 osrfLog(OSRF_DEBUG, "Found version string %s, caching and returning...", resultmd5 );
83 osrfCachePutString( paramsmd5, resultmd5, OSRF_VERSION_CACHE_TIME );
89 osrfAppSessionFree(ses);