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