]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/libstack/osrf_app_session.c
added 'named' configs.
[working/Evergreen.git] / OpenSRF / src / libstack / osrf_app_session.c
1 #include "opensrf/osrf_app_session.h"
2 #include <time.h>
3
4 /* the global app_session cache */
5 osrf_app_session* app_session_cache;
6
7
8
9 // --------------------------------------------------------------------------
10 // --------------------------------------------------------------------------
11 // Request API
12 // --------------------------------------------------------------------------
13
14 /** Allocations and initializes a new app_request object */
15 osrf_app_request* _osrf_app_request_init( 
16                 osrf_app_session* session, osrf_message* msg ) {
17
18         osrf_app_request* req = 
19                 (osrf_app_request*) safe_malloc(sizeof(osrf_app_request));
20
21         req->session            = session;
22         req->request_id = msg->thread_trace;
23         req->complete           = 0;
24         req->payload            = msg;
25         req->result                     = NULL;
26
27         return req;
28
29 }
30
31 /** Frees memory used by an app_request object */
32 void _osrf_app_request_free( osrf_app_request * req ){
33         if( req == NULL ) return;
34         osrf_message* cur_msg = req->result;
35         while( cur_msg != NULL ) {
36                 osrf_message* next_msg = cur_msg->next;
37                 osrf_message_free( cur_msg );
38                 cur_msg = next_msg;
39         }
40         osrf_message_free( req->payload );
41         free( req );
42 }
43
44 /** Pushes the given message onto the list of 'responses' to this request */
45 void _osrf_app_request_push_queue( osrf_app_request* req, osrf_message* result ){
46         if(req == NULL || result == NULL) return;
47         debug_handler( "App Session pushing [%d] onto request queue", result->thread_trace );
48         if(req->result == NULL)
49                 req->result = result;
50         else {
51                 result->next = req->result;
52                 req->result = result;
53         }
54 }
55
56 /** Removes this app_request from our session request set */
57 void osrf_app_session_request_finish( 
58                 osrf_app_session* session, int req_id ){
59
60         if(session == NULL) return;
61         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
62         if(req == NULL) return;
63         _osrf_app_session_remove_request( req->session, req );
64         _osrf_app_request_free( req );
65 }
66
67 /** Checks the receive queue for messages.  If any are found, the first
68   * is popped off and returned.  Otherwise, this method will wait at most timeout 
69   * seconds for a message to appear in the receive queue.  Once it arrives it is returned.
70   * If no messages arrive in the timeout provided, null is returned.
71   */
72 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) {
73
74         if(req == NULL) return NULL;
75
76         if( req->result != NULL ) {
77                 debug_handler("app_request receive already has a message, returning it");
78                 /* pop off the first message in the list */
79                 osrf_message* tmp_msg = req->result->next;
80                 req->result = tmp_msg;
81                 return req->result;
82         }
83
84         time_t start = time(NULL);      
85         time_t remaining = (time_t) timeout;
86
87         while( remaining >= 0 ) {
88                 /* tell the session to wait for stuff */
89                 debug_handler( "In app_request receive with remaining time [%d]", (int) remaining );
90
91                 osrf_app_session_queue_wait( req->session, 0 );
92
93                 if( req->result != NULL ) { /* if we received anything */
94                         /* pop off the first message in the list */
95                         debug_handler( "app_request_recv received a message, returning it");
96                         osrf_message* ret_msg = req->result;
97                         osrf_message* tmp_msg = ret_msg->next;
98                         req->result = tmp_msg;
99                         return ret_msg;
100                 }
101
102                 if( req->complete )
103                         return NULL;
104
105                 osrf_app_session_queue_wait( req->session, (int) remaining );
106
107                 if( req->result != NULL ) { /* if we received anything */
108                         /* pop off the first message in the list */
109                         debug_handler( "app_request_recv received a message, returning it");
110                         osrf_message* ret_msg = req->result;
111                         osrf_message* tmp_msg = ret_msg->next;
112                         req->result = tmp_msg;
113                         return ret_msg;
114                 }
115                 if( req->complete )
116                         return NULL;
117
118                 remaining -= (int) (time(NULL) - start);
119         }
120
121         return NULL;
122 }
123
124 /** Resend this requests original request message */
125 int _osrf_app_request_resend( osrf_app_request* req ) {
126         if(req == NULL) return 0;
127         debug_handler( "Resending request [%d]", req->request_id );
128         return _osrf_app_session_send( req->session, req->payload );
129 }
130
131
132
133 // --------------------------------------------------------------------------
134 // --------------------------------------------------------------------------
135 // Session API
136 // --------------------------------------------------------------------------
137
138 /** returns a session from the global session hash */
139 osrf_app_session* osrf_app_session_find_session( char* session_id ) {
140         osrf_app_session* ptr = app_session_cache;
141         debug_handler("Searching for session in global cache with id [%s]", session_id );
142         while( ptr != NULL ) {
143                 if( !strcmp(ptr->session_id,session_id) )
144                         return ptr;
145                 ptr = ptr->next;
146         }
147         return NULL;
148 }
149
150
151 /** adds a session to the global session cache */
152 void _osrf_app_session_push_session( osrf_app_session* session ) {
153
154         if( app_session_cache == NULL ) {
155                 app_session_cache = session;
156                 return;
157         }
158
159         osrf_app_session* ptr = app_session_cache;
160         debug_handler( "Pushing [%s] onto global session cache", session->session_id );
161         while( ptr != NULL ) {
162                 if( !strcmp(ptr->session_id, session->session_id) )
163                         return;
164                 if( ptr->next == NULL ) {
165                         ptr->next = session;
166                         return;
167                 }
168                 ptr = ptr->next;
169         }
170 }
171
172
173 /** unlinks from global session cache */
174 void _osrf_app_session_remove_session( char* session_id ) {
175
176         if( app_session_cache == NULL )
177                 return;
178
179         debug_handler( "App Session removing session [%s] from global cache", session_id );
180         if( !strcmp(app_session_cache->session_id, session_id) ) {
181                 if( app_session_cache->next != NULL ) {
182                         osrf_app_session* next = app_session_cache->next;
183                         app_session_cache = next;
184                         return;
185                 } else {
186                         app_session_cache = NULL;
187                         return;
188                 }
189         }
190
191         if( app_session_cache->next == NULL )
192                 return;
193
194         osrf_app_session* prev = app_session_cache;
195         osrf_app_session* ptr = prev->next;
196         while( ptr != NULL ) {
197                 if( ptr->session_id == session_id ) {
198                         osrf_app_session* tmp = ptr->next;
199                         prev->next = tmp;
200                         return;
201                 }
202                 ptr = ptr->next;
203         }
204 }
205
206 /** Allocates a initializes a new app_session */
207
208 osrf_app_session* osrf_app_client_session_init( char* remote_service ) {
209         osrf_app_session* session = safe_malloc(sizeof(osrf_app_session));      
210
211         session->transport_handle = osrf_system_get_transport_client();
212         if( session->transport_handle == NULL ) {
213                 warning_handler("No transport client for service 'client'");
214                 return NULL;
215         }
216
217         char target_buf[512];
218         memset(target_buf,0,512);
219         char* domain    = config_value( "opensrf.bootstrap", "//bootstrap/domains/domain1" ); /* just the first for now */
220         char* router_name = config_value( "opensrf.bootstrap", "//bootstrap/router_name" );
221         sprintf( target_buf, "%s@%s/%s",  router_name, domain, remote_service );
222         free(domain);
223         free(router_name);
224
225         session->request_queue = NULL;
226         session->remote_id = strdup(target_buf);
227         session->orig_remote_id = strdup(session->remote_id);
228
229         /* build a chunky, random session id */
230         char id[256];
231         memset(id,0,256);
232         srand((int)time(NULL));
233         sprintf(id, "%d.%d%d", rand(), (int)time(NULL), getpid());
234         session->session_id = strdup(id);
235         debug_handler( "Building a new client session with id [%s]", session->session_id );
236
237         session->thread_trace = 0;
238         session->state = OSRF_SESSION_DISCONNECTED;
239         session->type = OSRF_SESSION_CLIENT;
240         session->next = NULL;
241         _osrf_app_session_push_session( session );
242         return session;
243 }
244
245 osrf_app_session* osrf_app_server_session_init( 
246                 char* session_id, char* our_app, char* remote_service, char* remote_id ) {
247
248         osrf_app_session* session = osrf_app_session_find_session( session_id );
249         if(session)
250                 return session;
251
252         debug_handler( "Building a new server session with id [%s]", session_id );
253
254         session = safe_malloc(sizeof(osrf_app_session));        
255
256         session->transport_handle = osrf_system_get_transport_client();
257         if( session->transport_handle == NULL ) {
258                 warning_handler("No transport client for service '%s'", our_app );
259                 return NULL;
260         }
261         session->request_queue = NULL;
262         session->remote_id = strdup(remote_id);
263         session->orig_remote_id = strdup(remote_id);
264         session->session_id = strdup(session_id);
265
266         session->thread_trace = 0;
267         session->state = OSRF_SESSION_DISCONNECTED;
268         session->type = OSRF_SESSION_CLIENT;
269         session->next = NULL;
270
271         _osrf_app_session_push_session( session );
272         return session;
273
274 }
275
276
277
278 /** frees memory held by a session */
279 void _osrf_app_session_free( osrf_app_session* session ){
280         if(session==NULL)
281                 return;
282         
283         free(session->remote_id);
284         free(session->orig_remote_id);
285         free(session);
286 }
287
288
289 int osrf_app_session_make_request( 
290                 osrf_app_session* session, json* params, char* method_name, int protocol ) {
291         if(session == NULL) return -1;
292
293         osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
294         osrf_message_set_request_info( req_msg,  method_name, params );
295         osrf_app_request* req = _osrf_app_request_init( session, req_msg );
296         if(!_osrf_app_session_send( session, req_msg ) ) {
297                 warning_handler( "Error sending request message [%d]", session->thread_trace );
298                 return -1;
299         }
300
301         _osrf_app_session_push_request( session, req );
302         return req->request_id;
303 }
304
305 /** Adds an app_request to the request set */
306 void _osrf_app_session_push_request( osrf_app_session* session, osrf_app_request* req ){
307         if(session == NULL || req == NULL)
308                 return;
309
310         debug_handler( "Pushing [%d] onto requeust queue for session [%s]",
311                         req->request_id, session->session_id );
312
313         if(session->request_queue == NULL) 
314                 session->request_queue = req;
315         else {
316                 osrf_app_request* req2 = session->request_queue->next;
317                 session->request_queue = req;
318                 req->next = req2;
319         }
320 }
321
322
323
324 /** Removes an app_request from this session request set */
325 void _osrf_app_session_remove_request( osrf_app_session* session, osrf_app_request* req ){
326         if(session == NULL || req == NULL)
327                 return;
328
329         if(session->request_queue == NULL)
330                 return;
331
332         debug_handler("Removing request [%d] from session [%s]",
333                         req->request_id, session->session_id );
334         osrf_app_request* first = session->request_queue;
335         if(first->request_id == req->request_id) {
336                 if(first->next == NULL) { /* only one in the list */
337                         session->request_queue = NULL;
338                 } else {
339                         osrf_app_request* tmp = first->next;
340                         session->request_queue = tmp;
341                 }
342         }
343
344         osrf_app_request* lead = first->next;
345
346         while( lead != NULL ) {
347                 if(lead->request_id == req->request_id) {
348                         osrf_app_request* tmp = lead->next;
349                         first->next = tmp;
350                         return;
351                 }
352                 first = lead;
353                 lead = lead->next;
354         }
355 }
356
357
358 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
359         if(session == NULL)
360                 return;
361
362         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
363         if(req) req->complete = 1;
364 }
365
366 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
367         if(session == NULL)
368                 return 0;
369         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
370         return req->complete;
371 }
372
373 /** Returns the app_request with the given request_id (request_id) */
374 osrf_app_request* _osrf_app_session_get_request( 
375                 osrf_app_session* session, int request_id ){
376         if(session == NULL)
377                 return NULL;
378
379         debug_handler( "App Session searching for request [%d] in request queue",request_id );
380         osrf_app_request* req = session->request_queue;
381         while( req != NULL ) {
382                 if(req->request_id == request_id)
383                         return req;
384                 req = req->next;
385         }
386         return NULL;
387 }
388
389
390 /** Resets the remote connection id to that of the original*/
391 void osrf_app_session_reset_remote( osrf_app_session* session ){
392         if( session==NULL )
393                 return;
394
395         free(session->remote_id);
396         debug_handler( "App Session [%s] resetting remote it to %s",
397                         session->session_id, session->orig_remote_id );
398
399         session->remote_id = strdup(session->orig_remote_id);
400 }
401
402 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
403         if(session == NULL)
404                 return;
405         if( session->remote_id )
406                 free(session->remote_id );
407         session->remote_id = strdup( remote_id );
408 }
409
410 /** pushes the given message into the result list of the app_request
411   with the given request_id */
412 int osrf_app_session_push_queue( 
413                 osrf_app_session* session, osrf_message* msg ){
414
415         if(session == NULL || msg == NULL)
416                 return 0;
417
418         debug_handler( "AppSession pushing result for [%d] onto request payload queue",
419                         msg->thread_trace );
420
421         osrf_app_request* req = session->request_queue;
422
423         if(req == NULL) {
424                 warning_handler( "app_session has no app_requests in its queue yet we have a result for [%d]", msg->thread_trace );
425                 return 0;
426         }
427
428         debug_handler( "The first request in the request queue has tt [%d]", req->request_id );
429
430         while( req != NULL ) {
431                 if(req->request_id == msg->thread_trace) {
432                         debug_handler( "Found app_request for tt [%d]", msg->thread_trace );
433                         _osrf_app_request_push_queue( req, msg );
434                         return 1;
435                 }
436                 req = req->next;
437         } 
438
439         return 0;
440         
441 }
442
443 /** Attempts to connect to the remote service */
444 int osrf_app_session_connect(osrf_app_session* session){
445         
446         if(session == NULL)
447                 return 0;
448
449         int timeout = 5; /* XXX CONFIG VALUE */
450
451         debug_handler( "AppSession connecting to %s", session->remote_id );
452
453         /* defaulting to protocol 1 for now */
454         osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 );
455         osrf_app_session_reset_remote( session );
456         session->state = OSRF_SESSION_CONNECTING;
457         if(!_osrf_app_session_send( session, con_msg ) )
458                 return 0;
459
460         time_t start = time(NULL);      
461         time_t remaining = (time_t) timeout;
462
463         while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
464                 osrf_app_session_queue_wait( session, remaining );
465                 remaining -= (int) (time(NULL) - start);
466         }
467
468         if(session->state != OSRF_SESSION_CONNECTED)
469                 return 0;
470
471         return 1;
472 }
473
474
475
476 /** Disconnects from the remote service */
477 int osrf_app_session_disconnect( osrf_app_session* session){
478         if(session == NULL)
479                 return 1;
480
481         if(session->state == OSRF_SESSION_DISCONNECTED)
482                 return 1;
483         debug_handler( "AppSession disconnecting from %s", session->remote_id );
484
485         osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
486         session->state = OSRF_SESSION_DISCONNECTED;
487         _osrf_app_session_send( session, dis_msg );
488
489         osrf_message_free( dis_msg );
490         osrf_app_session_reset_remote( session );
491         return 1;
492 }
493
494 int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
495         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
496         return _osrf_app_request_resend( req );
497 }
498
499 /** Send the given message */
500 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
501         if(session == NULL) return 0;
502         int ret_val= 0;
503
504         osrf_app_session_queue_wait( session, 0 );
505         debug_handler( "AppSession sending type %d, and thread_trace %d",
506                         msg->m_type, msg->thread_trace );
507
508         if( (msg->m_type != CONNECT) && (msg->m_type != DISCONNECT) &&
509                         (session->state != OSRF_SESSION_CONNECTED) ) {
510                 if(!osrf_app_session_connect( session )) 
511                         return 0;
512         }
513
514         char* xml =  osrf_message_to_xml(msg);
515
516         transport_message* t_msg = message_init( 
517                         xml, "", session->session_id, session->remote_id, NULL );
518
519         debug_handler("Sending XML:\n%s", xml );
520         ret_val = client_send_message( session->transport_handle, t_msg );
521         message_free( t_msg );
522
523         return ret_val; 
524 }
525
526 /**  Waits up to 'timeout' seconds for some data to arrive.
527   * Any data that arrives will be processed according to its
528   * payload and message type.  This method will return after
529   * any data has arrived.
530   */
531 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout ){
532         if(session == NULL) return 0;
533         int ret_val = 0;
534         debug_handler( "AppSession in queue_wait with timeout %d", timeout );
535         ret_val = osrf_stack_process(session->transport_handle, timeout );
536         return ret_val;
537 }
538
539 /** Disconnects (if client) and removes the given session from the global session cache 
540   * ! This free's all attached app_requests ! 
541   */
542 void osrf_app_session_destroy ( osrf_app_session* session ){
543         if(session == NULL) return;
544
545         debug_handler( "AppSession [%s] destroying self and deleting requests", session->session_id );
546         if(session->type == OSRF_SESSION_CLIENT) { /* disconnect if we're a client */
547                 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
548                 _osrf_app_session_send( session, dis_msg ); 
549                 //osrf_app_session_reset_remote( session );
550         }
551         //session->state = OSRF_SESSION_DISCONNECTED;
552         _osrf_app_session_remove_session(session->session_id);
553         debug_handler("AppSession [%s] removed from cache", session->session_id );
554
555         osrf_app_request* req;
556         while( session->request_queue != NULL ) {
557                 req = session->request_queue->next;
558                 _osrf_app_request_free( session->request_queue );
559                 session->request_queue = req;
560         }
561
562         _osrf_app_session_free( session );
563 }
564
565 osrf_message* osrf_app_session_request_recv( 
566                 osrf_app_session* session, int req_id, int timeout ) {
567         if(req_id < 0 || session == NULL)
568                 return NULL;
569         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
570         return _osrf_app_request_recv( req, timeout );
571 }
572