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