00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 {
00026 namespace platform {
00027
00028
00029
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
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
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
00065 bool start_out_running = (getType() == Reactor::TYPE_COLLECTION? false : true);
00066
00067
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
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
00088 if (m_connections.find(output_reactor.getId()) != m_connections.end())
00089 throw AlreadyConnectedException(output_reactor.getId());
00090
00091
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
00102 if (m_connections.find(connection_id) != m_connections.end())
00103 throw AlreadyConnectedException(connection_id);
00104
00105
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
00114 ConnectionMap::iterator i = m_connections.find(connection_id);
00115 if (i == m_connections.end())
00116 throw ConnectionNotFoundException(connection_id);
00117
00118
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 }
00160 }