From ce036185efe2915a19c752269cf535427731cfec Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 2 Nov 2005 22:16:47 +0000 Subject: [PATCH] moved session code to osrfHash and osrfList instead of manual linked lists added some logging to the transport group stuff and a disconnect method git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@566 9efc2488-bf62-4759-914b-345cdb29e865 --- src/libstack/osrf_app_session.c | 203 ++++++---------------------------------- src/libstack/osrf_app_session.h | 19 ++-- src/libstack/osrf_hash.h | 4 + src/libstack/osrf_list.h | 5 + src/libstack/osrf_transgroup.c | 27 +++++- src/libstack/osrf_transgroup.h | 4 +- 6 files changed, 74 insertions(+), 188 deletions(-) diff --git a/src/libstack/osrf_app_session.c b/src/libstack/osrf_app_session.c index ebc35a4..c41945d 100644 --- a/src/libstack/osrf_app_session.c +++ b/src/libstack/osrf_app_session.c @@ -2,8 +2,7 @@ #include /* the global app_session cache */ -osrf_app_session* app_session_cache; - +osrfHash* osrfAppSessionCache = NULL; // -------------------------------------------------------------------------- @@ -29,12 +28,11 @@ osrf_app_request* _osrf_app_request_init( } /** 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; @@ -46,7 +44,7 @@ void _osrf_app_request_free( osrf_app_request * req ){ osrf_message_free( req->payload ); */ - free( req ); + free( r ); } /** Pushes the given message onto the list of 'responses' to this request */ @@ -66,17 +64,16 @@ void osrf_app_session_request_finish( 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; } @@ -163,68 +160,17 @@ int _osrf_app_request_resend( osrf_app_request* req ) { /** 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 */ @@ -256,7 +202,8 @@ osrf_app_session* osrf_app_client_session_init( char* remote_service ) { //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); @@ -281,7 +228,7 @@ osrf_app_session* osrf_app_client_session_init( char* 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; } @@ -309,7 +256,8 @@ osrf_app_session* osrf_app_server_session_init( 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); @@ -323,7 +271,6 @@ osrf_app_session* osrf_app_server_session_init( 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; @@ -341,6 +288,7 @@ void _osrf_app_session_free( osrf_app_session* session ){ free(session->orig_remote_id); free(session->session_id); free(session->remote_service); + osrfListFree(session->request_queue); free(session); } @@ -379,101 +327,29 @@ int osrf_app_session_make_req( 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 ){ @@ -499,24 +375,13 @@ void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) { 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 */ @@ -587,7 +452,7 @@ int osrf_app_session_disconnect( osrf_app_session* session){ } 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 ); } @@ -683,16 +548,8 @@ void osrf_app_session_destroy( osrf_app_session* session ){ _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 ); } @@ -704,7 +561,7 @@ osrf_message* osrf_app_session_request_recv( 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 ); } @@ -752,8 +609,6 @@ int osrfAppRequestRespondComplete( return 0; } - - int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message ) { if(ses) { diff --git a/src/libstack/osrf_app_session.h b/src/libstack/osrf_app_session.h index 8489cf2..9efc8f9 100644 --- a/src/libstack/osrf_app_session.h +++ b/src/libstack/osrf_app_session.h @@ -7,6 +7,8 @@ #include "osrf_system.h" #include "opensrf/string_array.h" #include "osrfConfig.h" +#include "osrf_hash.h" +#include "osrf_list.h" #include "objson/object.h" #include "objson/json_parser.h" @@ -38,9 +40,6 @@ struct osrf_app_request_struct { /* if set to true, then a call that is waiting on a response, will reset the timeout and set this variable back to false */ int reset_timeout; - - /** So we can be listified */ - struct osrf_app_request_struct* next; }; typedef struct osrf_app_request_struct osrf_app_request; typedef struct osrf_app_request_struct osrfAppRequest; @@ -50,7 +49,10 @@ struct osrf_app_session_struct { /** Our messag passing object */ transport_client* transport_handle; /** Cache of active app_request objects */ - osrf_app_request* request_queue; + + //osrf_app_request* request_queue; + + osrfList* request_queue; /** The original remote id of the remote service we're talking to */ char* orig_remote_id; @@ -74,10 +76,6 @@ struct osrf_app_session_struct { /** SERVER or CLIENT */ enum OSRF_SESSION_TYPE type; - - /** So we can be listified */ - struct osrf_app_session_struct* next; - }; typedef struct osrf_app_session_struct osrf_app_session; typedef struct osrf_app_session_struct osrfAppSession; @@ -175,7 +173,7 @@ void osrfAppSessionFree( osrfAppSession* ); osrf_app_request* _osrf_app_request_init( osrf_app_session* session, osrf_message* msg ); /** Frees memory used by an app_request object */ -void _osrf_app_request_free( osrf_app_request * req ); +void _osrf_app_request_free( void * req ); /** Pushes the given message onto the list of 'responses' to this request */ void _osrf_app_request_push_queue( osrf_app_request*, osrf_message* payload ); @@ -208,9 +206,6 @@ void _osrf_app_session_free( osrf_app_session* ); /** adds a session to the global session cache */ void _osrf_app_session_push_session( osrf_app_session* ); -/** removes from global session cache */ -void _osrf_app_session_remove_session( char* session_id ); - /** Adds an app_request to the request set */ void _osrf_app_session_push_request( osrf_app_session*, osrf_app_request* req ); diff --git a/src/libstack/osrf_hash.h b/src/libstack/osrf_hash.h index abaacc5..0290e23 100644 --- a/src/libstack/osrf_hash.h +++ b/src/libstack/osrf_hash.h @@ -1,3 +1,6 @@ +#ifndef OSRF_HASH_H +#define OSRF_HASH_H + #include #include "opensrf/utils.h" #include "opensrf/string_array.h" @@ -81,3 +84,4 @@ void osrfHashIteratorFree( osrfHashIterator* itr ); void osrfHashIteratorReset( osrfHashIterator* itr ); +#endif diff --git a/src/libstack/osrf_list.h b/src/libstack/osrf_list.h index 9486cd0..104407b 100644 --- a/src/libstack/osrf_list.h +++ b/src/libstack/osrf_list.h @@ -1,3 +1,7 @@ +#ifndef OSRF_LIST_H +#define OSRF_LIST_H + + #include #include "opensrf/utils.h" #include @@ -114,3 +118,4 @@ void __osrfListSetSize( osrfList* list ); unsigned long osrfListGetCount( osrfList* list ); +#endif diff --git a/src/libstack/osrf_transgroup.c b/src/libstack/osrf_transgroup.c index 42f0190..6dd554e 100644 --- a/src/libstack/osrf_transgroup.c +++ b/src/libstack/osrf_transgroup.c @@ -33,6 +33,7 @@ osrfTransportGroup* osrfNewTransportGroup( osrfTransportGroupNode* nodes[], int for( i = 0; i != count; i++ ) { if(!(nodes[i] && nodes[i]->domain) ) return NULL; osrfHashSet( grp->nodes, nodes[i], nodes[i]->domain ); + debug_handler("Adding domain %s to TransportGroup", nodes[i]->domain); } return grp; @@ -40,7 +41,7 @@ osrfTransportGroup* osrfNewTransportGroup( osrfTransportGroupNode* nodes[], int /* connect all of the nodes to their servers */ -int osrfTransportGroupConnect( osrfTransportGroup* grp ) { +int osrfTransportGroupConnectAll( osrfTransportGroup* grp ) { if(!grp) return -1; int active = 0; @@ -48,10 +49,18 @@ int osrfTransportGroupConnect( osrfTransportGroup* grp ) { osrfHashIteratorReset(grp->itr); while( (node = osrfHashIteratorNext(grp->itr)) ) { + info_handler("TransportGroup attempting to connect to domain %s", + node->connection->session->server); + if(client_connect( node->connection, node->username, node->password, node->resource, 10, AUTH_DIGEST )) { node->active = 1; active++; + info_handler("TransportGroup successfully connected to domain %s", + node->connection->session->server); + } else { + warning_handler("TransportGroup unable to connect to domain %s", + node->connection->session->server); } } @@ -59,6 +68,22 @@ int osrfTransportGroupConnect( osrfTransportGroup* grp ) { return active; } +void osrfTransportGroupDisconnectAll( osrfTransportGroup* grp ) { + if(!grp) return; + + osrfTransportGroupNode* node; + osrfHashIteratorReset(grp->itr); + + while( (node = osrfHashIteratorNext(grp->itr)) ) { + info_handler("TransportGroup disconnecting from domain %s", + node->connection->session->server); + client_disconnect(node->connection); + node->active = 0; + } + + osrfHashIteratorReset(grp->itr); +} + int osrfTransportGroupSendMatch( osrfTransportGroup* grp, transport_message* msg ) { if(!(grp && msg)) return -1; diff --git a/src/libstack/osrf_transgroup.h b/src/libstack/osrf_transgroup.h index a8f433b..57ee1be 100644 --- a/src/libstack/osrf_transgroup.h +++ b/src/libstack/osrf_transgroup.h @@ -56,7 +56,9 @@ osrfTransportGroup* osrfNewTransportGroup( osrfTransportGroupNode* nodes[], int @param grp The transport group @return The number of nodes successfully connected */ -int osrfTransportGroupConnect( osrfTransportGroup* grp ); +int osrfTransportGroupConnectAll( osrfTransportGroup* grp ); + +void osrfTransportGroupDisconnectAll( osrfTransportGroup* grp ); /** -- 2.11.0