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

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

Minor fixes. Clean-up.

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