]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_app_session.c
made the JID functions safer
[OpenSRF.git] / src / libstack / osrf_app_session.c
1 #include "osrf_app_session.h"
2 #include <time.h>
3
4 /* the global app_session cache */
5 osrf_app_session* app_session_cache;
6
7
8
9 // --------------------------------------------------------------------------
10 // --------------------------------------------------------------------------
11 // Request API
12 // --------------------------------------------------------------------------
13
14 /** Allocation and initializes a new app_request object */
15 osrf_app_request* _osrf_app_request_init( 
16                 osrf_app_session* session, osrf_message* msg ) {
17
18         osrf_app_request* req = 
19                 (osrf_app_request*) safe_malloc(sizeof(osrf_app_request));
20
21         req->session            = session;
22         req->request_id = msg->thread_trace;
23         req->complete           = 0;
24         req->payload            = msg;
25         req->result                     = NULL;
26
27         return req;
28
29 }
30
31 /** Frees memory used by an app_request object */
32 void _osrf_app_request_free( osrf_app_request * req ){
33         if( req == NULL ) return;
34
35         if( req->payload ) {
36                 osrf_message_free( req->payload );
37         }
38
39         /*
40         osrf_message* cur_msg = req->result;
41         while( cur_msg != NULL ) {
42                 osrf_message* next_msg = cur_msg->next;
43                 osrf_message_free( cur_msg );
44                 cur_msg = next_msg;
45         }
46         osrf_message_free( req->payload );
47         */
48
49         free( req );
50 }
51
52 /** Pushes the given message onto the list of 'responses' to this request */
53 void _osrf_app_request_push_queue( osrf_app_request* req, osrf_message* result ){
54         if(req == NULL || result == NULL) return;
55         debug_handler( "App Session pushing request [%d] onto request queue", result->thread_trace );
56         if(req->result == NULL)
57                 req->result = result;
58         else {
59                 result->next = req->result;
60                 req->result = result;
61         }
62 }
63
64 /** Removes this app_request from our session request set */
65 void osrf_app_session_request_finish( 
66                 osrf_app_session* session, int req_id ){
67
68         if(session == NULL) return;
69         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
70         if(req == NULL) return;
71         _osrf_app_session_remove_request( req->session, req );
72         _osrf_app_request_free( req );
73 }
74
75
76 void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id ) {
77         if(session == NULL) return;
78         debug_handler("Resetting request timeout %d", req_id );
79         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
80         if(req == NULL) return;
81         req->reset_timeout = 1;
82 }
83
84 /** Checks the receive queue for messages.  If any are found, the first
85   * is popped off and returned.  Otherwise, this method will wait at most timeout 
86   * seconds for a message to appear in the receive queue.  Once it arrives it is returned.
87   * If no messages arrive in the timeout provided, null is returned.
88   */
89 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) {
90
91         if(req == NULL) return NULL;
92
93         if( req->result != NULL ) {
94                 /* pop off the first message in the list */
95                 osrf_message* tmp_msg = req->result;
96                 req->result = req->result->next;
97                 return tmp_msg;
98         }
99
100         time_t start = time(NULL);      
101         time_t remaining = (time_t) timeout;
102
103         while( remaining >= 0 ) {
104                 /* tell the session to wait for stuff */
105                 debug_handler( "In app_request receive with remaining time [%d]", (int) remaining );
106
107                 osrf_app_session_queue_wait( req->session, 0 );
108
109                 if( req->result != NULL ) { /* if we received anything */
110                         /* pop off the first message in the list */
111                         debug_handler( "app_request_recv received a message, returning it");
112                         osrf_message* ret_msg = req->result;
113                         osrf_message* tmp_msg = ret_msg->next;
114                         req->result = tmp_msg;
115                         return ret_msg;
116                 }
117
118                 if( req->complete )
119                         return NULL;
120
121                 osrf_app_session_queue_wait( req->session, (int) remaining );
122
123                 if( req->result != NULL ) { /* if we received anything */
124                         /* pop off the first message in the list */
125                         debug_handler( "app_request_recv received a message, returning it");
126                         osrf_message* ret_msg = req->result;
127                         osrf_message* tmp_msg = ret_msg->next;
128                         req->result = tmp_msg;
129                         return ret_msg;
130                 }
131                 if( req->complete )
132                         return NULL;
133
134                 if(req->reset_timeout) {
135                         remaining = (time_t) timeout;
136                         req->reset_timeout = 0;
137                         debug_handler("Recevied a timeout reset");
138                 } else {
139                         remaining -= (int) (time(NULL) - start);
140                 }
141         }
142
143         debug_handler("Returning NULL from app_request_recv after timeout");
144         return NULL;
145 }
146
147 /** Resend this requests original request message */
148 int _osrf_app_request_resend( osrf_app_request* req ) {
149         if(req == NULL) return 0;
150         if(!req->complete) {
151                 debug_handler( "Resending request [%d]", req->request_id );
152                 return _osrf_app_session_send( req->session, req->payload );
153         }
154         return 1;
155 }
156
157
158
159 // --------------------------------------------------------------------------
160 // --------------------------------------------------------------------------
161 // Session API
162 // --------------------------------------------------------------------------
163
164 /** returns a session from the global session hash */
165 osrf_app_session* osrf_app_session_find_session( char* session_id ) {
166         osrf_app_session* ptr = app_session_cache;
167         while( ptr != NULL ) {
168                 if( !strcmp(ptr->session_id,session_id) )
169                         return ptr;
170                 ptr = ptr->next;
171         }
172         return NULL;
173 }
174
175
176 /** adds a session to the global session cache */
177 void _osrf_app_session_push_session( osrf_app_session* session ) {
178
179         if( app_session_cache == NULL ) {
180                 app_session_cache = session;
181                 return;
182         }
183
184         osrf_app_session* ptr = app_session_cache;
185         while( ptr != NULL ) {
186                 if( !strcmp(ptr->session_id, session->session_id) )
187                         return;
188                 if( ptr->next == NULL ) {
189                         ptr->next = session;
190                         return;
191                 }
192                 ptr = ptr->next;
193         }
194 }
195
196
197 /** unlinks from global session cache */
198 void _osrf_app_session_remove_session( char* session_id ) {
199
200         if( app_session_cache == NULL )
201                 return;
202
203         debug_handler( "App Session removing session [%s] from global cache", session_id );
204         if( !strcmp(app_session_cache->session_id, session_id) ) {
205                 if( app_session_cache->next != NULL ) {
206                         osrf_app_session* next = app_session_cache->next;
207                         app_session_cache = next;
208                         return;
209                 } else {
210                         app_session_cache = NULL;
211                         return;
212                 }
213         }
214
215         if( app_session_cache->next == NULL )
216                 return;
217
218         osrf_app_session* prev = app_session_cache;
219         osrf_app_session* ptr = prev->next;
220         while( ptr != NULL ) {
221                 if( ptr->session_id == session_id ) {
222                         osrf_app_session* tmp = ptr->next;
223                         prev->next = tmp;
224                         return;
225                 }
226                 ptr = ptr->next;
227         }
228 }
229
230 /** Allocates a initializes a new app_session */
231
232 osrf_app_session* osrfAppSessionClientInit( char* remote_service ) {
233         return osrf_app_client_session_init( remote_service );
234 }
235
236 osrf_app_session* osrf_app_client_session_init( char* remote_service ) {
237
238         osrf_app_session* session = safe_malloc(sizeof(osrf_app_session));      
239
240         session->transport_handle = osrf_system_get_transport_client();
241         if( session->transport_handle == NULL ) {
242                 warning_handler("No transport client for service 'client'");
243                 return NULL;
244         }
245
246         char target_buf[512];
247         memset(target_buf,0,512);
248
249         osrfStringArray* arr = osrfNewStringArray(8);
250         osrfConfigGetValueList(NULL, arr, "/domains/domain");
251         char* domain = osrfStringArrayGetString(arr, 0);
252         char* router_name = osrfConfigGetValue(NULL, "/router_name");
253         
254         sprintf( target_buf, "%s@%s/%s",  router_name, domain, remote_service );
255         osrfStringArrayFree(arr);
256         //free(domain);
257         free(router_name);
258
259         session->request_queue = NULL;
260         session->remote_id = strdup(target_buf);
261         session->orig_remote_id = strdup(session->remote_id);
262         session->remote_service = strdup(remote_service);
263
264         #ifdef ASSUME_STATELESS
265         session->stateless = 1;
266         debug_handler("%s session is stateless", remote_service );
267         #else
268         session->stateless = 0;
269         debug_handler("%s session is NOT stateless", remote_service );
270         #endif
271
272         /* build a chunky, random session id */
273         char id[256];
274         memset(id,0,256);
275
276         sprintf(id, "%lf.%d%d", get_timestamp_millis(), (int)time(NULL), getpid());
277         session->session_id = strdup(id);
278         debug_handler( "Building a new client session with id [%s] [%s]", 
279                         session->remote_service, session->session_id );
280
281         session->thread_trace = 0;
282         session->state = OSRF_SESSION_DISCONNECTED;
283         session->type = OSRF_SESSION_CLIENT;
284         session->next = NULL;
285         _osrf_app_session_push_session( session );
286         return session;
287 }
288
289 osrf_app_session* osrf_app_server_session_init( 
290                 char* session_id, char* our_app, char* remote_id ) {
291
292         info_handler("Initing server session with session id %s, service %s,"
293                         " and remote_id %s", session_id, our_app, remote_id );
294
295         osrf_app_session* session = osrf_app_session_find_session( session_id );
296         if(session) return session;
297
298         session = safe_malloc(sizeof(osrf_app_session));        
299
300         session->transport_handle = osrf_system_get_transport_client();
301         if( session->transport_handle == NULL ) {
302                 warning_handler("No transport client for service '%s'", our_app );
303                 return NULL;
304         }
305
306         int stateless = 0;
307         char* statel = osrf_settings_host_value("/apps/%s/stateless", our_app );
308         if(statel) stateless = atoi(statel);
309         free(statel);
310
311
312         session->request_queue = NULL;
313         session->remote_id = strdup(remote_id);
314         session->orig_remote_id = strdup(remote_id);
315         session->session_id = strdup(session_id);
316         session->remote_service = strdup(our_app);
317         session->stateless = stateless;
318
319         #ifdef ASSUME_STATELESS
320         session->stateless = 1;
321         #endif
322
323         session->thread_trace = 0;
324         session->state = OSRF_SESSION_DISCONNECTED;
325         session->type = OSRF_SESSION_SERVER;
326         session->next = NULL;
327
328         _osrf_app_session_push_session( session );
329         return session;
330
331 }
332
333
334
335 /** frees memory held by a session */
336 void _osrf_app_session_free( osrf_app_session* session ){
337         if(session==NULL)
338                 return;
339         
340         free(session->remote_id);
341         free(session->orig_remote_id);
342         free(session->session_id);
343         free(session->remote_service);
344         free(session);
345 }
346
347 int osrfAppSessionMakeRequest(
348                 osrf_app_session* session, jsonObject* params, 
349                 char* method_name, int protocol, string_array* param_strings ) {
350
351         return osrf_app_session_make_req( session, params, 
352                         method_name, protocol, param_strings );
353 }
354
355 int osrf_app_session_make_req( 
356                 osrf_app_session* session, jsonObject* params, 
357                 char* method_name, int protocol, string_array* param_strings ) {
358         if(session == NULL) return -1;
359
360         osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
361         osrf_message_set_method(req_msg, method_name);
362         if(params) {
363                 osrf_message_set_params(req_msg, params);
364
365         } else {
366
367                 if(param_strings) {
368                         int i;
369                         for(i = 0; i!= param_strings->size ; i++ ) {
370                                 osrf_message_add_param(req_msg,
371                                         string_array_get_string(param_strings,i));
372                         }
373                 }
374         }
375
376         osrf_app_request* req = _osrf_app_request_init( session, req_msg );
377         if(_osrf_app_session_send( session, req_msg ) ) {
378                 warning_handler( "Error sending request message [%d]", session->thread_trace );
379                 return -1;
380         }
381
382         _osrf_app_session_push_request( session, req );
383         return req->request_id;
384 }
385
386
387
388 /** Adds an app_request to the request set */
389 void _osrf_app_session_push_request( osrf_app_session* session, osrf_app_request* req ){
390         if(session == NULL || req == NULL)
391                 return;
392
393         debug_handler( "Pushing [%d] onto requeust queue for session [%s] [%s]",
394                         req->request_id, session->remote_service, session->session_id );
395
396         if(session->request_queue == NULL) 
397                 session->request_queue = req;
398         else {
399                 osrf_app_request* req2 = session->request_queue->next;
400                 session->request_queue = req;
401                 req->next = req2;
402         }
403 }
404
405
406
407 /** Removes an app_request from this session request set */
408 void _osrf_app_session_remove_request( osrf_app_session* session, osrf_app_request* req ){
409         if(session == NULL || req == NULL)
410                 return;
411
412         if(session->request_queue == NULL)
413                 return;
414
415         debug_handler("Removing request [%d] from session [%s] [%s]",
416                         req->request_id, session->remote_service, session->session_id );
417
418         osrf_app_request* first = session->request_queue;
419         if(first->request_id == req->request_id) {
420                 session->request_queue = first->next;
421                 return;
422                 /*
423                 if(first->next == NULL) { 
424                         session->request_queue = NULL;
425                 } else {
426                         osrf_app_request* tmp = first->next;
427                         session->request_queue = tmp;
428                 }
429                 */
430         }
431
432         osrf_app_request* lead = first->next;
433
434         while( lead != NULL ) {
435                 if(lead->request_id == req->request_id) {
436                         first->next = lead->next;
437                         return;
438                 }
439                 first = lead;
440                 lead = lead->next;
441         }
442 }
443
444
445 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
446         if(session == NULL)
447                 return;
448
449         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
450         if(req) req->complete = 1;
451 }
452
453 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
454         if(session == NULL)
455                 return 0;
456         osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
457         if(req)
458                 return req->complete;
459         return 0;
460 }
461
462 /** Returns the app_request with the given request_id (request_id) */
463 osrf_app_request* _osrf_app_session_get_request( 
464                 osrf_app_session* session, int request_id ){
465         if(session == NULL)
466                 return NULL;
467
468         osrf_app_request* req = session->request_queue;
469         while( req != NULL ) {
470                 if(req->request_id == request_id)
471                         return req;
472                 req = req->next;
473         }
474         return NULL;
475 }
476
477
478 /** Resets the remote connection id to that of the original*/
479 void osrf_app_session_reset_remote( osrf_app_session* session ){
480         if( session==NULL )
481                 return;
482
483         free(session->remote_id);
484         debug_handler( "App Session [%s] [%s] resetting remote id to %s",
485                         session->remote_service, session->session_id, session->orig_remote_id );
486
487         session->remote_id = strdup(session->orig_remote_id);
488 }
489
490 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
491         if(session == NULL)
492                 return;
493         if( session->remote_id )
494                 free(session->remote_id );
495         session->remote_id = strdup( remote_id );
496 }
497
498 /** pushes the given message into the result list of the app_request
499   with the given request_id */
500 int osrf_app_session_push_queue( 
501                 osrf_app_session* session, osrf_message* msg ){
502
503         if(session == NULL || msg == NULL)
504                 return 0;
505
506         osrf_app_request* req = session->request_queue;
507
508         if(req == NULL) return 0;
509
510         while( req != NULL ) {
511                 if(req->request_id == msg->thread_trace) {
512                         _osrf_app_request_push_queue( req, msg );
513                         return 1;
514                 }
515                 req = req->next;
516         } 
517
518         return 0;
519         
520 }
521
522 /** Attempts to connect to the remote service */
523 int osrf_app_session_connect(osrf_app_session* session){
524         
525         if(session == NULL)
526                 return 0;
527
528         if(session->state == OSRF_SESSION_CONNECTED) {
529                 return 1;
530         }
531
532         int timeout = 5; /* XXX CONFIG VALUE */
533
534         debug_handler( "AppSession connecting to %s", session->remote_id );
535
536         /* defaulting to protocol 1 for now */
537         osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 );
538         osrf_app_session_reset_remote( session );
539         session->state = OSRF_SESSION_CONNECTING;
540         int ret = _osrf_app_session_send( session, con_msg );
541         osrf_message_free(con_msg);
542         if(ret) return 0;
543
544         time_t start = time(NULL);      
545         time_t remaining = (time_t) timeout;
546
547         while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
548                 osrf_app_session_queue_wait( session, remaining );
549                 remaining -= (int) (time(NULL) - start);
550         }
551
552         if(session->state == OSRF_SESSION_CONNECTED)
553                 debug_handler(" * Connected Successfully to %s", session->remote_service );
554
555         if(session->state != OSRF_SESSION_CONNECTED)
556                 return 0;
557
558         return 1;
559 }
560
561
562
563 /** Disconnects from the remote service */
564 int osrf_app_session_disconnect( osrf_app_session* session){
565         if(session == NULL)
566                 return 1;
567
568         if(session->state == OSRF_SESSION_DISCONNECTED)
569                 return 1;
570
571         if(session->stateless && session->state != OSRF_SESSION_CONNECTED) {
572                 debug_handler(
573                                 "Exiting disconnect on stateless session %s", 
574                                 session->session_id);
575                 return 1;
576         }
577
578         debug_handler( "AppSession disconnecting from %s", session->remote_id );
579
580         osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
581         session->state = OSRF_SESSION_DISCONNECTED;
582         _osrf_app_session_send( session, dis_msg );
583
584         osrf_message_free( dis_msg );
585         osrf_app_session_reset_remote( session );
586         return 1;
587 }
588
589 int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
590         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
591         return _osrf_app_request_resend( req );
592 }
593
594
595 int osrfAppSessionSendBatch( osrfAppSession* session, osrf_message* msgs[], int size ) {
596
597         if( !(session && msgs && size > 0) ) return 0;
598         int retval = 0;
599
600
601         osrfMessage* msg = msgs[0];
602
603         if(msg) {
604
605                 osrf_app_session_queue_wait( session, 0 );
606
607                 /* if we're not stateless and not connected and the first 
608                         message is not a connect message, then we do the connect first */
609                 if(session->stateless) {
610                                 osrf_app_session_reset_remote(session);
611
612                 } else {
613
614                         if( (msg->m_type != CONNECT) && (msg->m_type != DISCONNECT) &&
615                                 (session->state != OSRF_SESSION_CONNECTED) ) {
616                                 if(!osrf_app_session_connect( session )) 
617                                         return 0;
618                         }
619                 }
620         }
621
622         char* string = osrfMessageSerializeBatch(msgs, size);
623
624         if( string ) {
625
626                 transport_message* t_msg = message_init( 
627                                 string, "", session->session_id, session->remote_id, NULL );
628         
629                 debug_handler("Session [%s] [%s]  sending to %s \nData: %s", 
630                                 session->remote_service, session->session_id, t_msg->recipient, string );
631
632                 retval = client_send_message( session->transport_handle, t_msg );
633         
634                 free(string);
635                 message_free( t_msg );
636         }
637
638         return retval; 
639
640 }
641
642
643
644 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
645         if( !(session && msg) ) return 0;
646         osrfMessage* a[1];
647         a[0] = msg;
648         return osrfAppSessionSendBatch( session, a, 1 );
649 }
650
651
652
653
654 /**  Waits up to 'timeout' seconds for some data to arrive.
655   * Any data that arrives will be processed according to its
656   * payload and message type.  This method will return after
657   * any data has arrived.
658   */
659 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout ){
660         if(session == NULL) return 0;
661         int ret_val = 0;
662         debug_handler( "AppSession in queue_wait with timeout %d", timeout );
663         ret_val = osrf_stack_entry_point(session->transport_handle, timeout);
664         return ret_val;
665 }
666
667 /** Disconnects (if client) and removes the given session from the global session cache 
668   * ! This free's all attached app_requests ! 
669   */
670 void osrfAppSessionFree( osrfAppSession* ses ) {
671         osrf_app_session_destroy( ses );
672 }
673
674
675 void osrf_app_session_destroy( osrf_app_session* session ){
676         if(session == NULL) return;
677
678         debug_handler( "AppSession [%s] [%s] destroying self and deleting requests", 
679                         session->remote_service, session->session_id );
680         if(session->type == OSRF_SESSION_CLIENT 
681                         && session->state != OSRF_SESSION_DISCONNECTED ) { /* disconnect if we're a client */
682                 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
683                 _osrf_app_session_send( session, dis_msg ); 
684                 osrf_message_free(dis_msg);
685         }
686         //session->state = OSRF_SESSION_DISCONNECTED;
687         _osrf_app_session_remove_session(session->session_id);
688
689         osrf_app_request* req;
690         while( session->request_queue != NULL ) {
691                 req = session->request_queue->next;
692                 _osrf_app_request_free( session->request_queue );
693                 session->request_queue = req;
694         }
695
696         _osrf_app_session_free( session );
697 }
698
699 osrf_message* osrfAppSessionRequestRecv(
700                 osrf_app_session* session, int req_id, int timeout ) {
701         return osrf_app_session_request_recv( session, req_id, timeout );
702 }
703 osrf_message* osrf_app_session_request_recv( 
704                 osrf_app_session* session, int req_id, int timeout ) {
705         if(req_id < 0 || session == NULL)
706                 return NULL;
707         osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
708         return _osrf_app_request_recv( req, timeout );
709 }
710
711
712
713 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data ) {
714         if(!ses || ! data ) return -1;
715
716         osrf_message* msg = osrf_message_init( RESULT, requestId, 1 );
717         char* json = jsonObjectToJSON( data );
718         osrf_message_set_result_content( msg, json );
719         _osrf_app_session_send( ses, msg ); 
720
721         free(json);
722         osrf_message_free( msg );
723
724         return 0;
725 }
726
727
728 int osrfAppRequestRespondComplete( 
729                 osrfAppSession* ses, int requestId, jsonObject* data ) {
730
731         osrf_message* payload = osrf_message_init( RESULT, requestId, 1 );
732         osrf_message_set_status_info( payload, NULL, "OK", OSRF_STATUS_OK );
733
734         char* json = jsonObjectToJSON( data );
735         osrf_message_set_result_content( payload, json );
736         free(json);
737
738         osrf_message* status = osrf_message_init( STATUS, requestId, 1);
739         osrf_message_set_status_info( status, "osrfConnectStatus", "Request Complete", OSRF_STATUS_COMPLETE );
740
741         osrfMessage* ms[2];
742         ms[0] = payload;
743         ms[1] = status;
744
745         osrfAppSessionSendBatch( ses, ms, 2 );
746
747         osrf_message_free( payload );
748         osrf_message_free( status );
749
750         /* join and free */
751
752         return 0;
753 }
754
755
756
757 int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message ) {
758
759         if(ses) {
760                 osrf_message* msg = osrf_message_init( STATUS, reqId, 1);
761                 osrf_message_set_status_info( msg, name, message, type );
762                 _osrf_app_session_send( ses, msg ); 
763                 osrf_message_free( msg );
764                 return 0;
765         }
766         return -1;
767 }
768
769
770
771
772
773