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