1 #include "osrf_application.h"
3 #include "objson/object.h"
5 osrfApplication* __osrfAppList = NULL;
8 int osrfAppRegisterApplication( char* appName, char* soFile ) {
9 if(!appName || ! soFile) return -1;
12 info_handler("Registering application %s with file %s", appName, soFile );
14 osrfApplication* app = safe_malloc(sizeof(osrfApplication));
15 app->handle = dlopen (soFile, RTLD_NOW);
18 warning_handler("Failed to dlopen library file %s: %s", soFile, dlerror() );
19 dlerror(); /* clear the error */
24 app->name = strdup(appName);
26 /* this has to be done before initting the application */
27 app->next = __osrfAppList;
31 /* see if we can run the initialize method */
33 *(void **) (&init) = dlsym(app->handle, "osrfAppInitialize");
35 if( (error = dlerror()) != NULL ) {
36 warning_handler("! Unable to locate method symbol [osrfAppInitialize] for app %s: %s", appName, error );
42 if( (ret = (*init)()) ) {
43 warning_handler("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 */
52 info_handler("Application %s registered successfully", appName );
59 int osrfAppRegisterMethod( char* appName,
60 char* methodName, char* symbolName, char* notes, int argc ) {
61 if( !appName || ! methodName || ! symbolName ) return -1;
63 osrfApplication* app = _osrfAppFindApplication(appName);
64 if(!app) return warning_handler("Unable to locate application %s", appName );
66 debug_handler("Registering method %s for app %s", appName, methodName );
68 osrfMethod* method = safe_malloc(sizeof(osrfMethod));
69 method->name = strdup(methodName);
70 method->symbol = strdup(symbolName);
71 if(notes) method->notes = strdup(notes);
74 /* plug the method into the list of methods */
75 method->next = app->methods;
76 app->methods = method;
80 osrfApplication* _osrfAppFindApplication( char* name ) {
81 if(!name) return NULL;
82 osrfApplication* app = __osrfAppList;
84 if(!strcmp(app->name, name))
91 osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ) {
92 if(!app || ! methodName) return NULL;
93 osrfMethod* method = app->methods;
95 if(!strcmp(method->name, methodName))
97 method = method->next;
102 osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ) {
103 if(!appName || ! methodName) return NULL;
104 return __osrfAppFindMethod( _osrfAppFindApplication(appName), methodName );
110 int osrfAppRunMethod( char* appName, char* methodName, osrfAppSession* ses, int reqId, jsonObject* params ) {
111 if( !(appName && methodName && ses) ) return -1;
114 osrfApplication* app;
116 osrfMethodContext context;
118 context.session = ses;
119 context.params = params;
120 context.request = reqId;
122 /* this is the method we're gonna run */
123 int (*meth) (osrfMethodContext*);
125 info_handler("Running method [%s] for app [%s] with request id %d and "
126 "thread trace %s", methodName, appName, reqId, ses->session_id );
128 if( !(app = _osrfAppFindApplication(appName)) )
129 return warning_handler( "Application not found: %s", appName );
132 if( !(method = __osrfAppFindMethod( app, methodName )) ) {
133 /* see if the unfound method is a system method */
134 info_handler("Method %s not found, checking to see if it's a system method...", methodName );
136 meth.name = methodName;
137 context.method = &meth;
138 int sysres = __osrfAppRunSystemMethod(&context);
139 if(sysres == 0) return 0;
141 osrfAppSessionStatus( ses, OSRF_STATUS_COMPLETE, "osrfConnectStatus", reqId, "Request Complete" );
144 return warning_handler( "NOT FOUND: app %s / method %s", appName, methodName );
148 context.method = method;
150 /* open the method */
151 *(void **) (&meth) = dlsym(app->handle, method->symbol);
153 if( (error = dlerror()) != NULL ) {
154 return warning_handler("Unable to locate method symbol [%s] "
155 "for method %s and app %s", method->symbol, method->name, app->name );
159 int ret = (*meth) (&context);
161 debug_handler("method returned %d", ret );
164 osrfAppSessionStatus( ses, OSRF_STATUS_INTERNALSERVERERROR,
165 "Server Error", reqId, "An unknown server error occurred" );
170 osrfAppSessionStatus( ses, OSRF_STATUS_COMPLETE, "osrfConnectStatus", reqId, "Request Complete" );
177 int __osrfAppRunSystemMethod(osrfMethodContext* ctx) {
178 OSRF_METHOD_VERIFY_CONTEXT(ctx);
180 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL )) {
182 jsonObject* resp = NULL;
183 osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
185 osrfMethod* method = app->methods;
187 resp = jsonNewObject(NULL);
188 jsonObjectSetKey(resp, "api_name", jsonNewObject(method->name));
189 jsonObjectSetKey(resp, "method", jsonNewObject(method->symbol));
190 jsonObjectSetKey(resp, "service", jsonNewObject(ctx->session->remote_service));
191 jsonObjectSetKey(resp, "notes", jsonNewObject(method->notes));
192 jsonObjectSetKey(resp, "argc", jsonNewNumberObject(method->argc));
193 osrfAppRequestRespond(ctx->session, ctx->request, resp);
194 method = method->next;
195 jsonObjectSetClass(resp, "method");
196 jsonObjectFree(resp);