source: pacpusframework/trunk/src/PacpusLib/ComponentManager.cpp@ 180

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

Added: addParameters() method in ComponentBase using Boost.Program_Options.
Each component can declare its parameters and they will be read automatically before configureComponent() method.
See example ProducerConsumerExample constructors.

  • Property svn:executable set to *
File size: 13.8 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/// @author Gerald Dherbomez <firstname.surname@utc.fr>
6/// @version $Id: ComponentManager.cpp 76 2013-01-10 17:05:10Z kurdejma $
7
8#include <Pacpus/kernel/ComponentFactoryBase.h>
9#include <Pacpus/kernel/ComponentManager.h>
10#include <Pacpus/kernel/ComponentBase.h>
11#include <Pacpus/kernel/ConnectionBase.h>
12#include <Pacpus/kernel/Log.h>
13#include <QObject>
14#include <QDomNodeList>
15
16using namespace pacpus;
17
18DECLARE_STATIC_LOGGER("pacpus.core.ComponentManager");
19
20ComponentManager * ComponentManager::mInstance = NULL;
21
22ComponentManager * ComponentManager::create()
23{
24 return getInstance();
25}
26
27ComponentManager * ComponentManager::getInstance()
28{
29 LOG_TRACE("getInstance()");
30 LOG_TRACE("before: mInstance = " << mInstance);
31
32 if (!mInstance) {
33 LOG_INFO("creating new instance...");
34 mInstance = new ComponentManager();
35 LOG_DEBUG("mInstance = " << mInstance);
36 }
37
38 LOG_TRACE("after : mInstance = " << mInstance);
39 return mInstance;
40}
41
42void ComponentManager::destroy()
43{
44 LOG_TRACE("destroy");
45
46 LOG_TRACE("before: mInstance = " << mInstance);
47 delete mInstance;
48 mInstance = NULL;
49 LOG_TRACE("after : mInstance = " << mInstance);
50}
51
52ComponentManager::ComponentManager()
53{
54 LOG_TRACE("constructor");
55
56 xmlTree_ = XmlConfigFile::create();
57 LOG_DEBUG("component manager was created");
58}
59
60ComponentManager::~ComponentManager()
61{
62 LOG_TRACE("destructor");
63
64 for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) {
65 bool unregisteredSuccessfully = unregisterComponent(it.key());
66 }
67
68 LOG_DEBUG("component manager was deleted");
69}
70
71bool ComponentManager::registerComponentFactory(ComponentFactoryBase* addr, const QString& type)
72{
73 LOG_TRACE("registerComponentFactory(type="<< type << ")");
74
75 if (factoryMap_.contains(type))
76 {
77 LOG_WARN("cannot register a component factory of type '" << type << "'. It already belongs to the manager");
78 return false;
79 }
80
81 factoryMap_[type] = addr;
82 LOG_INFO("registered component factory '" << type << "'");
83
84 return true;
85}
86
87bool ComponentManager::unregisterComponentFactory(const QString& type)
88{
89 LOG_TRACE("unregisterComponentFactory(type="<< type << ")");
90
91 if (!factoryMap_.contains(type)) {
92 LOG_WARN("cannot unregister component factory '" << type << "'. It was not registered");
93 return false;
94 }
95
96 factoryMap_.remove(type);
97 LOG_INFO("unregistered component factory '" << type << "'");
98
99 return true;
100}
101
102bool ComponentManager::registerComponent(ComponentBase* addr, const QString& name)
103{
104 LOG_TRACE("registerComponent(name="<< name << ")");
105
106 if (componentMap_.contains(name))
107 {
108 LOG_WARN("cannot register component '" << name << "'. A component with the same name exists already");
109 return false;
110 }
111
112 componentMap_[name] = addr;
113 LOG_INFO("registered component " << name);
114
115 return true;
116}
117
118bool ComponentManager::unregisterComponent(const QString& name)
119{
120 LOG_TRACE("unregisterComponent(name="<< name << ")");
121
122 if (!componentMap_.contains(name)) {
123 LOG_WARN("cannot unregister component '" << name << "'. It was not registered");
124 return false;
125 }
126
127 // FIXME: delete component
128 ComponentBase* component = componentMap_.value(name, NULL);
129 //delete component;
130
131 // FIXME: remove from map (causes segfault in QMap::qMapLessThanKey on Windows)
132 //componentMap_.remove(name);
133 LOG_INFO("unregistered component '" << name << "'");
134
135 return true;
136}
137
138bool ComponentManager::createComponent(const QString& type, const QString& name)
139{
140 LOG_TRACE("createComponent(type=" << type << ", " << "name="<< name << ")");
141
142 if (factoryMap_.contains(type)) {
143 ComponentFactoryBase* factory = factoryMap_.value(type);
144 assert(factory);
145 factory->addComponent(name);
146 return true;
147 }
148
149 LOG_WARN("cannot create component '" << name << "'"
150 << ". Component factory for type '" << type << "'"
151 << " does not exist or was not registered"
152 );
153 return false;
154}
155
156bool ComponentManager::loadPlugin(const QString& filename)
157{
158 LOG_TRACE("loadPlugin(filename=" << filename << ")");
159
160 pluginLoader_.setFileName(filename);
161
162 if (!pluginLoader_.load()) {
163 LOG_ERROR("cannot load plugin '" << filename << "'"
164 << ". Plugin loader returned error: " << pluginLoader_.errorString()
165 );
166 return false;
167 }
168
169 QObject * plugin = pluginLoader_.instance();
170 if (NULL == plugin) {
171 LOG_WARN("cannot create an instance of the plugin '" << filename << "'"
172 << ". Plugin loader returned error: " << pluginLoader_.errorString()
173 );
174 return false;
175 }
176 pluginList_.append(plugin);
177 LOG_INFO("loaded plugin '" << qobject_cast<PacpusPluginInterface*>(plugin)->name() << "'"
178 << " from file '" << pluginLoader_.fileName() << "'"
179 );
180 return true;
181}
182
183bool ComponentManager::createConnection(const QString& outputSignature, const QString& inputSignature, const QString& type, int priority = 0)
184{
185 QStringList output = outputSignature.split(".");
186 QStringList input = inputSignature.split(".");
187
188 if(getComponent(output[0])==NULL) {
189 LOG_WARN("cannot make connection : component " << output[0] << " not found");
190 return false;}
191 if(getComponent(output[0])->getOutput(output[1]) == NULL) {
192 LOG_WARN("cannot make connection : component " << output[0] << " doesn't have output " << output[1]);
193 return false;}
194 if(getComponent(input[0])==NULL) {
195 LOG_WARN("cannot make connection : component " << input[0] << " not found");
196 return false;}
197 if(getComponent(input[0])->getInput(input[1]) == NULL) {
198 LOG_WARN("cannot make connection : component " << input[0] << " doesn't have intput " << input[1]);
199 return false;}
200
201 // NOTE Create communicationInterface if needed ??
202
203 return connectInterface(getComponent(output[0])->getOutput(output[1]), getComponent(input[0])->getInput(input[1]), priority);
204
205}
206
207std::size_t ComponentManager::loadComponents(const QString& configFilename)
208{
209 LOG_TRACE("loadComponents(filename=" << configFilename << ")");
210
211 // load the components tree in memory
212 xmlTree_->loadFile(configFilename);
213
214 {
215 // Load the plugins containing the components
216 QStringList plugins = xmlTree_->getAllPluginsNames();
217 Q_FOREACH (QString plugin, plugins) {
218 if (!loadPlugin(plugin)) {
219 LOG_WARN("cannot load plugin '" << plugin << "'");
220 } else {
221 LOG_INFO("successfully loaded plugin '" << plugin << "'");
222 }
223 }
224 }
225
226 QDomNodeList componentsNodeList = xmlTree_->getAllComponents();
227 XmlComponentConfig cfg;
228
229 // First, create all the components in the XML list
230 for (int i = 0; i < componentsNodeList.size(); ++i) {
231 cfg.localCopy(componentsNodeList.item(i).toElement());
232 QString componentType = cfg.getComponentType();
233 QString componentName = cfg.getComponentName();
234 LOG_DEBUG("try to create component '" << componentName << "'");
235
236 // create the component and automatically add it to the component manager list
237 if (!createComponent(componentType, componentName)) {
238 LOG_ERROR("cannot create component '" << componentName << "'");
239 continue;
240 }
241 }
242
243 int componentsToConfigureCount = componentMap_.count();
244
245 // Second, try to configure the components without regarding the dependencies
246 for (int i = 0; i < componentsNodeList.size(); ++i) {
247 cfg.localCopy(componentsNodeList.item(i).toElement());
248 QString componentName = cfg.getComponentName();
249 LOG_DEBUG("try to configure component '" << componentName << "'");
250
251 // copy locally the config parameters of the component
252 ComponentBase * component = getComponent(componentName);
253 if (NULL == component) {
254 LOG_WARN("component '" << componentName << "' does not exist");
255 } else {
256 component->param.localCopy(cfg.qDomElement());
257 component->parseParameters(cfg);
258 component->setConfigurationState(component->configureComponent(cfg));
259 }
260 } // for
261
262 // Third, if some components requested a delayed configuration, retry
263 for (int i = 0; i < componentsNodeList.size(); ++i) {
264 cfg.localCopy(componentsNodeList.item(i).toElement());
265 QString componentName = cfg.getComponentName();
266
267 ComponentBase * component = getComponent(componentName);
268 if (NULL == component) {
269 LOG_WARN("component '" << componentName << "' does not exist");
270 } else {
271 // Pacpus 2.0 : add inputs and outputs
272 component->addInputs();
273 component->addOutputs();
274
275 if (ComponentBase::CONFIGURATION_DELAYED == component->configurationState()) {
276 LOG_DEBUG("try to configure component '" << componentName << "'");
277
278 // copy locally the config parameters of the component
279 component->param.localCopy(cfg.qDomElement());
280 component->setConfigurationState(component->configureComponent(cfg));
281 }
282
283 if (ComponentBase::CONFIGURED_OK == component->configurationState()) {
284 --componentsToConfigureCount;
285 } else {
286 LOG_ERROR("cannot configure component '" << componentName << "'"
287 << ". Dependencies with other components are too complex"
288 << ". It was not configured, please review your configuration and/or your component"
289 );
290 component->setConfigurationState(ComponentBase::CONFIGURED_FAILED);
291 }
292 }
293 } // for
294
295 LOG_INFO(componentMap_.count() << " component(s) were loaded");
296 if (componentsToConfigureCount > 0) {
297 LOG_WARN(componentsToConfigureCount << " component(s) were not configured");
298 }
299
300 // Fourthly, create connections find in the XML list
301 QDomNodeList connectionsNodeList = xmlTree_->getAllConnections();
302
303 for (int i = 0; i < connectionsNodeList.size(); ++i) {
304 cfg.localCopy(connectionsNodeList.item(i).toElement());
305 QString connectionInput = cfg.getConnectionInput();
306 QString connectionOutput = cfg.getConnectionOutput();
307 QString connectionType = cfg.getConnectionType();
308 int connectionPriority = cfg.getConnectionPriority();
309
310 //TODO set connection mode from string
311
312 //InputInterfaceBase::GetLast;
313 //InputInterfaceBase::NeverSkip;
314 //InputInterfaceBase::TimeBounded;
315
316 if (!createConnection(connectionOutput, connectionInput, connectionType,connectionPriority)) {
317 LOG_ERROR("cannot create connection '" << connectionOutput+"=>"+connectionInput << "'");
318 continue;
319 }
320 } // for
321
322 return componentMap_.count();
323}
324
325bool ComponentManager::start()
326{
327 LOG_TRACE("start()");
328
329 bool result = true;
330 for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) {
331 result &= start(it.key());
332 }
333
334 return result;
335}
336
337bool ComponentManager::start(const QString& componentName)
338{
339 LOG_TRACE("start(component=" << componentName << ")");
340
341 ComponentBase* component = getComponent(componentName);
342 if (!component) {
343 LOG_WARN("cannot start component '" << componentName << "'. It does not exist!");
344 return false;
345 }
346
347 LOG_INFO("starting component '" << componentName << "'...");
348 if (!component->startComponent()) {
349 LOG_WARN("cannot start component '" << componentName << "'. It can already be started");
350 }
351 LOG_INFO("successfully started component '" << componentName << "'");
352 return true;
353}
354
355bool ComponentManager::stop()
356{
357 LOG_TRACE("stop()");
358
359 bool result = true;
360 for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) {
361 result &= stop(*it);
362 }
363
364 return result;
365}
366
367bool ComponentManager::stop(ComponentBase* component) const
368{
369 if (!component) {
370 LOG_WARN("NULL component pointer");
371 return false;
372 }
373 if (!component->stopComponent()) {
374 return false;
375 }
376 return true;
377}
378
379bool ComponentManager::stop(const QString& componentName)
380{
381 LOG_TRACE("stop(component=" << componentName << ")");
382
383 ComponentBase* component = getComponent(componentName);
384 if (!component) {
385 LOG_WARN("cannot stop component '" << componentName << "'" << ". It does not exist");
386 return false;
387 }
388
389 LOG_INFO("stopping component '" << componentName << "'...");
390 if (!stop(component)) {
391 LOG_WARN("cannot stop component '" << componentName << "'" << ". It can be already stopped");
392 }
393
394 return true;
395}
396
397ComponentBase* ComponentManager::getComponent(const QString& name)
398{
399 LOG_TRACE("getComponent(name=" << name << ")");
400
401 ComponentMap::iterator it = componentMap_.find(name);
402 if (it != componentMap_.end()) {
403 return *it;
404 }
405
406 LOG_WARN("cannot retrieve component '" << name << "'" << ". It does not exist");
407 return NULL;
408}
409
410QStringList ComponentManager::getAllComponentsName() const
411{
412 LOG_TRACE("getAllComponentsName()");
413 return xmlTree_->getAllComponentsNames();
414}
Note: See TracBrowser for help on using the repository browser.