From 58cc9a0abdfb01c16ab7528947c7e8e2e4e0b81b Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Mon, 3 Aug 2015 13:27:56 -0400 Subject: [PATCH] Teach C code to pull the incoming TZ and apply it to outgoing messages Signed-off-by: Mike Rylander --- include/opensrf/osrf_app_session.h | 5 +++++ include/opensrf/osrf_message.h | 7 +++++++ src/libopensrf/osrf_app_session.c | 32 ++++++++++++++++++++++++++++++++ src/libopensrf/osrf_message.c | 33 +++++++++++++++++++++++++++++++++ src/libopensrf/osrf_stack.c | 5 +++++ 5 files changed, 82 insertions(+) diff --git a/include/opensrf/osrf_app_session.h b/include/opensrf/osrf_app_session.h index f0e2586..57d9092 100644 --- a/include/opensrf/osrf_app_session.h +++ b/include/opensrf/osrf_app_session.h @@ -74,6 +74,9 @@ struct osrf_app_session_struct { /** the current locale for this session. **/ char* session_locale; + /** the current TZ for this session. **/ + char* session_tz; + /** let the user use the session to store their own session data. */ void* userData; @@ -105,6 +108,8 @@ osrfAppSession* osrf_app_server_session_init( char* osrf_app_session_set_locale( osrfAppSession*, const char* ); +char* osrf_app_session_set_tz( osrfAppSession*, const char* ); + /* ingress used by all sessions until replaced */ char* osrfAppSessionSetIngress( const char* ); diff --git a/include/opensrf/osrf_message.h b/include/opensrf/osrf_message.h index 76091d0..14b601b 100644 --- a/include/opensrf/osrf_message.h +++ b/include/opensrf/osrf_message.h @@ -95,17 +95,24 @@ struct osrf_message_struct { /** Magical ingress hint. */ char* sender_ingress; + + /** Magical TZ hint. */ + char* sender_tz; }; typedef struct osrf_message_struct osrfMessage; const char* osrf_message_set_locale( osrfMessage* msg, const char* locale ); +const char* osrf_message_set_tz( osrfMessage* msg, const char* tz ); + 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); +const char* osrf_message_get_last_tz(void); + osrfMessage* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ); void osrf_message_set_status_info( osrfMessage*, diff --git a/src/libopensrf/osrf_app_session.c b/src/libopensrf/osrf_app_session.c index 8a14d2a..3181af6 100644 --- a/src/libopensrf/osrf_app_session.c +++ b/src/libopensrf/osrf_app_session.c @@ -372,6 +372,31 @@ char* osrf_app_session_set_locale( osrfAppSession* session, const char* locale ) } /** + @brief Install a copy of a TZ string in a specified session. + @param session Pointer to the osrfAppSession in which the TZ is to be installed. + @param TZ The TZ string to be copied and installed. + @return A pointer to the installed copy of the TZ string. +*/ +char* osrf_app_session_set_tz( osrfAppSession* session, const char* tz ) { + if (!session || !tz) + return NULL; + + if(session->session_tz) { + if( strlen(session->session_tz) >= strlen(tz) ) { + /* There's room available; just copy */ + strcpy(session->session_tz, tz); + } else { + free(session->session_tz); + session->session_tz = strdup( tz ); + } + } else { + session->session_tz = strdup( tz ); + } + + return session->session_tz; +} + +/** @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. @@ -499,6 +524,7 @@ osrfAppSession* osrfAppSessionClientInit( const char* remote_service ) { session->orig_remote_id = strdup(session->remote_id); session->remote_service = strdup(remote_service); session->session_locale = NULL; + session->session_tz = NULL; session->transport_error = 0; session->panic = 0; session->outbuf = NULL; // Not used by client @@ -603,6 +629,7 @@ osrfAppSession* osrf_app_server_session_init( session->state = OSRF_SESSION_DISCONNECTED; session->type = OSRF_SESSION_SERVER; session->session_locale = NULL; + session->session_tz = NULL; session->userData = NULL; session->userDataFree = NULL; @@ -711,6 +738,8 @@ static int osrfAppSessionMakeLocaleRequest( osrf_message_set_locale(req_msg, session->session_locale); } + osrf_message_set_tz(req_msg, session->session_tz); + if (!current_ingress) osrfAppSessionSetIngress("opensrf"); osrfMessageSetIngress(req_msg, current_ingress); @@ -1108,6 +1137,9 @@ void osrfAppSessionFree( osrfAppSession* session ){ if(session->session_locale) free(session->session_locale); + if(session->session_tz) + free(session->session_tz); + free(session->remote_id); free(session->orig_remote_id); free(session->session_id); diff --git a/src/libopensrf/osrf_message.c b/src/libopensrf/osrf_message.c index 3681dfa..9de5e0f 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_tz = NULL; msg->sender_ingress = NULL; return msg; @@ -84,6 +85,25 @@ const char* osrf_message_set_locale( osrfMessage* msg, const char* locale ) { } /** + @brief Set the TZ for a specified osrfMessage. + @param msg Pointer to the osrfMessage. + @param TZ Pointer to the TZ string to be installed in the osrfMessage. + @return Pointer to the new TZ string for the osrfMessage, or NULL if either + parameter is NULL. + + If no TZ is specified for an osrfMessage, we use the system TZ. + + Used for a REQUEST message. +*/ +const char* osrf_message_set_tz( osrfMessage* msg, const char* tz ) { + if( msg == NULL || tz == NULL ) + return NULL; + if( msg->sender_tz ) + free( msg->sender_tz ); + return msg->sender_tz = strdup( tz ); +} + +/** @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. @@ -307,6 +327,9 @@ void osrfMessageFree( osrfMessage* msg ) { if( msg->sender_locale != NULL ) free(msg->sender_locale); + if( msg->sender_tz != NULL ) + free(msg->sender_tz); + if( msg->sender_ingress != NULL ) free(msg->sender_ingress); @@ -384,6 +407,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" + - "tz" - "ingress" - "type" - "payload" (only for STATUS, REQUEST, and RESULT messages) @@ -422,6 +446,10 @@ jsonObject* osrfMessageToJSON( const osrfMessage* msg ) { jsonObjectSetKey(json, "locale", jsonNewObject(default_locale)); } + if (msg->sender_tz != NULL) { + jsonObjectSetKey(json, "tz", jsonNewObject(msg->sender_tz)); + } + if (msg->sender_ingress != NULL) jsonObjectSetKey(json, "ingress", jsonNewObject(msg->sender_ingress)); @@ -657,6 +685,11 @@ static osrfMessage* deserialize_one_message( const jsonObject* obj ) { osrfMessageSetIngress(msg, jsonObjectGetString(tmp)); } + tmp = jsonObjectGetKeyConst(obj, "tz"); + if (tmp) { + osrf_message_set_tz(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 73793f8..e176b9e 100644 --- a/src/libopensrf/osrf_stack.c +++ b/src/libopensrf/osrf_stack.c @@ -274,6 +274,11 @@ static void _do_server( osrfAppSession* session, osrfMessage* msg ) { osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type ); + osrf_app_session_set_tz(session, msg->sender_tz); + osrf_app_session_set_locale(session, msg->sender_locale); + + osrfLogDebug( OSRF_LOG_MARK, "Message has locale %s and tz %s", session->session_locale, session->session_tz ); + switch( msg->m_type ) { case STATUS: -- 2.11.0