1 #include <opensrf/osrf_stack.h>
2 #include <opensrf/osrf_application.h>
4 /* the max number of oilsMessage blobs present in any one root packet */
5 #define OSRF_MAX_MSGS_PER_PACKET 256
6 // -----------------------------------------------------------------------------
8 static int osrf_stack_process( transport_client* client, int timeout, int* msg_received );
9 static void osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg );
10 static void _do_client( osrfAppSession*, osrfMessage* );
11 static void _do_server( osrfAppSession*, osrfMessage* );
13 /* tell osrfAppSession where the stack entry is */
14 int (*osrf_stack_entry_point) (transport_client*, int, int*) = &osrf_stack_process;
16 static int osrf_stack_process( transport_client* client, int timeout, int* msg_received ) {
17 if( !client ) return -1;
18 transport_message* msg = NULL;
19 if(msg_received) *msg_received = 0;
21 while( (msg = client_recv( client, timeout )) ) {
22 if(msg_received) *msg_received = 1;
23 osrfLogDebug( OSRF_LOG_MARK, "Received message from transport code from %s", msg->sender );
24 osrf_stack_transport_handler( msg, NULL );
29 osrfLogWarning(OSRF_LOG_MARK, "transport_client had trouble reading from the socket..");
33 if( ! client_connected( client ) ) return -1;
40 // -----------------------------------------------------------------------------
41 // Entry point into the stack
42 // -----------------------------------------------------------------------------
43 osrfAppSession* osrf_stack_transport_handler( transport_message* msg,
44 const char* my_service ) {
48 osrfLogSetXid(msg->osrf_xid);
50 osrfLogDebug( OSRF_LOG_MARK, "Transport handler received new message \nfrom %s "
51 "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
53 if( msg->is_error && ! msg->thread ) {
54 osrfLogWarning( OSRF_LOG_MARK, "!! Received jabber layer error for %s ... exiting\n", msg->sender );
59 if(! msg->thread && ! msg->is_error ) {
60 osrfLogWarning( OSRF_LOG_MARK, "Received a non-error message with no thread trace... dropping");
65 osrfAppSession* session = osrf_app_session_find_session( msg->thread );
67 if( !session && my_service )
68 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
76 osrfLogDebug( OSRF_LOG_MARK, "Session [%s] found or built", session->session_id );
78 osrf_app_session_set_remote( session, msg->sender );
79 osrfMessage* arr[OSRF_MAX_MSGS_PER_PACKET];
81 /* Convert the message body into one or more osrfMessages */
82 int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET);
84 osrfLogDebug( OSRF_LOG_MARK, "We received %d messages from %s", num_msgs, msg->sender );
86 double starttime = get_timestamp_millis();
89 for( i = 0; i < num_msgs; i++ ) {
91 /* if we've received a jabber layer error message (probably talking to
92 someone who no longer exists) and we're not talking to the original
93 remote id for this server, consider it a redirect and pass it up */
95 osrfLogWarning( OSRF_LOG_MARK, " !!! Received Jabber layer error message" );
97 if(strcmp(session->remote_id,session->orig_remote_id)) {
98 osrfLogWarning( OSRF_LOG_MARK, "Treating jabber error as redirect for tt [%d] "
99 "and session [%s]", arr[i]->thread_trace, session->session_id );
101 arr[i]->m_type = STATUS;
102 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
105 osrfLogWarning( OSRF_LOG_MARK, " * Jabber Error is for top level remote "
106 " id [%s], no one to send my message to! Cutting request short...", session->remote_id );
107 session->transport_error = 1;
112 if( session->type == OSRF_SESSION_CLIENT )
113 _do_client( session, arr[i] );
115 _do_server( session, arr[i] );
118 double duration = get_timestamp_millis() - starttime;
119 osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %f", duration);
122 osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
127 /** If we return a message, that message should be passed up the stack,
128 * if we return NULL, we're finished for now...
130 static void _do_client( osrfAppSession* session, osrfMessage* msg ) {
131 if(session == NULL || msg == NULL)
134 osrfMessage* further_msg = NULL;
136 if( msg->m_type == STATUS ) {
138 switch( msg->status_code ) {
141 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
142 session->state = OSRF_SESSION_CONNECTED;
143 osrfLogDebug( OSRF_LOG_MARK, "State: %x => %s => %d", session, session->session_id, session->state );
146 case OSRF_STATUS_COMPLETE:
147 osrf_app_session_set_complete( session, msg->thread_trace );
150 case OSRF_STATUS_CONTINUE:
151 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
154 case OSRF_STATUS_REDIRECTED:
155 osrf_app_session_reset_remote( session );
156 session->state = OSRF_SESSION_DISCONNECTED;
157 osrf_app_session_request_resend( session, msg->thread_trace );
160 case OSRF_STATUS_EXPFAILED:
161 osrf_app_session_reset_remote( session );
162 session->state = OSRF_SESSION_DISCONNECTED;
165 case OSRF_STATUS_TIMEOUT:
166 osrf_app_session_reset_remote( session );
167 session->state = OSRF_SESSION_DISCONNECTED;
168 osrf_app_session_request_resend( session, msg->thread_trace );
172 /* Replace the old message with a new one */
173 further_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
174 osrf_message_set_status_info( further_msg,
175 msg->status_name, msg->status_text, msg->status_code );
176 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with "
177 "the provided message code: %d, name %s. Passing UP.",
178 msg->status_code, msg->status_name );
179 further_msg->is_exception = 1;
180 osrf_app_session_set_complete( session, msg->thread_trace );
184 } else if( msg->m_type == RESULT ) {
189 osrfLogDebug( OSRF_LOG_MARK, "passing client message %d / session %s to app handler",
190 msg->thread_trace, session->session_id );
191 osrf_stack_application_handler( session, further_msg );
194 if(msg != further_msg)
195 osrfMessageFree(msg);
201 /** If we return a message, that message should be passed up the stack,
202 * if we return NULL, we're finished for now...
204 static void _do_server( osrfAppSession* session, osrfMessage* msg ) {
206 if(session == NULL || msg == NULL) return;
208 osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
210 osrfMessage* further_msg = NULL;
212 switch( msg->m_type ) {
218 /* session will be freed by the forker */
219 osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
220 session->state = OSRF_SESSION_DISCONNECTED;
224 osrfAppSessionStatus( session, OSRF_STATUS_OK,
225 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
226 session->state = OSRF_SESSION_CONNECTED;
231 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
232 "for session %s", msg->thread_trace, session->session_id );
237 osrfLogWarning( OSRF_LOG_MARK, "Server cannot handle message of type %d", msg->m_type );
238 session->state = OSRF_SESSION_DISCONNECTED;
243 osrfLogDebug( OSRF_LOG_MARK, "passing server message %d / session %s to app handler",
244 msg->thread_trace, session->session_id );
245 osrf_stack_application_handler( session, further_msg );
248 if(msg != further_msg)
249 osrfMessageFree(msg);
256 static void osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg ) {
257 if(session == NULL || msg == NULL) return;
259 if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
260 /* Enqueue the RESULT message to be processed later */
261 osrf_app_session_push_queue( session, msg );
263 else if(msg->m_type == REQUEST) {
264 char* method = msg->method_name;
265 char* app = session->remote_service;
266 jsonObject* params = msg->_params;
268 osrfAppRunMethod( app, method, session, msg->thread_trace, params );
269 osrfMessageFree(msg);