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