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