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