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