1c2e4c8ef417bc46948aba9c715e465184a87e1e
[OpenSRF.git] / 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         return 0;
21 }
22
23
24
25 // -----------------------------------------------------------------------------
26 // Entry point into the stack
27 // -----------------------------------------------------------------------------
28 osrfAppSession* osrf_stack_transport_handler( transport_message* msg, char* my_service ) { 
29
30         if(!msg) return NULL;
31
32         osrfLogDebug( OSRF_LOG_MARK,  "Transport handler received new message \nfrom %s "
33                         "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
34
35         if( msg->is_error && ! msg->thread ) {
36                 osrfLogWarning( OSRF_LOG_MARK, "!! Received jabber layer error for %s ... exiting\n", msg->sender );
37                 message_free( msg );
38                 return NULL;
39         }
40
41         if(! msg->thread  && ! msg->is_error ) {
42                 osrfLogWarning( OSRF_LOG_MARK, "Received a non-error message with no thread trace... dropping");
43                 message_free( msg );
44                 return NULL;
45         }
46
47         osrf_app_session* session = osrf_app_session_find_session( msg->thread );
48
49         if( !session && my_service ) 
50                 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
51
52         if( !session ) return NULL;
53
54         if(!msg->is_error)
55                 osrfLogDebug( OSRF_LOG_MARK, "Session [%s] found or built", session->session_id );
56
57         osrf_app_session_set_remote( session, msg->sender );
58         osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
59         memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET );
60         int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET);
61
62         osrfLogDebug( OSRF_LOG_MARK,  "We received %d messages from %s", num_msgs, msg->sender );
63
64         int i;
65         for( i = 0; i != num_msgs; i++ ) {
66
67                 /* if we've received a jabber layer error message (probably talking to 
68                         someone who no longer exists) and we're not talking to the original
69                         remote id for this server, consider it a redirect and pass it up */
70                 if(msg->is_error) {
71                         osrfLogWarning( OSRF_LOG_MARK,  " !!! Received Jabber layer error message" ); 
72
73                         if(strcmp(session->remote_id,session->orig_remote_id)) {
74                                 osrfLogWarning( OSRF_LOG_MARK,  "Treating jabber error as redirect for tt [%d] "
75                                         "and session [%s]", arr[i]->thread_trace, session->session_id );
76
77                                 arr[i]->m_type = STATUS;
78                                 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
79
80                         } else {
81                                 osrfLogWarning( OSRF_LOG_MARK, " * Jabber Error is for top level remote id [%s], no one "
82                                                 "to send my message too!!!", session->remote_id );
83                         }
84                 }
85
86                 osrf_stack_message_handler( session, arr[i] );
87         }
88
89         message_free( msg );
90         osrfLogDebug( OSRF_LOG_MARK, "after msg delete");
91
92         return session;
93 }
94
95 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
96         if(session == NULL || msg == NULL)
97                 return 0;
98
99         osrf_message* ret_msg = NULL;
100
101         if( session->type ==  OSRF_SESSION_CLIENT )
102                  ret_msg = _do_client( session, msg );
103         else
104                 ret_msg= _do_server( session, msg );
105
106         if(ret_msg) {
107                 osrfLogDebug( OSRF_LOG_MARK, "passing message %d / session %s to app handler", 
108                                 msg->thread_trace, session->session_id );
109                 osrf_stack_application_handler( session, ret_msg );
110         } else
111                 osrf_message_free(msg);
112
113         return 1;
114
115
116
117 /** If we return a message, that message should be passed up the stack, 
118   * if we return NULL, we're finished for now...
119   */
120 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
121         if(session == NULL || msg == NULL)
122                 return NULL;
123
124         osrf_message* new_msg;
125
126         if( msg->m_type == STATUS ) {
127                 
128                 switch( msg->status_code ) {
129
130                         case OSRF_STATUS_OK:
131                                 osrfLogDebug( OSRF_LOG_MARK, "We connected successfully");
132                                 session->state = OSRF_SESSION_CONNECTED;
133                                 osrfLogDebug( OSRF_LOG_MARK,  "State: %x => %s => %d", session, session->session_id, session->state );
134                                 return NULL;
135
136                         case OSRF_STATUS_COMPLETE:
137                                 osrf_app_session_set_complete( session, msg->thread_trace );
138                                 return NULL;
139
140                         case OSRF_STATUS_CONTINUE:
141                                 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
142                                 return NULL;
143
144                         case OSRF_STATUS_REDIRECTED:
145                                 osrf_app_session_reset_remote( session );
146                                 session->state = OSRF_SESSION_DISCONNECTED;
147                                 osrf_app_session_request_resend( session, msg->thread_trace );
148                                 return NULL;
149
150                         case OSRF_STATUS_EXPFAILED: 
151                                 osrf_app_session_reset_remote( session );
152                                 session->state = OSRF_SESSION_DISCONNECTED;
153                                 /* set the session to 'stateful' then resend */
154                         //      osrf_app_session_request_resend( session, msg->thread_trace );
155                                 return NULL;
156
157                         case OSRF_STATUS_TIMEOUT:
158                                 osrf_app_session_reset_remote( session );
159                                 session->state = OSRF_SESSION_DISCONNECTED;
160                                 osrf_app_session_request_resend( session, msg->thread_trace );
161                                 return NULL;
162
163
164                         default:
165                                 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
166                                 osrf_message_set_status_info( new_msg, 
167                                                 msg->status_name, msg->status_text, msg->status_code );
168                                 osrfLogWarning( OSRF_LOG_MARK, "The stack doesn't know what to do with " 
169                                                 "the provided message code: %d, name %s. Passing UP.", 
170                                                 msg->status_code, msg->status_name );
171                                 new_msg->is_exception = 1;
172                                 osrf_app_session_set_complete( session, msg->thread_trace );
173                                 osrf_message_free(msg);
174                                 return new_msg;
175                 }
176
177                 return NULL;
178
179         } else if( msg->m_type == RESULT ) 
180                 return msg;
181
182         return NULL;
183
184 }
185
186
187 /** If we return a message, that message should be passed up the stack, 
188   * if we return NULL, we're finished for now...
189   */
190 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
191
192         if(session == NULL || msg == NULL) return NULL;
193
194         osrfLogDebug( OSRF_LOG_MARK, "Server received message of type %d", msg->m_type );
195
196         switch( msg->m_type ) {
197
198                 case STATUS:
199                                 return NULL;
200
201                 case DISCONNECT:
202                                 /* session will be freed by the forker */
203                                 return NULL;
204
205                 case CONNECT:
206                                 osrfAppSessionStatus( session, OSRF_STATUS_OK, 
207                                                 "osrfConnectStatus", msg->thread_trace, "Connection Successful" );
208                                 return NULL;
209
210                 case REQUEST:
211
212                                 osrfLogDebug( OSRF_LOG_MARK, "server passing message %d to application handler "
213                                                 "for session %s", msg->thread_trace, session->session_id );
214                                 return msg;
215
216                 default:
217                         osrfLogWarning( OSRF_LOG_MARK, "Server cannot handle message of type %d", msg->m_type );
218                         return NULL;
219
220         }
221 }
222
223
224
225
226 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
227
228         if(session == NULL || msg == NULL) return 0;
229
230         if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
231                 osrf_app_session_push_queue( session, msg ); 
232                 return 1;
233         }
234
235         if(msg->m_type != REQUEST) return 1;
236
237         char* method = msg->method_name;
238         char* app       = session->remote_service;
239         jsonObject* params = msg->_params;
240
241         osrfAppRunMethod( app, method,  session, msg->thread_trace, params );
242
243         osrfMessageFree(msg);
244                 
245         return 1;
246
247 }
248