source: pacpusframework/branches/0.1.x/src/PacpusLib/ComponentManager.cpp@ 235

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

Fixed: Qt4 compilation on Linux

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