[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 | }
|
---|
| 61 | }
|
---|
| 62 | return os;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | } // namespace std
|
---|
| 66 |
|
---|
[89] | 67 | DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase");
|
---|
| 68 |
|
---|
[182] | 69 | ComponentBase::ComponentBase(const QString & componentName)
|
---|
| 70 | : m_componentName(componentName)
|
---|
| 71 | , m_isActive(false)
|
---|
| 72 | , mIsRecording(true)
|
---|
| 73 | , m_manager(NULL)
|
---|
| 74 | , m_ui(NULL)
|
---|
| 75 | , m_componentState(NOT_MONITORED)
|
---|
| 76 | , mOptionsDescription("Component parameters")
|
---|
[89] | 77 | {
|
---|
| 78 | LOG_TRACE("constructor");
|
---|
| 79 | // Get a pointer on the instance of ComponentManager.
|
---|
[152] | 80 | m_manager = ComponentManager::getInstance();
|
---|
[176] | 81 | LOG_INFO("component " << getName() << " was created");
|
---|
| 82 |
|
---|
[179] | 83 | addParameters()
|
---|
| 84 | ("name", po::value<string>(&mName)->required(), "component name")
|
---|
| 85 | ("type", po::value<string>(&mTypeName)->required(), "component type")
|
---|
| 86 | ("ui", po::value<bool>(&mHasGui)->default_value(false), "whether to show GUI")
|
---|
| 87 | ("verbose", po::value<bool>(&mVerbose)->default_value(false), "set output verbose")
|
---|
| 88 | ("verbosity-level", po::value<int>(&mVerbosityLevel)->default_value(0), "set verbosity level")
|
---|
| 89 | ("recording", po::value<bool>(&mIsRecording)->default_value(false), "whether to record data")
|
---|
[176] | 90 | ;
|
---|
[89] | 91 | }
|
---|
| 92 |
|
---|
| 93 | ComponentBase::~ComponentBase()
|
---|
| 94 | {
|
---|
| 95 | LOG_TRACE("destructor");
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[152] | 98 | bool ComponentBase::isActive() const
|
---|
| 99 | {
|
---|
| 100 | return m_isActive;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | void ComponentBase::setActive(bool isActive)
|
---|
| 104 | {
|
---|
| 105 | m_isActive = isActive;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | bool ComponentBase::isRecording() const
|
---|
| 109 | {
|
---|
[177] | 110 | return mIsRecording;
|
---|
[152] | 111 | }
|
---|
| 112 |
|
---|
| 113 | void ComponentBase::setRecording(bool isRecording)
|
---|
| 114 | {
|
---|
[177] | 115 | mIsRecording = isRecording;
|
---|
[152] | 116 | }
|
---|
| 117 |
|
---|
| 118 | const XmlComponentConfig ComponentBase::xmlParameters() const
|
---|
| 119 | {
|
---|
| 120 | return param;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[89] | 123 | int ComponentBase::startComponent()
|
---|
| 124 | {
|
---|
[152] | 125 | if (isActive()) {
|
---|
| 126 | LOG_DEBUG("component already started, cannot (re-)start");
|
---|
| 127 | return false;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | setActive(true);
|
---|
[89] | 131 | startActivity();
|
---|
| 132 |
|
---|
| 133 | return true;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | int ComponentBase::stopComponent()
|
---|
| 137 | {
|
---|
[152] | 138 | if (!isActive()) {
|
---|
| 139 | LOG_DEBUG("component already stopped, cannot (re-)stop");
|
---|
| 140 | return false;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | setActive(false);
|
---|
[89] | 144 | stopActivity();
|
---|
| 145 |
|
---|
| 146 | return true;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | void ComponentBase::setState(const COMPONENT_STATE state)
|
---|
| 150 | {
|
---|
[152] | 151 | m_componentState = state;
|
---|
[89] | 152 | }
|
---|
| 153 |
|
---|
| 154 | // FIXME: this should be const.
|
---|
| 155 | ComponentBase::COMPONENT_STATE ComponentBase::getState()
|
---|
| 156 | {
|
---|
[152] | 157 | COMPONENT_STATE state = m_componentState;
|
---|
| 158 | if (ComponentBase::NOT_MONITORED != m_componentState) {
|
---|
| 159 | m_componentState = ComponentBase::MONITOR_NOK;
|
---|
[89] | 160 | }
|
---|
| 161 | return state;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[152] | 164 | ComponentBase::COMPONENT_CONFIGURATION ComponentBase::configurationState() const
|
---|
| 165 | {
|
---|
| 166 | return m_configurationState;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | void ComponentBase::setConfigurationState(COMPONENT_CONFIGURATION state)
|
---|
| 170 | {
|
---|
| 171 | m_configurationState = state;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[89] | 174 | bool ComponentBase::isConfigured() const
|
---|
| 175 | {
|
---|
[152] | 176 | return (m_configurationState == CONFIGURED_OK);
|
---|
[89] | 177 | }
|
---|
| 178 |
|
---|
| 179 | QString ComponentBase::getName() const
|
---|
| 180 | {
|
---|
[152] | 181 | return m_componentName;
|
---|
[89] | 182 | }
|
---|
| 183 |
|
---|
[152] | 184 | ComponentBase::InputsMap & ComponentBase::inputs()
|
---|
[89] | 185 | {
|
---|
[152] | 186 | return m_inputs;
|
---|
| 187 | }
|
---|
[89] | 188 |
|
---|
[152] | 189 | const ComponentBase::InputsMap & ComponentBase::inputs() const
|
---|
| 190 | {
|
---|
| 191 | return m_inputs;
|
---|
[89] | 192 | }
|
---|
| 193 |
|
---|
[152] | 194 | ComponentBase::OutputsMap & ComponentBase::outputs()
|
---|
[89] | 195 | {
|
---|
[152] | 196 | return m_outputs;
|
---|
| 197 | }
|
---|
[89] | 198 |
|
---|
[152] | 199 | const ComponentBase::OutputsMap & ComponentBase::outputs() const
|
---|
| 200 | {
|
---|
| 201 | return m_outputs;
|
---|
[89] | 202 | }
|
---|
[120] | 203 |
|
---|
[152] | 204 | InputInterfaceBase * ComponentBase::getInput(QString inputName) const
|
---|
[120] | 205 | {
|
---|
[152] | 206 | if (inputs().contains(inputName)) {
|
---|
| 207 | return inputs()[inputName];
|
---|
| 208 | }
|
---|
[176] | 209 | LOG_WARN("Component " << getName() << " does not contain input " << inputName);
|
---|
[152] | 210 | return NULL;
|
---|
[120] | 211 | }
|
---|
| 212 |
|
---|
[152] | 213 | OutputInterfaceBase * ComponentBase::getOutput(QString outputName) const
|
---|
[120] | 214 | {
|
---|
[152] | 215 | /* QList<QString> keys = output.keys();
|
---|
[182] | 216 | for (int i=0; i<keys.size();++i)
|
---|
[152] | 217 | LOG_INFO("Key : " << keys[i])*/;
|
---|
[120] | 218 |
|
---|
[152] | 219 | if (outputs().contains(outputName)) {
|
---|
| 220 | return outputs()[outputName];
|
---|
| 221 | }
|
---|
[176] | 222 | LOG_WARN("Component " << getName() << " does not containt output " << outputName);
|
---|
[152] | 223 | return NULL;
|
---|
[120] | 224 | }
|
---|
[176] | 225 |
|
---|
[181] | 226 | bool ComponentBase::hasGui() const
|
---|
| 227 | {
|
---|
| 228 | return mHasGui;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | bool ComponentBase::isOutputVerbose() const
|
---|
| 232 | {
|
---|
| 233 | return mVerbose || (getVerbosityLevel() > 0);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | int ComponentBase::getVerbosityLevel() const
|
---|
| 237 | {
|
---|
| 238 | return mVerbosityLevel;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[176] | 241 | po::options_description_easy_init ComponentBase::addParameters()
|
---|
| 242 | {
|
---|
| 243 | return mOptionsDescription.add_options();
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | void ComponentBase::parseParameters(const XmlComponentConfig & cfg)
|
---|
| 247 | {
|
---|
| 248 | LOG_INFO("Parsing parameters...");
|
---|
| 249 | LOG_INFO(mOptionsDescription);
|
---|
| 250 |
|
---|
| 251 | vector<string> xargs = convertAttributesToArgumentVector(cfg.getProperties());
|
---|
| 252 |
|
---|
[201] | 253 | po::variables_map vm;
|
---|
[176] | 254 | try {
|
---|
| 255 | po::store(
|
---|
| 256 | po::command_line_parser(xargs)
|
---|
| 257 | .options(mOptionsDescription)
|
---|
[180] | 258 | .allow_unregistered() // FIXME: temporary only, at term all the components specify all parameters
|
---|
[176] | 259 | .run()
|
---|
| 260 | , vm);
|
---|
| 261 | po::notify(vm);
|
---|
| 262 | } catch (po::error & e) {
|
---|
[201] | 263 | LOG_WARN(e.what());
|
---|
| 264 | throw PacpusException(e.what());
|
---|
[176] | 265 | }
|
---|
| 266 |
|
---|
[202] | 267 | LOG_INFO(vm);
|
---|
[176] | 268 | }
|
---|
| 269 |
|
---|
| 270 | vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes)
|
---|
| 271 | {
|
---|
| 272 | vector<string> xargs;
|
---|
| 273 | xargs.reserve(attributes.size());
|
---|
| 274 |
|
---|
| 275 | for (int i = 0; i < attributes.size(); ++i) {
|
---|
| 276 | QDomAttr parameter = attributes.item(i).toAttr();
|
---|
| 277 | if (parameter.isNull()) {
|
---|
| 278 | LOG_WARN("node is not a parameter");
|
---|
| 279 | continue;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | QString arg = QString("--") + parameter.name() + "=";
|
---|
| 283 |
|
---|
| 284 | bool shouldAddQuotes = parameter.value().contains(' ');
|
---|
| 285 | if (shouldAddQuotes) {
|
---|
| 286 | arg += '\"';
|
---|
| 287 | arg += parameter.value();
|
---|
| 288 | arg += '\"';
|
---|
| 289 | } else {
|
---|
| 290 | arg += parameter.value();
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | LOG_DEBUG("parameter: " << arg);
|
---|
| 294 | xargs.push_back(arg.toStdString());
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | return xargs;
|
---|
| 298 | }
|
---|