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