]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/libstack/osrf_app_session.c
525e559d4583af8155dfc5e92e70ba94fc8b6362
[working/Evergreen.git] / OpenSRF / src / libstack / osrf_app_session.c
1 #include "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 /** Allocation 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
35         if( req->payload ) {
36                 osrf_message_free( req->payload );
37         }
38
39         /*
40         osrf_message* cur_msg = req->result;
41         while( cur_msg != NULL ) {
42                 osrf_message* next_msg = cur_msg->next;
43                 osrf_message_free( cur_msg );
44                 cur_msg = next_msg;
45         }
46         osrf_message_free( req->payload );
47         */
48
49         free( req );
50         debug_handler("after request free");
51 }
52
53 /** Pushes the given message onto the list of 'responses' to this request */
54 void _osrf_app_request_push_queue( osrf_app_request* req, osrf_message* result ){
55         if(req == NULL || result == NULL) return;
56         debug_handler( "App Session pushing [%d] onto request queue", result->thread_trace );
57         if(req->result == NULL)
58                 req->result = result;
59         else {
60                 result->next = req->result;
61                 req->result = result;
62         }
63 }
64
65 /** Removes this app_request from our session request set */
66 void osrf_app_session_request_finish( 
67                 osrf_app_session* session, int req_id ){
68
69         if(session == NULL) return;
70         debug_handler("Finishing request %d", req_id );
71         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
72         if(req == NULL) return;
73         _osrf_app_session_remove_request( req->session, req );
74         _osrf_app_request_free( req );
75 }
76
77
78 void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id ) {
79         if(session == NULL) return;
80         debug_handler("Resetting timeout %d", req_id );
81         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
82         if(req == NULL) return;
83         req->reset_timeout = 1;
84 }
85
86 /** Checks the receive queue for messages.  If any are found, the first
87   * is popped off and returned.  Otherwise, this method will wait at most timeout 
88   * seconds for a message to appear in the receive queue.  Once it arrives it is returned.
89   * If no messages arrive in the timeout provided, null is returned.
90   */
91 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) {
92
93         if(req == NULL) return NULL;
94
95         if( req->result != NULL ) {
96                 debug_handler("app_request receive already has a message, returning it");
97                 /* pop off the first message in the list */
98                 osrf_message* tmp_msg = req->result;
99                 req->result = req->result->next;
100                 return tmp_msg;
101         }
102
103         time_t start = time(NULL);      
104         time_t remaining = (time_t) timeout;
105
106         while( remaining >= 0 ) {
107                 /* tell the session to wait for stuff */
108                 debug_handler( "In app_request receive with remaining time [%d]", (int) remaining );
109
110                 osrf_app_session_queue_wait( req->session, 0 );
111
112                 if( req->result != NULL ) { /* if we received anything */
113                         /* pop off the first message in the list */
114                         debug_handler( "app_request_recv received a message, returning it");
115                         osrf_message* ret_msg = req->result;
116                         osrf_message* tmp_msg = ret_msg->next;
117                         req->result = tmp_msg;
118                         return ret_msg;
119                 }
120
121                 if( req->complete )
122                         return NULL;
123
124                 osrf_app_session_queue_wait( req->session, (int) remaining );
125
126                 if( req->result != NULL ) { /* if we received anything */
127                         /* pop off the first message in the list */
128                         debug_handler( "app_request_recv received a message, returning it");
129                         osrf_message* ret_msg = req->result;
130                         osrf_message* tmp_msg = ret_msg->next;
131                         req->result = tmp_msg;
132                         return ret_msg;
133                 }
134                 if( req->complete )
135                         return NULL;
136
137                 if(req->reset_timeout) {
138                         remaining = (time_t) timeout;
139                         req->reset_timeout = 0;
140                         debug_handler("Recevied a timeout reset");
141                 } else {
142                         remaining -= (int) (time(NULL) - start);
143                 }
144         }
145
146         debug_handler("Returning NULL from app_request_recv after timeout");
147         return NULL;
148 }
149
150 /** Resend this requests original request message */
151 int _osrf_app_request_resend( osrf_app_request* req ) {
152         if(req == NULL) return 0;
153         if(!req->complete) {
154                 debug_handler( "Resending request [%d]", req->request_id );
155                 return _osrf_app_session_send( req->session, req->payload );
156         }
157         return 1;
158 }
159
160
161
162 // --------------------------------------------------------------------------
163 // --------------------------------------------------------------------------
164 // Session API
165 // --------------------------------------------------------------------------
166
167 /** returns a session from the global session hash */
168 osrf_app_session* osrf_app_session_find_session( char* session_id ) {
169         osrf_app_session* ptr = app_session_cache;
170         debug_handler("Searching for session in global cache with id [%s]", session_id );
171         while( ptr != NULL ) {
172                 if( !strcmp(ptr->session_id,session_id) )
173                         return ptr;
174                 ptr = ptr->next;
175         }
176         return NULL;
177 }
178
179
180 /** adds a session to the global session cache */
181 void _osrf_app_session_push_session( osrf_app_session* session ) {
182
183         if( app_session_cache == NULL ) {
184                 app_session_cache = session;
185                 return;
186         }
187
188         osrf_app_session* ptr = app_session_cache;
189         debug_handler( "Pushing [%s] [%s] onto global session cache", 
190                         session->remote_service, session->session_id );
191         while( ptr != NULL ) {
192                 if( !strcmp(ptr->session_id, session->session_id) )
193                         return;
194                 if( ptr->next == NULL ) {
195                         ptr->next = session;
196                         return;
197                 }
198                 ptr = ptr->next;
199         }
200 }
201
202
203 /** unlinks from global session cache */
204 void _osrf_app_session_remove_session( char* session_id ) {
205
206         if( app_session_cache == NULL )
207                 return;
208
209         debug_handler( "App Session removing session [%s] from global cache", session_id );
210         if( !strcmp(app_session_cache->session_id, session_id) ) {
211                 if( app_session_cache->next != NULL ) {
212                         osrf_app_session* next = app_session_cache->next;
213                         app_session_cache = next;
214                         return;
215                 } else {
216                         app_session_cache = NULL;
217                         return;
218                 }
219         }
220
221         if( app_session_cache->next == NULL )
222                 return;
223
224         osrf_app_session* prev = app_session_cache;
225         osrf_app_session* ptr = prev->next;
226         while( ptr != NULL ) {
227                 if( ptr->session_id == session_id ) {
228                         osrf_app_session* tmp = ptr->next;
229                         prev->next = tmp;
230                         return;
231                 }
232                 ptr = ptr->next;
233         }
234 }
235
236 /** Allocates a initializes a new app_session */
237
238 osrf_app_session* osrf_app_client_session_init( char* remote_service ) {
239
240         osrf_app_session* session = safe_malloc(sizeof(osrf_app_session));      
241
242         session->transport_handle = osrf_system_get_transport_client();
243         if( session->transport_handle == NULL ) {
244                 warning_handler("No transport client for service 'client'");
245                 return NULL;
246         }
247
248         char target_buf[512];
249         memset(target_buf,0,512);
250         char* domain    = config_value( "opensrf.bootstrap", "//bootstrap/domains/domain1" ); /* just the first for now */
251         char* router_name = config_value( "opensrf.bootstrap", "//bootstrap/router_name" );
252         sprintf( target_buf, "%s@%s/%s",  router_name, domain, remote_service );
253         free(domain);
254         free(router_name);
255
256         session->request_queue = NULL;
257         session->remote_id = strdup(target_buf);
258         session->orig_remote_id = strdup(session->remote_id);
259         session->remote_service = strdup(remote_service);
260
261         #ifdef ASSUME_STATELESS
262         session->stateless = 1;
263         debug_handler("session is stateless");
264         #else
265         session->stateless = 0;
266         debug_handler("session is NOT stateless");
267         #endif
268
269         /* build a chunky, random session id */
270         char id[256];
271         memset(id,0,256);
272
273         sprintf(id, "%lf.%d%d", get_timestamp_millis(), (int)time(NULL), getpid());
274         session->session_id = strdup(id);
275         debug_handler( "Building a new client session with id [%s] [%s]", 
276                         session->remote_service, session->session_id );
277
278         session->thread_trace = 0;
279         session->state = OSRF_SESSION_DISCONNECTED;
280         session->type = OSRF_SESSION_CLIENT;
281         session->next = NULL;
282         _osrf_app_session_push_session( session );
283         return session;
284 }
285
286 osrf_app_session* osrf_app_server_session_init( 
287                 char* session_id, char* our_app, char* remote_service, char* remote_id ) {
288
289         osrf_app_session* session = osrf_app_session_find_session( session_id );
290         if(session)
291                 return session;
292
293
294         session = safe_malloc(sizeof(osrf_app_session));        
295
296         session->transport_handle = osrf_system_get_transport_client();
297         if( session->transport_handle == NULL ) {
298                 warning_handler("No transport client for service '%s'", our_app );
299                 return NULL;
300         }
301         session->request_queue = NULL;
302         session->remote_id = strdup(remote_id);
303         session->orig_remote_id = strdup(remote_id);
304         session->session_id = strdup(session_id);
305         session->remote_service = strdup(remote_service);
306
307         #ifdef ASSUME_STATELESS
308         session->stateless = 1;
309         #else
310         session->stateless = 0;
311         #endif
312
313         debug_handler( "Building a new server session [%s] with id [%s]", 
314                         session->remote_service,  session_id );
315
316         session->thread_trace = 0;
317         session->state = OSRF_SESSION_DISCONNECTED;
318         session->type = OSRF_SESSION_CLIENT;
319         session->next = NULL;
320
321         _osrf_app_session_push_session( session );
322         return session;
323
324 }
325
326
327
328 /** frees memory held by a session */
329 void _osrf_app_session_free( osrf_app_session* session ){
330         if(session==NULL)
331                 return;
332         
333         free(session->remote_id);
334         free(session->orig_remote_id);
335         free(session->session_id);
336         free(session->remote_service);
337         free(session);
338 }
339
340
341 /*
342 int osrf_app_session_make_request( 
343                 osrf_app_session* session, json* params, 
344                 char* method_name, int protocol, string_array* param_strings ) {
345         if(session == NULL) return -1;
346
347         osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
348         osrf_message_set_request_info( req_msg,  method_name, params );
349
350         if(!req_msg->parse_json_params && param_strings) {
351                 int i;
352                 for(i = 0; i!= param_strings->size ; i++ ) {
353                         osrf_message_add_param(req_msg,
354                                 string_array_get_string(param_strings,i));
355                 }
356         }
357
358         osrf_app_request* req = _osrf_app_request_init( session, req_msg );
359         if(!_osrf_app_session_send( session, req_msg ) ) {
360                 warning_handler( "Error sending request message [%d]", session->thread_trace );
361                 return -1;
362         }
363
364         _osrf_app_session_push_request( session, req );
365         return req->request_id;
366 }
367 */
368
369
370
371
372 int osrf_app_session_make_req( 
373                 osrf_app_session* session, object* params, 
374                 char* method_name, int protocol, string_array* param_strings ) {
375         if(session == NULL) return -1;
376
377         osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
378         //osrf_message_set_request_info( req_msg,  method_name, params );
379         osrf_message_set_method(req_msg, method_name);
380         if(params) osrf_message_set_params(req_msg, params);
381
382         /* if we're not parsing the json, shove the strings in manually */
383         if(!req_msg->parse_json_params && param_strings) {
384                 int i;
385                 for(i = 0; i!= param_strings->size ; i++ ) {
386                         osrf_message_add_param(req_msg,
387                                 string_array_get_string(param_strings,i));
388                 }
389         }
390
391         osrf_app_request* req = _osrf_app_request_init( session, req_msg );
392         if(!_osrf_app_session_send( session, req_msg ) ) {
393                 warning_handler( "Error sending request message [%d]", session->thread_trace );
394                 return -1;
395         }
396
397         _osrf_app_session_push_request( session, req );
398         return req->request_id;
399 }
400
401
402
403 /** Adds an app_request to the request set */
404 void _osrf_app_session_push_request( osrf_app_session* session, osrf_app_request* req ){
405         if(session == NULL || req == NULL)
406                 return;
407
408         debug_handler( "Pushing [%d] onto requeust queue for session [%s] [%s]",
409                         req->request_id, session->remote_service, session->session_id );
410
411         if(session->request_queue == NULL) 
412                 session->request_queue = req;
413         else {
414                 osrf_app_request* req2 = session->request_queue->next;
415                 session->request_queue = req;
416                 req->next = req2;
417         }
418 }
419
420
421
422 /** Removes an app_request from this session request set */
423 void _osrf_app_session_remove_request( osrf_app_session* session, osrf_app_request* req ){
424         if(session == NULL || req == NULL)
425                 return;
426
427         if(session->request_queue == NULL)
428                 return;
429
430         debug_handler("Removing request [%d] from session [%s] [%s]",
431                         req->request_id, session->remote_service, session->session_id );
432
433         osrf_app_request* first = session->request_queue;
434         if(first->request_id == req->request_id) {
435                 session->request_queue = first->next;
436                 return;
437                 /*
438                 if(first->next == NULL) { 
439                         session->request_queue = NULL;
440                 } else {
441                         osrf_app_request* tmp = first->next;
442                         session->request_queue = tmp;
443                 }
444                 */
445         }
446
447         osrf_app_request* lead = first->next;
448
449         while( lead != NULL ) {
450                 if(lead->request_id == req->request_id) {
451                         first->next = lead->next;
452                         return;
453                 }
454                 first = lead;
455                 lead = lead->next;
456         }
457 }
458
459
460 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
461         if(session == NULL)
462                 return;
463
464         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
465         if(req) req->complete = 1;
466 }
467
468 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
469         if(session == NULL)
470                 return 0;
471         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
472         if(req)
473                 return req->complete;
474         return 0;
475 }
476
477 /** Returns the app_request with the given request_id (request_id) */
478 osrf_app_request* _osrf_app_session_get_request( 
479                 osrf_app_session* session, int request_id ){
480         if(session == NULL)
481                 return NULL;
482
483         debug_handler( "App Session searching for request [%d] in request queue",request_id );
484         osrf_app_request* req = session->request_queue;
485         while( req != NULL ) {
486                 if(req->request_id == request_id)
487                         return req;
488                 req = req->next;
489         }
490         return NULL;
491 }
492
493
494 /** Resets the remote connection id to that of the original*/
495 void osrf_app_session_reset_remote( osrf_app_session* session ){
496         if( session==NULL )
497                 return;
498
499         free(session->remote_id);
500         debug_handler( "App Session [%s] [%s] resetting remote id to %s",
501                         session->remote_service, session->session_id, session->orig_remote_id );
502
503         session->remote_id = strdup(session->orig_remote_id);
504 }
505
506 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
507         if(session == NULL)
508                 return;
509         if( session->remote_id )
510                 free(session->remote_id );
511         session->remote_id = strdup( remote_id );
512 }
513
514 /** pushes the given message into the result list of the app_request
515   with the given request_id */
516 int osrf_app_session_push_queue( 
517                 osrf_app_session* session, osrf_message* msg ){
518
519         if(session == NULL || msg == NULL)
520                 return 0;
521
522         debug_handler( "AppSession pushing result for [%d] onto request payload queue",
523                         msg->thread_trace );
524
525         osrf_app_request* req = session->request_queue;
526
527         if(req == NULL) {
528                 warning_handler( "app_session has no app_requests in its queue yet we have a result for [%d]", msg->thread_trace );
529                 return 0;
530         }
531
532         while( req != NULL ) {
533                 if(req->request_id == msg->thread_trace) {
534                         debug_handler( "Found app_request for tt [%d]", msg->thread_trace );
535                         _osrf_app_request_push_queue( req, msg );
536                         return 1;
537                 }
538                 req = req->next;
539         } 
540
541         return 0;
542         
543 }
544
545 /** Attempts to connect to the remote service */
546 int osrf_app_session_connect(osrf_app_session* session){
547         
548         if(session == NULL)
549                 return 0;
550
551         if(session->state == OSRF_SESSION_CONNECTED) {
552                 debug_handler("Already Connected, returning");
553                 return 1;
554         }
555
556         int timeout = 5; /* XXX CONFIG VALUE */
557
558         debug_handler( "AppSession connecting to %s", session->remote_id );
559
560         /* defaulting to protocol 1 for now */
561         osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 );
562         osrf_app_session_reset_remote( session );
563         session->state = OSRF_SESSION_CONNECTING;
564         int ret = _osrf_app_session_send( session, con_msg );
565         osrf_message_free(con_msg);
566         if(!ret)        return 0;
567
568         time_t start = time(NULL);      
569         time_t remaining = (time_t) timeout;
570
571         while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
572                 osrf_app_session_queue_wait( session, remaining );
573                 remaining -= (int) (time(NULL) - start);
574         }
575
576         if(session->state == OSRF_SESSION_CONNECTED)
577                 debug_handler(" * Connected Successfully");
578
579         if(session->state != OSRF_SESSION_CONNECTED)
580                 return 0;
581
582         return 1;
583 }
584
585
586
587 /** Disconnects from the remote service */
588 int osrf_app_session_disconnect( osrf_app_session* session){
589         if(session == NULL)
590                 return 1;
591
592         if(session->state == OSRF_SESSION_DISCONNECTED)
593                 return 1;
594
595         if(session->stateless && session->state != OSRF_SESSION_CONNECTED) {
596                 debug_handler(
597                                 "Exiting disconnect on stateless session %s", 
598                                 session->session_id);
599                 return 1;
600         }
601
602         debug_handler( "AppSession disconnecting from %s", session->remote_id );
603
604         osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
605         session->state = OSRF_SESSION_DISCONNECTED;
606         _osrf_app_session_send( session, dis_msg );
607
608         osrf_message_free( dis_msg );
609         osrf_app_session_reset_remote( session );
610         return 1;
611 }
612
613 int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
614         debug_handler("In reqeust resend searching for resend-able messages");
615         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
616         return _osrf_app_request_resend( req );
617 }
618
619 /** Send the given message */
620 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
621         if(session == NULL) return 0;
622         int ret_val= 0;
623
624         osrf_app_session_queue_wait( session, 0 );
625         debug_handler( "AppSession sending type %d, and thread_trace %d",
626                         msg->m_type, msg->thread_trace );
627
628         if(session->stateless) {
629                 osrf_app_session_reset_remote(session);
630
631         } else {
632
633                 if( (msg->m_type != CONNECT) && (msg->m_type != DISCONNECT) &&
634                                 (session->state != OSRF_SESSION_CONNECTED) ) {
635                         if(!osrf_app_session_connect( session )) 
636                                 return 0;
637                 }
638         }
639
640         char* xml =  osrf_message_to_xml(msg);
641
642         debug_handler("[%s] [%s] Remote Id: %s", 
643                         session->remote_service, session->session_id, session->remote_id );
644
645         transport_message* t_msg = message_init( 
646                         xml, "", session->session_id, session->remote_id, NULL );
647
648         debug_handler("Session [%s] [%s]  sending to %s \nXML:\n%s", 
649                         session->remote_service, session->session_id, t_msg->recipient, xml );
650         ret_val = client_send_message( session->transport_handle, t_msg );
651         free(xml);
652         message_free( t_msg );
653
654         return ret_val; 
655 }
656
657 /**  Waits up to 'timeout' seconds for some data to arrive.
658   * Any data that arrives will be processed according to its
659   * payload and message type.  This method will return after
660   * any data has arrived.
661   */
662 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout ){
663         if(session == NULL) return 0;
664         int ret_val = 0;
665         debug_handler( "AppSession in queue_wait with timeout %d", timeout );
666         ret_val = osrf_stack_process(session->transport_handle, timeout );
667         return ret_val;
668 }
669
670 /** Disconnects (if client) and removes the given session from the global session cache 
671   * ! This free's all attached app_requests ! 
672   */
673 void osrf_app_session_destroy ( osrf_app_session* session ){
674         if(session == NULL) return;
675
676         debug_handler( "AppSession [%s] [%s] destroying self and deleting requests", 
677                         session->remote_service, session->session_id );
678         if(session->type == OSRF_SESSION_CLIENT 
679                         && session->state != OSRF_SESSION_DISCONNECTED ) { /* disconnect if we're a client */
680                 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
681                 _osrf_app_session_send( session, dis_msg ); 
682                 osrf_message_free(dis_msg);
683         }
684         //session->state = OSRF_SESSION_DISCONNECTED;
685         _osrf_app_session_remove_session(session->session_id);
686         debug_handler("AppSession [%s] [%s] removed from cache", 
687                         session->remote_service, session->session_id );
688
689         osrf_app_request* req;
690         while( session->request_queue != NULL ) {
691                 req = session->request_queue->next;
692                 _osrf_app_request_free( session->request_queue );
693                 session->request_queue = req;
694         }
695
696         _osrf_app_session_free( session );
697 }
698
699 osrf_message* osrf_app_session_request_recv( 
700                 osrf_app_session* session, int req_id, int timeout ) {
701         if(req_id < 0 || session == NULL)
702                 return NULL;
703         debug_handler("somebody callled recv");
704         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
705         return _osrf_app_request_recv( req, timeout );
706 }
707