1 #include "opensrf/osrf_app_session.h"
4 /* the global app_session cache */
5 osrf_app_session* app_session_cache;
9 // --------------------------------------------------------------------------
10 // --------------------------------------------------------------------------
12 // --------------------------------------------------------------------------
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 ) {
18 osrf_app_request* req =
19 (osrf_app_request*) safe_malloc(sizeof(osrf_app_request));
21 req->session = session;
22 req->request_id = msg->thread_trace;
31 /** Frees memory used by an app_request object */
32 void _osrf_app_request_free( osrf_app_request * req ){
33 if( req == NULL ) return;
36 osrf_message_free( req->payload );
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 );
45 osrf_message_free( req->payload );
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)
58 result->next = req->result;
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 ){
67 if(session == NULL) return;
68 osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
69 if(req == NULL) return;
70 _osrf_app_session_remove_request( req->session, req );
71 _osrf_app_request_free( req );
74 /** Checks the receive queue for messages. If any are found, the first
75 * is popped off and returned. Otherwise, this method will wait at most timeout
76 * seconds for a message to appear in the receive queue. Once it arrives it is returned.
77 * If no messages arrive in the timeout provided, null is returned.
79 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) {
81 if(req == NULL) return NULL;
83 if( req->result != NULL ) {
84 debug_handler("app_request receive already has a message, returning it");
85 /* pop off the first message in the list */
86 osrf_message* tmp_msg = req->result->next;
87 req->result = tmp_msg;
91 time_t start = time(NULL);
92 time_t remaining = (time_t) timeout;
94 while( remaining >= 0 ) {
95 /* tell the session to wait for stuff */
96 debug_handler( "In app_request receive with remaining time [%d]", (int) remaining );
98 osrf_app_session_queue_wait( req->session, 0 );
100 if( req->result != NULL ) { /* if we received anything */
101 /* pop off the first message in the list */
102 debug_handler( "app_request_recv received a message, returning it");
103 osrf_message* ret_msg = req->result;
104 osrf_message* tmp_msg = ret_msg->next;
105 req->result = tmp_msg;
112 osrf_app_session_queue_wait( req->session, (int) remaining );
114 if( req->result != NULL ) { /* if we received anything */
115 /* pop off the first message in the list */
116 debug_handler( "app_request_recv received a message, returning it");
117 osrf_message* ret_msg = req->result;
118 osrf_message* tmp_msg = ret_msg->next;
119 req->result = tmp_msg;
125 remaining -= (int) (time(NULL) - start);
131 /** Resend this requests original request message */
132 int _osrf_app_request_resend( osrf_app_request* req ) {
133 if(req == NULL) return 0;
135 debug_handler( "Resending request [%d]", req->request_id );
136 return _osrf_app_session_send( req->session, req->payload );
143 // --------------------------------------------------------------------------
144 // --------------------------------------------------------------------------
146 // --------------------------------------------------------------------------
148 /** returns a session from the global session hash */
149 osrf_app_session* osrf_app_session_find_session( char* session_id ) {
150 osrf_app_session* ptr = app_session_cache;
151 debug_handler("Searching for session in global cache with id [%s]", session_id );
152 while( ptr != NULL ) {
153 if( !strcmp(ptr->session_id,session_id) )
161 /** adds a session to the global session cache */
162 void _osrf_app_session_push_session( osrf_app_session* session ) {
164 if( app_session_cache == NULL ) {
165 app_session_cache = session;
169 osrf_app_session* ptr = app_session_cache;
170 debug_handler( "Pushing [%s] onto global session cache", session->session_id );
171 while( ptr != NULL ) {
172 if( !strcmp(ptr->session_id, session->session_id) )
174 if( ptr->next == NULL ) {
183 /** unlinks from global session cache */
184 void _osrf_app_session_remove_session( char* session_id ) {
186 if( app_session_cache == NULL )
189 debug_handler( "App Session removing session [%s] from global cache", session_id );
190 if( !strcmp(app_session_cache->session_id, session_id) ) {
191 if( app_session_cache->next != NULL ) {
192 osrf_app_session* next = app_session_cache->next;
193 app_session_cache = next;
196 app_session_cache = NULL;
201 if( app_session_cache->next == NULL )
204 osrf_app_session* prev = app_session_cache;
205 osrf_app_session* ptr = prev->next;
206 while( ptr != NULL ) {
207 if( ptr->session_id == session_id ) {
208 osrf_app_session* tmp = ptr->next;
216 /** Allocates a initializes a new app_session */
218 osrf_app_session* osrf_app_client_session_init( char* remote_service ) {
220 osrf_app_session* session = safe_malloc(sizeof(osrf_app_session));
222 session->transport_handle = osrf_system_get_transport_client();
223 if( session->transport_handle == NULL ) {
224 warning_handler("No transport client for service 'client'");
228 char target_buf[512];
229 memset(target_buf,0,512);
230 char* domain = config_value( "opensrf.bootstrap", "//bootstrap/domains/domain1" ); /* just the first for now */
231 char* router_name = config_value( "opensrf.bootstrap", "//bootstrap/router_name" );
232 sprintf( target_buf, "%s@%s/%s", router_name, domain, remote_service );
236 session->request_queue = NULL;
237 session->remote_id = strdup(target_buf);
238 session->orig_remote_id = strdup(session->remote_id);
240 /* build a chunky, random session id */
243 //srand((int)time(NULL));
245 sprintf(id, "%lf.%d%d", get_timestamp_millis(), (int)time(NULL), getpid());
246 session->session_id = strdup(id);
247 debug_handler( "Building a new client session with id [%s]", session->session_id );
249 session->thread_trace = 0;
250 session->state = OSRF_SESSION_DISCONNECTED;
251 session->type = OSRF_SESSION_CLIENT;
252 session->next = NULL;
253 _osrf_app_session_push_session( session );
257 osrf_app_session* osrf_app_server_session_init(
258 char* session_id, char* our_app, char* remote_service, char* remote_id ) {
260 osrf_app_session* session = osrf_app_session_find_session( session_id );
264 debug_handler( "Building a new server session with id [%s]", session_id );
266 session = safe_malloc(sizeof(osrf_app_session));
268 session->transport_handle = osrf_system_get_transport_client();
269 if( session->transport_handle == NULL ) {
270 warning_handler("No transport client for service '%s'", our_app );
273 session->request_queue = NULL;
274 session->remote_id = strdup(remote_id);
275 session->orig_remote_id = strdup(remote_id);
276 session->session_id = strdup(session_id);
278 session->thread_trace = 0;
279 session->state = OSRF_SESSION_DISCONNECTED;
280 session->type = OSRF_SESSION_CLIENT;
281 session->next = NULL;
283 _osrf_app_session_push_session( session );
290 /** frees memory held by a session */
291 void _osrf_app_session_free( osrf_app_session* session ){
295 free(session->remote_id);
296 free(session->orig_remote_id);
297 free(session->session_id);
302 int osrf_app_session_make_request(
303 osrf_app_session* session, json* params, char* method_name, int protocol ) {
304 if(session == NULL) return -1;
306 osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
307 osrf_message_set_request_info( req_msg, method_name, params );
308 osrf_app_request* req = _osrf_app_request_init( session, req_msg );
309 if(!_osrf_app_session_send( session, req_msg ) ) {
310 warning_handler( "Error sending request message [%d]", session->thread_trace );
314 _osrf_app_session_push_request( session, req );
315 return req->request_id;
318 /** Adds an app_request to the request set */
319 void _osrf_app_session_push_request( osrf_app_session* session, osrf_app_request* req ){
320 if(session == NULL || req == NULL)
323 debug_handler( "Pushing [%d] onto requeust queue for session [%s]",
324 req->request_id, session->session_id );
326 if(session->request_queue == NULL)
327 session->request_queue = req;
329 osrf_app_request* req2 = session->request_queue->next;
330 session->request_queue = req;
337 /** Removes an app_request from this session request set */
338 void _osrf_app_session_remove_request( osrf_app_session* session, osrf_app_request* req ){
339 if(session == NULL || req == NULL)
342 if(session->request_queue == NULL)
345 debug_handler("Removing request [%d] from session [%s]",
346 req->request_id, session->session_id );
347 osrf_app_request* first = session->request_queue;
348 if(first->request_id == req->request_id) {
349 if(first->next == NULL) { /* only one in the list */
350 session->request_queue = NULL;
352 osrf_app_request* tmp = first->next;
353 session->request_queue = tmp;
357 osrf_app_request* lead = first->next;
359 while( lead != NULL ) {
360 if(lead->request_id == req->request_id) {
361 osrf_app_request* tmp = lead->next;
371 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
375 osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
376 if(req) req->complete = 1;
379 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
382 osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
384 return req->complete;
388 /** Returns the app_request with the given request_id (request_id) */
389 osrf_app_request* _osrf_app_session_get_request(
390 osrf_app_session* session, int request_id ){
394 debug_handler( "App Session searching for request [%d] in request queue",request_id );
395 osrf_app_request* req = session->request_queue;
396 while( req != NULL ) {
397 if(req->request_id == request_id)
405 /** Resets the remote connection id to that of the original*/
406 void osrf_app_session_reset_remote( osrf_app_session* session ){
410 free(session->remote_id);
411 debug_handler( "App Session [%s] resetting remote id to %s",
412 session->session_id, session->orig_remote_id );
414 session->remote_id = strdup(session->orig_remote_id);
417 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
420 if( session->remote_id )
421 free(session->remote_id );
422 session->remote_id = strdup( remote_id );
425 /** pushes the given message into the result list of the app_request
426 with the given request_id */
427 int osrf_app_session_push_queue(
428 osrf_app_session* session, osrf_message* msg ){
430 if(session == NULL || msg == NULL)
433 debug_handler( "AppSession pushing result for [%d] onto request payload queue",
436 osrf_app_request* req = session->request_queue;
439 warning_handler( "app_session has no app_requests in its queue yet we have a result for [%d]", msg->thread_trace );
443 debug_handler( "The first request in the request queue has tt [%d]", req->request_id );
445 while( req != NULL ) {
446 if(req->request_id == msg->thread_trace) {
447 debug_handler( "Found app_request for tt [%d]", msg->thread_trace );
448 _osrf_app_request_push_queue( req, msg );
458 /** Attempts to connect to the remote service */
459 int osrf_app_session_connect(osrf_app_session* session){
464 if(session->state == OSRF_SESSION_CONNECTED) {
465 debug_handler("Already Connected, returning");
469 int timeout = 5; /* XXX CONFIG VALUE */
471 debug_handler( "AppSession connecting to %s", session->remote_id );
473 /* defaulting to protocol 1 for now */
474 osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 );
475 osrf_app_session_reset_remote( session );
476 session->state = OSRF_SESSION_CONNECTING;
477 int ret = _osrf_app_session_send( session, con_msg );
478 osrf_message_free(con_msg);
481 time_t start = time(NULL);
482 time_t remaining = (time_t) timeout;
484 while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
485 osrf_app_session_queue_wait( session, remaining );
486 debug_handler("In connect while with state: %x, %d [%s]", session, session->state, session->session_id );
487 remaining -= (int) (time(NULL) - start);
490 if(session->state == OSRF_SESSION_CONNECTED)
491 debug_handler(" * Connected Successfully");
493 if(session->state != OSRF_SESSION_CONNECTED)
501 /** Disconnects from the remote service */
502 int osrf_app_session_disconnect( osrf_app_session* session){
506 if(session->state == OSRF_SESSION_DISCONNECTED)
508 debug_handler( "AppSession disconnecting from %s", session->remote_id );
510 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
511 session->state = OSRF_SESSION_DISCONNECTED;
512 _osrf_app_session_send( session, dis_msg );
514 osrf_message_free( dis_msg );
515 osrf_app_session_reset_remote( session );
519 int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
520 osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
521 return _osrf_app_request_resend( req );
524 /** Send the given message */
525 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
526 if(session == NULL) return 0;
529 osrf_app_session_queue_wait( session, 0 );
530 debug_handler( "AppSession sending type %d, and thread_trace %d",
531 msg->m_type, msg->thread_trace );
533 if( (msg->m_type != CONNECT) && (msg->m_type != DISCONNECT) &&
534 (session->state != OSRF_SESSION_CONNECTED) ) {
535 if(!osrf_app_session_connect( session ))
539 char* xml = osrf_message_to_xml(msg);
541 transport_message* t_msg = message_init(
542 xml, "", session->session_id, session->remote_id, NULL );
544 debug_handler("Sending XML:\n%s", xml );
545 ret_val = client_send_message( session->transport_handle, t_msg );
547 message_free( t_msg );
552 /** Waits up to 'timeout' seconds for some data to arrive.
553 * Any data that arrives will be processed according to its
554 * payload and message type. This method will return after
555 * any data has arrived.
557 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout ){
558 if(session == NULL) return 0;
560 debug_handler( "AppSession in queue_wait with timeout %d", timeout );
561 ret_val = osrf_stack_process(session->transport_handle, timeout );
565 /** Disconnects (if client) and removes the given session from the global session cache
566 * ! This free's all attached app_requests !
568 void osrf_app_session_destroy ( osrf_app_session* session ){
569 if(session == NULL) return;
571 debug_handler( "AppSession [%s] destroying self and deleting requests", session->session_id );
572 if(session->type == OSRF_SESSION_CLIENT
573 && session->state != OSRF_SESSION_DISCONNECTED ) { /* disconnect if we're a client */
574 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
575 _osrf_app_session_send( session, dis_msg );
576 osrf_message_free(dis_msg);
578 //session->state = OSRF_SESSION_DISCONNECTED;
579 _osrf_app_session_remove_session(session->session_id);
580 debug_handler("AppSession [%s] removed from cache", session->session_id );
582 osrf_app_request* req;
583 while( session->request_queue != NULL ) {
584 req = session->request_queue->next;
585 _osrf_app_request_free( session->request_queue );
586 session->request_queue = req;
589 _osrf_app_session_free( session );
592 osrf_message* osrf_app_session_request_recv(
593 osrf_app_session* session, int req_id, int timeout ) {
594 if(req_id < 0 || session == NULL)
596 osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
597 return _osrf_app_request_recv( req, timeout );