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

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

Minor: output format.

  • Property svn:executable set to *
File size: 8.0 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/* QList<QString> keys = output.keys();
217 for (int i=0; i<keys.size();++i)
218 LOG_INFO("Key : " << keys[i])*/;
219
220 if (outputs().contains(outputName)) {
221 return outputs()[outputName];
222 }
223 LOG_WARN("Component " << getName() << " does not containt output " << outputName);
224 return NULL;
225}
226
227bool ComponentBase::hasGui() const
228{
229 return mHasGui;
230}
231
232bool ComponentBase::isOutputVerbose() const
233{
234 return mVerbose || (getVerbosityLevel() > 0);
235}
236
237int ComponentBase::getVerbosityLevel() const
238{
239 return mVerbosityLevel;
240}
241
242po::options_description_easy_init ComponentBase::addParameters()
243{
244 return mOptionsDescription.add_options();
245}
246
247void ComponentBase::parseParameters(const XmlComponentConfig & cfg)
248{
249 LOG_INFO("Parsing parameters...");
250 LOG_INFO(mOptionsDescription);
251
252 vector<string> xargs = convertAttributesToArgumentVector(cfg.getProperties());
253
254 po::variables_map vm;
255 try {
256 po::store(
257 po::command_line_parser(xargs)
258 .options(mOptionsDescription)
259 .allow_unregistered() // FIXME: temporary only, at term all the components specify all parameters
260 .run()
261 , vm);
262 po::notify(vm);
263 } catch (po::error & e) {
264 LOG_WARN(e.what());
265 throw PacpusException(e.what());
266 }
267
268 LOG_INFO("Parsed parameter values:\n" << vm);
269}
270
271vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes)
272{
273 vector<string> xargs;
274 xargs.reserve(attributes.size());
275
276 for (int i = 0; i < attributes.size(); ++i) {
277 QDomAttr parameter = attributes.item(i).toAttr();
278 if (parameter.isNull()) {
279 LOG_WARN("node is not a parameter");
280 continue;
281 }
282
283 QString arg = QString("--") + parameter.name() + "=";
284
285 bool shouldAddQuotes = parameter.value().contains(' ');
286 if (shouldAddQuotes) {
287 arg += '\"';
288 arg += parameter.value();
289 arg += '\"';
290 } else {
291 arg += parameter.value();
292 }
293
294 LOG_DEBUG("parameter: " << arg);
295 xargs.push_back(arg.toStdString());
296 }
297
298 return xargs;
299}
Note: See TracBrowser for help on using the repository browser.