]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_stack.c
Fix a bug in md5sum() (which only affected code compiled
[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 static int osrf_stack_process( transport_client* client, int timeout, int* msg_received ) {
17         if( !client ) return -1;
18         transport_message* msg = NULL;
19         if(msg_received) *msg_received = 0;
20
21         while( (msg = client_recv( client, timeout )) ) {
22                 if(msg_received) *msg_received = 1;
23                 osrfLogDebug( OSRF_LOG_MARK,  "Received message from transport code from %s", msg->sender );
24                 osrf_stack_transport_handler( msg, NULL );
25                 timeout = 0;
26         }
27
28         if( client->error ) {
29                 osrfLogWarning(OSRF_LOG_MARK, "transport_client had trouble reading from the socket..");
30                 return -1;
31         }
32
33         if( ! client_connected( client ) ) return -1;
34
35         return 0;
36 }
37
38
39
40 // -----------------------------------------------------------------------------
41 // Entry point into the stack
42 // -----------------------------------------------------------------------------
43 osrfAppSession* osrf_stack_transport_handler( transport_message* msg,
44                 const char* my_service ) {
45
46         if(!msg) return NULL;
47
48    osrfLogSetXid(msg->osrf_xid);
49
50         osrfLogDebug( OSRF_LOG_MARK,  "Transport handler received new message \nfrom %s "
51                         "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
52
53         if( msg->is_error && ! msg->thread ) {
54                 osrfLogWarning( OSRF_LOG_MARK, "!! Received jabber layer error for %s ... exiting\n", msg->sender );
55                 message_free( msg );
56                 return NULL;
57         }
58
59         if(! msg->thread  && ! msg->is_error ) {
60                 osrfLogWarning( OSRF_LOG_MARK, "Received a non-error message with no thread trace... dropping");
61                 message_free( msg );
62                 return NULL;
63         }
64
65         osrfAppSession* session = osrf_app_session_find_session( msg->thread );
66
67         if( !session && my_service ) 
68                 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
69
70         if( !session ) {
71                 message_free( msg );
72                 return NULL;
73         }
74         
75         if(!msg->is_error)
76                 osrfLogDebug( OSRF_LOG_MARK, "Session [%s] found or built", session->session_id );
77
78         osrf_app_session_set_remote( session, msg->sender );
79         osrfMessage* arr[OSRF_MAX_MSGS_PER_PACKET];
80
81         /* Convert the message body into one or more osrfMessages */
82         int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET);
83
84         osrfLogDebug( OSRF_LOG_MARK,  "We received %d messages from %s", num_msgs, msg->sender );
85
86         double starttime = get_timestamp_millis();
87
88         int i;
89         for( i = 0; i < num_msgs; i++ ) {
90
91                 /* if we've received a jabber layer error message (probably talking to 
92                         someone who no longer exists) and we're not talking to the original
93                         remote id for this server, consider it a redirect and pass it up */
94                 if(msg->is_error) {
95                         osrfLogWarning( OSRF_LOG_MARK,  " !!! Received Jabber layer error message" ); 
96
97                         if(strcmp(session->remote_id,session->orig_remote_id)) {
98                                 osrfLogWarning( OSRF_LOG_MARK,  "Treating jabber error as redirect for tt [%d] "
99                                         "and session [%s]", arr[i]->thread_trace, session->session_id );
100
101                                 arr[i]->m_type = STATUS;
102                                 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
103
104                         } else {
105                                 osrfLogWarning( OSRF_LOG_MARK, " * Jabber Error is for top level remote "
106                     " id [%s], no one to send my message to!  Cutting request short...", session->remote_id );
107                 session->transport_error = 1;
108                 break;
109                         }
110                 }
111
112                 if( session->type ==  OSRF_SESSION_CLIENT )
113                         _do_client( session, arr[i] );
114                 else
115                         _do_server( session, arr[i] );
116         }
117
118         double duration = get_timestamp_millis() - starttime;
119         osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %f", duration);
120
121         message_free( msg );
122         osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
123
124         return session;
125 }
126
127 /** If we return a message, that message should be passed up the stack,
128   * if we return NULL, we're finished for now...
129   */
130 static void _do_client( osrfAppSession* session, osrfMessage* msg ) {
131         if(session == NULL || msg == NULL)
132                 return;
133
134         osrfMessage* further_msg = NULL;
135
136         if( msg->m_type == STATUS ) {
137                 
138                 switch( msg->status_code ) {
139
140                         case OSRF_STATUS_OK:
141                                 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
142                                 session->state = OSRF_SESSION_CONNECTED;
143                                 osrfLogDebug( OSRF_LOG_MARK,  "State: %x => %s => %d", session, session->session_id, session->state );
144                                 break;
145
146                         case OSRF_STATUS_COMPLETE:
147                                 osrf_app_session_set_complete( session, msg->thread_trace );
148                                 break;
149
150                         case OSRF_STATUS_CONTINUE:
151                                 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
152                                 break;
153
154                         case OSRF_STATUS_REDIRECTED:
155                                 osrf_app_session_reset_remote( session );
156                                 session->state = OSRF_SESSION_DISCONNECTED;
157                                 osrf_app_session_request_resend( session, msg->thread_trace );
158                                 break;
159
160                         case OSRF_STATUS_EXPFAILED: 
161                                 osrf_app_session_reset_remote( session );
162                                 session->state = OSRF_SESSION_DISCONNECTED;
163                                 break;
164
165                         case OSRF_STATUS_TIMEOUT:
166                                 osrf_app_session_reset_remote( session );
167                                 session->state = OSRF_SESSION_DISCONNECTED;
168                                 osrf_app_session_request_resend( session, msg->thread_trace );
169                                 break;
170
171                         default:
172                                 /* Replace the old message with a new one */
173                                 further_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
174                                 osrf_message_set_status_info( further_msg,
175                                                 msg->status_name, msg->status_text, msg->status_code );
176                                 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with " 
177                                                 "the provided message code: %d, name %s. Passing UP.", 
178                                                 msg->status_code, msg->status_name );
179                                 further_msg->is_exception = 1;
180                                 osrf_app_session_set_complete( session, msg->thread_trace );
181                                 break;
182                 }
183
184         } else if( msg->m_type == RESULT ) {
185                 further_msg = msg;
186         }
187
188         if(further_msg) {
189                 osrfLogDebug( OSRF_LOG_MARK, "passing client message %d / session %s to app handler",
190                                           msg->thread_trace, session->session_id );
191                 osrf_stack_application_handler( session, further_msg );
192         }
193
194         if(msg != further_msg)
195                 osrfMessageFree(msg);
196
197         return;
198 }
199
200
201 /** If we return a message, that message should be passed up the stack, 
202   * if we return NULL, we're finished for now...
203   */
204 static void _do_server( osrfAppSession* session, osrfMessage* msg ) {
205
206         if(session == NULL || msg == NULL) return;
207
208         osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
209
210         osrfMessage* further_msg = NULL;
211         
212         switch( msg->m_type ) {
213
214                 case STATUS:
215                         break;
216
217                 case DISCONNECT:
218                         /* session will be freed by the forker */
219                         osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
220                         session->state = OSRF_SESSION_DISCONNECTED;
221                         break;
222
223                 case CONNECT:
224                         osrfAppSessionStatus( session, OSRF_STATUS_OK,
225                                         "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
226                         session->state = OSRF_SESSION_CONNECTED;
227                         break;
228
229                 case REQUEST:
230
231                         osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
232                                         "for session %s", msg->thread_trace, session->session_id );
233                         further_msg = msg;
234                         break;
235
236                 default:
237                         osrfLogWarning( OSRF_LOG_MARK, "Server cannot handle message of type %d", msg->m_type );
238                         session->state = OSRF_SESSION_DISCONNECTED;
239                         break;
240         }
241
242         if(further_msg) {
243                 osrfLogDebug( OSRF_LOG_MARK, "passing server message %d / session %s to app handler",
244                                           msg->thread_trace, session->session_id );
245                 osrf_stack_application_handler( session, further_msg );
246         }
247
248         if(msg != further_msg)
249                 osrfMessageFree(msg);
250
251         return;
252 }
253
254
255
256 static void osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg ) {
257         if(session == NULL || msg == NULL) return;
258
259         if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
260                 /* Enqueue the RESULT message to be processed later */
261                 osrf_app_session_push_queue( session, msg );
262         }
263         else if(msg->m_type == REQUEST) {
264                 char* method = msg->method_name;
265                 char* app       = session->remote_service;
266                 jsonObject* params = msg->_params;
267
268                 osrfAppRunMethod( app, method,  session, msg->thread_trace, params );
269                 osrfMessageFree(msg);
270         }
271         
272         return;
273 }
274
275