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

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

Major: cleaned connection interfaces.

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