]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_stack.c
LP1999823: Bump libtool library version
[OpenSRF.git] / src / libopensrf / osrf_stack.c
1 #include <opensrf/osrf_stack.h>
2 #include <opensrf/osrf_application.h>
3
4 /**
5         @file osrf_stack.c
6         @brief Routines to receive and process input osrfMessages.
7 */
8
9 /* the max number of oilsMessage blobs present in any one root packet */
10 #define OSRF_MAX_MSGS_PER_PACKET 256
11 // -----------------------------------------------------------------------------
12
13 static void _do_client( osrfAppSession*, osrfMessage* );
14 static void _do_server( osrfAppSession*, osrfMessage* );
15
16 /**
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.
22
23         Read and process all available transport_messages from the socket of the specified
24         transport_client.  Pass each one through osrf_stack_transport().
25
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.
30
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
34         boolean to false.
35 */
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;
40
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 );
46                 timeout = 0;
47         }
48
49         if( client->error ) {
50                 osrfLogWarning(OSRF_LOG_MARK, "transport_client had trouble reading from the socket..");
51                 return -1;
52         }
53
54         if( ! client_connected( client ) ) return -1;
55
56         return 0;
57 }
58
59 // -----------------------------------------------------------------------------
60 // Entry point into the stack
61 // -----------------------------------------------------------------------------
62 /**
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.
67
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.
71
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.
75
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.
79 */
80 struct osrf_app_session_struct* osrf_stack_transport_handler( transport_message* msg,
81                 const char* my_service ) {
82
83         if(!msg) return NULL;
84
85         osrfLogSetXid(msg->osrf_xid);
86
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 );
89
90         if( msg->is_error && ! msg->thread ) {
91                 osrfLogWarning( OSRF_LOG_MARK,
92                                 "!! Received jabber layer error for %s ... exiting\n", msg->sender );
93                 message_free( msg );
94                 return NULL;
95         }
96
97         if(! msg->thread  && ! msg->is_error ) {
98                 osrfLogWarning( OSRF_LOG_MARK,
99                                 "Received a non-error message with no thread trace... dropping");
100                 message_free( msg );
101                 return NULL;
102         }
103
104         osrfAppSession* session = osrf_app_session_find_session( msg->thread );
105
106         if( !session && my_service )
107                 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
108
109         if( !session ) {
110                 message_free( msg );
111                 return NULL;
112         }
113
114         if(!msg->is_error)
115                 osrfLogDebug( OSRF_LOG_MARK, "Session [%s] found or built", session->session_id );
116
117         osrf_app_session_set_remote( session, msg->sender );
118         osrfMessage* arr[OSRF_MAX_MSGS_PER_PACKET];
119
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);
122
123         osrfLogDebug( OSRF_LOG_MARK, "We received %d messages from %s", num_msgs, msg->sender );
124
125         double starttime = get_timestamp_millis();
126
127         int i;
128         for( i = 0; i < num_msgs; i++ ) {
129
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 */
133                 if(msg->is_error) {
134                         osrfLogWarning( OSRF_LOG_MARK,  " !!! Received Jabber layer error message" );
135
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 );
139
140                                 arr[i]->m_type = STATUS;
141                                 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
142
143                         } else {
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;
148                                 break;
149                         }
150                 }
151
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);
155
156                 if( session->type == OSRF_SESSION_CLIENT )
157                         _do_client( session, arr[i] );
158                 else
159                         _do_server( session, arr[i] );
160         }
161
162         double duration = get_timestamp_millis() - starttime;
163         osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %f", duration);
164
165         message_free( msg );
166         osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
167
168         return session;
169 }
170
171 /**
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.
175
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,
178         to be handled later.
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.
182 */
183 static void _do_client( osrfAppSession* session, osrfMessage* msg ) {
184         if(session == NULL || msg == NULL)
185                 return;
186
187         if( msg->m_type == STATUS ) {
188
189                 switch( msg->status_code ) {
190
191                         case OSRF_STATUS_OK:
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);
199                                 break;
200
201                         case OSRF_STATUS_COMPLETE:
202                                 osrf_app_session_set_complete( session, msg->thread_trace );
203                                 osrfMessageFree(msg);
204                                 break;
205
206                         case OSRF_STATUS_CONTINUE:
207                                 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
208                                 osrfMessageFree(msg);
209                                 break;
210
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);
216                                 break;
217
218                         case OSRF_STATUS_EXPFAILED:
219                                 osrf_app_session_reset_remote( session );
220                                 session->state = OSRF_SESSION_DISCONNECTED;
221                                 osrfMessageFree(msg);
222                                 break;
223
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);
229                                 break;
230
231                         default:
232                         {
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 );
249                                 break;
250                         } // end default
251                 } // end switch
252
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 );
258
259         }
260
261         return;
262 }
263
264 /**
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.
268
269         Branch on the message type.  In particular, if it's a REQUEST, call the requested method.
270 */
271 static void _do_server( osrfAppSession* session, osrfMessage* msg ) {
272
273         if(session == NULL || msg == NULL) return;
274
275         osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
276
277         osrf_app_session_set_tz(session, msg->sender_tz);
278         osrf_app_session_set_locale(session, msg->sender_locale);
279
280         osrfLogDebug( OSRF_LOG_MARK, "Message has locale %s and tz %s", session->session_locale, session->session_tz );
281
282         switch( msg->m_type ) {
283
284                 case STATUS:
285                         break;
286
287                 case DISCONNECT:
288                         /* session will be freed by the forker */
289                         osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
290                         session->state = OSRF_SESSION_DISCONNECTED;
291                         break;
292
293                 case CONNECT:
294                         osrfAppSessionStatus( session, OSRF_STATUS_OK,
295                                         "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
296                         session->state = OSRF_SESSION_CONNECTED;
297                         break;
298
299                 case REQUEST:
300                         osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
301                                         "for session %s", msg->thread_trace, session->session_id );
302
303                         osrfAppRunMethod( session->remote_service, msg->method_name,
304                                 session, msg->thread_trace, msg->_params );
305
306                         break;
307
308                 default:
309                         osrfLogWarning( OSRF_LOG_MARK,
310                                         "Server cannot handle message of type %d", msg->m_type );
311                         session->state = OSRF_SESSION_DISCONNECTED;
312                         break;
313         }
314
315         osrfMessageFree(msg);
316         return;
317 }