From 2f780d0fed8fac8b8098019b3225e10c29b235a6 Mon Sep 17 00:00:00 2001 From: erickson Date: Thu, 11 Aug 2005 22:28:52 +0000 Subject: [PATCH] moved C over to all JSON all the time git-svn-id: svn://svn.open-ils.org/ILS/trunk@1639 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- OpenSRF/src/gateway/mod_ils_gateway.c | 6 +- OpenSRF/src/libstack/osrf_app_session.c | 27 +-- OpenSRF/src/libstack/osrf_message.c | 227 +++++++++++++++++------- OpenSRF/src/libstack/osrf_message.h | 6 + OpenSRF/src/libstack/osrf_stack.c | 7 +- OpenSRF/src/libstack/osrf_stack.h | 2 +- OpenSRF/src/objson/json_parser.c | 6 +- OpenSRF/src/objson/objson_test.c | 3 +- OpenSRF/src/srfsh/srfsh.c | 3 +- 9 files changed, 207 insertions(+), 80 deletions(-) diff --git a/OpenSRF/src/gateway/mod_ils_gateway.c b/OpenSRF/src/gateway/mod_ils_gateway.c index 9751fb9115..58767b90ad 100644 --- a/OpenSRF/src/gateway/mod_ils_gateway.c +++ b/OpenSRF/src/gateway/mod_ils_gateway.c @@ -307,9 +307,11 @@ static int mod_ils_gateway_method_handler (request_rec *r) { /* gather result data */ while((omsg = osrf_app_session_request_recv( session, req_id, 60 ))) { - if( omsg->result_string ) { - buffer_add(result_data, omsg->result_string); + if( omsg->_result_content ) { + char* content = object_to_json(omsg->_result_content); + buffer_add(result_data, content); buffer_add( result_data, ","); + free(content); } else { diff --git a/OpenSRF/src/libstack/osrf_app_session.c b/OpenSRF/src/libstack/osrf_app_session.c index 525e559d45..40a7bc1b56 100644 --- a/OpenSRF/src/libstack/osrf_app_session.c +++ b/OpenSRF/src/libstack/osrf_app_session.c @@ -375,16 +375,18 @@ int osrf_app_session_make_req( if(session == NULL) return -1; osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol ); - //osrf_message_set_request_info( req_msg, method_name, params ); osrf_message_set_method(req_msg, method_name); - if(params) osrf_message_set_params(req_msg, params); + if(params) { + osrf_message_set_params(req_msg, params); - /* if we're not parsing the json, shove the strings in manually */ - if(!req_msg->parse_json_params && param_strings) { - int i; - for(i = 0; i!= param_strings->size ; i++ ) { - osrf_message_add_param(req_msg, - string_array_get_string(param_strings,i)); + } else { + + if(param_strings) { + int i; + for(i = 0; i!= param_strings->size ; i++ ) { + osrf_message_add_param(req_msg, + string_array_get_string(param_strings,i)); + } } } @@ -637,18 +639,19 @@ int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){ } } - char* xml = osrf_message_to_xml(msg); + //char* xml = osrf_message_to_xml(msg); + char* string = osrf_message_serialize(msg); debug_handler("[%s] [%s] Remote Id: %s", session->remote_service, session->session_id, session->remote_id ); transport_message* t_msg = message_init( - xml, "", session->session_id, session->remote_id, NULL ); + string, "", session->session_id, session->remote_id, NULL ); debug_handler("Session [%s] [%s] sending to %s \nXML:\n%s", - session->remote_service, session->session_id, t_msg->recipient, xml ); + session->remote_service, session->session_id, t_msg->recipient, string ); ret_val = client_send_message( session->transport_handle, t_msg ); - free(xml); + free(string); message_free( t_msg ); return ret_val; diff --git a/OpenSRF/src/libstack/osrf_message.c b/OpenSRF/src/libstack/osrf_message.c index ff110527b8..0fcebc5ec1 100644 --- a/OpenSRF/src/libstack/osrf_message.c +++ b/OpenSRF/src/libstack/osrf_message.c @@ -36,34 +36,13 @@ osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protoco void osrf_message_set_json_parse_result( int ibool ) { - parse_json_result = ibool; +// parse_json_result = ibool; } void osrf_message_set_json_parse_params( int ibool ) { - parse_json_params = ibool; +// parse_json_params = ibool; } -/* -void osrf_message_set_request_info( - osrf_message* msg, char* method_name, json* json_params ) { - - if( msg == NULL || method_name == NULL ) - fatal_handler( "Bad params to osrf_message_set_request_params()" ); - - if(msg->parse_json_params) { - if( json_params != NULL ) { - msg->params = json_tokener_parse(json_object_to_json_string(json_params)); - msg->_params = json_parse_string(json_object_to_json_string(json_params)); - } else { - msg->params = json_tokener_parse("[]"); - msg->_params = json_parse_string("[]"); - } - } - - msg->method_name = strdup( method_name ); -} -*/ - void osrf_message_set_method( osrf_message* msg, char* method_name ) { if( msg == NULL || method_name == NULL ) return; msg->method_name = strdup( method_name ); @@ -100,12 +79,16 @@ void osrf_message_set_params( osrf_message* msg, object* o ) { } -/* only works of parse_json_params is false */ +/* only works if parse_json_params is false */ void osrf_message_add_param( osrf_message* msg, char* param_string ) { - if(msg == NULL || param_string == NULL) - return; + if(msg == NULL || param_string == NULL) return; + if(!msg->_params) msg->_params = new_object(NULL); + msg->_params->push(msg->_params, json_parse_string(param_string)); + + /* if(!msg->parse_json_params) string_array_add(msg->parray, param_string); + */ } @@ -130,27 +113,10 @@ void osrf_message_set_result_content( osrf_message* msg, char* json_string ) { warning_handler( "Bad params to osrf_message_set_result_content()" ); msg->result_string = strdup(json_string); - - /* ----------------------------------------------------- */ - /* - object* o = json_parse_string(json_string); - char* string = o->to_json(o); - debug_handler("---------------------------------------------------"); - debug_handler("Parsed JSON string \n%s\n", string); - if(o->classname) - debug_handler("Class is %s\n", o->classname); - debug_handler("---------------------------------------------------"); - free_object(o); - free(string); - */ - /* ----------------------------------------------------- */ - debug_handler( "Message Parse JSON result is set to: %d", msg->parse_json_result ); - if(msg->parse_json_result) { - //msg->result_content = json_tokener_parse(msg->result_string); + if(msg->parse_json_result) msg->_result_content = json_parse_string(msg->result_string); - } } @@ -165,11 +131,6 @@ void osrf_message_free( osrf_message* msg ) { if( msg->status_text != NULL ) free(msg->status_text); - /* - if( msg->result_content != NULL ) - json_object_put( msg->result_content ); - */ - if( msg->_result_content != NULL ) free_object( msg->_result_content ); @@ -179,31 +140,175 @@ void osrf_message_free( osrf_message* msg ) { if( msg->method_name != NULL ) free(msg->method_name); - /* - if( msg->params != NULL ) - json_object_put( msg->params ); - */ - if( msg->_params != NULL ) free_object(msg->_params); - string_array_destroy(msg->parray); free(msg); } +char* osrf_message_serialize(osrf_message* msg) { + if( msg == NULL ) return NULL; + object* json = new_object(NULL); + json->set_class(json, "osrfMessage"); + object* payload; + char sc[64]; memset(sc,0,64); + + char* str; + + char tt[64]; + memset(tt,0,64); + sprintf(tt,"%d",msg->thread_trace); + json->add_key(json, "threadTrace", new_object(tt)); + + switch(msg->m_type) { + + case CONNECT: + json->add_key(json, "type", new_object("CONNECT")); + break; + + case DISCONNECT: + json->add_key(json, "type", new_object("DISCONNECT")); + break; + + case STATUS: + json->add_key(json, "type", new_object("STATUS")); + payload = new_object(NULL); + payload->set_class(payload, msg->status_name); + payload->add_key(payload, "status", new_object(msg->status_text)); + sprintf(sc,"%d",msg->status_code); + payload->add_key(payload, "statusCode", new_object(sc)); + json->add_key(json, "payload", payload); + break; + + case REQUEST: + json->add_key(json, "type", new_object("REQUEST")); + payload = new_object(NULL); + payload->set_class(payload, "osrfMethod"); + payload->add_key(payload, "method", new_object(msg->method_name)); + str = object_to_json(msg->_params); + payload->add_key(payload, "params", json_parse_string(str)); + free(str); + json->add_key(json, "payload", payload); + + break; + + case RESULT: + json->add_key(json, "type", new_object("RESULT")); + payload = new_object(NULL); + payload->set_class(payload,"osrfResult"); + payload->add_key(payload, "status", new_object(msg->status_text)); + sprintf(sc,"%d",msg->status_code); + payload->add_key(payload, "statusCode", new_object(sc)); + str = object_to_json(msg->_result_content); + payload->add_key(payload, "content", json_parse_string(str)); + free(str); + json->add_key(json, "payload", payload); + break; + } + + object* wrapper = new_object(NULL); + wrapper->push(wrapper, json); + char* j = wrapper->to_json(wrapper); + free_object(wrapper); + return j; +} + + +int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) { + if(!string || !msgs || count <= 0) return 0; + int numparsed = 0; + object* json = json_parse_string(string); + if(json == NULL) return 0; + int x; + + + for( x = 0; x < json->size && x < count; x++ ) { + + object* message = json->get_index(json, x); + + if(message && !message->is_null && + message->classname && !strcmp(message->classname, "osrfMessage")) { + + osrf_message* new_msg = safe_malloc(sizeof(osrf_message)); + + object* tmp = message->get_key(message, "type"); + + if(tmp && tmp->string_data) { + char* t = tmp->string_data; + + if(!strcmp(t, "CONNECT")) new_msg->m_type = CONNECT; + if(!strcmp(t, "DISCONNECT")) new_msg->m_type = DISCONNECT; + if(!strcmp(t, "STATUS")) new_msg->m_type = STATUS; + if(!strcmp(t, "REQUEST")) new_msg->m_type = REQUEST; + if(!strcmp(t, "RESULT")) new_msg->m_type = RESULT; + } + + tmp = message->get_key(message, "threadTrace"); + if(tmp) { + if(tmp->is_number) + new_msg->thread_trace = tmp->num_value; + if(tmp->is_string) + new_msg->thread_trace = atoi(tmp->string_data); + } + + + tmp = message->get_key(message, "protocol"); + if(tmp) { + if(tmp->is_number) + new_msg->protocol = tmp->num_value; + if(tmp->is_string) + new_msg->protocol = atoi(tmp->string_data); + } + + tmp = message->get_key(message, "payload"); + if(tmp) { + if(tmp->classname) + new_msg->status_name = strdup(tmp->classname); + + object* tmp0 = tmp->get_key(tmp,"method"); + if(tmp0 && tmp0->string_data) + new_msg->method_name = strdup(tmp0->string_data); + + tmp0 = tmp->get_key(tmp,"params"); + if(tmp0) { + char* s = tmp0->to_json(tmp0); + new_msg->_params = json_parse_string(s); + free(s); + } + + tmp0 = tmp->get_key(tmp,"status"); + if(tmp0 && tmp0->string_data) + new_msg->status_text = strdup(tmp0->string_data); + + tmp0 = tmp->get_key(tmp,"statusCode"); + if(tmp0) { + if(tmp0->is_string && tmp0->string_data) + new_msg->status_code = atoi(tmp0->string_data); + if(tmp0->is_number) + new_msg->status_code = tmp0->num_value; + } + + tmp0 = tmp->get_key(tmp,"content"); + if(tmp0) { + char* s = tmp0->to_json(tmp0); + new_msg->_result_content = json_parse_string(s); + free(s); + } + + } + msgs[numparsed++] = new_msg; + } + } + return numparsed; +} /* here's where things get interesting */ char* osrf_message_to_xml( osrf_message* msg ) { - if( msg == NULL ) - return NULL; - - //int bufsize; - //xmlChar* xmlbuf; - //char* encoded_msg; + if( msg == NULL ) return NULL; xmlKeepBlanksDefault(0); diff --git a/OpenSRF/src/libstack/osrf_message.h b/OpenSRF/src/libstack/osrf_message.h index a49509ce46..054df2d437 100644 --- a/OpenSRF/src/libstack/osrf_message.h +++ b/OpenSRF/src/libstack/osrf_message.h @@ -92,6 +92,12 @@ void osrf_message_set_status_info( osrf_message*, char* status_name, char* statu void osrf_message_set_result_content( osrf_message*, char* json_string ); void osrf_message_free( osrf_message* ); char* osrf_message_to_xml( osrf_message* ); +char* osrf_message_serialize(osrf_message*); + +/* count is the max number of messages we'll put into msgs[] */ +int osrf_message_deserialize(char* json, osrf_message* msgs[], int count); + + /** Pushes any message retreived from the xml into the 'msgs' array. * it is assumed that 'msgs' has beenn pre-allocated. * Returns the number of message that are in the buffer. diff --git a/OpenSRF/src/libstack/osrf_stack.c b/OpenSRF/src/libstack/osrf_stack.c index 422eb89ef9..1ba158a455 100644 --- a/OpenSRF/src/libstack/osrf_stack.c +++ b/OpenSRF/src/libstack/osrf_stack.c @@ -38,6 +38,7 @@ int osrf_stack_transport_handler( transport_message* msg ) { if( session == NULL ) { /* we must be a server, build a new session */ info_handler( "Received message for nonexistant session. Dropping..." ); + //osrf_app_server_session_init( msg->thread, message_free( msg ); return 1; } @@ -47,7 +48,8 @@ int osrf_stack_transport_handler( transport_message* msg ) { osrf_app_session_set_remote( session, msg->sender ); osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET]; memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET ); - int num_msgs = osrf_message_from_xml( msg->body, arr ); + //int num_msgs = osrf_message_from_xml( msg->body, arr ); + int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET); debug_handler( "We received %d messages from %s", num_msgs, msg->sender ); @@ -181,6 +183,9 @@ osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) { if(session == NULL || msg == NULL) return NULL; + + if( msg->m_type == STATUS ) { return NULL; } + warning_handler( "We dont' do servers yet !!" ); return msg; diff --git a/OpenSRF/src/libstack/osrf_stack.h b/OpenSRF/src/libstack/osrf_stack.h index 6c5cedcf16..6cacbd22a0 100644 --- a/OpenSRF/src/libstack/osrf_stack.h +++ b/OpenSRF/src/libstack/osrf_stack.h @@ -7,7 +7,7 @@ #define OSRF_STACK_H /* the max number of oilsMessage blobs present in any one root packet */ -#define OSRF_MAX_MSGS_PER_PACKET 16 +#define OSRF_MAX_MSGS_PER_PACKET 64 // ----------------------------------------------------------------------------- int osrf_stack_transport_handler( transport_message* msg ); diff --git a/OpenSRF/src/objson/json_parser.c b/OpenSRF/src/objson/json_parser.c index ac0de5f3d8..a3e4692374 100644 --- a/OpenSRF/src/objson/json_parser.c +++ b/OpenSRF/src/objson/json_parser.c @@ -207,10 +207,13 @@ int json_parse_json_number(char* string, unsigned long* index, object* obj) { /* negative number? */ if(c == '-') { buffer_add(buf, "-"); (*index)++; } + c = string[*index]; + while(*index < current_strlen) { - if(is_number(c)) + if(is_number(c)) { buffer_add_char(buf, c); + } else if( c == '.' ) { if(dot_seen) { @@ -222,6 +225,7 @@ int json_parse_json_number(char* string, unsigned long* index, object* obj) { } else { done = 1; break; } + (*index)++; c = string[*index]; if(done) break; diff --git a/OpenSRF/src/objson/objson_test.c b/OpenSRF/src/objson/objson_test.c index f3a5c39efa..89bd2d2845 100644 --- a/OpenSRF/src/objson/objson_test.c +++ b/OpenSRF/src/objson/objson_test.c @@ -31,7 +31,8 @@ int main() { printf("------------------------------------------------------------------\n"); - o = json_parse_string("[,,{\"key\":,\"key1\":\"a\"}]"); + //o = json_parse_string("-1"); + o = json_parse_string("{\"key\":-1}"); char* h = o->to_json(o); printf("\nParsed number: %s\n", h); free_object(o); diff --git a/OpenSRF/src/srfsh/srfsh.c b/OpenSRF/src/srfsh/srfsh.c index 04460d42b9..ee963e203b 100644 --- a/OpenSRF/src/srfsh/srfsh.c +++ b/OpenSRF/src/srfsh/srfsh.c @@ -880,7 +880,7 @@ int do_math( int count, int style ) { params->push(params,new_object("2")); char* methods[] = { "add", "sub", "mult", "div" }; - char* answers[] = { "\"3\"", "\"-1\"", "\"2\"", "\"0.5\"" }; + char* answers[] = { "3", "-1", "2", "0.500000" }; float times[ count * 4 ]; memset(times,0,count*4); @@ -912,6 +912,7 @@ int do_math( int count, int style ) { osrf_message* omsg = osrf_app_session_request_recv( session, req_id, 5 ); + ftime(&t2); double start = ( (int)t1.time + ( ((float)t1.millitm) / 1000 ) ); -- 2.43.2