source: pacpusframework/branches/2.0-beta1/src/PacpusLib/ComponentManager.cpp@ 162

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

Update: merged changes from trunk.

  • Property svn:executable set to *
File size: 13.7 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->setConfigurationState(component->configureComponent(cfg));
258 }
259 } // for
260
261 // Third, if some components requested a delayed configuration, retry
262 for (int i = 0; i < componentsNodeList.size(); ++i) {
263 cfg.localCopy(componentsNodeList.item(i).toElement());
264 QString componentName = cfg.getComponentName();
265
266 ComponentBase * component = getComponent(componentName);
267 if (NULL == component) {
268 LOG_WARN("component '" << componentName << "' does not exist");
269 } else {
270 // Pacpus 2.0 : add inputs and outputs
271 component->addInputs();
272 component->addOutputs();
273
274 if (ComponentBase::CONFIGURATION_DELAYED == component->configurationState()) {
275 LOG_DEBUG("try to configure component '" << componentName << "'");
276
277 // copy locally the config parameters of the component
278 component->param.localCopy(cfg.qDomElement());
279 component->setConfigurationState(component->configureComponent(cfg));
280 }
281
282 if (ComponentBase::CONFIGURED_OK == component->configurationState()) {
283 --componentsToConfigureCount;
284 } else {
285 LOG_ERROR("cannot configure component '" << componentName << "'"
286 << ". Dependencies with other components are too complex"
287 << ". It was not configured, please review your configuration and/or your component"
288 );
289 component->setConfigurationState(ComponentBase::CONFIGURED_FAILED);
290 }
291 }
292 } // for
293
294 LOG_INFO(componentMap_.count() << " component(s) were loaded");
295 if (componentsToConfigureCount > 0) {
296 LOG_WARN(componentsToConfigureCount << " component(s) were not configured");
297 }
298
299 // Fourthly, create connections find in the XML list
300 QDomNodeList connectionsNodeList = xmlTree_->getAllConnections();
301
302 for (int i = 0; i < connectionsNodeList.size(); ++i) {
303 cfg.localCopy(connectionsNodeList.item(i).toElement());
304 QString connectionInput = cfg.getConnectionInput();
305 QString connectionOutput = cfg.getConnectionOutput();
306 QString connectionType = cfg.getConnectionType();
307 int connectionPriority = cfg.getConnectionPriority();
308
309 //TODO set connection mode from string
310
311 //InputInterfaceBase::GetLast;
312 //InputInterfaceBase::NeverSkip;
313 //InputInterfaceBase::TimeBounded;
314
315 if (!createConnection(connectionOutput, connectionInput, connectionType,connectionPriority)) {
316 LOG_ERROR("cannot create connection '" << connectionOutput+"=>"+connectionInput << "'");
317 continue;
318 }
319 } // for
320
321 return componentMap_.count();
322}
323
324bool ComponentManager::start()
325{
326 LOG_TRACE("start()");
327
328 bool result = true;
329 for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) {
330 result &= start(it.key());
331 }
332
333 return result;
334}
335
336bool ComponentManager::start(const QString& componentName)
337{
338 LOG_TRACE("start(component=" << componentName << ")");
339
340 ComponentBase* component = getComponent(componentName);
341 if (!component) {
342 LOG_WARN("cannot start component '" << componentName << "'. It does not exist!");
343 return false;
344 }
345
346 LOG_INFO("starting component '" << componentName << "'...");
347 if (!component->startComponent()) {
348 LOG_WARN("cannot start component '" << componentName << "'. It can already be started");
349 }
350 LOG_INFO("successfully started component '" << componentName << "'");
351 return true;
352}
353
354bool ComponentManager::stop()
355{
356 LOG_TRACE("stop()");
357
358 bool result = true;
359 for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) {
360 result &= stop(*it);
361 }
362
363 return result;
364}
365
366bool ComponentManager::stop(ComponentBase* component) const
367{
368 if (!component) {
369 LOG_WARN("NULL component pointer");
370 return false;
371 }
372 if (!component->stopComponent()) {
373 return false;
374 }
375 return true;
376}
377
378bool ComponentManager::stop(const QString& componentName)
379{
380 LOG_TRACE("stop(component=" << componentName << ")");
381
382 ComponentBase* component = getComponent(componentName);
383 if (!component) {
384 LOG_WARN("cannot stop component '" << componentName << "'" << ". It does not exist");
385 return false;
386 }
387
388 LOG_INFO("stopping component '" << componentName << "'...");
389 if (!stop(component)) {
390 LOG_WARN("cannot stop component '" << componentName << "'" << ". It can be already stopped");
391 }
392
393 return true;
394}
395
396ComponentBase* ComponentManager::getComponent(const QString& name)
397{
398 LOG_TRACE("getComponent(name=" << name << ")");
399
400 ComponentMap::iterator it = componentMap_.find(name);
401 if (it != componentMap_.end()) {
402 return *it;
403 }
404
405 LOG_WARN("cannot retrieve component '" << name << "'" << ". It does not exist");
406 return NULL;
407}
408
409QStringList ComponentManager::getAllComponentsName() const
410{
411 LOG_TRACE("getAllComponentsName()");
412 return xmlTree_->getAllComponentsNames();
413}
Note: See TracBrowser for help on using the repository browser.