source: pacpusframework/trunk/src/PacpusLib/ComponentBase.cpp@ 263

Last change on this file since 263 was 263, checked in by Marek Kurdej, 10 years ago

Minor: write parameter name for unknown type to console.

  • Property svn:executable set to *
File size: 7.9 KB
RevLine 
[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
19namespace po = boost::program_options;
[89]20using namespace pacpus;
[176]21using namespace std;
[89]22
[182]23vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes);
[176]24
[202]25namespace std {
26
27template <typename _Elem, typename _Traits>
28std::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;
[263]60 } else {
61 // unknown value type
62 os << i->first;
63 }
[204]64 os << "\n";
[202]65 }
66 return os;
67}
68
69} // namespace std
70
[89]71DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase");
72
[182]73ComponentBase::ComponentBase(const QString & componentName)
74 : m_componentName(componentName)
75 , m_isActive(false)
76 , mIsRecording(true)
77 , m_manager(NULL)
78 , m_ui(NULL)
79 , m_componentState(NOT_MONITORED)
80 , mOptionsDescription("Component parameters")
[89]81{
82 LOG_TRACE("constructor");
83 // Get a pointer on the instance of ComponentManager.
[152]84 m_manager = ComponentManager::getInstance();
[176]85 LOG_INFO("component " << getName() << " was created");
86
[179]87 addParameters()
88 ("name", po::value<string>(&mName)->required(), "component name")
89 ("type", po::value<string>(&mTypeName)->required(), "component type")
90 ("ui", po::value<bool>(&mHasGui)->default_value(false), "whether to show GUI")
91 ("verbose", po::value<bool>(&mVerbose)->default_value(false), "set output verbose")
92 ("verbosity-level", po::value<int>(&mVerbosityLevel)->default_value(0), "set verbosity level")
93 ("recording", po::value<bool>(&mIsRecording)->default_value(false), "whether to record data")
[176]94 ;
[89]95}
96
97ComponentBase::~ComponentBase()
98{
99 LOG_TRACE("destructor");
100}
101
[152]102bool ComponentBase::isActive() const
103{
104 return m_isActive;
105}
106
107void ComponentBase::setActive(bool isActive)
108{
109 m_isActive = isActive;
110}
111
112bool ComponentBase::isRecording() const
113{
[177]114 return mIsRecording;
[152]115}
116
117void ComponentBase::setRecording(bool isRecording)
118{
[177]119 mIsRecording = isRecording;
[152]120}
121
122const XmlComponentConfig ComponentBase::xmlParameters() const
123{
124 return param;
125}
126
[89]127int ComponentBase::startComponent()
128{
[152]129 if (isActive()) {
130 LOG_DEBUG("component already started, cannot (re-)start");
131 return false;
132 }
133
134 setActive(true);
[89]135 startActivity();
136
137 return true;
138}
139
140int ComponentBase::stopComponent()
141{
[152]142 if (!isActive()) {
143 LOG_DEBUG("component already stopped, cannot (re-)stop");
144 return false;
145 }
146
147 setActive(false);
[89]148 stopActivity();
149
150 return true;
151}
152
153void ComponentBase::setState(const COMPONENT_STATE state)
154{
[152]155 m_componentState = state;
[89]156}
157
158// FIXME: this should be const.
159ComponentBase::COMPONENT_STATE ComponentBase::getState()
160{
[152]161 COMPONENT_STATE state = m_componentState;
162 if (ComponentBase::NOT_MONITORED != m_componentState) {
163 m_componentState = ComponentBase::MONITOR_NOK;
[89]164 }
165 return state;
166}
167
[152]168ComponentBase::COMPONENT_CONFIGURATION ComponentBase::configurationState() const
169{
170 return m_configurationState;
171}
172
173void ComponentBase::setConfigurationState(COMPONENT_CONFIGURATION state)
174{
175 m_configurationState = state;
176}
177
[89]178bool ComponentBase::isConfigured() const
179{
[152]180 return (m_configurationState == CONFIGURED_OK);
[89]181}
182
183QString ComponentBase::getName() const
184{
[152]185 return m_componentName;
[89]186}
187
[152]188ComponentBase::InputsMap & ComponentBase::inputs()
[89]189{
[152]190 return m_inputs;
191}
[89]192
[152]193const ComponentBase::InputsMap & ComponentBase::inputs() const
194{
195 return m_inputs;
[89]196}
197
[152]198ComponentBase::OutputsMap & ComponentBase::outputs()
[89]199{
[152]200 return m_outputs;
201}
[89]202
[152]203const ComponentBase::OutputsMap & ComponentBase::outputs() const
204{
205 return m_outputs;
[89]206}
[120]207
[152]208InputInterfaceBase * ComponentBase::getInput(QString inputName) const
[120]209{
[152]210 if (inputs().contains(inputName)) {
211 return inputs()[inputName];
212 }
[176]213 LOG_WARN("Component " << getName() << " does not contain input " << inputName);
[152]214 return NULL;
[120]215}
216
[152]217OutputInterfaceBase * ComponentBase::getOutput(QString outputName) const
[120]218{
[152]219 if (outputs().contains(outputName)) {
220 return outputs()[outputName];
221 }
[206]222 LOG_WARN("Component " << getName() << " does not contain output " << outputName);
[152]223 return NULL;
[120]224}
[176]225
[181]226bool ComponentBase::hasGui() const
227{
228 return mHasGui;
229}
230
231bool ComponentBase::isOutputVerbose() const
232{
233 return mVerbose || (getVerbosityLevel() > 0);
234}
235
236int ComponentBase::getVerbosityLevel() const
237{
238 return mVerbosityLevel;
239}
240
[176]241po::options_description_easy_init ComponentBase::addParameters()
242{
243 return mOptionsDescription.add_options();
244}
245
246void 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
[204]267 LOG_INFO("Parsed parameter values:\n" << vm);
[176]268}
269
270vector<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}
Note: See TracBrowser for help on using the repository browser.