1 #include <opensrf/osrf_stack.h>
2 #include <opensrf/osrf_application.h>
6 @brief Routines to receive and process input osrfMessages.
9 /* the max number of oilsMessage blobs present in any one root packet */
10 #define OSRF_MAX_MSGS_PER_PACKET 256
11 // -----------------------------------------------------------------------------
13 static void _do_client( osrfAppSession*, osrfMessage* );
14 static void _do_server( osrfAppSession*, osrfMessage* );
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 occurs), 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 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_message into one or more osrfMessages, and process each one.
64 @param msg Pointer to 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 struct osrf_app_session_struct* 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 @brief Acting as a client, process an incoming osrfMessage.
169 @param session Pointer to the osrfAppSession to which the message pertains.
170 @param msg Pointer to the osrfMessage.
172 What we do with the message depends on the combination of message type and status code:
173 - If it's a RESULT message, add it to the message queue of the appropriate app session,
175 - If it's a STATUS message, handle it according to its status code and return NULL --
176 unless it has an unexpected status code, in which case add it to the message queue of
177 the appropriate app session, to be handled later.
179 static void _do_client( osrfAppSession* session, osrfMessage* msg ) {
180 if(session == NULL || msg == NULL)
183 if( msg->m_type == STATUS ) {
185 switch( msg->status_code ) {
188 // This combination of message type and status code comes
189 // only from the router, in response to a CONNECT message.
190 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
191 session->state = OSRF_SESSION_CONNECTED;
192 osrfLogDebug( OSRF_LOG_MARK, "State: %x => %s => %d", session,
193 session->session_id, session->state );
194 osrfMessageFree(msg);
197 case OSRF_STATUS_COMPLETE:
198 osrf_app_session_set_complete( session, msg->thread_trace );
199 osrfMessageFree(msg);
202 case OSRF_STATUS_CONTINUE:
203 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
204 osrfMessageFree(msg);
207 case OSRF_STATUS_REDIRECTED:
208 osrf_app_session_reset_remote( session );
209 session->state = OSRF_SESSION_DISCONNECTED;
210 osrf_app_session_request_resend( session, msg->thread_trace );
211 osrfMessageFree(msg);
214 case OSRF_STATUS_EXPFAILED:
215 osrf_app_session_reset_remote( session );
216 session->state = OSRF_SESSION_DISCONNECTED;
217 osrfMessageFree(msg);
220 case OSRF_STATUS_TIMEOUT:
221 osrf_app_session_reset_remote( session );
222 session->state = OSRF_SESSION_DISCONNECTED;
223 osrf_app_session_request_resend( session, msg->thread_trace );
224 osrfMessageFree(msg);
229 /* Replace the old message with a new one */
230 osrfMessage* new_msg = osrf_message_init(
231 RESULT, msg->thread_trace, msg->protocol );
232 osrf_message_set_status_info( new_msg,
233 msg->status_name, msg->status_text, msg->status_code );
234 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with "
235 "the provided message code: %d, name %s. Passing UP.",
236 msg->status_code, msg->status_name );
237 new_msg->is_exception = 1;
238 osrf_app_session_set_complete( session, msg->thread_trace );
239 osrfLogDebug( OSRF_LOG_MARK,
240 "passing client message %d / session %s to app handler",
241 msg->thread_trace, session->session_id );
242 osrfMessageFree(msg);
243 // Enqueue the new message to be processed later
244 osrf_app_session_push_queue( session, new_msg );
249 } else if( msg->m_type == RESULT ) {
250 osrfLogDebug( OSRF_LOG_MARK, "passing client message %d / session %s to app handler",
251 msg->thread_trace, session->session_id );
252 // Enqueue the RESULT message to be processed later
253 osrf_app_session_push_queue( session, msg );
261 @brief Acting as a server, process an incoming osrfMessage.
262 @param session Pointer to the osrfAppSession to which the message pertains.
263 @param msg Pointer to the osrfMessage.
265 Branch on the message type. In particular, if it's a REQUEST, call the requested method.
267 static void _do_server( osrfAppSession* session, osrfMessage* msg ) {
269 if(session == NULL || msg == NULL) return;
271 osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
273 switch( msg->m_type ) {
279 /* session will be freed by the forker */
280 osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
281 session->state = OSRF_SESSION_DISCONNECTED;
285 osrfAppSessionStatus( session, OSRF_STATUS_OK,
286 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
287 session->state = OSRF_SESSION_CONNECTED;
291 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
292 "for session %s", msg->thread_trace, session->session_id );
294 osrfAppRunMethod( session->remote_service, msg->method_name,
295 session, msg->thread_trace, msg->_params );
300 osrfLogWarning( OSRF_LOG_MARK,
301 "Server cannot handle message of type %d", msg->m_type );
302 session->state = OSRF_SESSION_DISCONNECTED;
306 osrfMessageFree(msg);