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