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
RevLine 
[89]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
[176]11#include <boost/program_options/parsers.hpp>
12#include <boost/program_options/variables_map.hpp>
13#include <string>
14#include <vector>
15
[181]16const bool kPropertyVerboseDefaultValue = false;
17
[176]18namespace po = boost::program_options;
[89]19using namespace pacpus;
[176]20using namespace std;
[89]21
[176]22std::vector<std::string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes);
23void logVariablesMap(boost::program_options::variables_map vm);
24
[89]25DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase");
26
[152]27ComponentBase::ComponentBase(const QString& componentName)
28 : m_componentName(componentName)
29 , m_isActive(false)
[178]30 , mIsRecording(true)
[152]31 , m_manager(NULL)
32 , m_ui(NULL)
33 , m_componentState(NOT_MONITORED)
[176]34 , mOptionsDescription("Component parameters")
[89]35{
36 LOG_TRACE("constructor");
37 // Get a pointer on the instance of ComponentManager.
[152]38 m_manager = ComponentManager::getInstance();
[176]39 LOG_INFO("component " << getName() << " was created");
40
[179]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")
[176]48 ;
[89]49}
50
51ComponentBase::~ComponentBase()
52{
53 LOG_TRACE("destructor");
54}
55
[152]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{
[177]68 return mIsRecording;
[152]69}
70
71void ComponentBase::setRecording(bool isRecording)
72{
[177]73 mIsRecording = isRecording;
[152]74}
75
76const XmlComponentConfig ComponentBase::xmlParameters() const
77{
78 return param;
79}
80
[89]81int ComponentBase::startComponent()
82{
[152]83 if (isActive()) {
84 LOG_DEBUG("component already started, cannot (re-)start");
85 return false;
86 }
87
88 setActive(true);
[89]89 startActivity();
90
91 return true;
92}
93
94int ComponentBase::stopComponent()
95{
[152]96 if (!isActive()) {
97 LOG_DEBUG("component already stopped, cannot (re-)stop");
98 return false;
99 }
100
101 setActive(false);
[89]102 stopActivity();
103
104 return true;
105}
106
107void ComponentBase::setState(const COMPONENT_STATE state)
108{
[152]109 m_componentState = state;
[89]110}
111
112// FIXME: this should be const.
113ComponentBase::COMPONENT_STATE ComponentBase::getState()
114{
[152]115 COMPONENT_STATE state = m_componentState;
116 if (ComponentBase::NOT_MONITORED != m_componentState) {
117 m_componentState = ComponentBase::MONITOR_NOK;
[89]118 }
119 return state;
120}
121
[152]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
[89]132bool ComponentBase::isConfigured() const
133{
[152]134 return (m_configurationState == CONFIGURED_OK);
[89]135}
136
137QString ComponentBase::getName() const
138{
[152]139 return m_componentName;
[89]140}
141
[152]142ComponentBase::InputsMap & ComponentBase::inputs()
[89]143{
[152]144 return m_inputs;
145}
[89]146
[152]147const ComponentBase::InputsMap & ComponentBase::inputs() const
148{
149 return m_inputs;
[89]150}
151
[152]152ComponentBase::OutputsMap & ComponentBase::outputs()
[89]153{
[152]154 return m_outputs;
155}
[89]156
[152]157const ComponentBase::OutputsMap & ComponentBase::outputs() const
158{
159 return m_outputs;
[89]160}
[120]161
[152]162InputInterfaceBase * ComponentBase::getInput(QString inputName) const
[120]163{
[152]164 if (inputs().contains(inputName)) {
165 return inputs()[inputName];
166 }
[176]167 LOG_WARN("Component " << getName() << " does not contain input " << inputName);
[152]168 return NULL;
[120]169}
170
[152]171OutputInterfaceBase * ComponentBase::getOutput(QString outputName) const
[120]172{
[152]173/* QList<QString> keys = output.keys();
174 for(int i=0; i<keys.size();++i)
175 LOG_INFO("Key : " << keys[i])*/;
[120]176
[152]177 if (outputs().contains(outputName)) {
178 return outputs()[outputName];
179 }
[176]180 LOG_WARN("Component " << getName() << " does not containt output " << outputName);
[152]181 return NULL;
[120]182}
[176]183
[181]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
[176]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)
[180]216 .allow_unregistered() // FIXME: temporary only, at term all the components specify all parameters
[176]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.