Patch from Scott McKellar:
[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( osrfAppSession* session, osrfMessage* msg );
10 static int osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg );
11 static osrfMessage* _do_client( osrfAppSession*, osrfMessage* );
12 static osrfMessage* _do_server( osrfAppSession*, osrfMessage* );
13
14 /* tell osrfAppSession 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         osrfAppSession* 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         osrfMessage* 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 "
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                 osrf_stack_message_handler( session, arr[i] );
113         }
114
115         double duration = get_timestamp_millis() - starttime;
116         osrfLogInfo(OSRF_LOG_MARK, "Message processing duration %f", duration);
117
118         message_free( msg );
119         osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
120
121         return session;
122 }
123
124 static int osrf_stack_message_handler( osrfAppSession* session, osrfMessage* msg ) {
125         if(session == NULL || msg == NULL)
126                 return 0;
127
128         osrfMessage* ret_msg = NULL;
129
130         if( session->type ==  OSRF_SESSION_CLIENT )
131                  ret_msg = _do_client( session, msg );
132         else
133                 ret_msg= _do_server( session, msg );
134
135         if(ret_msg) {
136                 osrfLogDebug( OSRF_LOG_MARK, "passing message %d / session %s to app handler", 
137                                 msg->thread_trace, session->session_id );
138                 osrf_stack_application_handler( session, ret_msg );
139         } else
140                 osrfMessageFree(msg);
141
142         return 1;
143
144
145
146 /** If we return a message, that message should be passed up the stack, 
147   * if we return NULL, we're finished for now...
148   */
149 static osrfMessage* _do_client( osrfAppSession* session, osrfMessage* msg ) {
150         if(session == NULL || msg == NULL)
151                 return NULL;
152
153         osrfMessage* new_msg;
154
155         if( msg->m_type == STATUS ) {
156                 
157                 switch( msg->status_code ) {
158
159                         case OSRF_STATUS_OK:
160                                 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
161                                 session->state = OSRF_SESSION_CONNECTED;
162                                 osrfLogDebug( OSRF_LOG_MARK,  "State: %x => %s => %d", session, session->session_id, session->state );
163                                 return NULL;
164
165                         case OSRF_STATUS_COMPLETE:
166                                 osrf_app_session_set_complete( session, msg->thread_trace );
167                                 return NULL;
168
169                         case OSRF_STATUS_CONTINUE:
170                                 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
171                                 return NULL;
172
173                         case OSRF_STATUS_REDIRECTED:
174                                 osrf_app_session_reset_remote( session );
175                                 session->state = OSRF_SESSION_DISCONNECTED;
176                                 osrf_app_session_request_resend( session, msg->thread_trace );
177                                 return NULL;
178
179                         case OSRF_STATUS_EXPFAILED: 
180                                 osrf_app_session_reset_remote( session );
181                                 session->state = OSRF_SESSION_DISCONNECTED;
182                                 return NULL;
183
184                         case OSRF_STATUS_TIMEOUT:
185                                 osrf_app_session_reset_remote( session );
186                                 session->state = OSRF_SESSION_DISCONNECTED;
187                                 osrf_app_session_request_resend( session, msg->thread_trace );
188                                 return NULL;
189
190
191                         default:
192                                 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
193                                 osrf_message_set_status_info( new_msg, 
194                                                 msg->status_name, msg->status_text, msg->status_code );
195                                 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with " 
196                                                 "the provided message code: %d, name %s. Passing UP.", 
197                                                 msg->status_code, msg->status_name );
198                                 new_msg->is_exception = 1;
199                                 osrf_app_session_set_complete( session, msg->thread_trace );
200                                 osrfMessageFree(msg);
201                                 return new_msg;
202                 }
203
204                 return NULL;
205
206         } else if( msg->m_type == RESULT ) 
207                 return msg;
208
209         return NULL;
210
211 }
212
213
214 /** If we return a message, that message should be passed up the stack, 
215   * if we return NULL, we're finished for now...
216   */
217 static osrfMessage* _do_server( osrfAppSession* session, osrfMessage* msg ) {
218
219         if(session == NULL || msg == NULL) return NULL;
220
221         osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
222
223         switch( msg->m_type ) {
224
225                 case STATUS:
226                                 return NULL;
227
228                 case DISCONNECT:
229                                 /* session will be freed by the forker */
230                                 osrfLogDebug(OSRF_LOG_MARK, "Client sent explicit disconnect");
231                                 session->state = OSRF_SESSION_DISCONNECTED;
232                                 return NULL;
233
234                 case CONNECT:
235                                 osrfAppSessionStatus( session, OSRF_STATUS_OK, 
236                                                 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
237                                 session->state = OSRF_SESSION_CONNECTED;
238                                 return NULL;
239
240                 case REQUEST:
241
242                                 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
243                                                 "for session %s", msg->thread_trace, session->session_id );
244                                 return msg;
245
246                 default:
247                         osrfLogWarning( OSRF_LOG_MARK, "Server cannot handle message of type %d", msg->m_type );
248                         session->state = OSRF_SESSION_DISCONNECTED;
249                         return NULL;
250
251         }
252 }
253
254
255
256 static int osrf_stack_application_handler( osrfAppSession* session, osrfMessage* msg ) {
257         if(session == NULL || msg == NULL) return 0;
258
259         if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
260                 osrf_app_session_push_queue( session, msg ); 
261                 return 1;
262         }
263
264         if(msg->m_type != REQUEST) return 1;
265
266         char* method = msg->method_name;
267         char* app       = session->remote_service;
268         jsonObject* params = msg->_params;
269
270         osrfAppRunMethod( app, method,  session, msg->thread_trace, params );
271         osrfMessageFree(msg);
272                 
273         return 1;
274 }
275
276