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