1 #include <opensrf/osrf_stack.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_settings.h>
5 /* the max number of oilsMessage blobs present in any one root packet */
6 #define OSRF_MAX_MSGS_PER_PACKET 256
7 // -----------------------------------------------------------------------------
9 static int osrf_stack_process( transport_client* client, int timeout, int* msg_received );
10 static int osrf_stack_message_handler( osrfAppSession* session, osrfMessage* msg );
11 static int osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg );
12 static osrfMessage* _do_client( osrfAppSession*, osrfMessage* );
13 static osrfMessage* _do_server( osrfAppSession*, osrfMessage* );
15 /* tell osrfAppSession where the stack entry is */
16 int (*osrf_stack_entry_point) (transport_client*, int, int*) = &osrf_stack_process;
18 static int osrf_stack_process( transport_client* client, int timeout, int* msg_received ) {
19 if( !client ) return -1;
20 transport_message* msg = NULL;
21 if(msg_received) *msg_received = 0;
23 while( (msg = client_recv( client, timeout )) ) {
24 if(msg_received) *msg_received = 1;
25 osrfLogDebug( OSRF_LOG_MARK, "Received message from transport code from %s", msg->sender );
26 osrf_stack_transport_handler( msg, NULL );
31 osrfLogWarning(OSRF_LOG_MARK, "transport_client had trouble reading from the socket..");
35 if( ! client_connected( client ) ) return -1;
42 // -----------------------------------------------------------------------------
43 // Entry point into the stack
44 // -----------------------------------------------------------------------------
45 osrfAppSession* osrf_stack_transport_handler( transport_message* msg,
46 const char* my_service ) {
50 osrfLogSetXid(msg->osrf_xid);
52 osrfLogDebug( OSRF_LOG_MARK, "Transport handler received new message \nfrom %s "
53 "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
55 if( msg->is_error && ! msg->thread ) {
56 osrfLogWarning( OSRF_LOG_MARK, "!! Received jabber layer error for %s ... exiting\n", msg->sender );
61 if(! msg->thread && ! msg->is_error ) {
62 osrfLogWarning( OSRF_LOG_MARK, "Received a non-error message with no thread trace... dropping");
67 osrfAppSession* session = osrf_app_session_find_session( msg->thread );
70 session->type == OSRF_SESSION_SERVER &&
71 (strcmp(msg->sender, session->remote_id) != 0) ) {
73 /* unexpected client connection. See if we are running in "migratable" mode */
74 char* mable = osrf_settings_host_value("/apps/%s/migratable", session->remote_service);
79 osrfLogError("Connection to service %s from unknown client %s", session->remote_service, msg->sender);
84 if( !session && my_service )
85 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
93 osrfLogDebug( OSRF_LOG_MARK, "Session [%s] found or built", session->session_id );
95 osrf_app_session_set_remote( session, msg->sender );
96 osrfMessage* arr[OSRF_MAX_MSGS_PER_PACKET];
97 memset(arr, 0, sizeof(arr));
98 int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET);
100 osrfLogDebug( OSRF_LOG_MARK, "We received %d messages from %s", num_msgs, msg->sender );
102 double starttime = get_timestamp_millis();
105 for( i = 0; i != num_msgs; i++ ) {
107 /* if we've received a jabber layer error message (probably talking to
108 someone who no longer exists) and we're not talking to the original
109 remote id for this server, consider it a redirect and pass it up */
111 osrfLogWarning( OSRF_LOG_MARK, " !!! Received Jabber layer error message" );
113 if(strcmp(session->remote_id,session->orig_remote_id)) {
114 osrfLogWarning( OSRF_LOG_MARK, "Treating jabber error as redirect for tt [%d] "
115 "and session [%s]", arr[i]->thread_trace, session->session_id );
117 arr[i]->m_type = STATUS;
118 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
121 osrfLogWarning( OSRF_LOG_MARK, " * Jabber Error is for top level remote "
122 " id [%s], no one to send my message to! Cutting request short...", session->remote_id );
123 session->transport_error = 1;
128 osrf_stack_message_handler( session, arr[i] );
131 double duration = get_timestamp_millis() - starttime;
132 osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %f", duration);
135 osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
140 static int osrf_stack_message_handler( osrfAppSession* session, osrfMessage* msg ) {
141 if(session == NULL || msg == NULL)
144 osrfMessage* ret_msg = NULL;
146 if( session->type == OSRF_SESSION_CLIENT )
147 ret_msg = _do_client( session, msg );
149 ret_msg= _do_server( session, msg );
152 osrfLogDebug( OSRF_LOG_MARK, "passing message %d / session %s to app handler",
153 msg->thread_trace, session->session_id );
154 osrf_stack_application_handler( session, ret_msg );
156 osrfMessageFree(msg);
162 /** If we return a message, that message should be passed up the stack,
163 * if we return NULL, we're finished for now...
165 static osrfMessage* _do_client( osrfAppSession* session, osrfMessage* msg ) {
166 if(session == NULL || msg == NULL)
169 osrfMessage* new_msg;
171 if( msg->m_type == STATUS ) {
173 switch( msg->status_code ) {
176 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
177 session->state = OSRF_SESSION_CONNECTED;
178 osrfLogDebug( OSRF_LOG_MARK, "State: %x => %s => %d", session, session->session_id, session->state );
181 case OSRF_STATUS_COMPLETE:
182 osrf_app_session_set_complete( session, msg->thread_trace );
185 case OSRF_STATUS_CONTINUE:
186 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
189 case OSRF_STATUS_REDIRECTED:
190 osrf_app_session_reset_remote( session );
191 session->state = OSRF_SESSION_DISCONNECTED;
192 osrf_app_session_request_resend( session, msg->thread_trace );
195 case OSRF_STATUS_EXPFAILED:
196 osrf_app_session_reset_remote( session );
197 session->state = OSRF_SESSION_DISCONNECTED;
200 case OSRF_STATUS_TIMEOUT:
201 osrf_app_session_reset_remote( session );
202 session->state = OSRF_SESSION_DISCONNECTED;
203 osrf_app_session_request_resend( session, msg->thread_trace );
208 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
209 osrf_message_set_status_info( new_msg,
210 msg->status_name, msg->status_text, msg->status_code );
211 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with "
212 "the provided message code: %d, name %s. Passing UP.",
213 msg->status_code, msg->status_name );
214 new_msg->is_exception = 1;
215 osrf_app_session_set_complete( session, msg->thread_trace );
216 osrfMessageFree(msg);
222 } else if( msg->m_type == RESULT )
230 /** If we return a message, that message should be passed up the stack,
231 * if we return NULL, we're finished for now...
233 static osrfMessage* _do_server( osrfAppSession* session, osrfMessage* msg ) {
235 if(session == NULL || msg == NULL) return NULL;
237 osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
239 switch( msg->m_type ) {
245 /* session will be freed by the forker */
246 osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
247 session->state = OSRF_SESSION_DISCONNECTED;
251 osrfAppSessionStatus( session, OSRF_STATUS_OK,
252 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
253 session->state = OSRF_SESSION_CONNECTED;
258 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
259 "for session %s", msg->thread_trace, session->session_id );
263 osrfLogWarning( OSRF_LOG_MARK, "Server cannot handle message of type %d", msg->m_type );
264 session->state = OSRF_SESSION_DISCONNECTED;
272 static int osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg ) {
273 if(session == NULL || msg == NULL) return 0;
275 if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
276 osrf_app_session_push_queue( session, msg );
280 if(msg->m_type != REQUEST) return 1;
282 char* method = msg->method_name;
283 char* app = session->remote_service;
284 jsonObject* params = msg->_params;
286 osrfAppRunMethod( app, method, session, msg->thread_trace, params );
287 osrfMessageFree(msg);