[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 | {
|
---|
| 30 | for (po::variables_map::const_iterator i = vm.cbegin(); i != vm.cend(); ++i) {
|
---|
| 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 | /* QList<QString> keys = output.keys();
|
---|
[182] | 217 | for (int i=0; i<keys.size();++i)
|
---|
[152] | 218 | LOG_INFO("Key : " << keys[i])*/;
|
---|
[120] | 219 |
|
---|
[152] | 220 | if (outputs().contains(outputName)) {
|
---|
| 221 | return outputs()[outputName];
|
---|
| 222 | }
|
---|
[176] | 223 | LOG_WARN("Component " << getName() << " does not containt output " << outputName);
|
---|
[152] | 224 | return NULL;
|
---|
[120] | 225 | }
|
---|
[176] | 226 |
|
---|
[181] | 227 | bool ComponentBase::hasGui() const
|
---|
| 228 | {
|
---|
| 229 | return mHasGui;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | bool ComponentBase::isOutputVerbose() const
|
---|
| 233 | {
|
---|
| 234 | return mVerbose || (getVerbosityLevel() > 0);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | int ComponentBase::getVerbosityLevel() const
|
---|
| 238 | {
|
---|
| 239 | return mVerbosityLevel;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[176] | 242 | po::options_description_easy_init ComponentBase::addParameters()
|
---|
| 243 | {
|
---|
| 244 | return mOptionsDescription.add_options();
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | void ComponentBase::parseParameters(const XmlComponentConfig & cfg)
|
---|
| 248 | {
|
---|
| 249 | LOG_INFO("Parsing parameters...");
|
---|
| 250 | LOG_INFO(mOptionsDescription);
|
---|
| 251 |
|
---|
| 252 | vector<string> xargs = convertAttributesToArgumentVector(cfg.getProperties());
|
---|
| 253 |
|
---|
[201] | 254 | po::variables_map vm;
|
---|
[176] | 255 | try {
|
---|
| 256 | po::store(
|
---|
| 257 | po::command_line_parser(xargs)
|
---|
| 258 | .options(mOptionsDescription)
|
---|
[180] | 259 | .allow_unregistered() // FIXME: temporary only, at term all the components specify all parameters
|
---|
[176] | 260 | .run()
|
---|
| 261 | , vm);
|
---|
| 262 | po::notify(vm);
|
---|
| 263 | } catch (po::error & e) {
|
---|
[201] | 264 | LOG_WARN(e.what());
|
---|
| 265 | throw PacpusException(e.what());
|
---|
[176] | 266 | }
|
---|
| 267 |
|
---|
[204] | 268 | LOG_INFO("Parsed parameter values:\n" << vm);
|
---|
[176] | 269 | }
|
---|
| 270 |
|
---|
| 271 | vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes)
|
---|
| 272 | {
|
---|
| 273 | vector<string> xargs;
|
---|
| 274 | xargs.reserve(attributes.size());
|
---|
| 275 |
|
---|
| 276 | for (int i = 0; i < attributes.size(); ++i) {
|
---|
| 277 | QDomAttr parameter = attributes.item(i).toAttr();
|
---|
| 278 | if (parameter.isNull()) {
|
---|
| 279 | LOG_WARN("node is not a parameter");
|
---|
| 280 | continue;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | QString arg = QString("--") + parameter.name() + "=";
|
---|
| 284 |
|
---|
| 285 | bool shouldAddQuotes = parameter.value().contains(' ');
|
---|
| 286 | if (shouldAddQuotes) {
|
---|
| 287 | arg += '\"';
|
---|
| 288 | arg += parameter.value();
|
---|
| 289 | arg += '\"';
|
---|
| 290 | } else {
|
---|
| 291 | arg += parameter.value();
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | LOG_DEBUG("parameter: " << arg);
|
---|
| 295 | xargs.push_back(arg.toStdString());
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | return xargs;
|
---|
| 299 | }
|
---|