From 453decb5e5800b1e6cdf4680d725157875bd56d1 Mon Sep 17 00:00:00 2001 From: scottmk Date: Thu, 29 Jul 2010 19:38:21 +0000 Subject: [PATCH] 1. Add a buffer to osrfAppSession structure; for future use 2. New function osrfSendTransportPayload(). This a repackaging of existing functionality pulled out into a separate function so that it can be reused in other contexts. These changes are preparation for future changes, and will have no visible effect by themselves. M include/opensrf/osrf_app_session.h M src/libopensrf/osrf_app_session.c git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1986 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/osrf_app_session.h | 9 ++++-- src/libopensrf/osrf_app_session.c | 52 ++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/include/opensrf/osrf_app_session.h b/include/opensrf/osrf_app_session.h index 36fc396..c8ad2f0 100644 --- a/include/opensrf/osrf_app_session.h +++ b/include/opensrf/osrf_app_session.h @@ -19,7 +19,7 @@ extern "C" { #endif -enum OSRF_SESSION_STATE { +enum OSRF_SESSION_STATE { OSRF_SESSION_CONNECTING, OSRF_SESSION_CONNECTED, OSRF_SESSION_DISCONNECTED @@ -80,7 +80,7 @@ struct osrf_app_session_struct { /** Callback function for freeing user's session data. */ void (*userDataFree) (void*); - int transport_error; + int transport_error; /** Hash table of pending requests. */ osrfAppRequest* request_hash[ OSRF_REQUEST_HASH_SIZE ]; @@ -88,6 +88,9 @@ struct osrf_app_session_struct { /** Boolean: true if the app wants to terminate the process. Typically this means that */ /** a drone has lost its database connection and can therefore no longer function. */ int panic; + + /** Buffer used by server drone to collect outbound response messages */ + growing_buffer* outbuf; }; typedef struct osrf_app_session_struct osrfAppSession; @@ -124,6 +127,8 @@ void osrf_app_session_request_finish( osrfAppSession* session, int request_id ); int osrf_app_session_request_resend( osrfAppSession*, int request_id ); +int osrfSendTransportPayload( osrfAppSession* session, const char* payload ); + void osrf_app_session_reset_remote( osrfAppSession* ); void osrf_app_session_set_remote( osrfAppSession* session, const char* remote_id ); diff --git a/src/libopensrf/osrf_app_session.c b/src/libopensrf/osrf_app_session.c index ca5d14e..ac2b4c0 100644 --- a/src/libopensrf/osrf_app_session.c +++ b/src/libopensrf/osrf_app_session.c @@ -479,6 +479,7 @@ osrfAppSession* osrfAppSessionClientInit( const char* remote_service ) { session->session_locale = NULL; session->transport_error = 0; session->panic = 0; + session->outbuf = NULL; // Not used by client #ifdef ASSUME_STATELESS session->stateless = 1; @@ -560,7 +561,8 @@ osrfAppSession* osrf_app_server_session_init( // to the compile-time macro ASSUME_STATELESS. int stateless = 0; char* statel = osrf_settings_host_value("/apps/%s/stateless", our_app ); - if(statel) stateless = atoi(statel); + if( statel ) + stateless = atoi( statel ); free(statel); session->remote_id = strdup(remote_id); @@ -582,12 +584,16 @@ osrfAppSession* osrf_app_server_session_init( session->userData = NULL; session->userDataFree = NULL; + session->transport_error = 0; // Initialize the hash table int i; for( i = 0; i < OSRF_REQUEST_HASH_SIZE; ++i ) session->request_hash[ i ] = NULL; + session->panic = 0; + session->outbuf = buffer_init( 4096 ); + _osrf_app_session_push_session( session ); return session; } @@ -952,28 +958,42 @@ static int osrfAppSessionSendBatch( osrfAppSession* session, osrfMessage* msgs[] } } - // Bundle all the osrfMessages into a single transport_message, then send it. + // Translate the collection of osrfMessages into a JSON array char* string = osrfMessageSerializeBatch(msgs, size); + // Send the JSON as the payload of a transport_message if( string ) { + retval = osrfSendTransportPayload( session, string ); + free(string); + } + + return retval; +} - transport_message* t_msg = message_init( - string, "", session->session_id, session->remote_id, NULL ); - message_set_osrf_xid( t_msg, osrfLogGetXid() ); +/** + @brief Wrap a given string in a transport message and send it. + @param session Pointer to the osrfAppSession responsible for sending the message(s). + @param payload A string to be sent via Jabber. + @return 0 upon success, or -1 upon failure. - retval = client_send_message( session->transport_handle, t_msg ); - if( retval ) - osrfLogError(OSRF_LOG_MARK, "client_send_message failed"); + In practice the payload is normally a JSON string, but this function assumes nothing + about it. +*/ +int osrfSendTransportPayload( osrfAppSession* session, const char* payload ) { + transport_message* t_msg = message_init( + payload, "", session->session_id, session->remote_id, NULL ); + message_set_osrf_xid( t_msg, osrfLogGetXid() ); - osrfLogInfo(OSRF_LOG_MARK, "[%s] sent %d bytes of data to %s", - session->remote_service, strlen(string), t_msg->recipient ); + int retval = client_send_message( session->transport_handle, t_msg ); + if( retval ) + osrfLogError( OSRF_LOG_MARK, "client_send_message failed" ); - osrfLogDebug(OSRF_LOG_MARK, "Sent: %s", string ); + osrfLogInfo(OSRF_LOG_MARK, "[%s] sent %d bytes of data to %s", + session->remote_service, strlen( payload ), t_msg->recipient ); - free(string); - message_free( t_msg ); - } + osrfLogDebug( OSRF_LOG_MARK, "Sent: %s", payload ); + message_free( t_msg ); return retval; } @@ -1077,6 +1097,10 @@ void osrfAppSessionFree( osrfAppSession* session ){ app = next; } } + + if( session->outbuf ) + buffer_free( session->outbuf ); + free(session); } -- 2.43.2