int osrfMathRun( osrfMethodDispatcher* d ) {
- /*
- OSRF_METHOD_VERIFY_DISPATCHER(d)
- Verifies viability of the dispatcher components.
- Checks for NULLness of key components.
- Creates local variables :
- session - the app session ( osrfAppSession* )
- method - the method ( osrfMethod* )
- params - the methd parameters ( jsonObject* )
- request - the request id ( int ) */
-
OSRF_METHOD_VERIFY_DISPATCHER(d);
jsonObject* x = jsonObjectGetIndex(params, 0);
int initialize() {
+
+ /* tell the server about the methods we handle */
osrfAppRegisterMethod( "opensrf.math", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
osrfAppRegisterMethod( "opensrf.math", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
osrfAppRegisterMethod( "opensrf.math", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
int osrfMathRun( osrfMethodDispatcher* d ) {
- OSRF_METHOD_VERIFY_DISPATCHER(d);
+ OSRF_METHOD_VERIFY_DISPATCHER(d); /* see osrf_application.h */
+ /* collect the request params */
jsonObject* x = jsonObjectGetIndex(params, 0);
jsonObject* y = jsonObjectGetIndex(params, 1);
if( x && y ) {
+ /* pull out the params as strings since they may be either
+ strings or numbers depending on the client */
char* a = jsonObjectToSimpleString(x);
char* b = jsonObjectToSimpleString(y);
if( a && b ) {
- jsonObject* new_params = jsonParseString("[]");
- jsonObjectPush(new_params, jsonNewObject(a));
- jsonObjectPush(new_params, jsonNewObject(b));
-
+ /* construct a new params object to send to dbmath */
+ jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
free(a); free(b);
+ /* connect to db math */
osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
- int req_id = osrfAppSessionMakeRequest( ses, new_params, method->name, 1, NULL );
- osrf_message* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
+
+ /* dbmath uses the same method names that math does */
+ int req_id = osrfAppSessionMakeRequest( ses, newParams, method->name, 1, NULL );
+ osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
if(omsg) {
- osrfAppRequestRespond( session, request, omsg->_result_content );
- osrf_message_free(omsg);
+
+ /* return dbmath's response to the user */
+ osrfAppRequestRespond( session, request, osrfMessageGetResult(omsg) );
+ osrfMessageFree(omsg);
return 0;
}
}
if(ret == -1) {
- /* return an internal server error ? */
+ osrfAppSessionStatus( ses, OSRF_STATUS_INTERNALSERVERERROR,
+ reqId, "An unknown server error occurred" );
+ return -1;
}
return 0;
+void osrfMessageFree( osrfMessage* msg ) {
+ osrf_message_free( msg );
+}
+
void osrf_message_free( osrf_message* msg ) {
if( msg == NULL )
return;
}
+
+jsonObject* osrfMessageGetResult( osrfMessage* msg ) {
+ if(msg) return msg->_result_content;
+ return NULL;
+}
+
int is_exception;
/* if we're a RESULT */
- //json* result_content;
jsonObject* _result_content;
/* unparsed json string */
/* if we're a REQUEST */
char* method_name;
- //json* params;
+
jsonObject* _params;
/* in case anyone wants to make a list of us.
};
typedef struct osrf_message_struct osrf_message;
+typedef struct osrf_message_struct osrfMessage;
osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol );
//void osrf_message_set_request_info( osrf_message*, char* param_name, json* params );
void osrf_message_set_status_info( osrf_message*, char* status_name, char* status_text, int status_code );
void osrf_message_set_result_content( osrf_message*, char* json_string );
+void osrfMessageFree( osrfMessage* );
void osrf_message_free( osrf_message* );
char* osrf_message_to_xml( osrf_message* );
char* osrf_message_serialize(osrf_message*);
void osrf_message_add_param( osrf_message*, char* param_string );
+jsonObject* osrfMessageGetResult( osrfMessage* msg );
#endif
transport_message* msg = new_message_from_xml( data );
osrf_stack_transport_handler(msg, child->appname);
-
- /*
- transport_message* ret_msg = message_init(
- msg->body, msg->subject, msg->thread, msg->sender, NULL );
-
- client_send_message(child->connection, ret_msg);
- message_free( ret_msg );
-
- printf("Message body size %d\n", strlen(msg->body));
-
- printf( "Message Info\n" );
- printf( "%s\n", msg->sender );
- printf( "%s\n", msg->recipient );
- printf( "%s\n", msg->thread );
- printf( "%s\n", msg->body );
- printf( "%s\n", msg->subject );
- printf( "%s\n", msg->router_from );
- printf( "%d\n", msg->broadcast );
-
- message_free( msg );
- */
}
transport_message* cur_msg = NULL;
+
while(1) {
if( forker->first_child == NULL ) {/* no more children */
//fprintf(stderr, "Got Data %f\n", get_timestamp_millis() );
if( cur_msg == NULL ) continue;
-
+
int honored = 0; /* true if we've serviced the request */
while( ! honored ) {
if( cur_child->available ) {
debug_handler( "sending data to %d", cur_child->pid );
+
message_prepare_xml( cur_msg );
char* data = cur_msg->msg_xml;
if( ! data || strlen(data) < 1 ) break;
+
cur_child->available = 0;
debug_handler( "Writing to child fd %d", cur_child->write_data_fd );
} // honored?
+ message_free( cur_msg );
+
} /* top level listen loop */
}
int current_strlen; /* XXX need to move this into the function params for thread support */
-jsonObject* jsonParseString( char* string ) {
- return json_parse_string( string );
+jsonObject* jsonParseString( char* string, ... ) {
+ VA_LIST_TO_STRING(string);
+ return json_parse_string( VA_BUF );
}
//jsonObject* (*jsonParseString) (char* str) = &_jsonParseString;
jsonObject* json_parse_string(char* string);
-jsonObject* jsonParseString( char* string );
+jsonObject* jsonParseString( char* string, ... );
jsonObject* json_parse_file( const char* filename );