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