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