#include <time.h>
/* the global app_session cache */
-osrf_app_session* app_session_cache;
-
+osrfHash* osrfAppSessionCache = NULL;
// --------------------------------------------------------------------------
}
/** Frees memory used by an app_request object */
-void _osrf_app_request_free( osrf_app_request * req ){
+void _osrf_app_request_free( void * req ){
if( req == NULL ) return;
+ osrfAppRequest* r = (osrfAppRequest*) req;
- if( req->payload ) {
- osrf_message_free( req->payload );
- }
+ if( r->payload ) osrf_message_free( r->payload );
/*
osrf_message* cur_msg = req->result;
osrf_message_free( req->payload );
*/
- free( req );
+ free( r );
}
/** Pushes the given message onto the list of 'responses' to this request */
osrf_app_session* session, int req_id ){
if(session == NULL) return;
- osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
+ osrf_app_request* req = osrfListGetIndex( session->request_queue, req_id );
if(req == NULL) return;
- _osrf_app_session_remove_request( req->session, req );
- _osrf_app_request_free( req );
+ osrfListRemove( req->session->request_queue, req->request_id );
}
void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id ) {
if(session == NULL) return;
debug_handler("Resetting request timeout %d", req_id );
- osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
+ osrf_app_request* req = osrfListGetIndex( session->request_queue, req_id );
if(req == NULL) return;
req->reset_timeout = 1;
}
/** returns a session from the global session hash */
osrf_app_session* osrf_app_session_find_session( char* session_id ) {
- osrf_app_session* ptr = app_session_cache;
- while( ptr != NULL ) {
- if( !strcmp(ptr->session_id,session_id) )
- return ptr;
- ptr = ptr->next;
- }
+ if(session_id) return osrfHashGet(osrfAppSessionCache, session_id);
return NULL;
}
/** adds a session to the global session cache */
void _osrf_app_session_push_session( osrf_app_session* session ) {
-
- if( app_session_cache == NULL ) {
- app_session_cache = session;
- return;
- }
-
- osrf_app_session* ptr = app_session_cache;
- while( ptr != NULL ) {
- if( !strcmp(ptr->session_id, session->session_id) )
- return;
- if( ptr->next == NULL ) {
- ptr->next = session;
- return;
- }
- ptr = ptr->next;
- }
-}
-
-
-/** unlinks from global session cache */
-void _osrf_app_session_remove_session( char* session_id ) {
-
- if( app_session_cache == NULL )
- return;
-
- debug_handler( "App Session removing session [%s] from global cache", session_id );
- if( !strcmp(app_session_cache->session_id, session_id) ) {
- if( app_session_cache->next != NULL ) {
- osrf_app_session* next = app_session_cache->next;
- app_session_cache = next;
- return;
- } else {
- app_session_cache = NULL;
- return;
- }
- }
-
- if( app_session_cache->next == NULL )
- return;
-
- osrf_app_session* prev = app_session_cache;
- osrf_app_session* ptr = prev->next;
- while( ptr != NULL ) {
- if( ptr->session_id == session_id ) {
- osrf_app_session* tmp = ptr->next;
- prev->next = tmp;
- return;
- }
- ptr = ptr->next;
- }
+ if(!session) return;
+ if( osrfAppSessionCache == NULL ) osrfAppSessionCache = osrfNewHash();
+ if( osrfHashGet( osrfAppSessionCache, session->session_id ) ) return;
+ osrfHashSet( osrfAppSessionCache, session, session->session_id );
}
/** Allocates a initializes a new app_session */
//free(domain);
free(router_name);
- session->request_queue = NULL;
+ session->request_queue = osrfNewList();
+ session->request_queue->freeItem = &_osrf_app_request_free;
session->remote_id = strdup(target_buf);
session->orig_remote_id = strdup(session->remote_id);
session->remote_service = strdup(remote_service);
session->thread_trace = 0;
session->state = OSRF_SESSION_DISCONNECTED;
session->type = OSRF_SESSION_CLIENT;
- session->next = NULL;
+ //session->next = NULL;
_osrf_app_session_push_session( session );
return session;
}
free(statel);
- session->request_queue = NULL;
+ session->request_queue = osrfNewList();
+ session->request_queue->freeItem = &_osrf_app_request_free;
session->remote_id = strdup(remote_id);
session->orig_remote_id = strdup(remote_id);
session->session_id = strdup(session_id);
session->thread_trace = 0;
session->state = OSRF_SESSION_DISCONNECTED;
session->type = OSRF_SESSION_SERVER;
- session->next = NULL;
_osrf_app_session_push_session( session );
return session;
free(session->orig_remote_id);
free(session->session_id);
free(session->remote_service);
+ osrfListFree(session->request_queue);
free(session);
}
return -1;
}
- _osrf_app_session_push_request( session, req );
- return req->request_id;
-}
-
-
-
-/** Adds an app_request to the request set */
-void _osrf_app_session_push_request( osrf_app_session* session, osrf_app_request* req ){
- if(session == NULL || req == NULL)
- return;
-
debug_handler( "Pushing [%d] onto requeust queue for session [%s] [%s]",
req->request_id, session->remote_service, session->session_id );
-
- if(session->request_queue == NULL)
- session->request_queue = req;
- else {
- osrf_app_request* req2 = session->request_queue->next;
- session->request_queue = req;
- req->next = req2;
- }
-}
-
-
-
-/** Removes an app_request from this session request set */
-void _osrf_app_session_remove_request( osrf_app_session* session, osrf_app_request* req ){
- if(session == NULL || req == NULL)
- return;
-
- if(session->request_queue == NULL)
- return;
-
- debug_handler("Removing request [%d] from session [%s] [%s]",
- req->request_id, session->remote_service, session->session_id );
-
- osrf_app_request* first = session->request_queue;
- if(first->request_id == req->request_id) {
- session->request_queue = first->next;
- return;
- /*
- if(first->next == NULL) {
- session->request_queue = NULL;
- } else {
- osrf_app_request* tmp = first->next;
- session->request_queue = tmp;
- }
- */
- }
-
- osrf_app_request* lead = first->next;
-
- while( lead != NULL ) {
- if(lead->request_id == req->request_id) {
- first->next = lead->next;
- return;
- }
- first = lead;
- lead = lead->next;
- }
+ osrfListSet( session->request_queue, req, req->request_id );
+ return req->request_id;
}
-
void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
if(session == NULL)
return;
- osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
+ osrf_app_request* req = osrfListGetIndex( session->request_queue, request_id );
if(req) req->complete = 1;
}
int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
if(session == NULL)
return 0;
- osrf_app_request* req = _osrf_app_session_get_request( session, request_id );
+ osrf_app_request* req = osrfListGetIndex( session->request_queue, request_id );
if(req)
return req->complete;
return 0;
}
-/** Returns the app_request with the given request_id (request_id) */
-osrf_app_request* _osrf_app_session_get_request(
- osrf_app_session* session, int request_id ){
- if(session == NULL)
- return NULL;
-
- osrf_app_request* req = session->request_queue;
- while( req != NULL ) {
- if(req->request_id == request_id)
- return req;
- req = req->next;
- }
- return NULL;
-}
-
/** Resets the remote connection id to that of the original*/
void osrf_app_session_reset_remote( osrf_app_session* session ){
with the given request_id */
int osrf_app_session_push_queue(
osrf_app_session* session, osrf_message* msg ){
+ if(session == NULL || msg == NULL) return 0;
- if(session == NULL || msg == NULL)
- return 0;
-
- osrf_app_request* req = session->request_queue;
-
+ osrf_app_request* req = osrfListGetIndex( session->request_queue, msg->thread_trace );
if(req == NULL) return 0;
-
- while( req != NULL ) {
- if(req->request_id == msg->thread_trace) {
- _osrf_app_request_push_queue( req, msg );
- return 1;
- }
- req = req->next;
- }
+ _osrf_app_request_push_queue( req, msg );
return 0;
-
}
/** Attempts to connect to the remote service */
}
int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
- osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
+ osrf_app_request* req = osrfListGetIndex( session->request_queue, req_id );
return _osrf_app_request_resend( req );
}
_osrf_app_session_send( session, dis_msg );
osrf_message_free(dis_msg);
}
- //session->state = OSRF_SESSION_DISCONNECTED;
- _osrf_app_session_remove_session(session->session_id);
-
- osrf_app_request* req;
- while( session->request_queue != NULL ) {
- req = session->request_queue->next;
- _osrf_app_request_free( session->request_queue );
- session->request_queue = req;
- }
+ osrfHashRemove( osrfAppSessionCache, session->session_id );
_osrf_app_session_free( session );
}
osrf_app_session* session, int req_id, int timeout ) {
if(req_id < 0 || session == NULL)
return NULL;
- osrf_app_request* req = _osrf_app_session_get_request( session, req_id );
+ osrf_app_request* req = osrfListGetIndex( session->request_queue, req_id );
return _osrf_app_request_recv( req, timeout );
}
return 0;
}
-
-
int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message ) {
if(ses) {