C libs for OpenSRF ingress tracking
authorBill Erickson <berick@esilibrary.com>
Fri, 13 Jan 2012 14:15:05 +0000 (09:15 -0500)
committerMike Rylander <mrylander@gmail.com>
Wed, 7 Mar 2012 19:50:43 +0000 (14:50 -0500)
osrfAppSessionSetIngress(<ingress>);

Signed-off-by: Bill Erickson <berick@esilibrary.com>
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 c8ad2f0..f0e2586 100644 (file)
@@ -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. */
index 592c039..76091d0 100644 (file)
@@ -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);
index 890108a..8a14d2a 100644 (file)
@@ -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;
@@ -370,6 +372,26 @@ char* osrf_app_session_set_locale( osrfAppSession* session, const char* 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.
        @return Pointer to the corresponding osrfAppSession if found, or NULL if not.
@@ -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);
 
index f6e2b62..364c875 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_ingress         = NULL;
 
        return msg;
 }
@@ -83,6 +84,25 @@ const char* osrf_message_set_locale( osrfMessage* msg, const char* 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.
        @return A pointer to the new default locale if successful, or NULL if not.
@@ -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
index 186c2a8..73793f8 100644 (file)
@@ -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