Teach C code to pull the incoming TZ and apply it to outgoing messages
authorMike Rylander <mrylander@gmail.com>
Mon, 3 Aug 2015 17:27:56 +0000 (13:27 -0400)
committerMike Rylander <mrylander@gmail.com>
Tue, 4 Aug 2015 16:13:41 +0000 (12:13 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
include/opensrf/osrf_app_session.h
include/opensrf/osrf_message.h
src/libopensrf/osrf_app_session.c
src/libopensrf/osrf_message.c
src/libopensrf/osrf_stack.c

index f0e2586..57d9092 100644 (file)
@@ -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* );
 
index 76091d0..14b601b 100644 (file)
@@ -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*,
index 8a14d2a..3181af6 100644 (file)
@@ -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);
index 3681dfa..9de5e0f 100644 (file)
@@ -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
index 73793f8..e176b9e 100644 (file)
@@ -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: