/////////////////////////////////////////////////////////////////////////////// /// CConnectionManager::PutHostname /// @description Registers a hostname with the uuid to hostname map. /// @pre: None /// @post: The hostname is registered with the uuid to hostname map. /// @param u_: the uuid to enter into the map. /// @param host_: The hostname to enter into the map. /////////////////////////////////////////////////////////////////////////////// void CConnectionManager::PutHostname(std::string u_, std::string host_) { Logger::Notice << __PRETTY_FUNCTION__ << std::endl; { boost::lock_guard< boost::mutex > scopedLock_( m_Mutex ); m_hostnames.insert(std::pair<std::string, std::string>(u_, host_)); } }
/////////////////////////////////////////////////////////////////////////////// /// CConnectionManager::PutConnection /// @description Inserts a connection into the connection map. /// @param uuid: The uuid of the node the connection is to. /// @param c: The connection pointer that goes to the node in question. /// @pre: The connection is initialized. /// @post: The connection has been inserted into the connection map. /////////////////////////////////////////////////////////////////////////////// void CConnectionManager::PutConnection(std::string uuid, ConnectionPtr c) { Logger::Debug << __PRETTY_FUNCTION__ << std::endl; { boost::lock_guard< boost::mutex > scopedLock_( m_Mutex ); m_connections.insert(std::pair<std::string,ConnectionPtr>(uuid,c)); m_connections_r.insert(std::pair<ConnectionPtr,std::string>(c,uuid)); } }
/////////////////////////////////////////////////////////////////////////////// /// @fn CDispatcher::RegisterWriteHandler /// @description Registers a module that provides a write handler with the /// dispatcher. /// @pre A module that inhertis from IWriteHandler is provided /// @post The module will be registered to touch outgoing messages that contain /// the p_type key. /// @param module The module the read handler is on behalf of. /// @param p_type A ptree key that will be used to identify which messages /// should be touched. /// @param p_handler The module that will be invoked to perform the touch /////////////////////////////////////////////////////////////////////////////// void CDispatcher::RegisterWriteHandler(const std::string & /*module*/, const std::string &p_type, IWriteHandler *p_handler ) { Logger.Trace << __PRETTY_FUNCTION__ << std::endl; { // Scoped lock, will release mutex at end of {} boost::lock_guard< boost::mutex > scopedLock_( m_wMutex ); m_writeHandlers.insert( std::pair< const std::string, IWriteHandler *> (p_type, p_handler) ); } }
/////////////////////////////////////////////////////////////////////////////// /// @fn CDispatcher::RegisterReadHandler /// @description Registers a module that provides a read handler with the /// dispatcher. /// @pre A module that inherits from IReadHandler is provided. /// @post A module is registered with a read handler. /// @param module The module name that the handler is on behalf of. /// @param p_type the tree key used to identify which messages the module /// would like to receive. /// @param p_handler The module which will be called to receive the message. /////////////////////////////////////////////////////////////////////////////// void CDispatcher::RegisterReadHandler(const std::string &module, const std::string &p_type, IReadHandler *p_handler) { Logger.Trace << __PRETTY_FUNCTION__ << std::endl; { // Scoped lock, will release mutex at end of {} boost::lock_guard< boost::mutex > scopedLock_( m_rMutex ); m_readHandlers.insert( std::pair< const std::string, IReadHandler *> (p_type, p_handler)); m_handlerToModule.insert(std::pair<IReadHandler *, const std::string>(p_handler,module)); } }
/////////////////////////////////////////////////////////////////////////////// /// @fn CDispatcher::HandleWrite /// @description Handles calling modules write handlers which allows them to /// touch messages before they are sent. /// @pre Write handlers have been registered with the dispatcher /// @post The outgoing message is touched before being delivered. /// @param p_mesg The message to affect before sending them out. /////////////////////////////////////////////////////////////////////////////// void CDispatcher::HandleWrite( ptree &p_mesg ) { Logger.Trace << __PRETTY_FUNCTION__ << std::endl; ptree sub_; ptree::const_iterator it_; std::map< std::string, IWriteHandler *>::const_iterator mapIt_; std::string key_; try { // Scoped lock, will release mutex at end of try {} boost::lock_guard< boost::mutex > scopedLock_( m_wMutex ); // This allows for a handler to process all messages using // the special keyword "any". This happens before the other // sections in case a specific handler depends upon a // general handler for( mapIt_ = m_writeHandlers.lower_bound( "any" ); mapIt_ != m_writeHandlers.upper_bound( "any" ); ++mapIt_ ) { Logger.Debug << "Processing 'any'" << std::endl; (mapIt_->second)->HandleWrite( p_mesg ); } // Loop through all submessages of this message to call its // handler ptree sub_ = p_mesg.get_child("message.submessages"); for( it_ = sub_.begin(); it_ != sub_.end(); ++it_ ) { Logger.Debug << "Processing " << it_->first << std::endl; // Retrieve current key and iterate through all matching // handlers of that key. If the key doesn't exist in the // map, lower_bound(key) == upper_bound(key). key_ = it_->first; for( mapIt_ = m_writeHandlers.lower_bound( key_ ); mapIt_ != m_writeHandlers.upper_bound(key_); ++mapIt_ ) { (mapIt_->second)->HandleWrite( p_mesg ); } // XXX Should anything be done if the message didn't // have a handler? if( m_writeHandlers.lower_bound( key_ ) == m_writeHandlers.upper_bound( key_) ) { // Just log this for now Logger.Debug << "Submessage '" << key_ << "' had no write handlers."; } } } catch( boost::property_tree::ptree_bad_path &e ) { Logger.Error << __PRETTY_FUNCTION__ << " (" << __LINE__ << "): " << "Malformed message. Does not contain 'submessages'." << std::endl << "\t" << e.what() << std::endl; } }