Young C server code added
[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         transport_message* msg = client_recv( client, timeout );
12         if(msg == NULL) return 0;
13         debug_handler( "Received message from transport code from %s", msg->sender );
14         int status = osrf_stack_transport_handler( msg, NULL );
15
16         while(1) {
17                 transport_message* m = client_recv( client, 0 );
18                 if(m) {
19                         debug_handler( "Received additional message from transport code");
20                         status = osrf_stack_transport_handler( m, NULL );
21                 } else  {
22                         debug_handler( "osrf_stack_process returning with only 1 received message" );
23                         break;
24                 }
25         }
26
27         return status;
28 }
29
30
31
32 // -----------------------------------------------------------------------------
33 // Entry point into the stack
34 // -----------------------------------------------------------------------------
35 int osrf_stack_transport_handler( transport_message* msg, char* my_service ) { 
36
37         if(!msg) return -1;
38
39         debug_handler( "Transport handler received new message \nfrom %s "
40                         "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
41
42         if(! msg->thread  && ! msg->is_error ) {
43                 warning_handler("Received a non-error message with no thread trace... dropping");
44                 message_free( msg );
45         }
46
47         osrf_app_session* session = osrf_app_session_find_session( msg->thread );
48
49         if( session == NULL ) 
50                 session = osrf_app_server_session_init( msg->thread, my_service, msg->sender);
51
52         if(!msg->is_error)
53                 debug_handler("Session [%s] found or built", session->session_id );
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
100         if( session->type ==  OSRF_SESSION_CLIENT )
101                  ret_msg = _do_client( session, msg );
102         else
103                 ret_msg= _do_server( session, msg );
104
105         if(ret_msg) {
106                 debug_handler("passing message %d / session %s to app handler", 
107                                 msg->thread_trace, session->session_id );
108                 osrf_stack_application_handler( session, ret_msg );
109         } else
110                 osrf_message_free(msg);
111
112         return 1;
113
114
115
116 /** If we return a message, that message should be passed up the stack, 
117   * if we return NULL, we're finished for now...
118   */
119 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
120         if(session == NULL || msg == NULL)
121                 return NULL;
122
123         osrf_message* new_msg;
124
125         if( msg->m_type == STATUS ) {
126                 
127                 switch( msg->status_code ) {
128
129                         case OSRF_STATUS_OK:
130                                 debug_handler("We connected successfully");
131                                 session->state = OSRF_SESSION_CONNECTED;
132                                 debug_handler( "State: %x => %s => %d", session, session->session_id, session->state );
133                                 return NULL;
134
135                         case OSRF_STATUS_COMPLETE:
136                                 osrf_app_session_set_complete( session, msg->thread_trace );
137                                 return NULL;
138
139                         case OSRF_STATUS_CONTINUE:
140                                 osrf_app_session_request_reset_timeout( session, msg->thread_trace );
141                                 return NULL;
142
143                         case OSRF_STATUS_REDIRECTED:
144                                 osrf_app_session_reset_remote( session );
145                                 session->state = OSRF_SESSION_DISCONNECTED;
146                                 osrf_app_session_request_resend( session, msg->thread_trace );
147                                 return NULL;
148
149                         case OSRF_STATUS_EXPFAILED: 
150                                 osrf_app_session_reset_remote( session );
151                                 session->state = OSRF_SESSION_DISCONNECTED;
152                                 /* set the session to 'stateful' then resend */
153                         //      osrf_app_session_request_resend( session, msg->thread_trace );
154                                 return NULL;
155
156                         case OSRF_STATUS_TIMEOUT:
157                                 osrf_app_session_reset_remote( session );
158                                 session->state = OSRF_SESSION_DISCONNECTED;
159                                 osrf_app_session_request_resend( session, msg->thread_trace );
160                                 return NULL;
161
162
163                         default:
164                                 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
165                                 osrf_message_set_status_info( new_msg, 
166                                                 msg->status_name, msg->status_text, msg->status_code );
167                                 warning_handler("The stack doesn't know what to do with " 
168                                                 "the provided message code: %d, name %s. Passing UP.", 
169                                                 msg->status_code, msg->status_name );
170                                 new_msg->is_exception = 1;
171                                 osrf_app_session_set_complete( session, msg->thread_trace );
172                                 osrf_message_free(msg);
173                                 return new_msg;
174                 }
175
176                 return NULL;
177
178         } else if( msg->m_type == RESULT ) 
179                 return msg;
180
181         return NULL;
182
183 }
184
185
186 /** If we return a message, that message should be passed up the stack, 
187   * if we return NULL, we're finished for now...
188   */
189 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
190
191         if(session == NULL || msg == NULL) return NULL;
192
193         debug_handler("Server received message of type %d", msg->m_type );
194
195         switch( msg->m_type ) {
196
197                 case STATUS:
198                                 return NULL;
199
200                 case DISCONNECT:
201                                 osrf_app_session_destroy(session);      
202                                 return NULL;
203
204                 case CONNECT:
205                                 /* handle connect message */
206                                 return NULL;
207
208                 case REQUEST:
209
210                                 debug_handler("server passing message %d to application handler "
211                                                 "for session %s", msg->thread_trace, session->session_id );
212                                 return msg;
213
214                 default:
215                         warning_handler("Server cannot handle message of type %d", msg->m_type );
216                         return NULL;
217
218         }
219 }
220
221
222
223
224 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
225
226         if(session == NULL || msg == NULL) return 0;
227
228         if(msg->m_type == RESULT && session->type == OSRF_SESSION_CLIENT) {
229                 osrf_app_session_push_queue( session, msg ); 
230                 return 1;
231         }
232
233         if(msg->m_type != REQUEST) return 1;
234
235         char* method = msg->method_name;
236         char* app       = session->remote_service;
237         jsonObject* params = msg->_params;
238
239         osrfAppRunMethod( app, method,  session, msg->thread_trace, params );
240                 
241         return 1;
242
243 }
244