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

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

Added: OutputInterface::checkedSend.
Added: ComponentManager: prints InputsMap, OutputsMap.

  • 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 }
62 return os;
63}
64
65} // namespace std
66
67DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase");
68
69ComponentBase::ComponentBase(const QString & componentName)
70 : m_componentName(componentName)
71 , m_isActive(false)
72 , mIsRecording(true)
73 , m_manager(NULL)
74 , m_ui(NULL)
75 , m_componentState(NOT_MONITORED)
76 , mOptionsDescription("Component parameters")
77{
78 LOG_TRACE("constructor");
79 // Get a pointer on the instance of ComponentManager.
80 m_manager = ComponentManager::getInstance();
81 LOG_INFO("component " << getName() << " was created");
82
83 addParameters()
84 ("name", po::value<string>(&mName)->required(), "component name")
85 ("type", po::value<string>(&mTypeName)->required(), "component type")
86 ("ui", po::value<bool>(&mHasGui)->default_value(false), "whether to show GUI")
87 ("verbose", po::value<bool>(&mVerbose)->default_value(false), "set output verbose")
88 ("verbosity-level", po::value<int>(&mVerbosityLevel)->default_value(0), "set verbosity level")
89 ("recording", po::value<bool>(&mIsRecording)->default_value(false), "whether to record data")
90 ;
91}
92
93ComponentBase::~ComponentBase()
94{
95 LOG_TRACE("destructor");
96}
97
98bool ComponentBase::isActive() const
99{
100 return m_isActive;
101}
102
103void ComponentBase::setActive(bool isActive)
104{
105 m_isActive = isActive;
106}
107
108bool ComponentBase::isRecording() const
109{
110 return mIsRecording;
111}
112
113void ComponentBase::setRecording(bool isRecording)
114{
115 mIsRecording = isRecording;
116}
117
118const XmlComponentConfig ComponentBase::xmlParameters() const
119{
120 return param;
121}
122
123int ComponentBase::startComponent()
124{
125 if (isActive()) {
126 LOG_DEBUG("component already started, cannot (re-)start");
127 return false;
128 }
129
130 setActive(true);
131 startActivity();
132
133 return true;
134}
135
136int ComponentBase::stopComponent()
137{
138 if (!isActive()) {
139 LOG_DEBUG("component already stopped, cannot (re-)stop");
140 return false;
141 }
142
143 setActive(false);
144 stopActivity();
145
146 return true;
147}
148
149void ComponentBase::setState(const COMPONENT_STATE state)
150{
151 m_componentState = state;
152}
153
154// FIXME: this should be const.
155ComponentBase::COMPONENT_STATE ComponentBase::getState()
156{
157 COMPONENT_STATE state = m_componentState;
158 if (ComponentBase::NOT_MONITORED != m_componentState) {
159 m_componentState = ComponentBase::MONITOR_NOK;
160 }
161 return state;
162}
163
164ComponentBase::COMPONENT_CONFIGURATION ComponentBase::configurationState() const
165{
166 return m_configurationState;
167}
168
169void ComponentBase::setConfigurationState(COMPONENT_CONFIGURATION state)
170{
171 m_configurationState = state;
172}
173
174bool ComponentBase::isConfigured() const
175{
176 return (m_configurationState == CONFIGURED_OK);
177}
178
179QString ComponentBase::getName() const
180{
181 return m_componentName;
182}
183
184ComponentBase::InputsMap & ComponentBase::inputs()
185{
186 return m_inputs;
187}
188
189const ComponentBase::InputsMap & ComponentBase::inputs() const
190{
191 return m_inputs;
192}
193
194ComponentBase::OutputsMap & ComponentBase::outputs()
195{
196 return m_outputs;
197}
198
199const ComponentBase::OutputsMap & ComponentBase::outputs() const
200{
201 return m_outputs;
202}
203
204InputInterfaceBase * ComponentBase::getInput(QString inputName) const
205{
206 if (inputs().contains(inputName)) {
207 return inputs()[inputName];
208 }
209 LOG_WARN("Component " << getName() << " does not contain input " << inputName);
210 return NULL;
211}
212
213OutputInterfaceBase * ComponentBase::getOutput(QString outputName) const
214{
215/* QList<QString> keys = output.keys();
216 for (int i=0; i<keys.size();++i)
217 LOG_INFO("Key : " << keys[i])*/;
218
219 if (outputs().contains(outputName)) {
220 return outputs()[outputName];
221 }
222 LOG_WARN("Component " << getName() << " does not containt 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(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.