code cleanup and memory debugging
[Evergreen.git] / OpenSRF / src / libstack / osrf_stack.c
1 #include "osrf_stack.h"
2
3 osrf_message* _do_client( osrf_app_session*, osrf_message* );
4 osrf_message* _do_server( osrf_app_session*, osrf_message* );
5
6 int osrf_stack_process( transport_client* client, int timeout ) {
7         transport_message* msg = client_recv( client, timeout );
8         if(msg == NULL) return 0;
9         debug_handler( "Received message from transport code from %s", msg->sender );
10         int status = osrf_stack_transport_handler( msg );
11
12         /* see if there are multiple message to be received */
13         while(1) {
14                 transport_message* m = client_recv( client, 0 );
15                 if(m) {
16                         debug_handler( "Received additional message from transport code");
17                         status = osrf_stack_transport_handler( m );
18                 } else  {
19                         debug_handler( "osrf_stack_process returning with only 1 received message" );
20                         break;
21                 }
22         }
23
24         return status;
25 }
26
27
28
29 // -----------------------------------------------------------------------------
30 // Entry point into the stack
31 // -----------------------------------------------------------------------------
32 int osrf_stack_transport_handler( transport_message* msg ) { 
33
34         debug_handler( "Transport handler received new message \nfrom %s "
35                         "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
36
37         osrf_app_session* session = osrf_app_session_find_session( msg->thread );
38
39         if( session == NULL ) {  /* we must be a server, build a new session */
40                 info_handler( "Received message for nonexistant session. Dropping..." );
41                 //osrf_app_server_session_init( msg->thread, 
42                 message_free( msg );
43                 return 1;
44         }
45
46         debug_handler("Session [%s] found, building message", msg->thread );
47
48         osrf_app_session_set_remote( session, msg->sender );
49         osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
50         memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET );
51         int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET);
52
53         debug_handler( "We received %d messages from %s", num_msgs, msg->sender );
54
55         /* XXX ERROR CHECKING, BAD XML, ETC... */
56         int i;
57         for( i = 0; i != num_msgs; i++ ) {
58
59
60                 /* if we've received a jabber layer error message (probably talking to 
61                         someone who no longer exists) and we're not talking to the original
62                         remote id for this server, consider it a redirect and pass it up */
63                 if(msg->is_error) {
64                         warning_handler( "Received Jabber layer error message" ); 
65
66                         if(strcmp(session->remote_id,session->orig_remote_id)) {
67                                 warning_handler( "Treating jabber error as redirect for tt [%d] "
68                                         "and session [%s]", arr[i]->thread_trace, session->session_id );
69
70                                 arr[i]->m_type = STATUS;
71                                 arr[i]->status_code = OSRF_STATUS_REDIRECTED;
72
73                         } else {
74                                 warning_handler(" * Jabber Error is for top level remote id [%s], no one "
75                                                 "to send my message too!!!", session->remote_id );
76                         }
77                 }
78
79                 osrf_stack_message_handler( session, arr[i] );
80                 debug_handler("transport layer: message_handler returned, do we have more?");
81         }
82
83         debug_handler("transport layer: no more messages to process in this batch");
84         message_free( msg );
85         debug_handler("after msg delete");
86
87         return 1;
88 }
89
90 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
91         if(session == NULL || msg == NULL)
92                 return 0;
93
94         osrf_message* ret_msg = NULL;
95         if( session->type ==  OSRF_SESSION_CLIENT )
96                  ret_msg = _do_client( session, msg );
97         else
98                 ret_msg= _do_server( session, msg );
99
100         if(ret_msg)
101                 osrf_stack_application_handler( session, ret_msg );
102         else
103                 osrf_message_free(msg);
104
105         return 1;
106
107
108
109 /** If we return a message, that message should be passed up the stack, 
110   * if we return NULL, we're finished for now...
111   */
112 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
113         if(session == NULL || msg == NULL)
114                 return NULL;
115
116         osrf_message* new_msg;
117
118         if( msg->m_type == STATUS ) {
119                 
120                 switch( msg->status_code ) {
121
122                         case OSRF_STATUS_OK:
123                                 debug_handler("We connected successfully");
124                                 session->state = OSRF_SESSION_CONNECTED;
125                                 debug_handler( "State: %x => %s => %d", session, session->session_id, session->state );
126                                 return NULL;
127
128                         case OSRF_STATUS_COMPLETE:
129                                 osrf_app_session_set_complete( session, msg->thread_trace );
130                                 return NULL;
131
132                         case OSRF_STATUS_CONTINUE:
133                                 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
134                                 return NULL;
135
136                         case OSRF_STATUS_REDIRECTED:
137                                 osrf_app_session_reset_remote( session );
138                                 session->state = OSRF_SESSION_DISCONNECTED;
139                                 osrf_app_session_request_resend( session, msg->thread_trace );
140                                 return NULL;
141
142                         case OSRF_STATUS_EXPFAILED: 
143                                 osrf_app_session_reset_remote( session );
144                                 session->state = OSRF_SESSION_DISCONNECTED;
145                                 osrf_app_session_request_resend( session, msg->thread_trace );
146                                 return NULL;
147
148                         case OSRF_STATUS_TIMEOUT:
149                                 osrf_app_session_reset_remote( session );
150                                 session->state = OSRF_SESSION_DISCONNECTED;
151                                 osrf_app_session_request_resend( session, msg->thread_trace );
152                                 return NULL;
153
154
155                         default:
156                                 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
157                                 osrf_message_set_status_info( new_msg, 
158                                                 msg->status_name, msg->status_text, msg->status_code );
159                                 warning_handler("The stack doesn't know what to do with " 
160                                                 "the provided message code: %d, name %s. Passing UP.", 
161                                                 msg->status_code, msg->status_name );
162                                 new_msg->is_exception = 1;
163                                 osrf_app_session_set_complete( session, msg->thread_trace );
164                                 osrf_message_free(msg);
165                                 return new_msg;
166                 }
167
168                 return NULL;
169
170         } else if( msg->m_type == RESULT ) 
171                 return msg;
172
173         return NULL;
174
175 }
176
177
178 /** If we return a message, that message should be passed up the stack, 
179   * if we return NULL, we're finished for now...
180   */
181 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
182         if(session == NULL || msg == NULL)
183                 return NULL;
184
185
186         if( msg->m_type == STATUS ) { return NULL; }
187
188         warning_handler( "We dont' do servers yet !!" );
189
190         return msg;
191 }
192
193
194
195
196 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
197         if(session == NULL || msg == NULL)
198                 return 0;
199
200         if(msg->m_type == RESULT) {
201                 osrf_app_session_push_queue( session, msg ); 
202                 return 1;
203         }
204
205         warning_handler( "application_handler can't handle whatever you sent, type %d", msg->m_type);
206
207         return 1;
208
209 }