From b686b3cac6ad976161cef34e140a9931d4dcd5a1 Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 2 Sep 2005 22:28:27 +0000 Subject: [PATCH] added some more api wrappers json parser now takes variable length args added some comments to osrf_math found hienous memory leak in forker code, C code is now memory stable :) git-svn-id: svn://svn.open-ils.org/ILS/trunk@1794 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- OpenSRF/src/c-apps/osrf_dbmath.c | 10 --------- OpenSRF/src/c-apps/osrf_math.c | 26 +++++++++++++++-------- OpenSRF/src/libstack/osrf_application.c | 4 +++- OpenSRF/src/libstack/osrf_message.c | 10 +++++++++ OpenSRF/src/libstack/osrf_message.h | 6 ++++-- OpenSRF/src/libstack/osrf_prefork.c | 28 ++++++------------------- OpenSRF/src/objson/json_parser.c | 5 +++-- OpenSRF/src/objson/json_parser.h | 2 +- 8 files changed, 44 insertions(+), 47 deletions(-) diff --git a/OpenSRF/src/c-apps/osrf_dbmath.c b/OpenSRF/src/c-apps/osrf_dbmath.c index 84feba258c..02b0ecc3d0 100644 --- a/OpenSRF/src/c-apps/osrf_dbmath.c +++ b/OpenSRF/src/c-apps/osrf_dbmath.c @@ -21,16 +21,6 @@ int childInit() { 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); diff --git a/OpenSRF/src/c-apps/osrf_math.c b/OpenSRF/src/c-apps/osrf_math.c index 844a7c5b55..d695e0fca6 100644 --- a/OpenSRF/src/c-apps/osrf_math.c +++ b/OpenSRF/src/c-apps/osrf_math.c @@ -8,6 +8,8 @@ int osrfMathRun( osrfMethodDispatcher* ); 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 ); @@ -21,31 +23,37 @@ int childInit() { 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; } } diff --git a/OpenSRF/src/libstack/osrf_application.c b/OpenSRF/src/libstack/osrf_application.c index 8ede7908b2..3106af57e1 100644 --- a/OpenSRF/src/libstack/osrf_application.c +++ b/OpenSRF/src/libstack/osrf_application.c @@ -143,7 +143,9 @@ int osrfAppRunMethod( char* appName, char* methodName, osrfAppSession* ses, int if(ret == -1) { - /* return an internal server error ? */ + osrfAppSessionStatus( ses, OSRF_STATUS_INTERNALSERVERERROR, + reqId, "An unknown server error occurred" ); + return -1; } return 0; diff --git a/OpenSRF/src/libstack/osrf_message.c b/OpenSRF/src/libstack/osrf_message.c index 49b6ea11cd..4a10c8155d 100644 --- a/OpenSRF/src/libstack/osrf_message.c +++ b/OpenSRF/src/libstack/osrf_message.c @@ -80,6 +80,10 @@ void osrf_message_set_result_content( osrf_message* msg, char* json_string ) { +void osrfMessageFree( osrfMessage* msg ) { + osrf_message_free( msg ); +} + void osrf_message_free( osrf_message* msg ) { if( msg == NULL ) return; @@ -276,3 +280,9 @@ int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) { } + +jsonObject* osrfMessageGetResult( osrfMessage* msg ) { + if(msg) return msg->_result_content; + return NULL; +} + diff --git a/OpenSRF/src/libstack/osrf_message.h b/OpenSRF/src/libstack/osrf_message.h index 42df49a5fb..dfb8f9c662 100644 --- a/OpenSRF/src/libstack/osrf_message.h +++ b/OpenSRF/src/libstack/osrf_message.h @@ -60,7 +60,6 @@ struct osrf_message_struct { int is_exception; /* if we're a RESULT */ - //json* result_content; jsonObject* _result_content; /* unparsed json string */ @@ -68,7 +67,7 @@ struct osrf_message_struct { /* if we're a REQUEST */ char* method_name; - //json* params; + jsonObject* _params; /* in case anyone wants to make a list of us. @@ -79,12 +78,14 @@ struct osrf_message_struct { }; 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*); @@ -105,6 +106,7 @@ void osrf_message_add_object_param( osrf_message* msg, jsonObject* o ); void osrf_message_add_param( osrf_message*, char* param_string ); +jsonObject* osrfMessageGetResult( osrfMessage* msg ); #endif diff --git a/OpenSRF/src/libstack/osrf_prefork.c b/OpenSRF/src/libstack/osrf_prefork.c index f34f9a574d..8dfeaf99f0 100644 --- a/OpenSRF/src/libstack/osrf_prefork.c +++ b/OpenSRF/src/libstack/osrf_prefork.c @@ -111,27 +111,6 @@ void prefork_child_process_request(prefork_child* child, char* data) { 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 ); - */ } @@ -248,6 +227,7 @@ void prefork_run(prefork_simple* forker) { transport_message* cur_msg = NULL; + while(1) { if( forker->first_child == NULL ) {/* no more children */ @@ -261,7 +241,7 @@ void prefork_run(prefork_simple* forker) { //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 ) { @@ -280,9 +260,11 @@ void prefork_run(prefork_simple* forker) { 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 ); @@ -336,6 +318,8 @@ void prefork_run(prefork_simple* forker) { } // honored? + message_free( cur_msg ); + } /* top level listen loop */ } diff --git a/OpenSRF/src/objson/json_parser.c b/OpenSRF/src/objson/json_parser.c index 95e385ae3c..63ba6b3718 100644 --- a/OpenSRF/src/objson/json_parser.c +++ b/OpenSRF/src/objson/json_parser.c @@ -22,8 +22,9 @@ GNU General Public License for more details. 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; diff --git a/OpenSRF/src/objson/json_parser.h b/OpenSRF/src/objson/json_parser.h index e47a429bdf..866d9617bf 100644 --- a/OpenSRF/src/objson/json_parser.h +++ b/OpenSRF/src/objson/json_parser.h @@ -34,7 +34,7 @@ GNU General Public License for more details. jsonObject* json_parse_string(char* string); -jsonObject* jsonParseString( char* string ); +jsonObject* jsonParseString( char* string, ... ); jsonObject* json_parse_file( const char* filename ); -- 2.43.2