| 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 |
|
|---|
| 14 | #include <cassert>
|
|---|
| 15 | #include <QObject>
|
|---|
| 16 | #include <QDomNodeList>
|
|---|
| 17 |
|
|---|
| 18 | using namespace pacpus;
|
|---|
| 19 |
|
|---|
| 20 | DECLARE_STATIC_LOGGER("pacpus.core.ComponentManager");
|
|---|
| 21 |
|
|---|
| 22 | ComponentManager * ComponentManager::mInstance = NULL;
|
|---|
| 23 |
|
|---|
| 24 | ComponentManager * ComponentManager::create()
|
|---|
| 25 | {
|
|---|
| 26 | return getInstance();
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | ComponentManager * ComponentManager::getInstance()
|
|---|
| 30 | {
|
|---|
| 31 | LOG_TRACE("getInstance()");
|
|---|
| 32 | LOG_TRACE("before: mInstance = " << mInstance);
|
|---|
| 33 |
|
|---|
| 34 | if (!mInstance) {
|
|---|
| 35 | LOG_INFO("creating new instance...");
|
|---|
| 36 | mInstance = new ComponentManager();
|
|---|
| 37 | LOG_DEBUG("mInstance = " << mInstance);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | LOG_TRACE("after : mInstance = " << mInstance);
|
|---|
| 41 | return mInstance;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | void ComponentManager::destroy()
|
|---|
| 45 | {
|
|---|
| 46 | LOG_TRACE("destroy");
|
|---|
| 47 |
|
|---|
| 48 | LOG_TRACE("before: mInstance = " << mInstance);
|
|---|
| 49 | delete mInstance;
|
|---|
| 50 | mInstance = NULL;
|
|---|
| 51 | LOG_TRACE("after : mInstance = " << mInstance);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | ComponentManager::ComponentManager()
|
|---|
| 55 | {
|
|---|
| 56 | LOG_TRACE("constructor");
|
|---|
| 57 |
|
|---|
| 58 | xmlTree_ = XmlConfigFile::create();
|
|---|
| 59 | LOG_DEBUG("component manager was created");
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | ComponentManager::~ComponentManager()
|
|---|
| 63 | {
|
|---|
| 64 | LOG_TRACE("destructor");
|
|---|
| 65 |
|
|---|
| 66 | for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) {
|
|---|
| 67 | bool unregisteredSuccessfully = unregisterComponent(it.key());
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | LOG_DEBUG("component manager was deleted");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | bool 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 |
|
|---|
| 89 | bool ComponentManager::unregisterComponentFactory(const QString& type)
|
|---|
| 90 | {
|
|---|
| 91 | LOG_TRACE("unregisterComponentFactory(type="<< type << ")");
|
|---|
| 92 |
|
|---|
| 93 | if (!factoryMap_.contains(type)) {
|
|---|
| 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 |
|
|---|
| 104 | bool 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 |
|
|---|
| 120 | bool ComponentManager::unregisterComponent(const QString& name)
|
|---|
| 121 | {
|
|---|
| 122 | LOG_TRACE("unregisterComponent(name="<< name << ")");
|
|---|
| 123 |
|
|---|
| 124 | if (!componentMap_.contains(name)) {
|
|---|
| 125 | LOG_WARN("cannot unregister component '" << name << "'. It was not registered");
|
|---|
| 126 | return false;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | // FIXME: delete component
|
|---|
| 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);
|
|---|
| 135 | LOG_INFO("unregistered component '" << name << "'");
|
|---|
| 136 |
|
|---|
| 137 | return true;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | bool ComponentManager::createComponent(const QString& type, const QString& name)
|
|---|
| 141 | {
|
|---|
| 142 | LOG_TRACE("createComponent(type=" << type << ", " << "name="<< name << ")");
|
|---|
| 143 |
|
|---|
| 144 | if (factoryMap_.contains(type)) {
|
|---|
| 145 | ComponentFactoryBase* factory = factoryMap_.value(type);
|
|---|
| 146 | assert(factory);
|
|---|
| 147 | factory->addComponent(name);
|
|---|
| 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 |
|
|---|
| 158 | bool 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 |
|
|---|
| 185 | bool 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 |
|
|---|
| 203 | // NOTE Create communicationInterface if needed ??
|
|---|
| 204 |
|
|---|
| 205 | return connectInterface(getComponent(output[0])->getOutput(output[1]), getComponent(input[0])->getInput(input[1]), priority);
|
|---|
| 206 |
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | std::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
|
|---|
| 218 | QStringList plugins = xmlTree_->getAllPluginsNames();
|
|---|
| 219 | Q_FOREACH (QString plugin, plugins) {
|
|---|
| 220 | if (!loadPlugin(plugin)) {
|
|---|
| 221 | LOG_WARN("cannot load plugin '" << plugin << "'");
|
|---|
| 222 | } else {
|
|---|
| 223 | LOG_INFO("successfully loaded plugin '" << plugin << "'");
|
|---|
| 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();
|
|---|
| 236 | LOG_DEBUG("try to create component '" << componentName << "'");
|
|---|
| 237 |
|
|---|
| 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();
|
|---|
| 251 | LOG_DEBUG("try to configure component '" << componentName << "'");
|
|---|
| 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());
|
|---|
| 259 | component->setConfigurationState(component->configureComponent(cfg));
|
|---|
| 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 {
|
|---|
| 272 | // Pacpus 2.0 : add inputs and outputs
|
|---|
| 273 | component->addInputs();
|
|---|
| 274 | component->addOutputs();
|
|---|
| 275 |
|
|---|
| 276 | if (ComponentBase::CONFIGURATION_DELAYED == component->configurationState()) {
|
|---|
| 277 | LOG_DEBUG("try to configure component '" << componentName << "'");
|
|---|
| 278 |
|
|---|
| 279 | // copy locally the config parameters of the component
|
|---|
| 280 | component->param.localCopy(cfg.qDomElement());
|
|---|
| 281 | component->setConfigurationState(component->configureComponent(cfg));
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | if (ComponentBase::CONFIGURED_OK == component->configurationState()) {
|
|---|
| 285 | --componentsToConfigureCount;
|
|---|
| 286 | } else {
|
|---|
| 287 | LOG_ERROR("cannot configure component '" << componentName << "'"
|
|---|
| 288 | << ". Dependencies with other components are too complex"
|
|---|
| 289 | << ". It was not configured, please review your configuration and/or your component"
|
|---|
| 290 | );
|
|---|
| 291 | component->setConfigurationState(ComponentBase::CONFIGURED_FAILED);
|
|---|
| 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 |
|
|---|
| 311 | //TODO set connection mode from string
|
|---|
| 312 |
|
|---|
| 313 | //InputInterfaceBase::GetLast;
|
|---|
| 314 | //InputInterfaceBase::NeverSkip;
|
|---|
| 315 | //InputInterfaceBase::TimeBounded;
|
|---|
| 316 |
|
|---|
| 317 | if (!createConnection(connectionOutput, connectionInput, connectionType,connectionPriority)) {
|
|---|
| 318 | LOG_ERROR("cannot create connection '" << connectionOutput+"=>"+connectionInput << "'");
|
|---|
| 319 | continue;
|
|---|
| 320 | }
|
|---|
| 321 | } // for
|
|---|
| 322 |
|
|---|
| 323 | return componentMap_.count();
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | bool ComponentManager::start()
|
|---|
| 327 | {
|
|---|
| 328 | LOG_TRACE("start()");
|
|---|
| 329 |
|
|---|
| 330 | bool result = true;
|
|---|
| 331 | for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) {
|
|---|
| 332 | result &= start(it.key());
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | return result;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | bool ComponentManager::start(const QString& componentName)
|
|---|
| 339 | {
|
|---|
| 340 | LOG_TRACE("start(component=" << componentName << ")");
|
|---|
| 341 |
|
|---|
| 342 | ComponentBase* component = getComponent(componentName);
|
|---|
| 343 | if (!component) {
|
|---|
| 344 | LOG_WARN("cannot start component '" << componentName << "'. It does not exist!");
|
|---|
| 345 | return false;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | LOG_INFO("starting component '" << componentName << "'...");
|
|---|
| 349 | if (!component->startComponent()) {
|
|---|
| 350 | LOG_WARN("cannot start component '" << componentName << "'. It can already be started");
|
|---|
| 351 | }
|
|---|
| 352 | LOG_INFO("successfully started component '" << componentName << "'");
|
|---|
| 353 | return true;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | bool ComponentManager::stop()
|
|---|
| 357 | {
|
|---|
| 358 | LOG_TRACE("stop()");
|
|---|
| 359 |
|
|---|
| 360 | bool result = true;
|
|---|
| 361 | for (ComponentMap::iterator it = componentMap_.begin(), itend = componentMap_.end(); it != itend; ++it) {
|
|---|
| 362 | result &= stop(*it);
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | return result;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | bool 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 |
|
|---|
| 380 | bool ComponentManager::stop(const QString& componentName)
|
|---|
| 381 | {
|
|---|
| 382 | LOG_TRACE("stop(component=" << componentName << ")");
|
|---|
| 383 |
|
|---|
| 384 | ComponentBase* component = getComponent(componentName);
|
|---|
| 385 | if (!component) {
|
|---|
| 386 | LOG_WARN("cannot stop component '" << componentName << "'" << ". It does not exist");
|
|---|
| 387 | return false;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | LOG_INFO("stopping component '" << componentName << "'...");
|
|---|
| 391 | if (!stop(component)) {
|
|---|
| 392 | LOG_WARN("cannot stop component '" << componentName << "'" << ". It can be already stopped");
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | return true;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | ComponentBase* ComponentManager::getComponent(const QString& name)
|
|---|
| 399 | {
|
|---|
| 400 | LOG_TRACE("getComponent(name=" << name << ")");
|
|---|
| 401 |
|
|---|
| 402 | ComponentMap::iterator it = componentMap_.find(name);
|
|---|
| 403 | if (it != componentMap_.end()) {
|
|---|
| 404 | return *it;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | LOG_WARN("cannot retrieve component '" << name << "'" << ". It does not exist");
|
|---|
| 408 | return NULL;
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | QStringList ComponentManager::getAllComponentsName() const
|
|---|
| 412 | {
|
|---|
| 413 | LOG_TRACE("getAllComponentsName()");
|
|---|
| 414 | return xmlTree_->getAllComponentsNames();
|
|---|
| 415 | }
|
|---|