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);
21 osrfLogWarning( OSRF_LOG_MARK, "Failed to dlopen library file %s: %s", soFile, dlerror() );
22 dlerror(); /* clear the error */
27 app->methods = osrfNewHash();
28 osrfHashSet( __osrfAppHash, app, appName );
30 /* see if we can run the initialize method */
32 *(void **) (&init) = dlsym(app->handle, "osrfAppInitialize");
34 if( (error = dlerror()) != NULL ) {
35 osrfLogWarning( OSRF_LOG_MARK,
36 "! Unable to locate method symbol [osrfAppInitialize] for app %s: %s", appName, error );
42 if( (ret = (*init)()) ) {
43 osrfLogWarning( OSRF_LOG_MARK, "Application %s returned non-zero value from "
44 "'osrfAppInitialize', not registering...", appName );
45 //free(app->name); /* need a method to remove an application from the list */
51 __osrfAppRegisterSysMethods(appName);
53 osrfLogInfo( OSRF_LOG_MARK, "Application %s registered successfully", appName );
55 osrfLogSetAppname(appName);
61 int osrfAppRegisterMethod( char* appName, char* methodName,
62 char* symbolName, char* notes, int argc, int options ) {
64 if( !appName || ! methodName ) return -1;
66 osrfApplication* app = _osrfAppFindApplication(appName);
68 osrfLogWarning( OSRF_LOG_MARK, "Unable to locate application %s", appName );
72 osrfLogDebug( OSRF_LOG_MARK, "Registering method %s for app %s", methodName, appName );
74 osrfMethod* method = _osrfAppBuildMethod(
75 methodName, symbolName, notes, argc, options );
76 method->options = options;
78 /* plug the method into the list of methods */
79 osrfHashSet( app->methods, method, method->name );
81 if( options & OSRF_METHOD_STREAMING ) { /* build the atomic counterpart */
82 int newops = options | OSRF_METHOD_ATOMIC;
83 osrfMethod* atomicMethod = _osrfAppBuildMethod(
84 methodName, symbolName, notes, argc, newops );
85 osrfHashSet( app->methods, atomicMethod, atomicMethod->name );
93 osrfMethod* _osrfAppBuildMethod( char* methodName,
94 char* symbolName, char* notes, int argc, int options ) {
96 osrfMethod* method = safe_malloc(sizeof(osrfMethod));
98 if(methodName) method->name = strdup(methodName);
99 if(symbolName) method->symbol = strdup(symbolName);
100 if(notes) method->notes = strdup(notes);
103 method->options = options;
105 if(options & OSRF_METHOD_ATOMIC) { /* add ".atomic" to the end of the name */
106 char mb[strlen(method->name) + 8];
107 sprintf(mb, "%s.atomic", method->name);
109 method->name = strdup(mb);
110 method->options |= OSRF_METHOD_STREAMING;
117 int __osrfAppRegisterSysMethods( char* app ) {
119 osrfAppRegisterMethod(
120 app, OSRF_SYSMETHOD_INTROSPECT, NULL,
121 "Return a list of methods whose names have the same initial "
122 "substring as that of the provided method name PARAMS( methodNameSubstring )",
123 1, OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
125 osrfAppRegisterMethod(
126 app, OSRF_SYSMETHOD_INTROSPECT_ALL, NULL,
127 "Returns a complete list of methods. PARAMS()", 0,
128 OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
130 osrfAppRegisterMethod(
131 app, OSRF_SYSMETHOD_ECHO, NULL,
132 "Echos all data sent to the server back to the client. PARAMS([a, b, ...])", 0,
133 OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
138 osrfApplication* _osrfAppFindApplication( char* name ) {
139 if(!name) return NULL;
140 return (osrfApplication*) osrfHashGet(__osrfAppHash, name);
143 osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ) {
144 if(!app || ! methodName) return NULL;
145 return (osrfMethod*) osrfHashGet( app->methods, methodName );
148 osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ) {
149 if(!appName || ! methodName) return NULL;
150 return __osrfAppFindMethod( _osrfAppFindApplication(appName), methodName );
154 int osrfAppRunMethod( char* appName, char* methodName,
155 osrfAppSession* ses, int reqId, jsonObject* params ) {
157 if( !(appName && methodName && ses) ) return -1;
160 osrfApplication* app;
162 osrfMethodContext context;
164 context.session = ses;
165 context.params = params;
166 context.request = reqId;
167 context.responses = NULL;
169 /* this is the method we're gonna run */
170 int (*meth) (osrfMethodContext*);
172 osrfLogInfo( OSRF_LOG_MARK, "Running method [%s] for app [%s] with request id %d and "
173 "thread trace %s", methodName, appName, reqId, ses->session_id );
175 if( !(app = _osrfAppFindApplication(appName)) )
176 return osrfAppRequestRespondException( ses,
177 reqId, "Application not found: %s", appName );
179 if( !(method = __osrfAppFindMethod( app, methodName )) )
180 return osrfAppRequestRespondException( ses, reqId,
181 "Method [%s] not found for service %s", methodName, appName );
183 context.method = method;
185 #ifdef OSRF_STRICT_PARAMS
186 if( method->argc > 0 ) {
187 if(!params || params->type != JSON_ARRAY || params->size < method->argc )
188 return osrfAppRequestRespondException( ses, reqId,
189 "Not enough params for method %s / service %s", methodName, appName );
195 if( method->options & OSRF_METHOD_SYSTEM ) {
196 retcode = __osrfAppRunSystemMethod(&context);
200 /* open and now run the method */
201 *(void **) (&meth) = dlsym(app->handle, method->symbol);
203 if( (error = dlerror()) != NULL ) {
204 return osrfAppRequestRespondException( ses, reqId,
205 "Unable to execute method [%s] for service %s", methodName, appName );
208 retcode = (*meth) (&context);
212 return osrfAppRequestRespondException(
213 ses, reqId, "An unknown server error occurred" );
215 return __osrfAppPostProcess( &context, retcode );
220 int osrfAppRespond( osrfMethodContext* ctx, jsonObject* data ) {
221 return _osrfAppRespond( ctx, data, 0 );
224 int osrfAppRespondComplete( osrfMethodContext* context, jsonObject* data ) {
225 return _osrfAppRespond( context, data, 1 );
228 int _osrfAppRespond( osrfMethodContext* ctx, jsonObject* data, int complete ) {
229 if(!(ctx && ctx->method)) return -1;
231 if( ctx->method->options & OSRF_METHOD_ATOMIC ) {
232 osrfLogDebug( OSRF_LOG_MARK,
233 "Adding responses to stash for atomic method %s", ctx->method );
235 if( ctx->responses == NULL )
236 ctx->responses = jsonParseString("[]");
237 jsonObjectPush( ctx->responses, jsonObjectClone(data) );
241 if( !(ctx->method->options & OSRF_METHOD_ATOMIC) &&
242 !(ctx->method->options & OSRF_METHOD_CACHABLE) ) {
245 osrfAppRequestRespondComplete( ctx->session, ctx->request, data );
247 osrfAppRequestRespond( ctx->session, ctx->request, data );
257 int __osrfAppPostProcess( osrfMethodContext* ctx, int retcode ) {
258 if(!(ctx && ctx->method)) return -1;
260 osrfLogDebug( OSRF_LOG_MARK, "Postprocessing method %s with retcode %d",
261 ctx->method->name, retcode );
263 if(ctx->responses) { /* we have cached responses to return (no responses have been sent) */
265 osrfAppRequestRespondComplete( ctx->session, ctx->request, ctx->responses );
266 jsonObjectFree(ctx->responses);
267 ctx->responses = NULL;
272 osrfAppSessionStatus( ctx->session, OSRF_STATUS_COMPLETE,
273 "osrfConnectStatus", ctx->request, "Request Complete" );
280 int osrfAppRequestRespondException( osrfAppSession* ses, int request, char* msg, ... ) {
283 VA_LIST_TO_STRING(msg);
284 osrfLogWarning( OSRF_LOG_MARK, "Returning method exception with message: %s", VA_BUF );
285 osrfAppSessionStatus( ses, OSRF_STATUS_NOTFOUND, "osrfMethodException", request, VA_BUF );
290 static void __osrfAppSetIntrospectMethod( osrfMethodContext* ctx, osrfMethod* method, jsonObject* resp ) {
291 if(!(ctx && resp)) return;
293 jsonObjectSetKey(resp, "api_name", jsonNewObject(method->name));
294 jsonObjectSetKey(resp, "method", jsonNewObject(method->symbol));
295 jsonObjectSetKey(resp, "service", jsonNewObject(ctx->session->remote_service));
296 jsonObjectSetKey(resp, "notes", jsonNewObject(method->notes));
297 jsonObjectSetKey(resp, "argc", jsonNewNumberObject(method->argc));
299 jsonObjectSetKey(resp, "sysmethod",
300 jsonNewNumberObject( (method->options & OSRF_METHOD_SYSTEM) ? 1 : 0 ));
301 jsonObjectSetKey(resp, "atomic",
302 jsonNewNumberObject( (method->options & OSRF_METHOD_ATOMIC) ? 1 : 0 ));
303 jsonObjectSetKey(resp, "cachable",
304 jsonNewNumberObject( (method->options & OSRF_METHOD_CACHABLE) ? 1 : 0 ));
306 jsonObjectSetClass(resp, "method");
311 int __osrfAppRunSystemMethod(osrfMethodContext* ctx) {
312 OSRF_METHOD_VERIFY_CONTEXT(ctx);
314 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL ) ||
315 !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL_ATOMIC )) {
317 return osrfAppIntrospectAll(ctx);
321 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT ) ||
322 !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ATOMIC )) {
324 return osrfAppIntrospect(ctx);
327 osrfAppRequestRespondException( ctx->session,
328 ctx->request, "System method implementation not found");
334 int osrfAppIntrospect( osrfMethodContext* ctx ) {
336 jsonObject* resp = NULL;
337 char* methodSubstring = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
338 osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
341 if(!methodSubstring) return 1; /* respond with no methods */
345 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
348 while( (method = osrfHashIteratorNext(itr)) ) {
349 if( (len = strlen(methodSubstring)) <= strlen(method->name) ) {
350 if( !strncmp( method->name, methodSubstring, len) ) {
351 resp = jsonNewObject(NULL);
352 __osrfAppSetIntrospectMethod( ctx, method, resp );
353 osrfAppRespond(ctx, resp);
354 jsonObjectFree(resp);
358 osrfHashIteratorFree(itr);
367 int osrfAppIntrospectAll( osrfMethodContext* ctx ) {
368 jsonObject* resp = NULL;
369 osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
372 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
374 while( (method = osrfHashIteratorNext(itr)) ) {
375 resp = jsonNewObject(NULL);
376 __osrfAppSetIntrospectMethod( ctx, method, resp );
377 osrfAppRespond(ctx, resp);
378 jsonObjectFree(resp);
380 osrfHashIteratorFree(itr);