[89] | 1 | // %pacpus:license{
|
---|
| 2 | // This file is part of the PACPUS framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %pacpus:license}
|
---|
| 5 | /// @version $Id: ComponentBase.cpp 76 2013-01-10 17:05:10Z kurdejma $
|
---|
| 6 |
|
---|
| 7 | #include <Pacpus/kernel/ComponentBase.h>
|
---|
[182] | 8 |
|
---|
[89] | 9 | #include <Pacpus/kernel/ComponentManager.h>
|
---|
| 10 | #include <Pacpus/kernel/Log.h>
|
---|
[201] | 11 | #include <Pacpus/kernel/PacpusException.h>
|
---|
[89] | 12 |
|
---|
[176] | 13 | #include <boost/program_options/parsers.hpp>
|
---|
| 14 | #include <boost/program_options/variables_map.hpp>
|
---|
[202] | 15 | #include <ostream>
|
---|
[176] | 16 | #include <string>
|
---|
| 17 | #include <vector>
|
---|
| 18 |
|
---|
| 19 | namespace po = boost::program_options;
|
---|
[89] | 20 | using namespace pacpus;
|
---|
[176] | 21 | using namespace std;
|
---|
[89] | 22 |
|
---|
[182] | 23 | vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes);
|
---|
[176] | 24 |
|
---|
[202] | 25 | namespace std {
|
---|
| 26 |
|
---|
| 27 | template <typename _Elem, typename _Traits>
|
---|
| 28 | std::basic_ostream<_Elem, _Traits> & operator<<(std::basic_ostream<_Elem, _Traits> & os, const boost::program_options::variables_map & vm)
|
---|
| 29 | {
|
---|
[231] | 30 | for (po::variables_map::const_iterator i = vm.begin(); i != vm.end(); ++i) {
|
---|
[202] | 31 | const po::variable_value & v = i->second;
|
---|
| 32 | if (v.empty()) {
|
---|
| 33 | continue;
|
---|
| 34 | }
|
---|
| 35 | const type_info & type = v.value().type();
|
---|
| 36 | if (type == typeid(string)) {
|
---|
| 37 | const string & val = v.as<string>();
|
---|
| 38 | os << i->first << "=" << val;
|
---|
| 39 | } else if (type == typeid(long)) {
|
---|
| 40 | int val = v.as<long>();
|
---|
| 41 | os << i->first << "=" << val;
|
---|
| 42 | } else if (type == typeid(int)) {
|
---|
| 43 | int val = v.as<int>();
|
---|
| 44 | os << i->first << "=" << val;
|
---|
| 45 | } else if (type == typeid(unsigned long)) {
|
---|
| 46 | int val = v.as<unsigned long>();
|
---|
| 47 | os << i->first << "=" << val;
|
---|
| 48 | } else if (type == typeid(unsigned int)) {
|
---|
| 49 | int val = v.as<unsigned int>();
|
---|
| 50 | os << i->first << "=" << val;
|
---|
| 51 | } else if (type == typeid(double)) {
|
---|
| 52 | int val = v.as<double>();
|
---|
| 53 | os << i->first << "=" << val;
|
---|
| 54 | } else if (type == typeid(float)) {
|
---|
| 55 | int val = v.as<float>();
|
---|
| 56 | os << i->first << "=" << val;
|
---|
| 57 | } else if (type == typeid(bool)) {
|
---|
| 58 | int val = v.as<bool>();
|
---|
| 59 | os << i->first << "=" << val;
|
---|
| 60 | }
|
---|
[204] | 61 | os << "\n";
|
---|
[202] | 62 | }
|
---|
| 63 | return os;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | } // namespace std
|
---|
| 67 |
|
---|
[89] | 68 | DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase");
|
---|
| 69 |
|
---|
[182] | 70 | ComponentBase::ComponentBase(const QString & componentName)
|
---|
| 71 | : m_componentName(componentName)
|
---|
| 72 | , m_isActive(false)
|
---|
| 73 | , mIsRecording(true)
|
---|
| 74 | , m_manager(NULL)
|
---|
| 75 | , m_ui(NULL)
|
---|
| 76 | , m_componentState(NOT_MONITORED)
|
---|
| 77 | , mOptionsDescription("Component parameters")
|
---|
[89] | 78 | {
|
---|
| 79 | LOG_TRACE("constructor");
|
---|
| 80 | // Get a pointer on the instance of ComponentManager.
|
---|
[152] | 81 | m_manager = ComponentManager::getInstance();
|
---|
[176] | 82 | LOG_INFO("component " << getName() << " was created");
|
---|
| 83 |
|
---|
[179] | 84 | addParameters()
|
---|
| 85 | ("name", po::value<string>(&mName)->required(), "component name")
|
---|
| 86 | ("type", po::value<string>(&mTypeName)->required(), "component type")
|
---|
| 87 | ("ui", po::value<bool>(&mHasGui)->default_value(false), "whether to show GUI")
|
---|
| 88 | ("verbose", po::value<bool>(&mVerbose)->default_value(false), "set output verbose")
|
---|
| 89 | ("verbosity-level", po::value<int>(&mVerbosityLevel)->default_value(0), "set verbosity level")
|
---|
| 90 | ("recording", po::value<bool>(&mIsRecording)->default_value(false), "whether to record data")
|
---|
[176] | 91 | ;
|
---|
[89] | 92 | }
|
---|
| 93 |
|
---|
| 94 | ComponentBase::~ComponentBase()
|
---|
| 95 | {
|
---|
| 96 | LOG_TRACE("destructor");
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[152] | 99 | bool ComponentBase::isActive() const
|
---|
| 100 | {
|
---|
| 101 | return m_isActive;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | void ComponentBase::setActive(bool isActive)
|
---|
| 105 | {
|
---|
| 106 | m_isActive = isActive;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | bool ComponentBase::isRecording() const
|
---|
| 110 | {
|
---|
[177] | 111 | return mIsRecording;
|
---|
[152] | 112 | }
|
---|
| 113 |
|
---|
| 114 | void ComponentBase::setRecording(bool isRecording)
|
---|
| 115 | {
|
---|
[177] | 116 | mIsRecording = isRecording;
|
---|
[152] | 117 | }
|
---|
| 118 |
|
---|
| 119 | const XmlComponentConfig ComponentBase::xmlParameters() const
|
---|
| 120 | {
|
---|
| 121 | return param;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[89] | 124 | int ComponentBase::startComponent()
|
---|
| 125 | {
|
---|
[152] | 126 | if (isActive()) {
|
---|
| 127 | LOG_DEBUG("component already started, cannot (re-)start");
|
---|
| 128 | return false;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | setActive(true);
|
---|
[89] | 132 | startActivity();
|
---|
| 133 |
|
---|
| 134 | return true;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | int ComponentBase::stopComponent()
|
---|
| 138 | {
|
---|
[152] | 139 | if (!isActive()) {
|
---|
| 140 | LOG_DEBUG("component already stopped, cannot (re-)stop");
|
---|
| 141 | return false;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | setActive(false);
|
---|
[89] | 145 | stopActivity();
|
---|
| 146 |
|
---|
| 147 | return true;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | void ComponentBase::setState(const COMPONENT_STATE state)
|
---|
| 151 | {
|
---|
[152] | 152 | m_componentState = state;
|
---|
[89] | 153 | }
|
---|
| 154 |
|
---|
| 155 | // FIXME: this should be const.
|
---|
| 156 | ComponentBase::COMPONENT_STATE ComponentBase::getState()
|
---|
| 157 | {
|
---|
[152] | 158 | COMPONENT_STATE state = m_componentState;
|
---|
| 159 | if (ComponentBase::NOT_MONITORED != m_componentState) {
|
---|
| 160 | m_componentState = ComponentBase::MONITOR_NOK;
|
---|
[89] | 161 | }
|
---|
| 162 | return state;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[152] | 165 | ComponentBase::COMPONENT_CONFIGURATION ComponentBase::configurationState() const
|
---|
| 166 | {
|
---|
| 167 | return m_configurationState;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | void ComponentBase::setConfigurationState(COMPONENT_CONFIGURATION state)
|
---|
| 171 | {
|
---|
| 172 | m_configurationState = state;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[89] | 175 | bool ComponentBase::isConfigured() const
|
---|
| 176 | {
|
---|
[152] | 177 | return (m_configurationState == CONFIGURED_OK);
|
---|
[89] | 178 | }
|
---|
| 179 |
|
---|
| 180 | QString ComponentBase::getName() const
|
---|
| 181 | {
|
---|
[152] | 182 | return m_componentName;
|
---|
[89] | 183 | }
|
---|
| 184 |
|
---|
[152] | 185 | ComponentBase::InputsMap & ComponentBase::inputs()
|
---|
[89] | 186 | {
|
---|
[152] | 187 | return m_inputs;
|
---|
| 188 | }
|
---|
[89] | 189 |
|
---|
[152] | 190 | const ComponentBase::InputsMap & ComponentBase::inputs() const
|
---|
| 191 | {
|
---|
| 192 | return m_inputs;
|
---|
[89] | 193 | }
|
---|
| 194 |
|
---|
[152] | 195 | ComponentBase::OutputsMap & ComponentBase::outputs()
|
---|
[89] | 196 | {
|
---|
[152] | 197 | return m_outputs;
|
---|
| 198 | }
|
---|
[89] | 199 |
|
---|
[152] | 200 | const ComponentBase::OutputsMap & ComponentBase::outputs() const
|
---|
| 201 | {
|
---|
| 202 | return m_outputs;
|
---|
[89] | 203 | }
|
---|
[120] | 204 |
|
---|
[152] | 205 | InputInterfaceBase * ComponentBase::getInput(QString inputName) const
|
---|
[120] | 206 | {
|
---|
[152] | 207 | if (inputs().contains(inputName)) {
|
---|
| 208 | return inputs()[inputName];
|
---|
| 209 | }
|
---|
[176] | 210 | LOG_WARN("Component " << getName() << " does not contain input " << inputName);
|
---|
[152] | 211 | return NULL;
|
---|
[120] | 212 | }
|
---|
| 213 |
|
---|
[152] | 214 | OutputInterfaceBase * ComponentBase::getOutput(QString outputName) const
|
---|
[120] | 215 | {
|
---|
[152] | 216 | if (outputs().contains(outputName)) {
|
---|
| 217 | return outputs()[outputName];
|
---|
| 218 | }
|
---|
[206] | 219 | LOG_WARN("Component " << getName() << " does not contain output " << outputName);
|
---|
[152] | 220 | return NULL;
|
---|
[120] | 221 | }
|
---|
[176] | 222 |
|
---|
[181] | 223 | bool ComponentBase::hasGui() const
|
---|
| 224 | {
|
---|
| 225 | return mHasGui;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | bool ComponentBase::isOutputVerbose() const
|
---|
| 229 | {
|
---|
| 230 | return mVerbose || (getVerbosityLevel() > 0);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | int ComponentBase::getVerbosityLevel() const
|
---|
| 234 | {
|
---|
| 235 | return mVerbosityLevel;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[176] | 238 | po::options_description_easy_init ComponentBase::addParameters()
|
---|
| 239 | {
|
---|
| 240 | return mOptionsDescription.add_options();
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | void ComponentBase::parseParameters(const XmlComponentConfig & cfg)
|
---|
| 244 | {
|
---|
| 245 | LOG_INFO("Parsing parameters...");
|
---|
| 246 | LOG_INFO(mOptionsDescription);
|
---|
| 247 |
|
---|
| 248 | vector<string> xargs = convertAttributesToArgumentVector(cfg.getProperties());
|
---|
| 249 |
|
---|
[201] | 250 | po::variables_map vm;
|
---|
[176] | 251 | try {
|
---|
| 252 | po::store(
|
---|
| 253 | po::command_line_parser(xargs)
|
---|
| 254 | .options(mOptionsDescription)
|
---|
[180] | 255 | .allow_unregistered() // FIXME: temporary only, at term all the components specify all parameters
|
---|
[176] | 256 | .run()
|
---|
| 257 | , vm);
|
---|
| 258 | po::notify(vm);
|
---|
| 259 | } catch (po::error & e) {
|
---|
[201] | 260 | LOG_WARN(e.what());
|
---|
| 261 | throw PacpusException(e.what());
|
---|
[176] | 262 | }
|
---|
| 263 |
|
---|
[204] | 264 | LOG_INFO("Parsed parameter values:\n" << vm);
|
---|
[176] | 265 | }
|
---|
| 266 |
|
---|
| 267 | vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes)
|
---|
| 268 | {
|
---|
| 269 | vector<string> xargs;
|
---|
| 270 | xargs.reserve(attributes.size());
|
---|
| 271 |
|
---|
| 272 | for (int i = 0; i < attributes.size(); ++i) {
|
---|
| 273 | QDomAttr parameter = attributes.item(i).toAttr();
|
---|
| 274 | if (parameter.isNull()) {
|
---|
| 275 | LOG_WARN("node is not a parameter");
|
---|
| 276 | continue;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | QString arg = QString("--") + parameter.name() + "=";
|
---|
| 280 |
|
---|
| 281 | bool shouldAddQuotes = parameter.value().contains(' ');
|
---|
| 282 | if (shouldAddQuotes) {
|
---|
| 283 | arg += '\"';
|
---|
| 284 | arg += parameter.value();
|
---|
| 285 | arg += '\"';
|
---|
| 286 | } else {
|
---|
| 287 | arg += parameter.value();
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | LOG_DEBUG("parameter: " << arg);
|
---|
| 291 | xargs.push_back(arg.toStdString());
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | return xargs;
|
---|
| 295 | }
|
---|