platform/src/Reactor.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 <boost/lexical_cast.hpp>
00021 #include <pion/platform/ConfigManager.hpp>
00022 #include <pion/platform/Reactor.hpp>
00023 
00024 
00025 namespace pion {        // begin namespace pion
00026 namespace platform {    // begin namespace platform (Pion Platform Library)
00027 
00028 
00029 // static members of Reactor
00030 
00031 const std::string           Reactor::REACTOR_ELEMENT_NAME = "Reactor";
00032 const std::string           Reactor::RUNNING_ELEMENT_NAME = "Running";
00033 const std::string           Reactor::EVENTS_IN_ELEMENT_NAME = "EventsIn";
00034 const std::string           Reactor::EVENTS_OUT_ELEMENT_NAME = "EventsOut";
00035 const std::string           Reactor::ID_ATTRIBUTE_NAME = "id";
00036 const std::string           Reactor::WORKSPACE_ELEMENT_NAME = "Workspace";
00037 const std::string           Reactor::X_COORDINATE_ELEMENT_NAME = "X";
00038 const std::string           Reactor::Y_COORDINATE_ELEMENT_NAME = "Y";
00039 
00040 
00041 // Reactor member functions
00042 
00043 void Reactor::setConfig(const Vocabulary& v, const xmlNodePtr config_ptr)
00044 {
00045     ConfigWriteLock cfg_lock(*this);
00046     PlatformPlugin::setConfig(v, config_ptr);
00047 
00048     // get the Workspace ID
00049     std::string workspace_str;
00050     if (! ConfigManager::getConfigOption(WORKSPACE_ELEMENT_NAME, workspace_str, config_ptr))
00051         throw MissingWorkspaceException(getId());
00052 
00053     m_workspace_id = workspace_str;
00054 }
00055 
00056 void Reactor::updateVocabulary(const Vocabulary& v)
00057 {
00058     ConfigWriteLock cfg_lock(*this);
00059     PlatformPlugin::updateVocabulary(v);
00060 }
00061 
00062 bool Reactor::startOutRunning(const xmlNodePtr config_ptr, bool exec_start)
00063 {
00064     // Get the default behavior regarding whether the Reactor should start out running.
00065     bool start_out_running = (getType() == Reactor::TYPE_COLLECTION? false : true);
00066 
00067     // Override the default behavior, if the Reactor's run status is specified in the configuration.
00068     if (config_ptr) {
00069         std::string run_status_str;
00070         if (ConfigManager::getConfigOption(Reactor::RUNNING_ELEMENT_NAME, run_status_str, config_ptr)) {
00071             start_out_running = (run_status_str == "true");
00072         }
00073     }
00074     
00075     // start the reactor ?
00076     if (exec_start && start_out_running && !isRunning()) {
00077         start();
00078     }
00079     
00080     return start_out_running;
00081 }
00082     
00083 void Reactor::addConnection(Reactor& output_reactor)
00084 {
00085     ConfigWriteLock cfg_lock(*this);
00086     
00087     // check if it already connected
00088     if (m_connections.find(output_reactor.getId()) != m_connections.end())
00089         throw AlreadyConnectedException(output_reactor.getId());
00090     
00091     // add the new connection
00092     m_connections.insert(std::make_pair(output_reactor.getId(),
00093                                         OutputConnection(&output_reactor)));
00094 }
00095 
00096 void Reactor::addConnection(const std::string& connection_id,
00097                             EventHandler connection_handler)
00098 {
00099     ConfigWriteLock cfg_lock(*this);
00100 
00101     // check if it already connected
00102     if (m_connections.find(connection_id) != m_connections.end())
00103         throw AlreadyConnectedException(connection_id);
00104     
00105     // add the new connection
00106     m_connections.insert(std::make_pair(connection_id, connection_handler));
00107 }
00108 
00109 void Reactor::removeConnection(const std::string& connection_id)
00110 {
00111     ConfigWriteLock cfg_lock(*this);
00112     
00113     // find the connection to remove
00114     ConnectionMap::iterator i = m_connections.find(connection_id);
00115     if (i == m_connections.end())
00116         throw ConnectionNotFoundException(connection_id);
00117     
00118     // remove the connection
00119     m_connections.erase(i);
00120 }
00121 
00122 void Reactor::clearConnections(void)
00123 {
00124     ConfigWriteLock cfg_lock(*this);
00125     m_connections.clear();
00126 }
00127 
00128 void Reactor::query(std::ostream& out, const QueryBranches& branches,
00129     const QueryParams& qp)
00130 {
00131     writeBeginReactorXML(out);
00132     writeStatsOnlyXML(out);
00133     writeEndReactorXML(out);
00134 }
00135 
00136 void Reactor::writeStatsOnlyXML(std::ostream& out) const
00137 {
00138     out << '<' << RUNNING_ELEMENT_NAME << '>'
00139         << (isRunning() ? "true" : "false")
00140         << "</" << RUNNING_ELEMENT_NAME << '>' << std::endl
00141         << '<' << EVENTS_IN_ELEMENT_NAME << '>' << getEventsIn()
00142         << "</" << EVENTS_IN_ELEMENT_NAME << '>' << std::endl
00143         << '<' << EVENTS_OUT_ELEMENT_NAME << '>' << getEventsOut()
00144         << "</" << EVENTS_OUT_ELEMENT_NAME << '>' << std::endl;
00145 }
00146 
00147 void Reactor::writeBeginReactorXML(std::ostream& out) const
00148 {
00149     out << '<' << REACTOR_ELEMENT_NAME << ' ' << ID_ATTRIBUTE_NAME
00150         << "=\"" << getId() << "\">" << std::endl;
00151 }
00152 
00153 void Reactor::writeEndReactorXML(std::ostream& out) const
00154 {
00155     out << "</" << REACTOR_ELEMENT_NAME << '>' << std::endl;
00156 }
00157     
00158     
00159 }   // end namespace platform
00160 }   // end namespace pion

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