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( OSRF_LOG_MARK, "Received message from transport code from %s", msg->sender );
16 osrf_stack_transport_handler( msg, NULL );
20 if( ! client_connected( client ) ) return -1;
27 // -----------------------------------------------------------------------------
28 // Entry point into the stack
29 // -----------------------------------------------------------------------------
30 osrfAppSession* osrf_stack_transport_handler( transport_message* msg, char* my_service ) {
34 osrfLogDebug( OSRF_LOG_MARK, "Transport handler received new message \nfrom %s "
35 "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
37 if( msg->is_error && ! msg->thread ) {
38 osrfLogWarning( OSRF_LOG_MARK, "!! Received jabber layer error for %s ... exiting\n", msg->sender );
43 if(! msg->thread && ! msg->is_error ) {
44 osrfLogWarning( OSRF_LOG_MARK, "Received a non-error message with no thread trace... dropping");
49 osrf_app_session* session = osrf_app_session_find_session( msg->thread );
51 if( !session && my_service )
52 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
54 if( !session ) return NULL;
57 osrfLogDebug( OSRF_LOG_MARK, "Session [%s] found or built", session->session_id );
59 osrf_app_session_set_remote( session, msg->sender );
60 osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
61 memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET );
62 int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET);
64 osrfLogDebug( OSRF_LOG_MARK, "We received %d messages from %s", num_msgs, msg->sender );
67 for( i = 0; i != num_msgs; i++ ) {
69 /* if we've received a jabber layer error message (probably talking to
70 someone who no longer exists) and we're not talking to the original
71 remote id for this server, consider it a redirect and pass it up */
73 osrfLogWarning( OSRF_LOG_MARK, " !!! Received Jabber layer error message" );
75 if(strcmp(session->remote_id,session->orig_remote_id)) {
76 osrfLogWarning( OSRF_LOG_MARK, "Treating jabber error as redirect for tt [%d] "
77 "and session [%s]", arr[i]->thread_trace, session->session_id );
79 arr[i]->m_type = STATUS;
80 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
83 osrfLogWarning( OSRF_LOG_MARK, " * Jabber Error is for top level remote id [%s], no one "
84 "to send my message too!!!", session->remote_id );
88 osrf_stack_message_handler( session, arr[i] );
92 osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
97 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
98 if(session == NULL || msg == NULL)
101 osrf_message* ret_msg = NULL;
103 if( session->type == OSRF_SESSION_CLIENT )
104 ret_msg = _do_client( session, msg );
106 ret_msg= _do_server( session, msg );
109 osrfLogDebug( OSRF_LOG_MARK, "passing message %d / session %s to app handler",
110 msg->thread_trace, session->session_id );
111 osrf_stack_application_handler( session, ret_msg );
113 osrf_message_free(msg);
119 /** If we return a message, that message should be passed up the stack,
120 * if we return NULL, we're finished for now...
122 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
123 if(session == NULL || msg == NULL)
126 osrf_message* new_msg;
128 if( msg->m_type == STATUS ) {
130 switch( msg->status_code ) {
133 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
134 session->state = OSRF_SESSION_CONNECTED;
135 osrfLogDebug( OSRF_LOG_MARK, "State: %x => %s => %d", session, session->session_id, session->state );
138 case OSRF_STATUS_COMPLETE:
139 osrf_app_session_set_complete( session, msg->thread_trace );
142 case OSRF_STATUS_CONTINUE:
143 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
146 case OSRF_STATUS_REDIRECTED:
147 osrf_app_session_reset_remote( session );
148 session->state = OSRF_SESSION_DISCONNECTED;
149 osrf_app_session_request_resend( session, msg->thread_trace );
152 case OSRF_STATUS_EXPFAILED:
153 osrf_app_session_reset_remote( session );
154 session->state = OSRF_SESSION_DISCONNECTED;
155 /* set the session to 'stateful' then resend */
156 // osrf_app_session_request_resend( session, msg->thread_trace );
159 case OSRF_STATUS_TIMEOUT:
160 osrf_app_session_reset_remote( session );
161 session->state = OSRF_SESSION_DISCONNECTED;
162 osrf_app_session_request_resend( session, msg->thread_trace );
167 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
168 osrf_message_set_status_info( new_msg,
169 msg->status_name, msg->status_text, msg->status_code );
170 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with "
171 "the provided message code: %d, name %s. Passing UP.",
172 msg->status_code, msg->status_name );
173 new_msg->is_exception = 1;
174 osrf_app_session_set_complete( session, msg->thread_trace );
175 osrf_message_free(msg);
181 } else if( msg->m_type == RESULT )
189 /** If we return a message, that message should be passed up the stack,
190 * if we return NULL, we're finished for now...
192 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
194 if(session == NULL || msg == NULL) return NULL;
196 osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
198 switch( msg->m_type ) {
204 /* session will be freed by the forker */
205 osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
206 session->state = OSRF_SESSION_DISCONNECTED;
210 osrfAppSessionStatus( session, OSRF_STATUS_OK,
211 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
212 session->state = OSRF_SESSION_CONNECTED;
217 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
218 "for session %s", msg->thread_trace, session->session_id );
222 osrfLogWarning( OSRF_LOG_MARK, "Server cannot handle message of type %d", msg->m_type );
223 session->state = OSRF_SESSION_DISCONNECTED;
232 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
234 if(session == NULL || msg == NULL) return 0;
236 if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
237 osrf_app_session_push_queue( session, msg );
241 if(msg->m_type != REQUEST) return 1;
243 char* method = msg->method_name;
244 char* app = session->remote_service;
245 jsonObject* params = msg->_params;
247 osrfAppRunMethod( app, method, session, msg->thread_trace, params );
249 osrfMessageFree(msg);