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