]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_app_session.c
new json api changes
[OpenSRF.git] / 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", "//%s/domains/domain1", osrf_get_config_context() ); /* just the first for now */
251         //char* router_name = config_value( "opensrf.bootstrap", "//%s/router_name", osrf_get_config_context() );
252
253         osrfStringArray* arr = osrfNewStringArray(8);
254         osrfConfigGetValueList(NULL, arr, "/domains/domain");
255         char* domain = osrfStringArrayGetString(arr, 0);
256         char* router_name = osrfConfigGetValue(NULL, "/router_name");
257         osrfStringArrayFree(arr);
258         
259         sprintf( target_buf, "%s@%s/%s",  router_name, domain, remote_service );
260         //free(domain);
261         free(router_name);
262
263         session->request_queue = NULL;
264         session->remote_id = strdup(target_buf);
265         session->orig_remote_id = strdup(session->remote_id);
266         session->remote_service = strdup(remote_service);
267
268         #ifdef ASSUME_STATELESS
269         session->stateless = 1;
270         debug_handler("session is stateless");
271         #else
272         session->stateless = 0;
273         debug_handler("session is NOT stateless");
274         #endif
275
276         /* build a chunky, random session id */
277         char id[256];
278         memset(id,0,256);
279
280         sprintf(id, "%lf.%d%d", get_timestamp_millis(), (int)time(NULL), getpid());
281         session->session_id = strdup(id);
282         debug_handler( "Building a new client session with id [%s] [%s]", 
283                         session->remote_service, session->session_id );
284
285         session->thread_trace = 0;
286         session->state = OSRF_SESSION_DISCONNECTED;
287         session->type = OSRF_SESSION_CLIENT;
288         session->next = NULL;
289         _osrf_app_session_push_session( session );
290         return session;
291 }
292
293 osrf_app_session* osrf_app_server_session_init( 
294                 char* session_id, char* our_app, char* remote_id ) {
295
296         osrf_app_session* session = osrf_app_session_find_session( session_id );
297         if(session)
298                 return session;
299
300
301         session = safe_malloc(sizeof(osrf_app_session));        
302
303         session->transport_handle = osrf_system_get_transport_client();
304         if( session->transport_handle == NULL ) {
305                 warning_handler("No transport client for service '%s'", our_app );
306                 return NULL;
307         }
308         session->request_queue = NULL;
309         session->remote_id = strdup(remote_id);
310         session->orig_remote_id = strdup(remote_id);
311         session->session_id = strdup(session_id);
312         session->remote_service = strdup(our_app);
313
314         #ifdef ASSUME_STATELESS
315         session->stateless = 1;
316         #else
317         session->stateless = 0;
318         #endif
319
320         debug_handler( "Building a new server session [%s] with id [%s]", 
321                         session->remote_service,  session_id );
322
323         session->thread_trace = 0;
324         session->state = OSRF_SESSION_DISCONNECTED;
325         session->type = OSRF_SESSION_CLIENT;
326         session->next = NULL;
327
328         _osrf_app_session_push_session( session );
329         return session;
330
331 }
332
333
334
335 /** frees memory held by a session */
336 void _osrf_app_session_free( osrf_app_session* session ){
337         if(session==NULL)
338                 return;
339         
340         free(session->remote_id);
341         free(session->orig_remote_id);
342         free(session->session_id);
343         free(session->remote_service);
344         free(session);
345 }
346
347 int osrf_app_session_make_req( 
348                 osrf_app_session* session, jsonObject* params, 
349                 char* method_name, int protocol, string_array* param_strings ) {
350         if(session == NULL) return -1;
351
352         osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
353         osrf_message_set_method(req_msg, method_name);
354         if(params) {
355                 osrf_message_set_params(req_msg, params);
356
357         } else {
358
359                 if(param_strings) {
360                         int i;
361                         for(i = 0; i!= param_strings->size ; i++ ) {
362                                 osrf_message_add_param(req_msg,
363                                         string_array_get_string(param_strings,i));
364                         }
365                 }
366         }
367
368         osrf_app_request* req = _osrf_app_request_init( session, req_msg );
369         if(!_osrf_app_session_send( session, req_msg ) ) {
370                 warning_handler( "Error sending request message [%d]", session->thread_trace );
371                 return -1;
372         }
373
374         _osrf_app_session_push_request( session, req );
375         return req->request_id;
376 }
377
378
379
380 /** Adds an app_request to the request set */
381 void _osrf_app_session_push_request( osrf_app_session* session, osrf_app_request* req ){
382         if(session == NULL || req == NULL)
383                 return;
384
385         debug_handler( "Pushing [%d] onto requeust queue for session [%s] [%s]",
386                         req->request_id, session->remote_service, session->session_id );
387
388         if(session->request_queue == NULL) 
389                 session->request_queue = req;
390         else {
391                 osrf_app_request* req2 = session->request_queue->next;
392                 session->request_queue = req;
393                 req->next = req2;
394         }
395 }
396
397
398
399 /** Removes an app_request from this session request set */
400 void _osrf_app_session_remove_request( osrf_app_session* session, osrf_app_request* req ){
401         if(session == NULL || req == NULL)
402                 return;
403
404         if(session->request_queue == NULL)
405                 return;
406
407         debug_handler("Removing request [%d] from session [%s] [%s]",
408                         req->request_id, session->remote_service, session->session_id );
409
410         osrf_app_request* first = session->request_queue;
411         if(first->request_id == req->request_id) {
412                 session->request_queue = first->next;
413                 return;
414                 /*
415                 if(first->next == NULL) { 
416                         session->request_queue = NULL;
417                 } else {
418                         osrf_app_request* tmp = first->next;
419                         session->request_queue = tmp;
420                 }
421                 */
422         }
423
424         osrf_app_request* lead = first->next;
425
426         while( lead != NULL ) {
427                 if(lead->request_id == req->request_id) {
428                         first->next = lead->next;
429                         return;
430                 }
431                 first = lead;
432                 lead = lead->next;
433         }
434 }
435
436
437 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
438         if(session == NULL)
439                 return;
440
441         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
442         if(req) req->complete = 1;
443 }
444
445 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
446         if(session == NULL)
447                 return 0;
448         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
449         if(req)
450                 return req->complete;
451         return 0;
452 }
453
454 /** Returns the app_request with the given request_id (request_id) */
455 osrf_app_request* _osrf_app_session_get_request( 
456                 osrf_app_session* session, int request_id ){
457         if(session == NULL)
458                 return NULL;
459
460         debug_handler( "App Session searching for request [%d] in request queue",request_id );
461         osrf_app_request* req = session->request_queue;
462         while( req != NULL ) {
463                 if(req->request_id == request_id)
464                         return req;
465                 req = req->next;
466         }
467         return NULL;
468 }
469
470
471 /** Resets the remote connection id to that of the original*/
472 void osrf_app_session_reset_remote( osrf_app_session* session ){
473         if( session==NULL )
474                 return;
475
476         free(session->remote_id);
477         debug_handler( "App Session [%s] [%s] resetting remote id to %s",
478                         session->remote_service, session->session_id, session->orig_remote_id );
479
480         session->remote_id = strdup(session->orig_remote_id);
481 }
482
483 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
484         if(session == NULL)
485                 return;
486         if( session->remote_id )
487                 free(session->remote_id );
488         session->remote_id = strdup( remote_id );
489 }
490
491 /** pushes the given message into the result list of the app_request
492   with the given request_id */
493 int osrf_app_session_push_queue( 
494                 osrf_app_session* session, osrf_message* msg ){
495
496         if(session == NULL || msg == NULL)
497                 return 0;
498
499         debug_handler( "AppSession pushing result for [%d] onto request payload queue",
500                         msg->thread_trace );
501
502         osrf_app_request* req = session->request_queue;
503
504         if(req == NULL) {
505                 warning_handler( "app_session has no app_requests in its queue yet we have a result for [%d]", msg->thread_trace );
506                 return 0;
507         }
508
509         while( req != NULL ) {
510                 if(req->request_id == msg->thread_trace) {
511                         debug_handler( "Found app_request for tt [%d]", msg->thread_trace );
512                         _osrf_app_request_push_queue( req, msg );
513                         return 1;
514                 }
515                 req = req->next;
516         } 
517
518         return 0;
519         
520 }
521
522 /** Attempts to connect to the remote service */
523 int osrf_app_session_connect(osrf_app_session* session){
524         
525         if(session == NULL)
526                 return 0;
527
528         if(session->state == OSRF_SESSION_CONNECTED) {
529                 debug_handler("Already Connected, returning");
530                 return 1;
531         }
532
533         int timeout = 5; /* XXX CONFIG VALUE */
534
535         debug_handler( "AppSession connecting to %s", session->remote_id );
536
537         /* defaulting to protocol 1 for now */
538         osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 );
539         osrf_app_session_reset_remote( session );
540         session->state = OSRF_SESSION_CONNECTING;
541         int ret = _osrf_app_session_send( session, con_msg );
542         osrf_message_free(con_msg);
543         if(!ret)        return 0;
544
545         time_t start = time(NULL);      
546         time_t remaining = (time_t) timeout;
547
548         while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
549                 osrf_app_session_queue_wait( session, remaining );
550                 remaining -= (int) (time(NULL) - start);
551         }
552
553         if(session->state == OSRF_SESSION_CONNECTED)
554                 debug_handler(" * Connected Successfully");
555
556         if(session->state != OSRF_SESSION_CONNECTED)
557                 return 0;
558
559         return 1;
560 }
561
562
563
564 /** Disconnects from the remote service */
565 int osrf_app_session_disconnect( osrf_app_session* session){
566         if(session == NULL)
567                 return 1;
568
569         if(session->state == OSRF_SESSION_DISCONNECTED)
570                 return 1;
571
572         if(session->stateless && session->state != OSRF_SESSION_CONNECTED) {
573                 debug_handler(
574                                 "Exiting disconnect on stateless session %s", 
575                                 session->session_id);
576                 return 1;
577         }
578
579         debug_handler( "AppSession disconnecting from %s", session->remote_id );
580
581         osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
582         session->state = OSRF_SESSION_DISCONNECTED;
583         _osrf_app_session_send( session, dis_msg );
584
585         osrf_message_free( dis_msg );
586         osrf_app_session_reset_remote( session );
587         return 1;
588 }
589
590 int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
591         debug_handler("In reqeust resend searching for resend-able messages");
592         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
593         return _osrf_app_request_resend( req );
594 }
595
596 /** Send the given message */
597 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
598         if(session == NULL) return 0;
599         int ret_val= 0;
600
601         osrf_app_session_queue_wait( session, 0 );
602         debug_handler( "AppSession sending type %d, and thread_trace %d",
603                         msg->m_type, msg->thread_trace );
604
605         if(session->stateless) {
606                 osrf_app_session_reset_remote(session);
607
608         } else {
609
610                 if( (msg->m_type != CONNECT) && (msg->m_type != DISCONNECT) &&
611                                 (session->state != OSRF_SESSION_CONNECTED) ) {
612                         if(!osrf_app_session_connect( session )) 
613                                 return 0;
614                 }
615         }
616
617         //char* xml =  osrf_message_to_xml(msg);
618         char* string =  osrf_message_serialize(msg);
619
620         debug_handler("[%s] [%s] Remote Id: %s", 
621                         session->remote_service, session->session_id, session->remote_id );
622
623         transport_message* t_msg = message_init( 
624                         string, "", session->session_id, session->remote_id, NULL );
625
626         debug_handler("Session [%s] [%s]  sending to %s \nXML:\n%s", 
627                         session->remote_service, session->session_id, t_msg->recipient, string );
628         ret_val = client_send_message( session->transport_handle, t_msg );
629         free(string);
630         message_free( t_msg );
631
632         return ret_val; 
633 }
634
635 /**  Waits up to 'timeout' seconds for some data to arrive.
636   * Any data that arrives will be processed according to its
637   * payload and message type.  This method will return after
638   * any data has arrived.
639   */
640 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout ){
641         if(session == NULL) return 0;
642         int ret_val = 0;
643         debug_handler( "AppSession in queue_wait with timeout %d", timeout );
644         ret_val = osrf_stack_entry_point(session->transport_handle, timeout);
645         return ret_val;
646 }
647
648 /** Disconnects (if client) and removes the given session from the global session cache 
649   * ! This free's all attached app_requests ! 
650   */
651 void osrf_app_session_destroy ( osrf_app_session* session ){
652         if(session == NULL) return;
653
654         debug_handler( "AppSession [%s] [%s] destroying self and deleting requests", 
655                         session->remote_service, session->session_id );
656         if(session->type == OSRF_SESSION_CLIENT 
657                         && session->state != OSRF_SESSION_DISCONNECTED ) { /* disconnect if we're a client */
658                 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
659                 _osrf_app_session_send( session, dis_msg ); 
660                 osrf_message_free(dis_msg);
661         }
662         //session->state = OSRF_SESSION_DISCONNECTED;
663         _osrf_app_session_remove_session(session->session_id);
664         debug_handler("AppSession [%s] [%s] removed from cache", 
665                         session->remote_service, session->session_id );
666
667         osrf_app_request* req;
668         while( session->request_queue != NULL ) {
669                 req = session->request_queue->next;
670                 _osrf_app_request_free( session->request_queue );
671                 session->request_queue = req;
672         }
673
674         _osrf_app_session_free( session );
675 }
676
677 osrf_message* osrf_app_session_request_recv( 
678                 osrf_app_session* session, int req_id, int timeout ) {
679         if(req_id < 0 || session == NULL)
680                 return NULL;
681         debug_handler("somebody callled recv");
682         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
683         return _osrf_app_request_recv( req, timeout );
684 }
685