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, int*) = &osrf_stack_process;
10 int osrf_stack_process( transport_client* client, int timeout, int* msg_received ) {
11 if( !client ) return -1;
12 transport_message* msg = NULL;
13 if(msg_received) *msg_received = 0;
15 while( (msg = client_recv( client, timeout )) ) {
16 if(msg_received) *msg_received = 1;
17 osrfLogDebug( OSRF_LOG_MARK, "Received message from transport code from %s", msg->sender );
18 osrf_stack_transport_handler( msg, NULL );
23 osrfLogWarning(OSRF_LOG_MARK, "transport_client had trouble reading from the socket..");
27 if( ! client_connected( client ) ) return -1;
34 // -----------------------------------------------------------------------------
35 // Entry point into the stack
36 // -----------------------------------------------------------------------------
37 osrfAppSession* osrf_stack_transport_handler( transport_message* msg, char* my_service ) {
41 osrfLogDebug( OSRF_LOG_MARK, "Transport handler received new message \nfrom %s "
42 "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
44 if( msg->is_error && ! msg->thread ) {
45 osrfLogWarning( OSRF_LOG_MARK, "!! Received jabber layer error for %s ... exiting\n", msg->sender );
50 if(! msg->thread && ! msg->is_error ) {
51 osrfLogWarning( OSRF_LOG_MARK, "Received a non-error message with no thread trace... dropping");
56 osrf_app_session* session = osrf_app_session_find_session( msg->thread );
58 if( !session && my_service )
59 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
61 if( !session ) return NULL;
64 osrfLogDebug( OSRF_LOG_MARK, "Session [%s] found or built", session->session_id );
66 osrf_app_session_set_remote( session, msg->sender );
67 osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
68 memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET );
69 int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET);
71 osrfLogDebug( OSRF_LOG_MARK, "We received %d messages from %s", num_msgs, msg->sender );
73 double starttime = get_timestamp_millis();
76 for( i = 0; i != num_msgs; i++ ) {
78 /* if we've received a jabber layer error message (probably talking to
79 someone who no longer exists) and we're not talking to the original
80 remote id for this server, consider it a redirect and pass it up */
82 osrfLogWarning( OSRF_LOG_MARK, " !!! Received Jabber layer error message" );
84 if(strcmp(session->remote_id,session->orig_remote_id)) {
85 osrfLogWarning( OSRF_LOG_MARK, "Treating jabber error as redirect for tt [%d] "
86 "and session [%s]", arr[i]->thread_trace, session->session_id );
88 arr[i]->m_type = STATUS;
89 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
92 osrfLogWarning( OSRF_LOG_MARK, " * Jabber Error is for top level remote id [%s], no one "
93 "to send my message too!!!", session->remote_id );
97 osrf_stack_message_handler( session, arr[i] );
100 double duration = get_timestamp_millis() - starttime;
101 osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %lf", duration);
104 osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
109 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
110 if(session == NULL || msg == NULL)
113 osrf_message* ret_msg = NULL;
115 if( session->type == OSRF_SESSION_CLIENT )
116 ret_msg = _do_client( session, msg );
118 ret_msg= _do_server( session, msg );
121 osrfLogDebug( OSRF_LOG_MARK, "passing message %d / session %s to app handler",
122 msg->thread_trace, session->session_id );
123 osrf_stack_application_handler( session, ret_msg );
125 osrf_message_free(msg);
131 /** If we return a message, that message should be passed up the stack,
132 * if we return NULL, we're finished for now...
134 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
135 if(session == NULL || msg == NULL)
138 osrf_message* new_msg;
140 if( msg->m_type == STATUS ) {
142 switch( msg->status_code ) {
145 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
146 session->state = OSRF_SESSION_CONNECTED;
147 osrfLogDebug( OSRF_LOG_MARK, "State: %x => %s => %d", session, session->session_id, session->state );
150 case OSRF_STATUS_COMPLETE:
151 osrf_app_session_set_complete( session, msg->thread_trace );
154 case OSRF_STATUS_CONTINUE:
155 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
158 case OSRF_STATUS_REDIRECTED:
159 osrf_app_session_reset_remote( session );
160 session->state = OSRF_SESSION_DISCONNECTED;
161 osrf_app_session_request_resend( session, msg->thread_trace );
164 case OSRF_STATUS_EXPFAILED:
165 osrf_app_session_reset_remote( session );
166 session->state = OSRF_SESSION_DISCONNECTED;
169 case OSRF_STATUS_TIMEOUT:
170 osrf_app_session_reset_remote( session );
171 session->state = OSRF_SESSION_DISCONNECTED;
172 osrf_app_session_request_resend( session, msg->thread_trace );
177 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
178 osrf_message_set_status_info( new_msg,
179 msg->status_name, msg->status_text, msg->status_code );
180 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with "
181 "the provided message code: %d, name %s. Passing UP.",
182 msg->status_code, msg->status_name );
183 new_msg->is_exception = 1;
184 osrf_app_session_set_complete( session, msg->thread_trace );
185 osrf_message_free(msg);
191 } else if( msg->m_type == RESULT )
199 /** If we return a message, that message should be passed up the stack,
200 * if we return NULL, we're finished for now...
202 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
204 if(session == NULL || msg == NULL) return NULL;
206 osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
208 switch( msg->m_type ) {
214 /* session will be freed by the forker */
215 osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
216 session->state = OSRF_SESSION_DISCONNECTED;
220 osrfAppSessionStatus( session, OSRF_STATUS_OK,
221 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
222 session->state = OSRF_SESSION_CONNECTED;
227 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
228 "for session %s", msg->thread_trace, session->session_id );
232 osrfLogWarning( OSRF_LOG_MARK, "Server cannot handle message of type %d", msg->m_type );
233 session->state = OSRF_SESSION_DISCONNECTED;
241 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
242 if(session == NULL || msg == NULL) return 0;
244 if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
245 osrf_app_session_push_queue( session, msg );
249 if(msg->m_type != REQUEST) return 1;
251 char* method = msg->method_name;
252 char* app = session->remote_service;
253 jsonObject* params = msg->_params;
255 osrfAppRunMethod( app, method, session, msg->thread_trace, params );
256 osrfMessageFree(msg);