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 void osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg );
10 static void _do_client( osrfAppSession*, osrfMessage* );
11 static void _do_server( osrfAppSession*, osrfMessage* );
13 /* tell osrfAppSession where the stack entry is */
14 int (*osrf_stack_entry_point) (transport_client*, int, int*) = &osrf_stack_process;
17 @brief Read and process available transport_messages for a transport_client.
18 @param client Pointer to the transport_client whose socket is to be read.
19 @param timeout How many seconds to wait for the first message.
20 @param msg_received A pointer through which to report whether a message was received.
21 @return 0 upon success (even if a timeout occurred) or -1 upon failure.
23 Read and process all available transport_messages from the socket of the specified
24 transport_client. Pass each one through osrf_stack_transport().
26 The timeout applies only to the first message. Any subsequent messages must be
27 available immediately. Don't wait for them, even if the timeout has not expired. In
28 theory, a sufficiently large backlog of input messages could keep you working past the
29 nominal expiration of the timeout.
31 The @a msg_received parameter points to an int owned by the calling code and used as
32 a boolean. Set it to true if you receive at least one transport_message, or to false
33 if you don't. A timeout is not treated as an error; it just means you must set that
36 static int osrf_stack_process( transport_client* client, int timeout, int* msg_received ) {
37 if( !client ) return -1;
38 transport_message* msg = NULL;
39 if(msg_received) *msg_received = 0;
41 // Loop through the available input messages
42 while( (msg = client_recv( client, timeout )) ) {
43 if(msg_received) *msg_received = 1;
44 osrfLogDebug( OSRF_LOG_MARK, "Received message from transport code from %s", msg->sender );
45 osrf_stack_transport_handler( msg, NULL );
50 osrfLogWarning(OSRF_LOG_MARK, "transport_client had trouble reading from the socket..");
54 if( ! client_connected( client ) ) return -1;
59 // -----------------------------------------------------------------------------
60 // Entry point into the stack
61 // -----------------------------------------------------------------------------
63 @brief Unpack a transport into one or more osrfMessages, and process each one.
64 @param msg The transport message to be unpacked and processed.
65 @param my_service Application name (optional).
66 @return Pointer to an osrfAppSession -- either a pre-existing one or a new one.
68 Look for an existing osrfAppSession with which the message is associated. Such a session
69 may already exist if, for example, you're a client waiting for a response from some other
70 application, or if you're a server that has opened a stateful session with a client.
72 If you can't find an existing session for the current message, and the @a my_service
73 parameter has provided an application name, then you're presumably a server receiving
74 something from a new client. Create an application server session to own the new message.
76 Barring various errors and malformations, extract one or more osrfMessages from the
77 transport_message. Pass each one to the appropriate routine for processing, depending
78 on whether you're acting as a client or as a server.
80 osrfAppSession* osrf_stack_transport_handler( transport_message* msg,
81 const char* my_service ) {
85 osrfLogSetXid(msg->osrf_xid);
87 osrfLogDebug( OSRF_LOG_MARK, "Transport handler received new message \nfrom %s "
88 "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
90 if( msg->is_error && ! msg->thread ) {
91 osrfLogWarning( OSRF_LOG_MARK,
92 "!! Received jabber layer error for %s ... exiting\n", msg->sender );
97 if(! msg->thread && ! msg->is_error ) {
98 osrfLogWarning( OSRF_LOG_MARK,
99 "Received a non-error message with no thread trace... dropping");
104 osrfAppSession* session = osrf_app_session_find_session( msg->thread );
106 if( !session && my_service )
107 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
115 osrfLogDebug( OSRF_LOG_MARK, "Session [%s] found or built", session->session_id );
117 osrf_app_session_set_remote( session, msg->sender );
118 osrfMessage* arr[OSRF_MAX_MSGS_PER_PACKET];
120 /* Convert the message body into one or more osrfMessages */
121 int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET);
123 osrfLogDebug( OSRF_LOG_MARK, "We received %d messages from %s", num_msgs, msg->sender );
125 double starttime = get_timestamp_millis();
128 for( i = 0; i < num_msgs; i++ ) {
130 /* if we've received a jabber layer error message (probably talking to
131 someone who no longer exists) and we're not talking to the original
132 remote id for this server, consider it a redirect and pass it up */
134 osrfLogWarning( OSRF_LOG_MARK, " !!! Received Jabber layer error message" );
136 if( strcmp( session->remote_id, session->orig_remote_id ) ) {
137 osrfLogWarning( OSRF_LOG_MARK, "Treating jabber error as redirect for tt [%d] "
138 "and session [%s]", arr[i]->thread_trace, session->session_id );
140 arr[i]->m_type = STATUS;
141 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
144 osrfLogWarning( OSRF_LOG_MARK, " * Jabber Error is for top level remote "
145 " id [%s], no one to send my message to! Cutting request short...",
146 session->remote_id );
147 session->transport_error = 1;
152 if( session->type == OSRF_SESSION_CLIENT )
153 _do_client( session, arr[i] );
155 _do_server( session, arr[i] );
158 double duration = get_timestamp_millis() - starttime;
159 osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %f", duration);
162 osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
168 If we return a message, that message should be passed up the stack,
169 if we return NULL, we're finished for now...
171 static void _do_client( osrfAppSession* session, osrfMessage* msg ) {
172 if(session == NULL || msg == NULL)
175 osrfMessage* further_msg = NULL;
177 if( msg->m_type == STATUS ) {
179 switch( msg->status_code ) {
182 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
183 session->state = OSRF_SESSION_CONNECTED;
184 osrfLogDebug( OSRF_LOG_MARK, "State: %x => %s => %d", session,
185 session->session_id, session->state );
188 case OSRF_STATUS_COMPLETE:
189 osrf_app_session_set_complete( session, msg->thread_trace );
192 case OSRF_STATUS_CONTINUE:
193 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
196 case OSRF_STATUS_REDIRECTED:
197 osrf_app_session_reset_remote( session );
198 session->state = OSRF_SESSION_DISCONNECTED;
199 osrf_app_session_request_resend( session, msg->thread_trace );
202 case OSRF_STATUS_EXPFAILED:
203 osrf_app_session_reset_remote( session );
204 session->state = OSRF_SESSION_DISCONNECTED;
207 case OSRF_STATUS_TIMEOUT:
208 osrf_app_session_reset_remote( session );
209 session->state = OSRF_SESSION_DISCONNECTED;
210 osrf_app_session_request_resend( session, msg->thread_trace );
214 /* Replace the old message with a new one */
215 further_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
216 osrf_message_set_status_info( further_msg,
217 msg->status_name, msg->status_text, msg->status_code );
218 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with "
219 "the provided message code: %d, name %s. Passing UP.",
220 msg->status_code, msg->status_name );
221 further_msg->is_exception = 1;
222 osrf_app_session_set_complete( session, msg->thread_trace );
226 } else if( msg->m_type == RESULT ) {
231 osrfLogDebug( OSRF_LOG_MARK, "passing client message %d / session %s to app handler",
232 msg->thread_trace, session->session_id );
233 osrf_stack_application_handler( session, further_msg );
236 if(msg != further_msg)
237 osrfMessageFree(msg);
244 If we return a message, that message should be passed up the stack,
245 if we return NULL, we're finished for now...
247 static void _do_server( osrfAppSession* session, osrfMessage* msg ) {
249 if(session == NULL || msg == NULL) return;
251 osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
253 osrfMessage* further_msg = NULL;
255 switch( msg->m_type ) {
261 /* session will be freed by the forker */
262 osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
263 session->state = OSRF_SESSION_DISCONNECTED;
267 osrfAppSessionStatus( session, OSRF_STATUS_OK,
268 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
269 session->state = OSRF_SESSION_CONNECTED;
274 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
275 "for session %s", msg->thread_trace, session->session_id );
280 osrfLogWarning( OSRF_LOG_MARK,
281 "Server cannot handle message of type %d", msg->m_type );
282 session->state = OSRF_SESSION_DISCONNECTED;
287 osrfLogDebug( OSRF_LOG_MARK, "passing server message %d / session %s to app handler",
288 msg->thread_trace, session->session_id );
289 osrf_stack_application_handler( session, further_msg );
292 if(msg != further_msg)
293 osrfMessageFree(msg);
300 static void osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg ) {
301 if(session == NULL || msg == NULL) return;
303 if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
304 /* Enqueue the RESULT message to be processed later */
305 osrf_app_session_push_queue( session, msg );
307 else if(msg->m_type == REQUEST) {
308 char* method = msg->method_name;
309 char* app = session->remote_service;
310 jsonObject* params = msg->_params;
312 osrfAppRunMethod( app, method, session, msg->thread_trace, params );
313 osrfMessageFree(msg);