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