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 // grab the ingress value from the first message.
153 // they will all be the same
154 if (i == 0) osrfAppSessionSetIngress(arr[i]->sender_ingress);
156 if( session->type == OSRF_SESSION_CLIENT )
157 _do_client( session, arr[i] );
159 _do_server( session, arr[i] );
162 double duration = get_timestamp_millis() - starttime;
163 osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %f", duration);
166 osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
172 @brief Acting as a client, process an incoming osrfMessage.
173 @param session Pointer to the osrfAppSession to which the message pertains.
174 @param msg Pointer to the osrfMessage.
176 What we do with the message depends on the combination of message type and status code:
177 - If it's a RESULT message, add it to the message queue of the appropriate app session,
179 - If it's a STATUS message, handle it according to its status code and return NULL --
180 unless it has an unexpected status code, in which case add it to the message queue of
181 the appropriate app session, to be handled later.
183 static void _do_client( osrfAppSession* session, osrfMessage* msg ) {
184 if(session == NULL || msg == NULL)
187 if( msg->m_type == STATUS ) {
189 switch( msg->status_code ) {
192 // This combination of message type and status code comes
193 // only from the router, in response to a CONNECT message.
194 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
195 session->state = OSRF_SESSION_CONNECTED;
196 osrfLogDebug( OSRF_LOG_MARK, "State: %x => %s => %d", session,
197 session->session_id, session->state );
198 osrfMessageFree(msg);
201 case OSRF_STATUS_COMPLETE:
202 osrf_app_session_set_complete( session, msg->thread_trace );
203 osrfMessageFree(msg);
206 case OSRF_STATUS_CONTINUE:
207 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
208 osrfMessageFree(msg);
211 case OSRF_STATUS_REDIRECTED:
212 osrf_app_session_reset_remote( session );
213 session->state = OSRF_SESSION_DISCONNECTED;
214 osrf_app_session_request_resend( session, msg->thread_trace );
215 osrfMessageFree(msg);
218 case OSRF_STATUS_EXPFAILED:
219 osrf_app_session_reset_remote( session );
220 session->state = OSRF_SESSION_DISCONNECTED;
221 osrfMessageFree(msg);
224 case OSRF_STATUS_TIMEOUT:
225 osrf_app_session_reset_remote( session );
226 session->state = OSRF_SESSION_DISCONNECTED;
227 osrf_app_session_request_resend( session, msg->thread_trace );
228 osrfMessageFree(msg);
233 /* Replace the old message with a new one */
234 osrfMessage* new_msg = osrf_message_init(
235 RESULT, msg->thread_trace, msg->protocol );
236 osrf_message_set_status_info( new_msg,
237 msg->status_name, msg->status_text, msg->status_code );
238 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with "
239 "the provided message code: %d, name %s. Passing UP.",
240 msg->status_code, msg->status_name );
241 new_msg->is_exception = 1;
242 osrf_app_session_set_complete( session, msg->thread_trace );
243 osrfLogDebug( OSRF_LOG_MARK,
244 "passing client message %d / session %s to app handler",
245 msg->thread_trace, session->session_id );
246 osrfMessageFree(msg);
247 // Enqueue the new message to be processed later
248 osrf_app_session_push_queue( session, new_msg );
253 } else if( msg->m_type == RESULT ) {
254 osrfLogDebug( OSRF_LOG_MARK, "passing client message %d / session %s to app handler",
255 msg->thread_trace, session->session_id );
256 // Enqueue the RESULT message to be processed later
257 osrf_app_session_push_queue( session, msg );
265 @brief Acting as a server, process an incoming osrfMessage.
266 @param session Pointer to the osrfAppSession to which the message pertains.
267 @param msg Pointer to the osrfMessage.
269 Branch on the message type. In particular, if it's a REQUEST, call the requested method.
271 static void _do_server( osrfAppSession* session, osrfMessage* msg ) {
273 if(session == NULL || msg == NULL) return;
275 osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
277 switch( msg->m_type ) {
283 /* session will be freed by the forker */
284 osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
285 session->state = OSRF_SESSION_DISCONNECTED;
289 osrfAppSessionStatus( session, OSRF_STATUS_OK,
290 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
291 session->state = OSRF_SESSION_CONNECTED;
295 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
296 "for session %s", msg->thread_trace, session->session_id );
298 osrfAppRunMethod( session->remote_service, msg->method_name,
299 session, msg->thread_trace, msg->_params );
304 osrfLogWarning( OSRF_LOG_MARK,
305 "Server cannot handle message of type %d", msg->m_type );
306 session->state = OSRF_SESSION_DISCONNECTED;
310 osrfMessageFree(msg);