1 | // This file is part of the PACPUS framework distributed under the
|
---|
2 | // CECILL-C License, Version 1.0.
|
---|
3 | //
|
---|
4 | /// @version $Id: ComponentManager.cpp 64 2013-01-09 16:41:12Z kurdejma $
|
---|
5 |
|
---|
6 | #include <Pacpus/kernel/ComponentManager.h>
|
---|
7 | #include <Pacpus/kernel/ComponentBase.h>
|
---|
8 | #include <Pacpus/kernel/Log.h>
|
---|
9 |
|
---|
10 | using namespace pacpus;
|
---|
11 |
|
---|
12 | DECLARE_STATIC_LOGGER("pacpus.core.ComponentManager");
|
---|
13 |
|
---|
14 | ComponentManager * ComponentManager::mInstance = NULL;
|
---|
15 |
|
---|
16 | ComponentManager * ComponentManager::create()
|
---|
17 | {
|
---|
18 | return getInstance();
|
---|
19 | }
|
---|
20 |
|
---|
21 | ComponentManager * ComponentManager::getInstance()
|
---|
22 | {
|
---|
23 | LOG_TRACE("getInstance()");
|
---|
24 | LOG_TRACE("before: mInstance = " << mInstance);
|
---|
25 |
|
---|
26 | if (!mInstance)
|
---|
27 | {
|
---|
28 | LOG_INFO("creating new instance...");
|
---|
29 | mInstance = new ComponentManager();
|
---|
30 | LOG_DEBUG("mInstance = " << mInstance);
|
---|
31 | }
|
---|
32 |
|
---|
33 | LOG_TRACE("after: mInstance = " << mInstance);
|
---|
34 | return mInstance;
|
---|
35 | }
|
---|
36 |
|
---|
37 | void ComponentManager::destroy()
|
---|
38 | {
|
---|
39 | LOG_TRACE("destroy");
|
---|
40 |
|
---|
41 | delete mInstance;
|
---|
42 | mInstance = NULL;
|
---|
43 | }
|
---|
44 |
|
---|
45 | ComponentManager::ComponentManager()
|
---|
46 | {
|
---|
47 | LOG_TRACE("constructor");
|
---|
48 |
|
---|
49 | xmlTree_ = XmlConfigFile::create();
|
---|
50 | LOG_DEBUG("component manager was created");
|
---|
51 | }
|
---|
52 |
|
---|
53 | ComponentManager::~ComponentManager()
|
---|
54 | {
|
---|
55 | LOG_TRACE("destructor");
|
---|
56 |
|
---|
57 | QMutableMapIterator<ComponentMap::key_type, ComponentMap::mapped_type> it(componentMap_);
|
---|
58 | while (it.hasNext())
|
---|
59 | unRegisterComponent(it.next().key());
|
---|
60 |
|
---|
61 | LOG_DEBUG("component manager was deleted");
|
---|
62 | }
|
---|
63 |
|
---|
64 | bool ComponentManager::registerComponentFactory(ComponentFactoryBase* addr, const QString& type)
|
---|
65 | {
|
---|
66 | LOG_TRACE("registerComponentFactory(type="<< type << ")");
|
---|
67 |
|
---|
68 | if (factoryMap_.contains(type))
|
---|
69 | {
|
---|
70 | LOG_WARN("cannot register a component factory of type '" << type << "'. It already belongs to the manager");
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | factoryMap_[type] = addr;
|
---|
75 | LOG_INFO("registered component factory '" << type << "'");
|
---|
76 |
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | bool ComponentManager::unRegisterComponentFactory(const QString& type)
|
---|
81 | {
|
---|
82 | LOG_TRACE("unRegisterComponentFactory(type="<< type << ")");
|
---|
83 |
|
---|
84 | if (!factoryMap_.contains(type))
|
---|
85 | {
|
---|
86 | LOG_WARN("cannot unregister component factory '" << type << "'. It was not registered");
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 |
|
---|
90 | factoryMap_.remove(type);
|
---|
91 | LOG_INFO("unregistered component factory '" << type << "'");
|
---|
92 |
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | bool ComponentManager::registerComponent(ComponentBase* addr, const QString& name)
|
---|
97 | {
|
---|
98 | LOG_TRACE("registerComponent(name="<< name << ")");
|
---|
99 |
|
---|
100 | if (componentMap_.contains(name))
|
---|
101 | {
|
---|
102 | LOG_WARN("cannot register component '" << name << "'. A component with the same name exists already");
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | componentMap_[name] = addr;
|
---|
107 | LOG_INFO("registered component " << name);
|
---|
108 |
|
---|
109 | return true;
|
---|
110 | }
|
---|
111 |
|
---|
112 | bool ComponentManager::unRegisterComponent(const QString& name)
|
---|
113 | {
|
---|
114 | LOG_TRACE("unRegisterComponent(name="<< name << ")");
|
---|
115 |
|
---|
116 | if (!componentMap_.contains(name))
|
---|
117 | {
|
---|
118 | LOG_WARN("cannot unregister component '" << name << "'. It was not registered");
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | // FIXME: delete component
|
---|
123 | //delete componentMap_[name];
|
---|
124 | componentMap_.remove(name);
|
---|
125 | LOG_INFO("unregistered component '" << name << "'");
|
---|
126 |
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 |
|
---|
130 | bool ComponentManager::createComponent(const QString& type, const QString& name)
|
---|
131 | {
|
---|
132 | LOG_TRACE("createComponent(type=" << type << ", " << "name="<< name << ")");
|
---|
133 |
|
---|
134 | FactoryMap::iterator it = factoryMap_.find(type);
|
---|
135 | if (it != factoryMap_.end())
|
---|
136 | {
|
---|
137 | (*it)->addComponent(name);
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | LOG_WARN("cannot create component '" << name << "'"
|
---|
142 | << ". Component factory for type '" << type << "'"
|
---|
143 | << " does not exist or was not registered"
|
---|
144 | );
|
---|
145 | return false;
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool ComponentManager::loadPlugin(const QString& filename)
|
---|
149 | {
|
---|
150 | LOG_TRACE("loadPlugin(filename=" << filename << ")");
|
---|
151 |
|
---|
152 | pluginLoader_.setFileName(filename);
|
---|
153 |
|
---|
154 | if (!pluginLoader_.load()) {
|
---|
155 | LOG_ERROR("cannot load plugin '" << filename << "'"
|
---|
156 | << ". Plugin loader returned error: " << pluginLoader_.errorString()
|
---|
157 | );
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 |
|
---|
161 | QObject * plugin = pluginLoader_.instance();
|
---|
162 | if (NULL == plugin) {
|
---|
163 | LOG_WARN("cannot create an instance of the plugin '" << filename << "'"
|
---|
164 | << ". Plugin loader returned error: " << pluginLoader_.errorString()
|
---|
165 | );
|
---|
166 | return false;
|
---|
167 | }
|
---|
168 | pluginList_.append(plugin);
|
---|
169 | LOG_INFO("loaded plugin '" << qobject_cast<PacpusPluginInterface*>(plugin)->name() << "'"
|
---|
170 | << " from file '" << pluginLoader_.fileName() << "'"
|
---|
171 | );
|
---|
172 | return true;
|
---|
173 | }
|
---|
174 |
|
---|
175 | std::size_t ComponentManager::loadComponents(const QString& configFilename)
|
---|
176 | {
|
---|
177 | LOG_TRACE("loadComponents(filename=" << configFilename << ")");
|
---|
178 |
|
---|
179 | // load the components tree in memory
|
---|
180 | xmlTree_->loadFile(configFilename);
|
---|
181 |
|
---|
182 | {
|
---|
183 | // Load the plugins containing the components
|
---|
184 | QStringList plugins = xmlTree_->getAllPlugins();
|
---|
185 | Q_FOREACH (QString plugin, plugins) {
|
---|
186 | if (!loadPlugin(plugin)) {
|
---|
187 | LOG_WARN("cannot load plugin '" << plugin << "'");
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | QStringList componentsNameList = xmlTree_->getAllComponents();
|
---|
193 | LOG_DEBUG("components in the config file: '" << componentsNameList.join("|") << "'");
|
---|
194 |
|
---|
195 | XmlComponentConfig cfg;
|
---|
196 |
|
---|
197 | // First, create all the components in the XML list
|
---|
198 | for (QStringList::iterator it = componentsNameList.begin(); it != componentsNameList.end(); ++it) {
|
---|
199 | LOG_DEBUG("try to create component '" << (*it) << "'");
|
---|
200 | cfg.localCopy(xmlTree_->getComponent(*it));
|
---|
201 | QString componentType = cfg.getComponentType();
|
---|
202 | QString componentName = cfg.getComponentName();
|
---|
203 | // create the component and automatically add it to the component manager list
|
---|
204 | if (!createComponent(componentType, componentName)) {
|
---|
205 | LOG_ERROR("cannot create component '" << componentName << "'");
|
---|
206 | continue;
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | int componentsToConfigureCount = componentMap_.count();
|
---|
211 |
|
---|
212 | // Second, try to configure the components without regarding the dependencies
|
---|
213 | for (QStringList::iterator it = componentsNameList.begin(); it != componentsNameList.end(); ++it) {
|
---|
214 | LOG_DEBUG("try to configure component '" << (*it) << "'");
|
---|
215 | cfg.localCopy(xmlTree_->getComponent(*it));
|
---|
216 | QString componentName = cfg.getComponentName();
|
---|
217 |
|
---|
218 | // copy locally the config parameters of the component
|
---|
219 | ComponentBase * component = getComponent(componentName);
|
---|
220 | if (NULL == component) {
|
---|
221 | LOG_WARN("component '" << componentName << "' does not exist");
|
---|
222 | } else {
|
---|
223 | component->param.localCopy(cfg.qDomElement());
|
---|
224 | component->configuration_ = component->configureComponent(cfg);
|
---|
225 |
|
---|
226 | }
|
---|
227 | } // for
|
---|
228 |
|
---|
229 | // Third, if some components requested a delayed configuration, retry
|
---|
230 | for (QStringList::iterator it = componentsNameList.begin(); it != componentsNameList.end() ; ++it) {
|
---|
231 | cfg.localCopy(xmlTree_->getComponent(*it));
|
---|
232 | QString componentName = cfg.getComponentName();
|
---|
233 |
|
---|
234 | ComponentBase * component = getComponent(componentName);
|
---|
235 | if (NULL == component) {
|
---|
236 | LOG_WARN("component '" << componentName << "' does not exist");
|
---|
237 | } else {
|
---|
238 | if (component->configuration_ == ComponentBase::CONFIGURATION_DELAYED) {
|
---|
239 | LOG_DEBUG("try to configure component '" << (*it) << "'");
|
---|
240 |
|
---|
241 | // copy locally the config parameters of the component
|
---|
242 | component->param.localCopy(cfg.qDomElement());
|
---|
243 | component->configuration_ = component->configureComponent(cfg);
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (component->configuration_ == ComponentBase::CONFIGURED_OK) {
|
---|
247 | --componentsToConfigureCount;
|
---|
248 | } else {
|
---|
249 | LOG_ERROR("cannot configure component '" << (*it) << "'"
|
---|
250 | << ". Dependencies with other components are too complex"
|
---|
251 | << ". It was not configured, please review your configuration and/or your component"
|
---|
252 | );
|
---|
253 | component->configuration_ = ComponentBase::CONFIGURED_FAILED;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | } // for
|
---|
257 |
|
---|
258 | LOG_INFO(componentMap_.count() << " component(s) were loaded");
|
---|
259 | if (componentsToConfigureCount > 0) {
|
---|
260 | LOG_WARN(componentsToConfigureCount << " component(s) were not configured");
|
---|
261 | }
|
---|
262 |
|
---|
263 | return componentMap_.count();
|
---|
264 | }
|
---|
265 |
|
---|
266 | bool ComponentManager::start()
|
---|
267 | {
|
---|
268 | LOG_TRACE("start()");
|
---|
269 |
|
---|
270 | bool result = true;
|
---|
271 | for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it )
|
---|
272 | result &= start(it.key());
|
---|
273 |
|
---|
274 | return result;
|
---|
275 | }
|
---|
276 |
|
---|
277 | bool ComponentManager::start(const QString& componentName)
|
---|
278 | {
|
---|
279 | LOG_TRACE("start(component=" << componentName << ")");
|
---|
280 |
|
---|
281 | ComponentBase* component = getComponent(componentName);
|
---|
282 | if (!component)
|
---|
283 | {
|
---|
284 | LOG_WARN("cannot start component '" << componentName << "'. It does not exist!");
|
---|
285 | return false;
|
---|
286 | }
|
---|
287 |
|
---|
288 | LOG_INFO("starting component '" << componentName << "'...");
|
---|
289 | if (!component->startComponent())
|
---|
290 | LOG_WARN("cannot start component '" << componentName << "'. It can already be started");
|
---|
291 |
|
---|
292 | return true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | bool ComponentManager::stop()
|
---|
296 | {
|
---|
297 | LOG_TRACE("stop()");
|
---|
298 |
|
---|
299 | bool result = true;
|
---|
300 | for (ComponentMap::iterator it = componentMap_.begin(); it != componentMap_.end(); ++it)
|
---|
301 | result &= stop(it.key());
|
---|
302 |
|
---|
303 | return result;
|
---|
304 | }
|
---|
305 |
|
---|
306 | bool ComponentManager::stop(const QString& componentName)
|
---|
307 | {
|
---|
308 | LOG_TRACE("stop(component=" << componentName << ")");
|
---|
309 |
|
---|
310 | ComponentBase* component = getComponent(componentName);
|
---|
311 | if (!component)
|
---|
312 | {
|
---|
313 | LOG_WARN("cannot stop component '" << componentName << "'" << ". It does not exist");
|
---|
314 | return false;
|
---|
315 | }
|
---|
316 |
|
---|
317 | LOG_INFO("stopping component '" << componentName << "'...");
|
---|
318 | if (!component->stopComponent())
|
---|
319 | LOG_WARN("cannot stop component '" << componentName << "'" << ". It can be already stopped");
|
---|
320 |
|
---|
321 | return true;
|
---|
322 | }
|
---|
323 |
|
---|
324 | ComponentBase* ComponentManager::getComponent(const QString& name)
|
---|
325 | {
|
---|
326 | LOG_TRACE("getComponent(name=" << name << ")");
|
---|
327 |
|
---|
328 | ComponentMap::iterator it = componentMap_.find(name);
|
---|
329 | if (it != componentMap_.end())
|
---|
330 | return *it;
|
---|
331 |
|
---|
332 | LOG_WARN("cannot retrieve component '" << name << "'" << ". It does not exist");
|
---|
333 | return NULL;
|
---|
334 | }
|
---|
335 |
|
---|
336 | QStringList ComponentManager::getAllComponentsName() const
|
---|
337 | {
|
---|
338 | LOG_TRACE("getAllComponentsName()");
|
---|
339 | return xmlTree_->getAllComponents();
|
---|
340 | }
|
---|