]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_stack.c
86a421b561a6cafc06d2df46b08c7a63c1848843
[OpenSRF.git] / src / libopensrf / osrf_stack.c
1 #include <opensrf/osrf_stack.h>
2 #include <opensrf/osrf_application.h>
3
4 /* the max number of oilsMessage blobs present in any one root packet */
5 #define OSRF_MAX_MSGS_PER_PACKET 256
6 // -----------------------------------------------------------------------------
7
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* );
12
13 /* tell osrfAppSession where the stack entry is */
14 int (*osrf_stack_entry_point) (transport_client*, int, int*)  = &osrf_stack_process;
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 occurred) 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 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;
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 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.
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 osrfAppSession* 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                 if( session->type == OSRF_SESSION_CLIENT )
153                         _do_client( session, arr[i] );
154                 else
155                         _do_server( session, arr[i] );
156         }
157
158         double duration = get_timestamp_millis() - starttime;
159         osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %f", duration);
160
161         message_free( msg );
162         osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
163
164         return session;
165 }
166
167 /**
168         If we return a message, that message should be passed up the stack,
169         if we return NULL, we're finished for now...
170 */
171 static void _do_client( osrfAppSession* session, osrfMessage* msg ) {
172         if(session == NULL || msg == NULL)
173                 return;
174
175         osrfMessage* further_msg = NULL;
176
177         if( msg->m_type == STATUS ) {
178
179                 switch( msg->status_code ) {
180
181                         case OSRF_STATUS_OK:
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 );
186                                 break;
187
188                         case OSRF_STATUS_COMPLETE:
189                                 osrf_app_session_set_complete( session, msg->thread_trace );
190                                 break;
191
192                         case OSRF_STATUS_CONTINUE:
193                                 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
194                                 break;
195
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 );
200                                 break;
201
202                         case OSRF_STATUS_EXPFAILED:
203                                 osrf_app_session_reset_remote( session );
204                                 session->state = OSRF_SESSION_DISCONNECTED;
205                                 break;
206
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 );
211                                 break;
212
213                         default:
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 );
223                                 break;
224                 }
225
226         } else if( msg->m_type == RESULT ) {
227                 further_msg = msg;
228         }
229
230         if(further_msg) {
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 );
234         }
235
236         if(msg != further_msg)
237                 osrfMessageFree(msg);
238
239         return;
240 }
241
242
243 /**
244         If we return a message, that message should be passed up the stack,
245         if we return NULL, we're finished for now...
246 */
247 static void _do_server( osrfAppSession* session, osrfMessage* msg ) {
248
249         if(session == NULL || msg == NULL) return;
250
251         osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
252
253         osrfMessage* further_msg = NULL;
254
255         switch( msg->m_type ) {
256
257                 case STATUS:
258                         break;
259
260                 case DISCONNECT:
261                         /* session will be freed by the forker */
262                         osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
263                         session->state = OSRF_SESSION_DISCONNECTED;
264                         break;
265
266                 case CONNECT:
267                         osrfAppSessionStatus( session, OSRF_STATUS_OK,
268                                         "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
269                         session->state = OSRF_SESSION_CONNECTED;
270                         break;
271
272                 case REQUEST:
273
274                         osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
275                                         "for session %s", msg->thread_trace, session->session_id );
276                         further_msg = msg;
277                         break;
278
279                 default:
280                         osrfLogWarning( OSRF_LOG_MARK,
281                                         "Server cannot handle message of type %d", msg->m_type );
282                         session->state = OSRF_SESSION_DISCONNECTED;
283                         break;
284         }
285
286         if(further_msg) {
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 );
290         }
291
292         if(msg != further_msg)
293                 osrfMessageFree(msg);
294
295         return;
296 }
297
298
299
300 static void osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg ) {
301         if(session == NULL || msg == NULL) return;
302
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 );
306         }
307         else if(msg->m_type == REQUEST) {
308                 char* method = msg->method_name;
309                 char* app    = session->remote_service;
310                 jsonObject* params = msg->_params;
311
312                 osrfAppRunMethod( app, method,  session, msg->thread_trace, params );
313                 osrfMessageFree(msg);
314         }
315
316         return;
317 }
318
319