From 7ec92808fea116ead923e475ad5242a54b688798 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Fri, 13 Jan 2012 09:15:05 -0500 Subject: [PATCH] C libs for OpenSRF ingress tracking osrfAppSessionSetIngress(); Signed-off-by: Bill Erickson Signed-off-by: Mike Rylander --- include/opensrf/osrf_app_session.h | 5 +++++ include/opensrf/osrf_message.h | 5 +++++ src/libopensrf/osrf_app_session.c | 26 ++++++++++++++++++++++++ src/libopensrf/osrf_message.c | 32 ++++++++++++++++++++++++++++++ src/libopensrf/osrf_stack.c | 4 ++++ 5 files changed, 72 insertions(+) diff --git a/include/opensrf/osrf_app_session.h b/include/opensrf/osrf_app_session.h index c8ad2f0..f0e2586 100644 --- a/include/opensrf/osrf_app_session.h +++ b/include/opensrf/osrf_app_session.h @@ -105,6 +105,11 @@ osrfAppSession* osrf_app_server_session_init( char* osrf_app_session_set_locale( osrfAppSession*, const char* ); +/* ingress used by all sessions until replaced */ +char* osrfAppSessionSetIngress( const char* ); + +const char* osrfAppSessionGetIngress(); + osrfAppSession* osrf_app_session_find_session( const char* session_id ); /* DEPRECATED; use osrfAppSessionSendRequest() instead. */ diff --git a/include/opensrf/osrf_message.h b/include/opensrf/osrf_message.h index 592c039..76091d0 100644 --- a/include/opensrf/osrf_message.h +++ b/include/opensrf/osrf_message.h @@ -92,11 +92,16 @@ struct osrf_message_struct { /** Magical LOCALE hint. */ char* sender_locale; + + /** Magical ingress hint. */ + char* sender_ingress; }; typedef struct osrf_message_struct osrfMessage; const char* osrf_message_set_locale( osrfMessage* msg, const char* locale ); +const char* osrfMessageSetIngress( osrfMessage* msg, const char* ingress ); + const char* osrf_message_set_default_locale( const char* locale ); const char* osrf_message_get_last_locale(void); diff --git a/src/libopensrf/osrf_app_session.c b/src/libopensrf/osrf_app_session.c index 890108a..8a14d2a 100644 --- a/src/libopensrf/osrf_app_session.c +++ b/src/libopensrf/osrf_app_session.c @@ -7,6 +7,8 @@ #include "opensrf/osrf_app_session.h" #include "opensrf/osrf_stack.h" +static char* current_ingress = NULL; + struct osrf_app_request_struct { /** The controlling session. */ struct osrf_app_session_struct* session; @@ -369,6 +371,26 @@ char* osrf_app_session_set_locale( osrfAppSession* session, const char* locale ) return session->session_locale; } +/** + @brief Install a copy of a ingress string as the new default. + @param session Pointer to the new strdup'ed default_ingress + @param ingress The ingress string to be copied and installed. +*/ +char* osrfAppSessionSetIngress(const char* ingress) { + if (!ingress) return NULL; + if(current_ingress) + free(current_ingress); + return current_ingress = strdup(ingress); +} + +/** + @brief Returns the current ingress value + @return A pointer to the installed copy of the ingress string +*/ +const char* osrfAppSessionGetIngress() { + return current_ingress; +} + /** @brief Find the osrfAppSession for a given session id. @param session_id The session id to look for. @@ -689,6 +711,10 @@ static int osrfAppSessionMakeLocaleRequest( osrf_message_set_locale(req_msg, session->session_locale); } + if (!current_ingress) + osrfAppSessionSetIngress("opensrf"); + osrfMessageSetIngress(req_msg, current_ingress); + if(params) { osrf_message_set_params(req_msg, params); diff --git a/src/libopensrf/osrf_message.c b/src/libopensrf/osrf_message.c index f6e2b62..364c875 100644 --- a/src/libopensrf/osrf_message.c +++ b/src/libopensrf/osrf_message.c @@ -42,6 +42,7 @@ osrfMessage* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol msg->_result_content = NULL; msg->method_name = NULL; msg->sender_locale = NULL; + msg->sender_ingress = NULL; return msg; } @@ -82,6 +83,25 @@ const char* osrf_message_set_locale( osrfMessage* msg, const char* locale ) { return msg->sender_locale = strdup( locale ); } +/** + @brief Set the ingress for a specified osrfMessage. + @param msg Pointer to the osrfMessage. + @param ingress Pointer to the ingress string to be installed in the osrfMessage. + @return Pointer to the new ingress string for the osrfMessage, or NULL if either + parameter is NULL. + + If no ingress is specified for an osrfMessage, we use the default ingress. + + Used for a REQUEST message. +*/ +const char* osrfMessageSetIngress( osrfMessage* msg, const char* ingress ) { + if( msg == NULL || ingress == NULL ) + return NULL; + if( msg->sender_ingress ) + free( msg->sender_ingress ); + return msg->sender_ingress = strdup( ingress ); +} + /** @brief Change the default locale. @param locale The new default locale. @@ -287,6 +307,9 @@ void osrfMessageFree( osrfMessage* msg ) { if( msg->sender_locale != NULL ) free(msg->sender_locale); + if( msg->sender_ingress != NULL ) + free(msg->sender_ingress); + if( msg->_params != NULL ) jsonObjectFree(msg->_params); @@ -361,6 +384,7 @@ char* osrf_message_serialize(const osrfMessage* msg) { The resulting jsonObject is a JSON_HASH with a classname of "osrfMessage", and the following keys: - "threadTrace" - "locale" + - "ingress" - "type" - "payload" (only for STATUS, REQUEST, and RESULT messages) @@ -398,6 +422,9 @@ jsonObject* osrfMessageToJSON( const osrfMessage* msg ) { jsonObjectSetKey(json, "locale", jsonNewObject(default_locale)); } + if (msg->sender_ingress != NULL) + jsonObjectSetKey(json, "ingress", jsonNewObject(msg->sender_ingress)); + switch(msg->m_type) { case CONNECT: @@ -622,6 +649,11 @@ static osrfMessage* deserialize_one_message( const jsonObject* obj ) { } } + tmp = jsonObjectGetKeyConst(obj, "ingress"); + if (tmp) { + osrfMessageSetIngress(msg, jsonObjectGetString(tmp)); + } + tmp = jsonObjectGetKeyConst( obj, "payload" ); if(tmp) { // Get method name and parameters for a REQUEST diff --git a/src/libopensrf/osrf_stack.c b/src/libopensrf/osrf_stack.c index 186c2a8..73793f8 100644 --- a/src/libopensrf/osrf_stack.c +++ b/src/libopensrf/osrf_stack.c @@ -149,6 +149,10 @@ struct osrf_app_session_struct* osrf_stack_transport_handler( transport_message* } } + // grab the ingress value from the first message. + // they will all be the same + if (i == 0) osrfAppSessionSetIngress(arr[i]->sender_ingress); + if( session->type == OSRF_SESSION_CLIENT ) _do_client( session, arr[i] ); else -- 2.43.2