platform/src/ProtocolFactory.cpp

00001 // ------------------------------------------------------------------------
00002 // Pion is a development platform for building Reactors that process Events
00003 // ------------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
00005 //
00006 // Pion is free software: you can redistribute it and/or modify it under the
00007 // terms of the GNU Affero General Public License as published by the Free
00008 // Software Foundation, either version 3 of the License, or (at your option)
00009 // any later version.
00010 //
00011 // Pion is distributed in the hope that it will be useful, but WITHOUT ANY
00012 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013 // FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
00014 // more details.
00015 //
00016 // You should have received a copy of the GNU Affero General Public License
00017 // along with Pion.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #include <pion/platform/ProtocolFactory.hpp>
00021 
00022 
00023 namespace pion {        // begin namespace pion
00024 namespace platform {    // begin namespace platform (Pion Platform Library)
00025 
00026 
00027 // static members of ProtocolFactory
00028 const std::string           ProtocolFactory::DEFAULT_CONFIG_FILE = "protocols.xml";
00029 const std::string           ProtocolFactory::PROTOCOL_ELEMENT_NAME = "Protocol";
00030 
00031 
00032 // ProtocolFactory member functions
00033 
00034 ProtocolFactory::ProtocolFactory(const VocabularyManager& vocab_mgr)
00035     : PluginConfig<Protocol>(vocab_mgr, DEFAULT_CONFIG_FILE, PROTOCOL_ELEMENT_NAME)
00036 {
00037     setLogger(PION_GET_LOGGER("pion.platform.ProtocolFactory"));
00038 }
00039     
00040 ProtocolPtr ProtocolFactory::getProtocol(const std::string& protocol_id)
00041 {
00042     boost::mutex::scoped_lock factory_lock(m_mutex);
00043     Protocol *protocol_ptr = m_plugins.get(protocol_id);
00044     // throw an exception if the protocol was not found
00045     if (protocol_ptr == NULL)
00046         throw ProtocolNotFoundException(protocol_id);
00047     // return a cloned instance of the Protocol since its state may change
00048     // while encoding or decoding data streams
00049     return protocol_ptr->clone();
00050 }
00051 
00052 void ProtocolFactory::setProtocolConfig(const std::string& protocol_id,
00053                                   const xmlNodePtr config_ptr)
00054 {
00055     // convert PluginNotFound exceptions into ProtocolNotFound exceptions
00056     try {
00057         PluginConfig<Protocol>::setPluginConfig(protocol_id, config_ptr);
00058     } catch (PluginManager<Protocol>::PluginNotFoundException&) {
00059         throw ProtocolNotFoundException(protocol_id);
00060     }
00061 }
00062 
00063 std::string ProtocolFactory::addProtocol(const xmlNodePtr config_ptr)
00064 {
00065     return PluginConfig<Protocol>::addPlugin(config_ptr);
00066 }
00067 
00068 void ProtocolFactory::removeProtocol(const std::string& protocol_id) {
00069     // convert PluginNotFound exceptions into ProtocolNotFound exceptions
00070     try {
00071         PluginConfig<Protocol>::removePlugin(protocol_id);
00072     } catch (PluginManager<Protocol>::PluginNotFoundException&) {
00073         throw ProtocolNotFoundException(protocol_id);
00074     }
00075 }
00076 
00077     
00078 }   // end namespace platform
00079 }   // end namespace pion

Generated on Wed Apr 13 16:38:34 2011 for pion-platform by  doxygen 1.4.7