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
Line 
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>
8
9#include <Pacpus/kernel/ComponentManager.h>
10#include <Pacpus/kernel/Log.h>
11#include <Pacpus/kernel/PacpusException.h>
12
13#include <boost/program_options/parsers.hpp>
14#include <boost/program_options/variables_map.hpp>
15#include <ostream>
16#include <string>
17#include <vector>
18
19namespace po = boost::program_options;
20using namespace pacpus;
21using namespace std;
22
23vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes);
24
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{
30 for (po::variables_map::const_iterator i = vm.begin(); i != vm.end(); ++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 } else {
61 // unknown value type
62 os << i->first;
63 }
64 os << "\n";
65 }
66 return os;
67}
68
69} // namespace std
70
71DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase");
72
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")
81{
82 LOG_TRACE("constructor");
83 // Get a pointer on the instance of ComponentManager.
84 m_manager = ComponentManager::getInstance();
85 LOG_INFO("component " << getName() << " was created");
86
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")
94 ;
95}
96
97ComponentBase::~ComponentBase()
98{
99 LOG_TRACE("destructor");
100}
101
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{
114 return mIsRecording;
115}
116
117void ComponentBase::setRecording(bool isRecording)
118{
119 mIsRecording = isRecording;
120}
121
122const XmlComponentConfig ComponentBase::xmlParameters() const
123{
124 return param;
125}
126
127int ComponentBase::startComponent()
128{
129 if (isActive()) {
130 LOG_DEBUG("component already started, cannot (re-)start");
131 return false;
132 }
133
134 setActive(true);
135 startActivity();
136
137 return true;
138}
139
140int ComponentBase::stopComponent()
141{
142 if (!isActive()) {
143 LOG_DEBUG("component already stopped, cannot (re-)stop");
144 return false;
145 }
146
147 setActive(false);
148 stopActivity();
149
150 return true;
151}
152
153void ComponentBase::setState(const COMPONENT_STATE state)
154{
155 m_componentState = state;
156}
157
158// FIXME: this should be const.
159ComponentBase::COMPONENT_STATE ComponentBase::getState()
160{
161 COMPONENT_STATE state = m_componentState;
162 if (ComponentBase::NOT_MONITORED != m_componentState) {
163 m_componentState = ComponentBase::MONITOR_NOK;
164 }
165 return state;
166}
167
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
178bool ComponentBase::isConfigured() const
179{
180 return (m_configurationState == CONFIGURED_OK);
181}
182
183QString ComponentBase::getName() const
184{
185 return m_componentName;
186}
187
188ComponentBase::InputsMap & ComponentBase::inputs()
189{
190 return m_inputs;
191}
192
193const ComponentBase::InputsMap & ComponentBase::inputs() const
194{
195 return m_inputs;
196}
197
198ComponentBase::OutputsMap & ComponentBase::outputs()
199{
200 return m_outputs;
201}
202
203const ComponentBase::OutputsMap & ComponentBase::outputs() const
204{
205 return m_outputs;
206}
207
208InputInterfaceBase * ComponentBase::getInput(QString inputName) const
209{
210 if (inputs().contains(inputName)) {
211 return inputs()[inputName];
212 }
213 LOG_WARN("Component " << getName() << " does not contain input " << inputName);
214 return NULL;
215}
216
217OutputInterfaceBase * ComponentBase::getOutput(QString outputName) const
218{
219 if (outputs().contains(outputName)) {
220 return outputs()[outputName];
221 }
222 LOG_WARN("Component " << getName() << " does not contain output " << outputName);
223 return NULL;
224}
225
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
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
253 po::variables_map vm;
254 try {
255 po::store(
256 po::command_line_parser(xargs)
257 .options(mOptionsDescription)
258 .allow_unregistered() // FIXME: temporary only, at term all the components specify all parameters
259 .run()
260 , vm);
261 po::notify(vm);
262 } catch (po::error & e) {
263 LOG_WARN(e.what());
264 throw PacpusException(e.what());
265 }
266
267 LOG_INFO("Parsed parameter values:\n" << vm);
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.