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 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg );
10 static int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg );
11 static osrf_message* _do_client( osrf_app_session*, osrf_message* );
12 static osrf_message* _do_server( osrf_app_session*, osrf_message* );
14 /* tell osrf_app_session where the stack entry is */
15 int (*osrf_stack_entry_point) (transport_client*, int, int*) = &osrf_stack_process;
17 static int osrf_stack_process( transport_client* client, int timeout, int* msg_received ) {
18 if( !client ) return -1;
19 transport_message* msg = NULL;
20 if(msg_received) *msg_received = 0;
22 while( (msg = client_recv( client, timeout )) ) {
23 if(msg_received) *msg_received = 1;
24 osrfLogDebug( OSRF_LOG_MARK, "Received message from transport code from %s", msg->sender );
25 osrf_stack_transport_handler( msg, NULL );
30 osrfLogWarning(OSRF_LOG_MARK, "transport_client had trouble reading from the socket..");
34 if( ! client_connected( client ) ) return -1;
41 // -----------------------------------------------------------------------------
42 // Entry point into the stack
43 // -----------------------------------------------------------------------------
44 osrfAppSession* osrf_stack_transport_handler( transport_message* msg,
45 const char* my_service ) {
49 osrfLogSetXid(msg->osrf_xid);
51 osrfLogDebug( OSRF_LOG_MARK, "Transport handler received new message \nfrom %s "
52 "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
54 if( msg->is_error && ! msg->thread ) {
55 osrfLogWarning( OSRF_LOG_MARK, "!! Received jabber layer error for %s ... exiting\n", msg->sender );
60 if(! msg->thread && ! msg->is_error ) {
61 osrfLogWarning( OSRF_LOG_MARK, "Received a non-error message with no thread trace... dropping");
66 osrf_app_session* session = osrf_app_session_find_session( msg->thread );
68 if( !session && my_service )
69 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
77 osrfLogDebug( OSRF_LOG_MARK, "Session [%s] found or built", session->session_id );
79 osrf_app_session_set_remote( session, msg->sender );
80 osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
81 memset(arr, 0, sizeof(arr));
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 id [%s], no one "
106 "to send my message to!!!", session->remote_id );
110 osrf_stack_message_handler( session, arr[i] );
113 double duration = get_timestamp_millis() - starttime;
114 osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %f", duration);
117 osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
122 static int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
123 if(session == NULL || msg == NULL)
126 osrf_message* ret_msg = NULL;
128 if( session->type == OSRF_SESSION_CLIENT )
129 ret_msg = _do_client( session, msg );
131 ret_msg= _do_server( session, msg );
134 osrfLogDebug( OSRF_LOG_MARK, "passing message %d / session %s to app handler",
135 msg->thread_trace, session->session_id );
136 osrf_stack_application_handler( session, ret_msg );
138 osrf_message_free(msg);
144 /** If we return a message, that message should be passed up the stack,
145 * if we return NULL, we're finished for now...
147 static osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
148 if(session == NULL || msg == NULL)
151 osrf_message* new_msg;
153 if( msg->m_type == STATUS ) {
155 switch( msg->status_code ) {
158 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
159 session->state = OSRF_SESSION_CONNECTED;
160 osrfLogDebug( OSRF_LOG_MARK, "State: %x => %s => %d", session, session->session_id, session->state );
163 case OSRF_STATUS_COMPLETE:
164 osrf_app_session_set_complete( session, msg->thread_trace );
167 case OSRF_STATUS_CONTINUE:
168 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
171 case OSRF_STATUS_REDIRECTED:
172 osrf_app_session_reset_remote( session );
173 session->state = OSRF_SESSION_DISCONNECTED;
174 osrf_app_session_request_resend( session, msg->thread_trace );
177 case OSRF_STATUS_EXPFAILED:
178 osrf_app_session_reset_remote( session );
179 session->state = OSRF_SESSION_DISCONNECTED;
182 case OSRF_STATUS_TIMEOUT:
183 osrf_app_session_reset_remote( session );
184 session->state = OSRF_SESSION_DISCONNECTED;
185 osrf_app_session_request_resend( session, msg->thread_trace );
190 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
191 osrf_message_set_status_info( new_msg,
192 msg->status_name, msg->status_text, msg->status_code );
193 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with "
194 "the provided message code: %d, name %s. Passing UP.",
195 msg->status_code, msg->status_name );
196 new_msg->is_exception = 1;
197 osrf_app_session_set_complete( session, msg->thread_trace );
198 osrf_message_free(msg);
204 } else if( msg->m_type == RESULT )
212 /** If we return a message, that message should be passed up the stack,
213 * if we return NULL, we're finished for now...
215 static osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
217 if(session == NULL || msg == NULL) return NULL;
219 osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
221 switch( msg->m_type ) {
227 /* session will be freed by the forker */
228 osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
229 session->state = OSRF_SESSION_DISCONNECTED;
233 osrfAppSessionStatus( session, OSRF_STATUS_OK,
234 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
235 session->state = OSRF_SESSION_CONNECTED;
240 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
241 "for session %s", msg->thread_trace, session->session_id );
245 osrfLogWarning( OSRF_LOG_MARK, "Server cannot handle message of type %d", msg->m_type );
246 session->state = OSRF_SESSION_DISCONNECTED;
254 static int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
255 if(session == NULL || msg == NULL) return 0;
257 if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
258 osrf_app_session_push_queue( session, msg );
262 if(msg->m_type != REQUEST) return 1;
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);