From df9a85f8d86e82a47ff92abcc56bcca04690b7a4 Mon Sep 17 00:00:00 2001 From: miker Date: Thu, 31 Jan 2008 18:18:29 +0000 Subject: [PATCH] Patch from Scott McKellar: 1. In the header: I added compilation guards. 2. In the header: I changed leading double underscores to leading single underscores. These identifiers don't appear elsewhere in the code base, with either double or single underscores. 3. I moved most of the header into the implementation file. The headers whose prototypes I moved are now static. Where there were comments associated with the prototypes, I moved them to the function definitions. 4. I removed an extra leading underscore from __osrfRouterFillFDSet(). 5. I replaced some deprecated identifiers with their camel-case equivalents: osrf_message_free ==> osrfMessageFree osrf_message ==> osrfMessage 6. I added the const qualifier to several function parameters. 7. In a couple of spots we were failing to free classname buffers. I plugged those leaks. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1232 9efc2488-bf62-4759-914b-345cdb29e865 --- src/router/osrf_router.c | 177 +++++++++++++++++++++++++++++++-------- src/router/osrf_router.h | 158 ++-------------------------------- 2 files changed, 152 insertions(+), 183 deletions(-) diff --git a/src/router/osrf_router.c b/src/router/osrf_router.c index 84744df..90be999 100644 --- a/src/router/osrf_router.c +++ b/src/router/osrf_router.c @@ -1,5 +1,51 @@ #include "osrf_router.h" +/* a class maintains a set of server nodes */ +struct _osrfRouterClassStruct { + osrfRouter* router; /* our router handle */ + osrfHashIterator* itr; + osrfHash* nodes; + transport_client* connection; +}; +typedef struct _osrfRouterClassStruct osrfRouterClass; + +/* represents a link to a single server's inbound connection */ +struct _osrfRouterNodeStruct { + char* remoteId; /* send message to me via this login */ + int count; /* how many message have been sent to this node */ + transport_message* lastMessage; +}; +typedef struct _osrfRouterNodeStruct osrfRouterNode; + +static osrfRouterClass* osrfRouterAddClass( osrfRouter* router, const char* classname ); +static int osrfRouterClassAddNode( osrfRouterClass* rclass, const char* remoteId ); +static int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg ); +static int osrfRouterClassHandleMessage( osrfRouter* router, + osrfRouterClass* rclass, transport_message* msg ); +static int osrfRouterRemoveClass( osrfRouter* router, const char* classname ); +static int osrfRouterClassRemoveNode( osrfRouter* router, const char* classname, + const char* remoteId ); +static void osrfRouterClassFree( char* classname, void* rclass ); +static void osrfRouterNodeFree( char* remoteId, void* node ); +static osrfRouterClass* osrfRouterFindClass( osrfRouter* router, const char* classname ); +static osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, + const char* remoteId ); +static int _osrfRouterFillFDSet( osrfRouter* router, fd_set* set ); +static void osrfRouterHandleIncoming( osrfRouter* router ); +static int osrfRouterClassHandleIncoming( osrfRouter* router, + const char* classname, osrfRouterClass* class ); +static transport_message* osrfRouterClassHandleBounce( osrfRouter* router, + const char* classname, osrfRouterClass* rclass, transport_message* msg ); +static int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg ); +static int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, + osrfMessage* omsg ); +static int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, + osrfMessage* omsg ); +static int osrfRouterHandleAppResponse( osrfRouter* router, + transport_message* msg, osrfMessage* omsg, const jsonObject* response ); +static int osrfRouterHandleMethodNFound( osrfRouter* router, transport_message* msg, + osrfMessage* omsg ); + #define ROUTER_SOCKFD connection->session->sock_id #define ROUTER_REGISTER "register" #define ROUTER_UNREGISTER "unregister" @@ -57,7 +103,7 @@ void osrfRouterRun( osrfRouter* router ) { while(1) { fd_set set; - int maxfd = __osrfRouterFillFDSet( router, &set ); + int maxfd = _osrfRouterFillFDSet( router, &set ); int numhandled = 0; if( (selectret = select(maxfd + 1, &set, NULL, NULL, NULL)) < 0 ) { @@ -103,7 +149,11 @@ void osrfRouterRun( osrfRouter* router ) { } -void osrfRouterHandleIncoming( osrfRouter* router ) { +/** + Utility method for handling incoming requests to the router + and making sure the sender is allowed. + */ +static void osrfRouterHandleIncoming( osrfRouter* router ) { if(!router) return; transport_message* msg = NULL; @@ -132,7 +182,11 @@ void osrfRouterHandleIncoming( osrfRouter* router ) { } } -int osrfRouterClassHandleIncoming( osrfRouter* router, const char* classname, +/** + Utility method for handling incoming requests to a router class, + makes sure sender is a trusted client + */ +static int osrfRouterClassHandleIncoming( osrfRouter* router, const char* classname, osrfRouterClass* class ) { if(!(router && class)) return -1; @@ -183,7 +237,11 @@ int osrfRouterClassHandleIncoming( osrfRouter* router, const char* classname, -int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg ) { +/** + Handles top level router messages + @return 0 on success + */ +static int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg ) { if(!(router && msg)) return -1; if( !msg->router_command || !strcmp(msg->router_command,"")) @@ -221,7 +279,14 @@ int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg ) { -osrfRouterClass* osrfRouterAddClass( osrfRouter* router, const char* classname ) { +/** + Allocates and adds a new router class handler to the router's list of handlers. + Also connects the class handler to the network at @domain/ + @param router The current router instance + @param classname The name of the class this node handles. + @return 0 on success, -1 on connection error. + */ +static osrfRouterClass* osrfRouterAddClass( osrfRouter* router, const char* classname ) { if(!(router && router->classes && classname)) return NULL; osrfRouterClass* class = safe_malloc(sizeof(osrfRouterClass)); @@ -248,7 +313,13 @@ osrfRouterClass* osrfRouterAddClass( osrfRouter* router, const char* classname ) } -int osrfRouterClassAddNode( osrfRouterClass* rclass, const char* remoteId ) { +/** + Adds a new server node to the given class. + @param rclass The Router class to add the node to + @param remoteId The remote login of this node + @return 0 on success, -1 on generic error + */ +static int osrfRouterClassAddNode( osrfRouterClass* rclass, const char* remoteId ) { if(!(rclass && rclass->nodes && remoteId)) return -1; osrfLogInfo( OSRF_LOG_MARK, "Adding router node for remote id %s", remoteId ); @@ -266,7 +337,12 @@ int osrfRouterClassAddNode( osrfRouterClass* rclass, const char* remoteId ) { ? return NULL if it's the last node ? */ -transport_message* osrfRouterClassHandleBounce( osrfRouter* router, +/* handles case where router node is not longer reachable. copies over the + data from the last sent message and returns a newly crafted suitable for treating + as a newly inconing message. Removes the dead node and If there are no more + nodes to send the new message to, returns NULL. + */ +static transport_message* osrfRouterClassHandleBounce( osrfRouter* router, const char* classname, osrfRouterClass* rclass, transport_message* msg ) { osrfLogDebug( OSRF_LOG_MARK, "osrfRouterClassHandleBounce()"); @@ -317,12 +393,14 @@ transport_message* osrfRouterClassHandleBounce( osrfRouter* router, /** + Handles class level requests If we get a regular message, we send it to the next node in the list of nodes if we get an error, it's a bounce back from a previous attempt. We take the body and thread from the last sent on the node that had the bounced message and propogate them on to the new message being sent - */ -int osrfRouterClassHandleMessage( + @return 0 on success + */ +static int osrfRouterClassHandleMessage( osrfRouter* router, osrfRouterClass* rclass, transport_message* msg ) { if(!(router && rclass && msg)) return -1; @@ -362,7 +440,10 @@ int osrfRouterClassHandleMessage( } -int osrfRouterRemoveClass( osrfRouter* router, const char* classname ) { +/** + Removes a given class from the router, freeing as it goes + */ +static int osrfRouterRemoveClass( osrfRouter* router, const char* classname ) { if(!(router && router->classes && classname)) return -1; osrfLogInfo( OSRF_LOG_MARK, "Removing router class %s", classname ); osrfHashRemove( router->classes, classname ); @@ -370,7 +451,14 @@ int osrfRouterRemoveClass( osrfRouter* router, const char* classname ) { } -int osrfRouterClassRemoveNode( +/** + Removes the given node from the class. Also, if this is that last node in the set, + removes the class from the router + @return 0 on successful removal with no class removal + @return 1 on successful remove with class removal + @return -1 error on removal + */ +static int osrfRouterClassRemoveNode( osrfRouter* router, const char* classname, const char* remoteId ) { if(!(router && router->classes && classname && remoteId)) return 0; @@ -394,7 +482,11 @@ int osrfRouterClassRemoveNode( } -void osrfRouterClassFree( char* classname, void* c ) { +/** + Frees a router class object + Takes a void* since it is freed by the hash code + */ +static void osrfRouterClassFree( char* classname, void* c ) { if(!(classname && c)) return; osrfRouterClass* rclass = (osrfRouterClass*) c; client_disconnect( rclass->connection ); @@ -413,7 +505,11 @@ void osrfRouterClassFree( char* classname, void* c ) { } -void osrfRouterNodeFree( char* remoteId, void* n ) { +/** + Frees a router node object + Takes a void* since it is freed by the list code + */ +static void osrfRouterNodeFree( char* remoteId, void* n ) { if(!n) return; osrfRouterNode* node = (osrfRouterNode*) n; free(node->remoteId); @@ -439,19 +535,32 @@ void osrfRouterFree( osrfRouter* router ) { -osrfRouterClass* osrfRouterFindClass( osrfRouter* router, const char* classname ) { +/** + Finds the class associated with the given class name in the list of classes + */ +static osrfRouterClass* osrfRouterFindClass( osrfRouter* router, const char* classname ) { if(!( router && router->classes && classname )) return NULL; return (osrfRouterClass*) osrfHashGet( router->classes, classname ); } -osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, const char* remoteId ) { +/** + Finds the router node within this class with the given remote id + */ +static osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, + const char* remoteId ) { if(!(rclass && remoteId)) return NULL; return (osrfRouterNode*) osrfHashGet( rclass->nodes, remoteId ); } -int __osrfRouterFillFDSet( osrfRouter* router, fd_set* set ) { +/** + Clears and populates the provided fd_set* with file descriptors + from the router's top level connection as well as each of the + router class connections + @return The largest file descriptor found in the filling process + */ +static int _osrfRouterFillFDSet( osrfRouter* router, fd_set* set ) { if(!(router && router->classes && set)) return -1; FD_ZERO(set); @@ -486,9 +595,11 @@ int __osrfRouterFillFDSet( osrfRouter* router, fd_set* set ) { return maxfd; } - - -int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg ) { +/** + handles messages that don't have a 'router_command' set. They are assumed to + be app request messages + */ +static int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg ) { int T = 32; osrfMessage* arr[T]; @@ -521,7 +632,8 @@ int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg ) { return 0; } -int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMessage* omsg ) { +static int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, + osrfMessage* omsg ) { if(!(router && msg && omsg)) return -1; osrfMessage* success = osrf_message_init( STATUS, omsg->thread_trace, omsg->protocol ); @@ -539,7 +651,7 @@ int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMe client_send_message(router->connection, return_m); free(data); - osrf_message_free(success); + osrfMessageFree(success); message_free(return_m); return 0; @@ -547,7 +659,8 @@ int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMe -int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osrfMessage* omsg ) { +static int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, + osrfMessage* omsg ) { if(!(router && msg && omsg && omsg->method_name)) return -1; @@ -573,12 +686,11 @@ int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osr char* classname = jsonObjectToSimpleString( jsonObjectGetIndex( omsg->_params, 0 ) ); - if (!classname) { - free(classname); + if (!classname) return -1; - } class = osrfHashGet(router->classes, classname); + free(classname); osrfHashIterator* node_itr = osrfNewHashIterator(class->nodes); while( (node = osrfHashIteratorNext(node_itr)) ) { @@ -596,13 +708,12 @@ int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osr char* classname = jsonObjectToSimpleString( jsonObjectGetIndex( omsg->_params, 0 ) ); - if (!classname) { - free(classname); + if (!classname) return -1; - } jresponse = jsonParseString("{}"); class = osrfHashGet(router->classes, classname); + free(classname); osrfHashIterator* node_itr = osrfNewHashIterator(class->nodes); while( (node = osrfHashIteratorNext(node_itr)) ) { @@ -672,10 +783,10 @@ int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osr -int osrfRouterHandleMethodNFound( +static int osrfRouterHandleMethodNFound( osrfRouter* router, transport_message* msg, osrfMessage* omsg ) { - osrf_message* err = osrf_message_init( STATUS, omsg->thread_trace, 1); + osrfMessage* err = osrf_message_init( STATUS, omsg->thread_trace, 1); osrf_message_set_status_info( err, "osrfMethodException", "Router method not found", OSRF_STATUS_NOTFOUND ); @@ -687,14 +798,14 @@ int osrfRouterHandleMethodNFound( client_send_message(router->connection, tresponse ); free(data); - osrf_message_free( err ); + osrfMessageFree( err ); message_free(tresponse); return 0; } -int osrfRouterHandleAppResponse( osrfRouter* router, +static int osrfRouterHandleAppResponse( osrfRouter* router, transport_message* msg, osrfMessage* omsg, const jsonObject* response ) { if( response ) { /* send the response message */ @@ -721,7 +832,7 @@ int osrfRouterHandleAppResponse( osrfRouter* router, /* now send the 'request complete' message */ - osrf_message* status = osrf_message_init( STATUS, omsg->thread_trace, 1); + osrfMessage* status = osrf_message_init( STATUS, omsg->thread_trace, 1); osrf_message_set_status_info( status, "osrfConnectStatus", "Request Complete", OSRF_STATUS_COMPLETE ); char* statusdata = osrf_message_serialize(status); diff --git a/src/router/osrf_router.h b/src/router/osrf_router.h index 7b3b639..8d5e21d 100644 --- a/src/router/osrf_router.h +++ b/src/router/osrf_router.h @@ -1,3 +1,6 @@ +#ifndef OSRF_ROUTER_H +#define OSRF_ROUTER_H + #include #include #include @@ -16,7 +19,7 @@ /* a router maintains a list of server classes */ -struct __osrfRouterStruct { +struct _osrfRouterStruct { osrfHash* classes; /* our list of server classes */ char* domain; /* our login domain */ @@ -31,25 +34,7 @@ struct __osrfRouterStruct { transport_client* connection; }; -typedef struct __osrfRouterStruct osrfRouter; - - -/* a class maintains a set of server nodes */ -struct __osrfRouterClassStruct { - osrfRouter* router; /* our router handle */ - osrfHashIterator* itr; - osrfHash* nodes; - transport_client* connection; -}; -typedef struct __osrfRouterClassStruct osrfRouterClass; - -/* represents a link to a single server's inbound connection */ -struct __osrfRouterNodeStruct { - char* remoteId; /* send message to me via this login */ - int count; /* how many message have been sent to this node */ - transport_message* lastMessage; -}; -typedef struct __osrfRouterNodeStruct osrfRouterNode; +typedef struct _osrfRouterStruct osrfRouter; /** Allocates a new router. @@ -79,147 +64,20 @@ int osrfRouterConnect( osrfRouter* router ); void osrfRouterRun( osrfRouter* router ); -/** - Allocates and adds a new router class handler to the router's list of handlers. - Also connects the class handler to the network at @domain/ - @param router The current router instance - @param classname The name of the class this node handles. - @return 0 on success, -1 on connection error. - */ -osrfRouterClass* osrfRouterAddClass( osrfRouter* router, const char* classname ); - -/** - Adds a new server node to the given class. - @param rclass The Router class to add the node to - @param remoteId The remote login of this node - @return 0 on success, -1 on generic error - */ -int osrfRouterClassAddNode( osrfRouterClass* rclass, const char* remoteId ); - - -/** - Handles top level router messages - @return 0 on success - */ -int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg ); - - -/** - Handles class level requests - @return 0 on success - */ -int osrfRouterClassHandleMessage( osrfRouter* router, - osrfRouterClass* rclass, transport_message* msg ); - -/** - Removes a given class from the router, freeing as it goes - */ -int osrfRouterRemoveClass( osrfRouter* router, const char* classname ); - -/** - Removes the given node from the class. Also, if this is that last node in the set, - removes the class from the router - @return 0 on successful removal with no class removal - @return 1 on successful remove with class removal - @return -1 error on removal - */ -int osrfRouterClassRemoveNode( osrfRouter* router, const char* classname, - const char* remoteId ); - -/** - Frees a router class object - Takes a void* since it is freed by the hash code - */ -void osrfRouterClassFree( char* classname, void* rclass ); - -/** - Frees a router node object - Takes a void* since it is freed by the list code - */ -void osrfRouterNodeFree( char* remoteId, void* node ); - - /** Frees a router */ void osrfRouterFree( osrfRouter* router ); -/** - Finds the class associated with the given class name in the list of classes - */ -osrfRouterClass* osrfRouterFindClass( osrfRouter* router, const char* classname ); - -/** - Finds the router node within this class with the given remote id - */ -osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, const char* remoteId ); - - -/** - Clears and populates the provided fd_set* with file descriptors - from the router's top level connection as well as each of the - router class connections - @return The largest file descriptor found in the filling process - */ -int __osrfRouterFillFDSet( osrfRouter* router, fd_set* set ); - - - -/** - Utility method for handling incoming requests to the router - and making sure the sender is allowed. - */ -void osrfRouterHandleIncoming( osrfRouter* router ); - -/** - Utility method for handling incoming requests to a router class, - makes sure sender is a trusted client - */ -int osrfRouterClassHandleIncoming( osrfRouter* router, - const char* classname, osrfRouterClass* class ); - -/* handles case where router node is not longer reachable. copies over the - data from the last sent message and returns a newly crafted suitable for treating - as a newly inconing message. Removes the dead node and If there are no more - nodes to send the new message to, returns NULL. - */ -transport_message* osrfRouterClassHandleBounce( osrfRouter* router, - const char* classname, osrfRouterClass* rclass, transport_message* msg ); - - - -/** - handles messages that don't have a 'router_command' set. They are assumed to - be app request messages - */ -int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg ); - - /** Handles connects, disconnects, etc. */ -int osrfRouterHandeStatusMessage( osrfRouter* router, transport_message* msg ); - +//int osrfRouterHandeStatusMessage( osrfRouter* router, transport_message* msg ); /** Handles REQUEST messages */ -int osrfRouterHandleRequestMessage( osrfRouter* router, transport_message* msg ); - - - -int osrfRouterHandleAppRequest( osrfRouter* router, transport_message* msg ); - - -int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMessage* omsg ); - - - -int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osrfMessage* omsg ); - -int osrfRouterHandleAppResponse( osrfRouter* router, - transport_message* msg, osrfMessage* omsg, const jsonObject* response ); - +//int osrfRouterHandleRequestMessage( osrfRouter* router, transport_message* msg ); -int osrfRouterHandleMethodNFound( osrfRouter* router, transport_message* msg, osrfMessage* omsg ); +#endif -- 2.43.2