]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_app_session.c
found the (i believe) the remaining leaks in the transport code
[OpenSRF.git] / 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         osrf_app_session* session = safe_malloc(sizeof(osrf_app_session));      
220
221         session->transport_handle = osrf_system_get_transport_client();
222         if( session->transport_handle == NULL ) {
223                 warning_handler("No transport client for service 'client'");
224                 return NULL;
225         }
226
227         char target_buf[512];
228         memset(target_buf,0,512);
229         char* domain    = config_value( "opensrf.bootstrap", "//bootstrap/domains/domain1" ); /* just the first for now */
230         char* router_name = config_value( "opensrf.bootstrap", "//bootstrap/router_name" );
231         sprintf( target_buf, "%s@%s/%s",  router_name, domain, remote_service );
232         free(domain);
233         free(router_name);
234
235         session->request_queue = NULL;
236         session->remote_id = strdup(target_buf);
237         session->orig_remote_id = strdup(session->remote_id);
238
239         /* build a chunky, random session id */
240         char id[256];
241         memset(id,0,256);
242         srand((int)time(NULL));
243         sprintf(id, "%d.%d%d", rand(), (int)time(NULL), getpid());
244         session->session_id = strdup(id);
245         debug_handler( "Building a new client session with id [%s]", session->session_id );
246
247         session->thread_trace = 0;
248         session->state = OSRF_SESSION_DISCONNECTED;
249         session->type = OSRF_SESSION_CLIENT;
250         session->next = NULL;
251         _osrf_app_session_push_session( session );
252         return session;
253 }
254
255 osrf_app_session* osrf_app_server_session_init( 
256                 char* session_id, char* our_app, char* remote_service, char* remote_id ) {
257
258         osrf_app_session* session = osrf_app_session_find_session( session_id );
259         if(session)
260                 return session;
261
262         debug_handler( "Building a new server session with id [%s]", session_id );
263
264         session = safe_malloc(sizeof(osrf_app_session));        
265
266         session->transport_handle = osrf_system_get_transport_client();
267         if( session->transport_handle == NULL ) {
268                 warning_handler("No transport client for service '%s'", our_app );
269                 return NULL;
270         }
271         session->request_queue = NULL;
272         session->remote_id = strdup(remote_id);
273         session->orig_remote_id = strdup(remote_id);
274         session->session_id = strdup(session_id);
275
276         session->thread_trace = 0;
277         session->state = OSRF_SESSION_DISCONNECTED;
278         session->type = OSRF_SESSION_CLIENT;
279         session->next = NULL;
280
281         _osrf_app_session_push_session( session );
282         return session;
283
284 }
285
286
287
288 /** frees memory held by a session */
289 void _osrf_app_session_free( osrf_app_session* session ){
290         if(session==NULL)
291                 return;
292         
293         free(session->remote_id);
294         free(session->orig_remote_id);
295         free(session->session_id);
296         free(session);
297 }
298
299
300 int osrf_app_session_make_request( 
301                 osrf_app_session* session, json* params, char* method_name, int protocol ) {
302         if(session == NULL) return -1;
303
304         osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
305         osrf_message_set_request_info( req_msg,  method_name, params );
306         osrf_app_request* req = _osrf_app_request_init( session, req_msg );
307         if(!_osrf_app_session_send( session, req_msg ) ) {
308                 warning_handler( "Error sending request message [%d]", session->thread_trace );
309                 return -1;
310         }
311
312         _osrf_app_session_push_request( session, req );
313         return req->request_id;
314 }
315
316 /** Adds an app_request to the request set */
317 void _osrf_app_session_push_request( osrf_app_session* session, osrf_app_request* req ){
318         if(session == NULL || req == NULL)
319                 return;
320
321         debug_handler( "Pushing [%d] onto requeust queue for session [%s]",
322                         req->request_id, session->session_id );
323
324         if(session->request_queue == NULL) 
325                 session->request_queue = req;
326         else {
327                 osrf_app_request* req2 = session->request_queue->next;
328                 session->request_queue = req;
329                 req->next = req2;
330         }
331 }
332
333
334
335 /** Removes an app_request from this session request set */
336 void _osrf_app_session_remove_request( osrf_app_session* session, osrf_app_request* req ){
337         if(session == NULL || req == NULL)
338                 return;
339
340         if(session->request_queue == NULL)
341                 return;
342
343         debug_handler("Removing request [%d] from session [%s]",
344                         req->request_id, session->session_id );
345         osrf_app_request* first = session->request_queue;
346         if(first->request_id == req->request_id) {
347                 if(first->next == NULL) { /* only one in the list */
348                         session->request_queue = NULL;
349                 } else {
350                         osrf_app_request* tmp = first->next;
351                         session->request_queue = tmp;
352                 }
353         }
354
355         osrf_app_request* lead = first->next;
356
357         while( lead != NULL ) {
358                 if(lead->request_id == req->request_id) {
359                         osrf_app_request* tmp = lead->next;
360                         first->next = tmp;
361                         return;
362                 }
363                 first = lead;
364                 lead = lead->next;
365         }
366 }
367
368
369 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
370         if(session == NULL)
371                 return;
372
373         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
374         if(req) req->complete = 1;
375 }
376
377 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
378         if(session == NULL)
379                 return 0;
380         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
381         if(req)
382                 return req->complete;
383         return 0;
384 }
385
386 /** Returns the app_request with the given request_id (request_id) */
387 osrf_app_request* _osrf_app_session_get_request( 
388                 osrf_app_session* session, int request_id ){
389         if(session == NULL)
390                 return NULL;
391
392         debug_handler( "App Session searching for request [%d] in request queue",request_id );
393         osrf_app_request* req = session->request_queue;
394         while( req != NULL ) {
395                 if(req->request_id == request_id)
396                         return req;
397                 req = req->next;
398         }
399         return NULL;
400 }
401
402
403 /** Resets the remote connection id to that of the original*/
404 void osrf_app_session_reset_remote( osrf_app_session* session ){
405         if( session==NULL )
406                 return;
407
408         free(session->remote_id);
409         debug_handler( "App Session [%s] resetting remote id to %s",
410                         session->session_id, session->orig_remote_id );
411
412         session->remote_id = strdup(session->orig_remote_id);
413 }
414
415 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
416         if(session == NULL)
417                 return;
418         if( session->remote_id )
419                 free(session->remote_id );
420         session->remote_id = strdup( remote_id );
421 }
422
423 /** pushes the given message into the result list of the app_request
424   with the given request_id */
425 int osrf_app_session_push_queue( 
426                 osrf_app_session* session, osrf_message* msg ){
427
428         if(session == NULL || msg == NULL)
429                 return 0;
430
431         debug_handler( "AppSession pushing result for [%d] onto request payload queue",
432                         msg->thread_trace );
433
434         osrf_app_request* req = session->request_queue;
435
436         if(req == NULL) {
437                 warning_handler( "app_session has no app_requests in its queue yet we have a result for [%d]", msg->thread_trace );
438                 return 0;
439         }
440
441         debug_handler( "The first request in the request queue has tt [%d]", req->request_id );
442
443         while( req != NULL ) {
444                 if(req->request_id == msg->thread_trace) {
445                         debug_handler( "Found app_request for tt [%d]", msg->thread_trace );
446                         _osrf_app_request_push_queue( req, msg );
447                         return 1;
448                 }
449                 req = req->next;
450         } 
451
452         return 0;
453         
454 }
455
456 /** Attempts to connect to the remote service */
457 int osrf_app_session_connect(osrf_app_session* session){
458         
459         if(session == NULL)
460                 return 0;
461
462         int timeout = 5; /* XXX CONFIG VALUE */
463
464         debug_handler( "AppSession connecting to %s", session->remote_id );
465
466         /* defaulting to protocol 1 for now */
467         osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 );
468         osrf_app_session_reset_remote( session );
469         session->state = OSRF_SESSION_CONNECTING;
470         int ret = _osrf_app_session_send( session, con_msg );
471         osrf_message_free(con_msg);
472         if(!ret)        return 0;
473
474         time_t start = time(NULL);      
475         time_t remaining = (time_t) timeout;
476
477         while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
478                 osrf_app_session_queue_wait( session, remaining );
479                 remaining -= (int) (time(NULL) - start);
480         }
481
482         if(session->state != OSRF_SESSION_CONNECTED)
483                 return 0;
484
485         return 1;
486 }
487
488
489
490 /** Disconnects from the remote service */
491 int osrf_app_session_disconnect( osrf_app_session* session){
492         if(session == NULL)
493                 return 1;
494
495         if(session->state == OSRF_SESSION_DISCONNECTED)
496                 return 1;
497         debug_handler( "AppSession disconnecting from %s", session->remote_id );
498
499         osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
500         session->state = OSRF_SESSION_DISCONNECTED;
501         _osrf_app_session_send( session, dis_msg );
502
503         osrf_message_free( dis_msg );
504         osrf_app_session_reset_remote( session );
505         return 1;
506 }
507
508 int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
509         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
510         return _osrf_app_request_resend( req );
511 }
512
513 /** Send the given message */
514 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
515         if(session == NULL) return 0;
516         int ret_val= 0;
517
518         osrf_app_session_queue_wait( session, 0 );
519         debug_handler( "AppSession sending type %d, and thread_trace %d",
520                         msg->m_type, msg->thread_trace );
521
522         if( (msg->m_type != CONNECT) && (msg->m_type != DISCONNECT) &&
523                         (session->state != OSRF_SESSION_CONNECTED) ) {
524                 if(!osrf_app_session_connect( session )) 
525                         return 0;
526         }
527
528         char* xml =  osrf_message_to_xml(msg);
529
530         transport_message* t_msg = message_init( 
531                         xml, "", session->session_id, session->remote_id, NULL );
532
533         debug_handler("Sending XML:\n%s", xml );
534         ret_val = client_send_message( session->transport_handle, t_msg );
535         free(xml);
536         message_free( t_msg );
537
538         return ret_val; 
539 }
540
541 /**  Waits up to 'timeout' seconds for some data to arrive.
542   * Any data that arrives will be processed according to its
543   * payload and message type.  This method will return after
544   * any data has arrived.
545   */
546 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout ){
547         if(session == NULL) return 0;
548         int ret_val = 0;
549         debug_handler( "AppSession in queue_wait with timeout %d", timeout );
550         ret_val = osrf_stack_process(session->transport_handle, timeout );
551         return ret_val;
552 }
553
554 /** Disconnects (if client) and removes the given session from the global session cache 
555   * ! This free's all attached app_requests ! 
556   */
557 void osrf_app_session_destroy ( osrf_app_session* session ){
558         if(session == NULL) return;
559
560         debug_handler( "AppSession [%s] destroying self and deleting requests", session->session_id );
561         if(session->type == OSRF_SESSION_CLIENT 
562                         && session->state != OSRF_SESSION_DISCONNECTED ) { /* disconnect if we're a client */
563                 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
564                 _osrf_app_session_send( session, dis_msg ); 
565                 osrf_message_free(dis_msg);
566         }
567         //session->state = OSRF_SESSION_DISCONNECTED;
568         _osrf_app_session_remove_session(session->session_id);
569         debug_handler("AppSession [%s] removed from cache", session->session_id );
570
571         osrf_app_request* req;
572         while( session->request_queue != NULL ) {
573                 req = session->request_queue->next;
574                 _osrf_app_request_free( session->request_queue );
575                 session->request_queue = req;
576         }
577
578         _osrf_app_session_free( session );
579 }
580
581 osrf_message* osrf_app_session_request_recv( 
582                 osrf_app_session* session, int req_id, int timeout ) {
583         if(req_id < 0 || session == NULL)
584                 return NULL;
585         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
586         return _osrf_app_request_recv( req, timeout );
587 }
588