1 #include "osrf_stack.h"
2 #include "osrf_application.h"
4 osrf_message* _do_client( osrf_app_session*, osrf_message* );
5 osrf_message* _do_server( osrf_app_session*, osrf_message* );
7 /* tell osrf_app_session where the stack entry is */
8 int (*osrf_stack_entry_point) (transport_client*, int) = &osrf_stack_process;
10 int osrf_stack_process( transport_client* client, int timeout ) {
11 if( !client ) return -1;
12 transport_message* msg = NULL;
14 while( (msg = client_recv( client, timeout )) ) {
15 osrfLogDebug( "Received message from transport code from %s", msg->sender );
16 osrf_stack_transport_handler( msg, NULL );
25 // -----------------------------------------------------------------------------
26 // Entry point into the stack
27 // -----------------------------------------------------------------------------
28 osrfAppSession* osrf_stack_transport_handler( transport_message* msg, char* my_service ) {
32 osrfLogDebug( "Transport handler received new message \nfrom %s "
33 "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
35 if( msg->is_error && ! msg->thread ) {
36 osrfLogWarning("!! Received jabber layer error for %s ... exiting\n", msg->sender );
41 if(! msg->thread && ! msg->is_error ) {
42 osrfLogWarning("Received a non-error message with no thread trace... dropping");
47 osrf_app_session* session = osrf_app_session_find_session( msg->thread );
49 if( !session && my_service )
50 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
52 if( !session ) return NULL;
55 osrfLogDebug("Session [%s] found or built", session->session_id );
57 osrf_app_session_set_remote( session, msg->sender );
58 osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
59 memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET );
60 int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET);
62 osrfLogDebug( "We received %d messages from %s", num_msgs, msg->sender );
65 for( i = 0; i != num_msgs; i++ ) {
67 /* if we've received a jabber layer error message (probably talking to
68 someone who no longer exists) and we're not talking to the original
69 remote id for this server, consider it a redirect and pass it up */
71 osrfLogWarning( " !!! Received Jabber layer error message" );
73 if(strcmp(session->remote_id,session->orig_remote_id)) {
74 osrfLogWarning( "Treating jabber error as redirect for tt [%d] "
75 "and session [%s]", arr[i]->thread_trace, session->session_id );
77 arr[i]->m_type = STATUS;
78 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
81 osrfLogWarning(" * Jabber Error is for top level remote id [%s], no one "
82 "to send my message too!!!", session->remote_id );
86 osrf_stack_message_handler( session, arr[i] );
90 osrfLogDebug("after msg delete");
95 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
96 if(session == NULL || msg == NULL)
99 osrf_message* ret_msg = NULL;
101 if( session->type == OSRF_SESSION_CLIENT )
102 ret_msg = _do_client( session, msg );
104 ret_msg= _do_server( session, msg );
107 osrfLogDebug("passing message %d / session %s to app handler",
108 msg->thread_trace, session->session_id );
109 osrf_stack_application_handler( session, ret_msg );
111 osrf_message_free(msg);
117 /** If we return a message, that message should be passed up the stack,
118 * if we return NULL, we're finished for now...
120 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
121 if(session == NULL || msg == NULL)
124 osrf_message* new_msg;
126 if( msg->m_type == STATUS ) {
128 switch( msg->status_code ) {
131 osrfLogDebug("We connected successfully");
132 session->state = OSRF_SESSION_CONNECTED;
133 osrfLogDebug( "State: %x => %s => %d", session, session->session_id, session->state );
136 case OSRF_STATUS_COMPLETE:
137 osrf_app_session_set_complete( session, msg->thread_trace );
140 case OSRF_STATUS_CONTINUE:
141 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
144 case OSRF_STATUS_REDIRECTED:
145 osrf_app_session_reset_remote( session );
146 session->state = OSRF_SESSION_DISCONNECTED;
147 osrf_app_session_request_resend( session, msg->thread_trace );
150 case OSRF_STATUS_EXPFAILED:
151 osrf_app_session_reset_remote( session );
152 session->state = OSRF_SESSION_DISCONNECTED;
153 /* set the session to 'stateful' then resend */
154 // osrf_app_session_request_resend( session, msg->thread_trace );
157 case OSRF_STATUS_TIMEOUT:
158 osrf_app_session_reset_remote( session );
159 session->state = OSRF_SESSION_DISCONNECTED;
160 osrf_app_session_request_resend( session, msg->thread_trace );
165 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
166 osrf_message_set_status_info( new_msg,
167 msg->status_name, msg->status_text, msg->status_code );
168 osrfLogWarning("The stack doesn't know what to do with "
169 "the provided message code: %d, name %s. Passing UP.",
170 msg->status_code, msg->status_name );
171 new_msg->is_exception = 1;
172 osrf_app_session_set_complete( session, msg->thread_trace );
173 osrf_message_free(msg);
179 } else if( msg->m_type == RESULT )
187 /** If we return a message, that message should be passed up the stack,
188 * if we return NULL, we're finished for now...
190 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
192 if(session == NULL || msg == NULL) return NULL;
194 osrfLogDebug("Server received message of type %d", msg->m_type );
196 switch( msg->m_type ) {
202 /* session will be freed by the forker */
206 osrfAppSessionStatus( session, OSRF_STATUS_OK,
207 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
212 osrfLogDebug("server passing message %d to application handler "
213 "for session %s", msg->thread_trace, session->session_id );
217 osrfLogWarning("Server cannot handle message of type %d", msg->m_type );
226 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
228 if(session == NULL || msg == NULL) return 0;
230 if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
231 osrf_app_session_push_queue( session, msg );
235 if(msg->m_type != REQUEST) return 1;
237 char* method = msg->method_name;
238 char* app = session->remote_service;
239 jsonObject* params = msg->_params;
241 osrfAppRunMethod( app, method, session, msg->thread_trace, params );
243 osrfMessageFree(msg);