1 #include <opensrf/osrf_application.h>
3 static osrfMethod* _osrfAppBuildMethod( const char* methodName, const char* symbolName,
4 const char* notes, int argc, int options, void* );
5 static void osrfAppSetOnExit(osrfApplication* app, const char* appName);
6 static int _osrfAppRegisterSysMethods( const char* app );
7 static osrfApplication* _osrfAppFindApplication( const char* name );
8 static osrfMethod* osrfAppFindMethod( osrfApplication* app, const char* methodName );
9 static int _osrfAppRespond( osrfMethodContext* context, const jsonObject* data, int complete );
10 static int _osrfAppPostProcess( osrfMethodContext* context, int retcode );
11 static int _osrfAppRunSystemMethod(osrfMethodContext* context);
12 static int osrfAppIntrospect( osrfMethodContext* ctx );
13 static int osrfAppIntrospectAll( osrfMethodContext* ctx );
14 static int osrfAppEcho( osrfMethodContext* ctx );
16 static osrfHash* _osrfAppHash = NULL;
18 int osrfAppRegisterApplication( const char* appName, const char* soFile ) {
19 if(!appName || ! soFile) return -1;
22 if(!_osrfAppHash) _osrfAppHash = osrfNewHash();
24 osrfLogInfo( OSRF_LOG_MARK, "Registering application %s with file %s", appName, soFile );
26 osrfApplication* app = safe_malloc(sizeof(osrfApplication));
27 app->handle = dlopen (soFile, RTLD_NOW);
31 osrfLogWarning( OSRF_LOG_MARK, "Failed to dlopen library file %s: %s", soFile, dlerror() );
32 dlerror(); /* clear the error */
37 app->methods = osrfNewHash();
38 osrfHashSet( _osrfAppHash, app, appName );
40 /* see if we can run the initialize method */
42 *(void **) (&init) = dlsym(app->handle, "osrfAppInitialize");
44 if( (error = dlerror()) != NULL ) {
45 osrfLogWarning( OSRF_LOG_MARK,
46 "! Unable to locate method symbol [osrfAppInitialize] for app %s: %s", appName, error );
52 if( (ret = (*init)()) ) {
53 osrfLogWarning( OSRF_LOG_MARK, "Application %s returned non-zero value from "
54 "'osrfAppInitialize', not registering...", appName );
55 //free(app->name); /* need a method to remove an application from the list */
61 _osrfAppRegisterSysMethods(appName);
63 osrfLogInfo( OSRF_LOG_MARK, "Application %s registered successfully", appName );
65 osrfLogSetAppname(appName);
67 osrfAppSetOnExit(app, appName);
73 static void osrfAppSetOnExit(osrfApplication* app, const char* appName) {
74 if(!(app && appName)) return;
76 /* see if we can run the initialize method */
78 void (*onExit) (void);
79 *(void **) (&onExit) = dlsym(app->handle, "osrfAppChildExit");
81 if( (error = dlerror()) != NULL ) {
82 osrfLogDebug(OSRF_LOG_MARK, "No exit handler defined for %s", appName);
86 osrfLogInfo(OSRF_LOG_MARK, "registering exit handler for %s", appName);
87 app->onExit = (*onExit);
91 int osrfAppRunChildInit(const char* appname) {
92 osrfApplication* app = _osrfAppFindApplication(appname);
97 int (*childInit) (void);
99 *(void**) (&childInit) = dlsym(app->handle, "osrfAppChildInit");
101 if( (error = dlerror()) != NULL ) {
102 osrfLogInfo( OSRF_LOG_MARK, "No child init defined for app %s : %s", appname, error);
106 if( (ret = (*childInit)()) ) {
107 osrfLogError(OSRF_LOG_MARK, "App %s child init failed", appname);
111 osrfLogInfo(OSRF_LOG_MARK, "%s child init succeeded", appname);
116 void osrfAppRunExitCode() {
117 osrfHashIterator* itr = osrfNewHashIterator(_osrfAppHash);
118 osrfApplication* app;
119 while( (app = osrfHashIteratorNext(itr)) ) {
121 osrfLogInfo(OSRF_LOG_MARK, "Running onExit handler for app %s", itr->current);
125 osrfHashIteratorFree(itr);
129 int osrfAppRegisterMethod( const char* appName, const char* methodName,
130 const char* symbolName, const char* notes, int argc, int options ) {
132 return osrfAppRegisterExtendedMethod(
144 int osrfAppRegisterExtendedMethod( const char* appName, const char* methodName,
145 const char* symbolName, const char* notes, int argc, int options, void * user_data ) {
147 if( !appName || ! methodName ) return -1;
149 osrfApplication* app = _osrfAppFindApplication(appName);
151 osrfLogWarning( OSRF_LOG_MARK, "Unable to locate application %s", appName );
155 osrfLogDebug( OSRF_LOG_MARK, "Registering method %s for app %s", methodName, appName );
157 osrfMethod* method = _osrfAppBuildMethod(
158 methodName, symbolName, notes, argc, options, user_data );
159 method->options = options;
161 /* plug the method into the list of methods */
162 osrfHashSet( app->methods, method, method->name );
164 if( options & OSRF_METHOD_STREAMING ) { /* build the atomic counterpart */
165 int newops = options | OSRF_METHOD_ATOMIC;
166 osrfMethod* atomicMethod = _osrfAppBuildMethod(
167 methodName, symbolName, notes, argc, newops, NULL );
168 osrfHashSet( app->methods, atomicMethod, atomicMethod->name );
169 atomicMethod->userData = method->userData;
177 static osrfMethod* _osrfAppBuildMethod( const char* methodName, const char* symbolName,
178 const char* notes, int argc, int options, void* user_data ) {
180 osrfMethod* method = safe_malloc(sizeof(osrfMethod));
182 if(methodName) method->name = strdup(methodName);
183 else method->name = NULL;
184 if(symbolName) method->symbol = strdup(symbolName);
185 else method->symbol = NULL;
186 if(notes) method->notes = strdup(notes);
187 else method->notes = NULL;
188 if(user_data) method->userData = user_data;
191 method->options = options;
193 if(options & OSRF_METHOD_ATOMIC) { /* add ".atomic" to the end of the name */
194 char mb[strlen(method->name) + 8];
195 sprintf(mb, "%s.atomic", method->name);
197 method->name = strdup(mb);
198 method->options |= OSRF_METHOD_STREAMING;
206 Registers all of the system methods for this app so that they may be
207 treated the same as other methods */
208 static int _osrfAppRegisterSysMethods( const char* app ) {
210 osrfAppRegisterMethod(
211 app, OSRF_SYSMETHOD_INTROSPECT, NULL,
212 "Return a list of methods whose names have the same initial "
213 "substring as that of the provided method name PARAMS( methodNameSubstring )",
214 1, OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
216 osrfAppRegisterMethod(
217 app, OSRF_SYSMETHOD_INTROSPECT_ALL, NULL,
218 "Returns a complete list of methods. PARAMS()", 0,
219 OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
221 osrfAppRegisterMethod(
222 app, OSRF_SYSMETHOD_ECHO, NULL,
223 "Echos all data sent to the server back to the client. PARAMS([a, b, ...])", 0,
224 OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
230 Finds the given app in the list of apps
231 @param name The name of the application
232 @return The application pointer or NULL if there is no such application
234 static osrfApplication* _osrfAppFindApplication( const char* name ) {
235 if(!name) return NULL;
236 return (osrfApplication*) osrfHashGet(_osrfAppHash, name);
240 Finds the given method for the given app
241 @param app The application object
242 @param methodName The method to find
243 @return A method pointer or NULL if no such method
244 exists for the given application
246 static osrfMethod* osrfAppFindMethod( osrfApplication* app, const char* methodName ) {
247 if(!app || ! methodName) return NULL;
248 return (osrfMethod*) osrfHashGet( app->methods, methodName );
251 osrfMethod* _osrfAppFindMethod( const char* appName, const char* methodName ) {
252 if(!appName || ! methodName) return NULL;
253 return osrfAppFindMethod( _osrfAppFindApplication(appName), methodName );
257 int osrfAppRunMethod( const char* appName, const char* methodName,
258 osrfAppSession* ses, int reqId, jsonObject* params ) {
260 if( !(appName && methodName && ses) ) return -1;
263 osrfApplication* app;
265 osrfMethodContext context;
267 context.session = ses;
268 context.params = params;
269 context.request = reqId;
270 context.responses = NULL;
272 /* this is the method we're gonna run */
273 int (*meth) (osrfMethodContext*);
275 if( !(app = _osrfAppFindApplication(appName)) )
276 return osrfAppRequestRespondException( ses,
277 reqId, "Application not found: %s", appName );
279 if( !(method = osrfAppFindMethod( app, methodName )) )
280 return osrfAppRequestRespondException( ses, reqId,
281 "Method [%s] not found for service %s", methodName, appName );
283 context.method = method;
285 #ifdef OSRF_STRICT_PARAMS
286 if( method->argc > 0 ) {
287 if(!params || params->type != JSON_ARRAY || params->size < method->argc )
288 return osrfAppRequestRespondException( ses, reqId,
289 "Not enough params for method %s / service %s", methodName, appName );
295 if( method->options & OSRF_METHOD_SYSTEM ) {
296 retcode = _osrfAppRunSystemMethod(&context);
300 /* open and now run the method */
301 *(void **) (&meth) = dlsym(app->handle, method->symbol);
303 if( (error = dlerror()) != NULL ) {
304 return osrfAppRequestRespondException( ses, reqId,
305 "Unable to execute method [%s] for service %s", methodName, appName );
308 retcode = (*meth) (&context);
312 return osrfAppRequestRespondException(
313 ses, reqId, "An unknown server error occurred" );
315 return _osrfAppPostProcess( &context, retcode );
320 int osrfAppRespond( osrfMethodContext* ctx, const jsonObject* data ) {
321 return _osrfAppRespond( ctx, data, 0 );
324 int osrfAppRespondComplete( osrfMethodContext* context, const jsonObject* data ) {
325 return _osrfAppRespond( context, data, 1 );
328 static int _osrfAppRespond( osrfMethodContext* ctx, const jsonObject* data, int complete ) {
329 if(!(ctx && ctx->method)) return -1;
331 if( ctx->method->options & OSRF_METHOD_ATOMIC ) {
332 osrfLogDebug( OSRF_LOG_MARK,
333 "Adding responses to stash for atomic method %s", ctx->method->name );
335 if( ctx->responses == NULL )
336 ctx->responses = jsonParseString("[]");
339 jsonObjectPush( ctx->responses, jsonObjectClone(data) );
343 if( !(ctx->method->options & OSRF_METHOD_ATOMIC) &&
344 !(ctx->method->options & OSRF_METHOD_CACHABLE) ) {
347 osrfAppRequestRespondComplete( ctx->session, ctx->request, data );
349 osrfAppRequestRespond( ctx->session, ctx->request, data );
359 static int _osrfAppPostProcess( osrfMethodContext* ctx, int retcode ) {
360 if(!(ctx && ctx->method)) return -1;
362 osrfLogDebug( OSRF_LOG_MARK, "Postprocessing method %s with retcode %d",
363 ctx->method->name, retcode );
365 if(ctx->responses) { /* we have cached responses to return (no responses have been sent) */
367 osrfAppRequestRespondComplete( ctx->session, ctx->request, ctx->responses );
368 jsonObjectFree(ctx->responses);
369 ctx->responses = NULL;
374 osrfAppSessionStatus( ctx->session, OSRF_STATUS_COMPLETE,
375 "osrfConnectStatus", ctx->request, "Request Complete" );
381 int osrfAppRequestRespondException( osrfAppSession* ses, int request, const char* msg, ... ) {
384 VA_LIST_TO_STRING(msg);
385 osrfLogWarning( OSRF_LOG_MARK, "Returning method exception with message: %s", VA_BUF );
386 osrfAppSessionStatus( ses, OSRF_STATUS_NOTFOUND, "osrfMethodException", request, VA_BUF );
391 static void _osrfAppSetIntrospectMethod( osrfMethodContext* ctx, const osrfMethod* method, jsonObject* resp ) {
392 if(!(ctx && resp)) return;
394 jsonObjectSetKey(resp, "api_name", jsonNewObject(method->name));
395 jsonObjectSetKey(resp, "method", jsonNewObject(method->symbol));
396 jsonObjectSetKey(resp, "service", jsonNewObject(ctx->session->remote_service));
397 jsonObjectSetKey(resp, "notes", jsonNewObject(method->notes));
398 jsonObjectSetKey(resp, "argc", jsonNewNumberObject(method->argc));
400 jsonObjectSetKey(resp, "sysmethod",
401 jsonNewNumberObject( (method->options & OSRF_METHOD_SYSTEM) ? 1 : 0 ));
402 jsonObjectSetKey(resp, "atomic",
403 jsonNewNumberObject( (method->options & OSRF_METHOD_ATOMIC) ? 1 : 0 ));
404 jsonObjectSetKey(resp, "cachable",
405 jsonNewNumberObject( (method->options & OSRF_METHOD_CACHABLE) ? 1 : 0 ));
407 jsonObjectSetClass(resp, "method");
411 Trys to run the requested method as a system method.
412 A system method is a well known method that all
414 @param context The current method context
415 @return 0 if the method is run successfully, return < 0 means
416 the method was not run, return > 0 means the method was run
417 and the application code now needs to send a 'request complete'
420 static int _osrfAppRunSystemMethod(osrfMethodContext* ctx) {
421 OSRF_METHOD_VERIFY_CONTEXT(ctx);
423 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL ) ||
424 !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL_ATOMIC )) {
426 return osrfAppIntrospectAll(ctx);
430 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT ) ||
431 !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ATOMIC )) {
433 return osrfAppIntrospect(ctx);
436 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_ECHO ) ||
437 !strcmp(ctx->method->name, OSRF_SYSMETHOD_ECHO_ATOMIC )) {
439 return osrfAppEcho(ctx);
443 osrfAppRequestRespondException( ctx->session,
444 ctx->request, "System method implementation not found");
450 static int osrfAppIntrospect( osrfMethodContext* ctx ) {
452 jsonObject* resp = NULL;
453 char* methodSubstring = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
454 osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
457 if(!methodSubstring) return 1; /* respond with no methods */
461 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
464 while( (method = osrfHashIteratorNext(itr)) ) {
465 if( (len = strlen(methodSubstring)) <= strlen(method->name) ) {
466 if( !strncmp( method->name, methodSubstring, len) ) {
467 resp = jsonNewObject(NULL);
468 _osrfAppSetIntrospectMethod( ctx, method, resp );
469 osrfAppRespond(ctx, resp);
470 jsonObjectFree(resp);
474 osrfHashIteratorFree(itr);
483 static int osrfAppIntrospectAll( osrfMethodContext* ctx ) {
484 jsonObject* resp = NULL;
485 osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
488 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
490 while( (method = osrfHashIteratorNext(itr)) ) {
491 resp = jsonNewObject(NULL);
492 _osrfAppSetIntrospectMethod( ctx, method, resp );
493 osrfAppRespond(ctx, resp);
494 jsonObjectFree(resp);
496 osrfHashIteratorFree(itr);
503 static int osrfAppEcho( osrfMethodContext* ctx ) {
504 OSRF_METHOD_VERIFY_CONTEXT(ctx);
506 for( i = 0; i < ctx->params->size; i++ ) {
507 const jsonObject* str = jsonObjectGetIndex(ctx->params,i);
508 osrfAppRespond(ctx, str);