1 #include "osrf_application.h"
2 #include "objson/object.h"
4 //osrfApplication* __osrfAppList = NULL;
6 osrfHash* __osrfAppHash = NULL;
9 int osrfAppRegisterApplication( char* appName, char* soFile ) {
10 if(!appName || ! soFile) return -1;
13 if(!__osrfAppHash) __osrfAppHash = osrfNewHash();
15 osrfLogInfo( OSRF_LOG_MARK, "Registering application %s with file %s", appName, soFile );
17 osrfApplication* app = safe_malloc(sizeof(osrfApplication));
18 app->handle = dlopen (soFile, RTLD_NOW);
22 osrfLogWarning( OSRF_LOG_MARK, "Failed to dlopen library file %s: %s", soFile, dlerror() );
23 dlerror(); /* clear the error */
28 app->methods = osrfNewHash();
29 osrfHashSet( __osrfAppHash, app, appName );
31 /* see if we can run the initialize method */
33 *(void **) (&init) = dlsym(app->handle, "osrfAppInitialize");
35 if( (error = dlerror()) != NULL ) {
36 osrfLogWarning( OSRF_LOG_MARK,
37 "! Unable to locate method symbol [osrfAppInitialize] for app %s: %s", appName, error );
43 if( (ret = (*init)()) ) {
44 osrfLogWarning( OSRF_LOG_MARK, "Application %s returned non-zero value from "
45 "'osrfAppInitialize', not registering...", appName );
46 //free(app->name); /* need a method to remove an application from the list */
52 __osrfAppRegisterSysMethods(appName);
54 osrfLogInfo( OSRF_LOG_MARK, "Application %s registered successfully", appName );
56 osrfLogSetAppname(appName);
58 osrfAppSetOnExit(app, appName);
64 void osrfAppSetOnExit(osrfApplication* app, char* appName) {
65 if(!(app && appName)) return;
67 /* see if we can run the initialize method */
69 void (*onExit) (void);
70 *(void **) (&onExit) = dlsym(app->handle, "osrfAppChildExit");
72 if( (error = dlerror()) != NULL ) {
73 osrfLogDebug(OSRF_LOG_MARK, "No exit handler defined for %s", appName);
77 osrfLogInfo(OSRF_LOG_MARK, "registering exit handler for %s", appName);
78 app->onExit = (*onExit);
79 //if( (ret = (*onExit)()) ) {
83 int osrfAppRunChildInit(char* appname) {
84 osrfApplication* app = _osrfAppFindApplication(appname);
89 int (*childInit) (void);
91 *(void**) (&childInit) = dlsym(app->handle, "osrfAppChildInit");
93 if( (error = dlerror()) != NULL ) {
94 osrfLogInfo( OSRF_LOG_MARK, "No child init defined for app %s : %s", appname, error);
98 if( (ret = (*childInit)()) ) {
99 osrfLogError(OSRF_LOG_MARK, "App %s child init failed", appname);
103 osrfLogInfo(OSRF_LOG_MARK, "%s child init succeeded", appname);
108 void osrfAppRunExitCode() {
109 osrfHashIterator* itr = osrfNewHashIterator(__osrfAppHash);
110 osrfApplication* app;
111 while( (app = osrfHashIteratorNext(itr)) ) {
113 osrfLogInfo(OSRF_LOG_MARK, "Running onExit handler for app %s", itr->current);
120 int osrfAppRegisterMethod( char* appName, char* methodName,
121 char* symbolName, char* notes, int argc, int options ) {
123 return osrfAppRegisterExtendedMethod(
135 int osrfAppRegisterExtendedMethod( char* appName, char* methodName,
136 char* symbolName, char* notes, int argc, int options, void * user_data ) {
138 if( !appName || ! methodName ) return -1;
140 osrfApplication* app = _osrfAppFindApplication(appName);
142 osrfLogWarning( OSRF_LOG_MARK, "Unable to locate application %s", appName );
146 osrfLogDebug( OSRF_LOG_MARK, "Registering method %s for app %s", methodName, appName );
148 osrfMethod* method = _osrfAppBuildMethod(
149 methodName, symbolName, notes, argc, options, user_data );
150 method->options = options;
152 /* plug the method into the list of methods */
153 osrfHashSet( app->methods, method, method->name );
155 if( options & OSRF_METHOD_STREAMING ) { /* build the atomic counterpart */
156 int newops = options | OSRF_METHOD_ATOMIC;
157 osrfMethod* atomicMethod = _osrfAppBuildMethod(
158 methodName, symbolName, notes, argc, newops, NULL );
159 osrfHashSet( app->methods, atomicMethod, atomicMethod->name );
160 atomicMethod->userData = method->userData;
168 osrfMethod* _osrfAppBuildMethod( char* methodName,
169 char* symbolName, char* notes, int argc, int options, void* user_data ) {
171 osrfMethod* method = safe_malloc(sizeof(osrfMethod));
173 if(methodName) method->name = strdup(methodName);
174 if(symbolName) method->symbol = strdup(symbolName);
175 if(notes) method->notes = strdup(notes);
176 if(user_data) method->userData = user_data;
179 method->options = options;
181 if(options & OSRF_METHOD_ATOMIC) { /* add ".atomic" to the end of the name */
182 char mb[strlen(method->name) + 8];
183 sprintf(mb, "%s.atomic", method->name);
185 method->name = strdup(mb);
186 method->options |= OSRF_METHOD_STREAMING;
193 int __osrfAppRegisterSysMethods( char* app ) {
195 osrfAppRegisterMethod(
196 app, OSRF_SYSMETHOD_INTROSPECT, NULL,
197 "Return a list of methods whose names have the same initial "
198 "substring as that of the provided method name PARAMS( methodNameSubstring )",
199 1, OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
201 osrfAppRegisterMethod(
202 app, OSRF_SYSMETHOD_INTROSPECT_ALL, NULL,
203 "Returns a complete list of methods. PARAMS()", 0,
204 OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
206 osrfAppRegisterMethod(
207 app, OSRF_SYSMETHOD_ECHO, NULL,
208 "Echos all data sent to the server back to the client. PARAMS([a, b, ...])", 0,
209 OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
214 osrfApplication* _osrfAppFindApplication( char* name ) {
215 if(!name) return NULL;
216 return (osrfApplication*) osrfHashGet(__osrfAppHash, name);
219 osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ) {
220 if(!app || ! methodName) return NULL;
221 return (osrfMethod*) osrfHashGet( app->methods, methodName );
224 osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ) {
225 if(!appName || ! methodName) return NULL;
226 return __osrfAppFindMethod( _osrfAppFindApplication(appName), methodName );
230 int osrfAppRunMethod( char* appName, char* methodName,
231 osrfAppSession* ses, int reqId, jsonObject* params ) {
233 if( !(appName && methodName && ses) ) return -1;
236 osrfApplication* app;
238 osrfMethodContext context;
240 context.session = ses;
241 context.params = params;
242 context.request = reqId;
243 context.responses = NULL;
245 /* this is the method we're gonna run */
246 int (*meth) (osrfMethodContext*);
248 if( !(app = _osrfAppFindApplication(appName)) )
249 return osrfAppRequestRespondException( ses,
250 reqId, "Application not found: %s", appName );
252 if( !(method = __osrfAppFindMethod( app, methodName )) )
253 return osrfAppRequestRespondException( ses, reqId,
254 "Method [%s] not found for service %s", methodName, appName );
256 context.method = method;
258 #ifdef OSRF_STRICT_PARAMS
259 if( method->argc > 0 ) {
260 if(!params || params->type != JSON_ARRAY || params->size < method->argc )
261 return osrfAppRequestRespondException( ses, reqId,
262 "Not enough params for method %s / service %s", methodName, appName );
268 if( method->options & OSRF_METHOD_SYSTEM ) {
269 retcode = __osrfAppRunSystemMethod(&context);
273 /* open and now run the method */
274 *(void **) (&meth) = dlsym(app->handle, method->symbol);
276 if( (error = dlerror()) != NULL ) {
277 return osrfAppRequestRespondException( ses, reqId,
278 "Unable to execute method [%s] for service %s", methodName, appName );
281 retcode = (*meth) (&context);
285 return osrfAppRequestRespondException(
286 ses, reqId, "An unknown server error occurred" );
288 return __osrfAppPostProcess( &context, retcode );
293 int osrfAppRespond( osrfMethodContext* ctx, jsonObject* data ) {
294 return _osrfAppRespond( ctx, data, 0 );
297 int osrfAppRespondComplete( osrfMethodContext* context, jsonObject* data ) {
298 return _osrfAppRespond( context, data, 1 );
301 int _osrfAppRespond( osrfMethodContext* ctx, jsonObject* data, int complete ) {
302 if(!(ctx && ctx->method)) return -1;
304 if( ctx->method->options & OSRF_METHOD_ATOMIC ) {
305 osrfLogDebug( OSRF_LOG_MARK,
306 "Adding responses to stash for atomic method %s", ctx->method->name );
308 if( ctx->responses == NULL )
309 ctx->responses = jsonParseString("[]");
312 jsonObjectPush( ctx->responses, jsonObjectClone(data) );
316 if( !(ctx->method->options & OSRF_METHOD_ATOMIC) &&
317 !(ctx->method->options & OSRF_METHOD_CACHABLE) ) {
320 osrfAppRequestRespondComplete( ctx->session, ctx->request, data );
322 osrfAppRequestRespond( ctx->session, ctx->request, data );
332 int __osrfAppPostProcess( osrfMethodContext* ctx, int retcode ) {
333 if(!(ctx && ctx->method)) return -1;
335 osrfLogDebug( OSRF_LOG_MARK, "Postprocessing method %s with retcode %d",
336 ctx->method->name, retcode );
338 if(ctx->responses) { /* we have cached responses to return (no responses have been sent) */
340 osrfAppRequestRespondComplete( ctx->session, ctx->request, ctx->responses );
341 jsonObjectFree(ctx->responses);
342 ctx->responses = NULL;
347 osrfAppSessionStatus( ctx->session, OSRF_STATUS_COMPLETE,
348 "osrfConnectStatus", ctx->request, "Request Complete" );
354 int osrfAppRequestRespondException( osrfAppSession* ses, int request, char* msg, ... ) {
357 VA_LIST_TO_STRING(msg);
358 osrfLogWarning( OSRF_LOG_MARK, "Returning method exception with message: %s", VA_BUF );
359 osrfAppSessionStatus( ses, OSRF_STATUS_NOTFOUND, "osrfMethodException", request, VA_BUF );
364 static void __osrfAppSetIntrospectMethod( osrfMethodContext* ctx, osrfMethod* method, jsonObject* resp ) {
365 if(!(ctx && resp)) return;
367 jsonObjectSetKey(resp, "api_name", jsonNewObject(method->name));
368 jsonObjectSetKey(resp, "method", jsonNewObject(method->symbol));
369 jsonObjectSetKey(resp, "service", jsonNewObject(ctx->session->remote_service));
370 jsonObjectSetKey(resp, "notes", jsonNewObject(method->notes));
371 jsonObjectSetKey(resp, "argc", jsonNewNumberObject(method->argc));
373 jsonObjectSetKey(resp, "sysmethod",
374 jsonNewNumberObject( (method->options & OSRF_METHOD_SYSTEM) ? 1 : 0 ));
375 jsonObjectSetKey(resp, "atomic",
376 jsonNewNumberObject( (method->options & OSRF_METHOD_ATOMIC) ? 1 : 0 ));
377 jsonObjectSetKey(resp, "cachable",
378 jsonNewNumberObject( (method->options & OSRF_METHOD_CACHABLE) ? 1 : 0 ));
380 jsonObjectSetClass(resp, "method");
385 int __osrfAppRunSystemMethod(osrfMethodContext* ctx) {
386 OSRF_METHOD_VERIFY_CONTEXT(ctx);
388 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL ) ||
389 !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL_ATOMIC )) {
391 return osrfAppIntrospectAll(ctx);
395 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT ) ||
396 !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ATOMIC )) {
398 return osrfAppIntrospect(ctx);
401 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_ECHO ) ||
402 !strcmp(ctx->method->name, OSRF_SYSMETHOD_ECHO_ATOMIC )) {
404 return osrfAppEcho(ctx);
408 osrfAppRequestRespondException( ctx->session,
409 ctx->request, "System method implementation not found");
415 int osrfAppIntrospect( osrfMethodContext* ctx ) {
417 jsonObject* resp = NULL;
418 char* methodSubstring = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
419 osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
422 if(!methodSubstring) return 1; /* respond with no methods */
426 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
429 while( (method = osrfHashIteratorNext(itr)) ) {
430 if( (len = strlen(methodSubstring)) <= strlen(method->name) ) {
431 if( !strncmp( method->name, methodSubstring, len) ) {
432 resp = jsonNewObject(NULL);
433 __osrfAppSetIntrospectMethod( ctx, method, resp );
434 osrfAppRespond(ctx, resp);
435 jsonObjectFree(resp);
439 osrfHashIteratorFree(itr);
448 int osrfAppIntrospectAll( osrfMethodContext* ctx ) {
449 jsonObject* resp = NULL;
450 osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
453 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
455 while( (method = osrfHashIteratorNext(itr)) ) {
456 resp = jsonNewObject(NULL);
457 __osrfAppSetIntrospectMethod( ctx, method, resp );
458 osrfAppRespond(ctx, resp);
459 jsonObjectFree(resp);
461 osrfHashIteratorFree(itr);
468 int osrfAppEcho( osrfMethodContext* ctx ) {
469 OSRF_METHOD_VERIFY_CONTEXT(ctx);
471 for( i = 0; i < ctx->params->size; i++ ) {
472 jsonObject* str = jsonObjectGetIndex(ctx->params,i);
473 osrfAppRespond(ctx, str);