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