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

Last change on this file since 201 was 201, checked in by Marek Kurdej, 11 years ago

Major: rethrow PacpusException on parameter parsing, set configurationState to ComponentBase::CONFIGURED_FAILED.

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