Commit 3851da70 authored by Julien Wintz's avatar Julien Wintz
Browse files

Architecturing managers and factories

parent 8988f7dc
......@@ -3,9 +3,9 @@
## Author: Julien Wintz
## Created: Mon Feb 4 10:17:21 2013 (+0100)
## Version:
## Last-Updated: Fri Feb 8 14:58:02 2013 (+0100)
## Last-Updated: Mon Feb 11 12:32:18 2013 (+0100)
## By: Julien Wintz
## Update #: 103
## Update #: 112
######################################################################
##
### Change Log:
......@@ -88,7 +88,6 @@ add_subdirectory(test)
## Exporting
## ###################################################################
export(TARGETS dtkDistributed FILE "${CMAKE_BINARY_DIR}/dtkDepends.cmake")
export(PACKAGE dtk)
configure_file(${PROJECT_SOURCE_DIR}/cmake/dtkConfig.cmake.in
......
......@@ -3,9 +3,9 @@
## Author: Julien Wintz
## Created: Fri Feb 8 16:49:52 2013 (+0100)
## Version:
## Last-Updated: Fri Feb 8 16:51:01 2013 (+0100)
## Last-Updated: Mon Feb 11 12:44:11 2013 (+0100)
## By: Julien Wintz
## Update #: 8
## Update #: 13
######################################################################
##
### Change Log:
......@@ -19,9 +19,11 @@ project(dtkCore)
## #################################################################
set(${PROJECT_NAME}_HEADERS
dtkCorePluginManager.h
dtkCoreSettings.h)
set(${PROJECT_NAME}_SOURCES
dtkCorePluginManager.cpp
dtkCoreSettings.cpp)
## #################################################################
......@@ -54,3 +56,9 @@ install(TARGETS ${PROJECT_NAME}
DESTINATION lib
COMPONENT core
EXPORT dtkDepends)
## ###################################################################
## Export rules
## ###################################################################
export(TARGETS dtkCore APPEND FILE "${CMAKE_BINARY_DIR}/dtkDepends.cmake")
#include "dtkCorePluginManager.h"
#include "dtkCoreSettings.h"
#include "dtkCorePluginManager.h"
/* dtkCorePluginManager.cpp ---
*
* Author: Julien Wintz
* Created: Mon Feb 11 12:08:44 2013 (+0100)
* Version:
* Last-Updated: Mon Feb 11 15:24:15 2013 (+0100)
* By: Julien Wintz
* Update #: 28
*/
/* Change Log:
*
*/
#include "dtkCorePluginManager.h"
#include "dtkCorePluginManager_p.h"
#include <QtCore>
#include <QtDebug>
// ///////////////////////////////////////////////////////////////////
// dtkCorePluginManagerPrivate
// ///////////////////////////////////////////////////////////////////
bool dtkCorePluginManagerPrivate::check(const QString& path)
{
bool status = true;
foreach(QVariant item, this->dependencies.value(path)) {
QVariantMap mitem = item.toMap();
QVariant na_mitem = mitem.value("name");
QVariant ve_mitem = mitem.value("version");
QString key = this->names.key(na_mitem);
if(!this->names.values().contains(na_mitem)) {
qDebug() << Q_FUNC_INFO << " Missing dependency:" << na_mitem.toString() << "for plugin" << path;
status = false;
continue;
}
if (this->versions.value(key) != ve_mitem) {
qDebug() << Q_FUNC_INFO << " Version mismatch:" << na_mitem.toString() << "version" << this->versions.value(this->names.key(na_mitem)).toString() << "but" << ve_mitem.toString() << "required for plugin" << path;
status = false;
continue;
}
if(!check(key)) {
qDebug() << Q_FUNC_INFO << "Corrupted dependency:" << na_mitem.toString() << "for plugin" << path;
status = false;
continue;
}
}
return status;
}
// ///////////////////////////////////////////////////////////////////
// dtkCorePluginManager
// ///////////////////////////////////////////////////////////////////
dtkCorePluginManager::dtkCorePluginManager(void) : d(new dtkCorePluginManagerPrivate)
{
}
dtkCorePluginManager::~dtkCorePluginManager(void)
{
delete d;
d = NULL;
}
void dtkCorePluginManager::initialize(const QString& path)
{
QDir dir(path);
foreach(QFileInfo info, dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot))
this->scan(info.absoluteFilePath());
foreach(QFileInfo info, dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot))
this->load(info.absoluteFilePath());
}
void dtkCorePluginManager::uninitialize(void)
{
foreach(const QString &path, d->loaders.keys())
this->unload(path);
}
void dtkCorePluginManager::scan(const QString& path)
{
if(!QLibrary::isLibrary(path))
return;
QPluginLoader *loader = new QPluginLoader(path);
d->names.insert(path, loader->metaData().value("MetaData").toObject().value("name").toVariant());
d->versions.insert(path, loader->metaData().value("MetaData").toObject().value("version").toVariant());
d->dependencies.insert(path, loader->metaData().value("MetaData").toObject().value("dependencies").toArray().toVariantList());
delete loader;
}
void dtkCorePluginManager::unload(const QString& path)
{
QPluginLoader *loader = d->loaders.value(path);
if(loader->unload()) {
d->loaders.remove(path);
delete loader;
}
}
QStringList dtkCorePluginManager::plugins(void)
{
return d->loaders.keys();
}
/* dtkCorePluginManager.h ---
*
* Author: Julien Wintz
* Created: Mon Feb 11 12:07:50 2013 (+0100)
* Version:
* Last-Updated: Mon Feb 11 15:23:46 2013 (+0100)
* By: Julien Wintz
* Update #: 29
*/
/* Change Log:
*
*/
#pragma once
#include <QtCore/QObject>
class dtkCorePluginManagerPrivate;
class dtkCorePluginManager : public QObject
{
Q_OBJECT
public:
dtkCorePluginManager(void);
~dtkCorePluginManager(void);
#pragma mark -
#pragma Manager Management
virtual void initialize(void) = 0;
virtual void initialize(const QString& path);
virtual void uninitialize(void);
#pragma mark -
#pragma Plugin Management
virtual void scan(const QString& path);
virtual void load(const QString& path) = 0;
virtual void unload(const QString& path);
#pragma mark -
#pragma Plugin Queries
QStringList plugins(void);
protected:
dtkCorePluginManagerPrivate *d;
};
#include "dtkCorePluginManager_p.h"
/* dtkCorePluginManager_p.h ---
*
* Author: Julien Wintz
* Created: Mon Feb 11 12:13:49 2013 (+0100)
* Version:
* Last-Updated: Mon Feb 11 12:44:57 2013 (+0100)
* By: Julien Wintz
* Update #: 3
*/
/* Change Log:
*
*/
#pragma once
#include <QtCore>
#include <QtDebug>
class dtkCorePluginManagerPrivate
{
public:
bool check(const QString& path);
public:
QHash<QString, QVariant> names;
QHash<QString, QVariant> versions;
QHash<QString, QVariantList> dependencies;
public:
QHash<QString, QPluginLoader *> loaders;
};
......@@ -3,9 +3,9 @@
* Author: Julien Wintz
* Created: Fri Feb 8 16:53:22 2013 (+0100)
* Version:
* Last-Updated: Fri Feb 8 17:05:32 2013 (+0100)
* Last-Updated: Mon Feb 11 15:35:14 2013 (+0100)
* By: Julien Wintz
* Update #: 4
* Update #: 14
*/
/* Change Log:
......@@ -14,7 +14,12 @@
#include "dtkCoreSettings.h"
dtkCoreSettings::dtkCoreSettings(void) : QSettings("inria", "dtk")
dtkCoreSettings::dtkCoreSettings(void) : QSettings(QSettings::IniFormat, QSettings::UserScope, "inria", "dtk")
{
this->setDefaultFormat(QSettings::IniFormat);
}
dtkCoreSettings::~dtkCoreSettings(void)
{
}
......@@ -3,9 +3,9 @@
* Author: Julien Wintz
* Created: Fri Feb 8 16:51:14 2013 (+0100)
* Version:
* Last-Updated: Fri Feb 8 16:53:10 2013 (+0100)
* Last-Updated: Mon Feb 11 14:19:25 2013 (+0100)
* By: Julien Wintz
* Update #: 3
* Update #: 4
*/
/* Change Log:
......@@ -20,4 +20,5 @@ class dtkCoreSettings : public QSettings
{
public:
explicit dtkCoreSettings(void);
virtual ~dtkCoreSettings(void);
};
......@@ -15,6 +15,8 @@ project(dtkDistributed)
## #################################################################
set(${PROJECT_NAME}_HEADERS
dtkDistributed
dtkDistributed.h
dtkDistributedCommunicator.h
dtkDistributedCommunicatorPlugin
dtkDistributedCommunicatorPlugin.h
......@@ -24,13 +26,17 @@ set(${PROJECT_NAME}_HEADERS
dtkDistributedCommunicatorPluginManager.h
dtkDistributedContainer.h
dtkDistributedContainer.tpp
dtkDistributedMapper.h)
dtkDistributedMapper.h
dtkDistributedSettings
dtkDistributedSettings.h)
set(${PROJECT_NAME}_SOURCES
dtkDistributed.cpp
dtkDistributedCommunicator.cpp
dtkDistributedCommunicatorPluginFactory.cpp
dtkDistributedCommunicatorPluginManager.cpp
dtkDistributedMapper.cpp)
dtkDistributedMapper.cpp
dtkDistributedSettings.cpp)
## #################################################################
## Build rules
......@@ -47,6 +53,8 @@ add_library(${PROJECT_NAME} SHARED
qt5_use_modules(${PROJECT_NAME} Core)
qt5_use_modules(${PROJECT_NAME} Network)
target_link_libraries(${PROJECT_NAME} dtkCore)
## ###################################################################
## Install rules - files
## ###################################################################
......@@ -63,3 +71,9 @@ install(TARGETS ${PROJECT_NAME}
DESTINATION lib
COMPONENT distributed
EXPORT dtkDepends)
## ###################################################################
## Export rules
## ###################################################################
export(TARGETS dtkDistributed APPEND FILE "${CMAKE_BINARY_DIR}/dtkDepends.cmake")
......@@ -4,3 +4,4 @@
#include "dtkDistributedCommunicatorPluginManager.h"
#include "dtkDistributedContainer.h"
#include "dtkDistributedMapper.h"
#include "dtkDistributedSettings.h"
/* dtkDistributed.cpp ---
*
* Author: Julien Wintz
* Created: Mon Feb 11 15:00:12 2013 (+0100)
* Version:
* Last-Updated: Mon Feb 11 15:27:09 2013 (+0100)
* By: Julien Wintz
* Update #: 25
*/
/* Change Log:
*
*/
#include "dtkDistributed.h"
#include "dtkDistributedCommunicator.h"
#include "dtkDistributedCommunicatorPlugin.h"
#include "dtkDistributedCommunicatorPluginFactory.h"
#include "dtkDistributedCommunicatorPluginManager.h"
namespace dtkDistributed
{
namespace communicator {
namespace _private {
dtkDistributedCommunicatorPluginFactory factory;
dtkDistributedCommunicatorPluginManager manager;
}
dtkDistributedCommunicatorPluginFactory& pluginFactory(void) {
return _private::factory;
}
dtkDistributedCommunicatorPluginManager& pluginManager(void) {
return _private::manager;
}
}
}
/* dtkDistributed.h ---
*
* Author: Julien Wintz
* Created: Mon Feb 11 13:45:24 2013 (+0100)
* Version:
* Last-Updated: Mon Feb 11 15:27:14 2013 (+0100)
* By: Julien Wintz
* Update #: 31
*/
/* Change Log:
*
*/
#pragma once
class dtkDistributedCommunicatorPlugin;
class dtkDistributedCommunicatorPluginFactory;
class dtkDistributedCommunicatorPluginManager;
namespace dtkDistributed
{
namespace communicator {
dtkDistributedCommunicatorPluginFactory& pluginFactory(void);
dtkDistributedCommunicatorPluginManager& pluginManager(void);
}
}
......@@ -3,9 +3,9 @@
* Author: Julien Wintz
* Created: Fri Feb 8 16:20:47 2013 (+0100)
* Version:
* Last-Updated: Fri Feb 8 16:25:11 2013 (+0100)
* Last-Updated: Mon Feb 11 15:26:55 2013 (+0100)
* By: Julien Wintz
* Update #: 7
* Update #: 9
*/
/* Change Log:
......@@ -21,14 +21,6 @@ class dtkDistributedCommunicatorPluginFactoryPrivate
public:
};
dtkDistributedCommunicatorPluginFactory *dtkDistributedCommunicatorPluginFactory::instance(void)
{
if(!s_instance)
s_instance = new dtkDistributedCommunicatorPluginFactory;
return s_instance;
}
dtkDistributedCommunicatorPluginFactory::dtkDistributedCommunicatorPluginFactory(void) : d(new dtkDistributedCommunicatorPluginFactoryPrivate)
{
......@@ -40,5 +32,3 @@ dtkDistributedCommunicatorPluginFactory::~dtkDistributedCommunicatorPluginFactor
d = NULL;
}
dtkDistributedCommunicatorPluginFactory *dtkDistributedCommunicatorPluginFactory::s_instance = NULL;
......@@ -3,9 +3,9 @@
* Author: Julien Wintz
* Created: Fri Feb 8 16:17:21 2013 (+0100)
* Version:
* Last-Updated: Fri Feb 8 16:20:28 2013 (+0100)
* Last-Updated: Mon Feb 11 15:26:36 2013 (+0100)
* By: Julien Wintz
* Update #: 7
* Update #: 9
*/
/* Change Log:
......@@ -19,15 +19,9 @@ class dtkDistributedCommunicatorPluginFactoryPrivate;
class dtkDistributedCommunicatorPluginFactory
{
public:
static dtkDistributedCommunicatorPluginFactory *instance(void);
protected:
dtkDistributedCommunicatorPluginFactory(void);
~dtkDistributedCommunicatorPluginFactory(void);
private:
static dtkDistributedCommunicatorPluginFactory *s_instance;
private:
dtkDistributedCommunicatorPluginFactoryPrivate *d;
};
......@@ -3,115 +3,25 @@
* Author: Julien Wintz
* Created: Mon Feb 4 12:51:32 2013 (+0100)
* Version:
* Last-Updated: Fri Feb 8 16:19:21 2013 (+0100)
* Last-Updated: Mon Feb 11 15:24:55 2013 (+0100)
* By: Julien Wintz
* Update #: 3
* Update #: 35
*/
/* Change Log:
*
*/
#include "dtkDistributed.h"
#include "dtkDistributedCommunicatorPlugin.h"
#include "dtkDistributedCommunicatorPluginManager.h"
#include <QtCore>
#include <QtDebug>
// /////////////////////////////////////////////////////////////////
// dtkDistributedCommunicatorPluginManagerPrivate
// /////////////////////////////////////////////////////////////////
class dtkDistributedCommunicatorPluginManagerPrivate
{
public:
bool check(const QString& path);
public:
QHash<QString, QVariant> names;
QHash<QString, QVariant> versions;
QHash<QString, QVariantList> dependencies;
public:
QHash<QString, QPluginLoader *> loaders;
};
bool dtkDistributedCommunicatorPluginManagerPrivate::check(const QString& path)
{
bool status = true;
foreach(QVariant item, this->dependencies.value(path)) {
QVariantMap mitem = item.toMap();
QVariant na_mitem = mitem.value("name");
QVariant ve_mitem = mitem.value("version");
QString key = this->names.key(na_mitem);
if(!this->names.values().contains(na_mitem)) {
qDebug() << Q_FUNC_INFO << " Missing dependency:" << na_mitem.toString() << "for plugin" << path;
status = false;
continue;
}
if (this->versions.value(key) != ve_mitem) {
qDebug() << Q_FUNC_INFO << " Version mismatch:" << na_mitem.toString() << "version" << this->versions.value(this->names.key(na_mitem)).toString() << "but" << ve_mitem.toString() << "required for plugin" << path;
status = false;
continue;
}
if(!check(key)) {
qDebug() << Q_FUNC_INFO << "Corrupted dependency:" << na_mitem.toString() << "for plugin" << path;
status = false;
continue;
}
}
return status;
}
// /////////////////////////////////////////////////////////////////
// dtkDistributedCommunicatorPluginManager
// /////////////////////////////////////////////////////////////////
dtkDistributedCommunicatorPluginManager *dtkDistributedCommunicatorPluginManager::instance(void)
{
if(!s_instance)
s_instance = new dtkDistributedCommunicatorPluginManager;
return s_instance;
}
#include "dtkDistributedSettings.h"
void dtkDistributedCommunicatorPluginManager::initialize(void)
{
QDir path = QDir(qApp->applicationDirPath());
path.cdUp();
path.cd("plugins");
foreach(QFileInfo info, path.entryInfoList(QDir::Files | QDir::NoDotAndDotDot))
this->scan(info.absoluteFilePath());
foreach(QFileInfo info, path.entryInfoList(QDir::Files | QDir::NoDotAndDotDot))
this->load(info.absoluteFilePath());
}
void dtkDistributedCommunicatorPluginManager::uninitialize(void)
{
foreach(const QString &path, d->loaders.keys())
this->unload(path);
}
void dtkDistributedCommunicatorPluginManager::scan(const QString& path)
{
if(!QLibrary::isLibrary(path))
return;
dtkDistributedSettings settings;
QPluginLoader *loader = new QPluginLoader(path);
d->names.insert(path, loader->metaData().value("MetaData").toObject().value("name").toVariant());
d->versions.insert(path, loader->metaData().value("MetaData").toObject().value("version").toVariant());
d->dependencies.insert(path, loader->metaData().value("MetaData").toObject().value("dependencies").toArray().toVariantList());
delete loader;
dtkCorePluginManager::initialize(settings.value("plugins").toString());
}
void dtkDistributedCommunicatorPluginManager::load(const QString& path)
......@@ -129,32 +39,3 @@ void dtkDistributedCommunicatorPluginManager::load(const QString& path)