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