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