diff --git a/src/plug-in/Makefile.am b/src/plug-in/Makefile.am index bd27f29a4ca093adb9eab042381127341503f2b3..ae5675eef08ac940e627a72bc08ace76bd99c370 100644 --- a/src/plug-in/Makefile.am +++ b/src/plug-in/Makefile.am @@ -5,6 +5,5 @@ noinst_LTLIBRARIES = libplugin.la SUBDIRS=test libplugin_la_SOURCES = \ - pluginmanager.cpp \ - plugin.cpp + pluginmanager.cpp diff --git a/src/plug-in/plugin.cpp b/src/plug-in/plugin.cpp deleted file mode 100644 index deaf1a4818fbe8a01a720b0b856c36f1ac33a277..0000000000000000000000000000000000000000 --- a/src/plug-in/plugin.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "plugin.h" - -::sflphone::Plugin::Plugin (void *handle, PluginInterface *interface) - :_handlePtr(handle), _interface(interface) -{ -} - -::sflphone::Plugin::Plugin (PluginInterface *interface) - :_interface(interface) -{ -} - -::sflphone::Plugin::~Plugin () -{ -} diff --git a/src/plug-in/plugin.h b/src/plug-in/plugin.h index fe548361eb1098e566ffacf00cb58d61ba31a236..6adac44f980256c13ab9f0c23d0a0e228ec37183 100644 --- a/src/plug-in/plugin.h +++ b/src/plug-in/plugin.h @@ -1,32 +1,43 @@ #ifndef PLUGIN_H #define PLUGIN_H -#include "plugininterface.h" +#include <string> +#include "global.h" -namespace sflphone { +/* + * @file plugin.h + * @brief Define a plugin object + */ - class PluginInterface; +namespace sflphone { class Plugin { public: + Plugin( const std::string &name ){ + _name = name; + } - Plugin (void*, PluginInterface *interface); - Plugin (PluginInterface *interface); + virtual ~Plugin() {} - ~Plugin (); + inline std::string getPluginName (void) { return _name; } - void setName (std::string name); + /** + * Return the minimal core version required so that the plugin could work + * @return int The version required + */ + virtual int initFunc (int i) = 0; + private: + Plugin &operator =(const Plugin &plugin); + std::string _name; - int _version_major; - int _version_minor; - int _required; - void *_handlePtr; - PluginInterface *_interface; - - friend class PluginTest; - friend class PluginManager; }; + } +typedef ::sflphone::Plugin* createFunc (void); + +typedef void destroyFunc (::sflphone::Plugin*); + #endif //PLUGIN_H + diff --git a/src/plug-in/plugininterface.h b/src/plug-in/plugininterface.h deleted file mode 100644 index 1bfad23e2fc2219a5a8211b03b52a2a51bad9742..0000000000000000000000000000000000000000 --- a/src/plug-in/plugininterface.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef PLUGIN_INTERFACE_H -#define PLUGIN_INTERFACE_H - -#include <string> -#include "global.h" - -#include "plugin.h" - -/* - * @file plugininterface.h - * @brief Define a plugin object - */ - -namespace sflphone { - - class PluginManager; - class Plugin; - - class PluginInterface { - - public: - PluginInterface( const std::string &name ){ - _name = name; - } - - virtual ~PluginInterface() {} - - inline std::string getInterfaceName (void) { return _name; } - - /** - * Return the minimal core version required so that the plugin could work - * @return int The version required - */ - virtual int initFunc () = 0; - - virtual int registerFunc (Plugin **plugin) = 0; - - private: - PluginInterface &operator =(const PluginInterface &plugin); - - std::string _name; - }; - - typedef PluginInterface* createFunc (void); - - typedef void destroyFunc( PluginInterface* ); - -} - -#endif //PLUGIN_INTERFACE_H - diff --git a/src/plug-in/pluginmanager.cpp b/src/plug-in/pluginmanager.cpp index f8135f57806590ea3c32f5c304f3de3be8814896..7a259f7fc91f20de57f9fecc75be8d26cfbc6d95 100644 --- a/src/plug-in/pluginmanager.cpp +++ b/src/plug-in/pluginmanager.cpp @@ -29,7 +29,7 @@ ::sflphone::PluginManager::loadPlugins (const std::string &path) { std::string pluginDir, current; - ::sflphone::PluginInterface *interface; + ::sflphone::Plugin *plugin; DIR *dir; dirent *dirStruct; int result=0; @@ -54,15 +54,18 @@ if( current != pDir && current != cDir ){ handle = loadDynamicLibrary( pluginDir + current ); - if(instanciatePlugin (handle, &interface) != 0) + if(instanciatePlugin (handle, &plugin) != 0) { _debug("Error instanciating the plugin ...\n"); return 1; } + /* if(registerPlugin (handle, interface) != 0) + { _debug("Error registering the plugin ...\n"); return 1; + }*/ } } } @@ -119,7 +122,7 @@ } int -::sflphone::PluginManager::instanciatePlugin (void *handlePtr, ::sflphone::PluginInterface **plugin) +::sflphone::PluginManager::instanciatePlugin (void *handlePtr, ::sflphone::Plugin **plugin) { createFunc *createPlugin; @@ -134,30 +137,25 @@ } int -::sflphone::PluginManager::registerPlugin (void *handlePtr, PluginInterface *interface) +::sflphone::PluginManager::registerPlugin (void *handlePtr, Plugin *plugin) { - Plugin *myplugin; std::string name; - if( !( handlePtr && interface!=0 ) ) + if( !(handlePtr && plugin!=0) ) return 1; - /* Fetch information from the loaded plugin interface */ - if(interface->registerFunc (&myplugin) != 0) - return 1; - /* Creation of the plugin wrapper */ - myplugin = new Plugin (handlePtr, interface); - + name = plugin->getPluginName(); /* Add the data in the loaded plugin map */ - _loadedPlugins[ myplugin->_name ] = myplugin; + _loadedPlugins[ name ] = plugin; return 0; } - void + int ::sflphone::PluginManager::unloadDynamicLibrary (void * pluginHandlePtr) { dlclose( pluginHandlePtr ); dlerror(); + return 0; } diff --git a/src/plug-in/pluginmanager.h b/src/plug-in/pluginmanager.h index 671834369da67dc151cd24e8ab9ca62a0261d6c1..f24b1779f0266a7eeaaf29209a254b0b30993c65 100644 --- a/src/plug-in/pluginmanager.h +++ b/src/plug-in/pluginmanager.h @@ -6,7 +6,7 @@ * @brief Base class of the plugin manager */ -#include "plugininterface.h" +#include "plugin.h" #include "global.h" #include <map> @@ -18,6 +18,7 @@ namespace sflphone { public: + /** * Destructor */ @@ -35,7 +36,7 @@ namespace sflphone { */ int loadPlugins( const std::string &path = "" ); - int instanciatePlugin( void *handlePtr, PluginInterface** plugin ); + int instanciatePlugin( void *handlePtr, Plugin** plugin ); /** * Check if a plugin has been already loaded @@ -44,27 +45,28 @@ namespace sflphone { */ Plugin* isPluginLoaded( const std::string &name ); - int registerPlugin (void *handle, PluginInterface *interface); - - private: - /** - * Default constructor - */ - PluginManager(); - + int registerPlugin (void *handle, Plugin *plugin); + /** * Load a unix dynamic/shared library * @param filename The path to the dynamic/shared library * @return void* A pointer on it */ void * loadDynamicLibrary( const std::string &filename ); - + /** * Unload a unix dynamic/shared library * @param pluginHandleptr The pointer on the loaded plugin */ - void unloadDynamicLibrary( void * pluginHandlePtr ); + int unloadDynamicLibrary( void * pluginHandlePtr ); + + private: + /** + * Default constructor + */ + PluginManager(); + /* Map of plugins associated by their string name */ typedef std::map<std::string, Plugin*> pluginMap; pluginMap _loadedPlugins; diff --git a/src/plug-in/test/pluginTest.cpp b/src/plug-in/test/pluginTest.cpp index 5e5f7cff111582bf0d88a31c624c32ee17d27027..3aeb927077cc4ef6fffa1745ced804dab5e434b3 100644 --- a/src/plug-in/test/pluginTest.cpp +++ b/src/plug-in/test/pluginTest.cpp @@ -1,39 +1,26 @@ -#include "../plugininterface.h" +#include "../plugin.h" namespace sflphone { - class PluginTest : public PluginInterface { + class PluginTest : public Plugin { public: - PluginTest( const std::string &name ):PluginInterface( name ){ - } + PluginTest( const std::string &name ) + :Plugin( name ) { + } - virtual int initFunc (void) + virtual int initFunc (int i) { - return 0; + return i; } - virtual int registerFunc (Plugin **plugin) - { - Plugin *ret; - - ret = new Plugin(this); - - ret->_name = getInterfaceName(); - ret->_required = 1; - ret->_version_major=1; - ret->_version_minor=0; - - *plugin = ret; - return 0; - } }; - } -extern "C" ::sflphone::PluginInterface* create (void){ - return new ::sflphone::PluginTest("test"); + +extern "C" ::sflphone::Plugin* create (void){ + return new ::sflphone::PluginTest("mytest"); } -extern "C" void* destroy( ::sflphone::PluginInterface *p ){ +extern "C" void destroy (::sflphone::Plugin *p){ delete p; } diff --git a/test/pluginmanagerTest.cpp b/test/pluginmanagerTest.cpp index ed3f4d7e281634803ac1c506c286899eb839214f..5a45ffabf2a7667f9276601b1b9d57464077d14b 100644 --- a/test/pluginmanagerTest.cpp +++ b/test/pluginmanagerTest.cpp @@ -19,33 +19,67 @@ #include <stdio.h> #include <sstream> +#include <dlfcn.h> #include "pluginmanagerTest.h" using std::cout; using std::endl; +#define PLUGIN_TEST_DIR "/usr/lib/sflphone/plugins/libplugintest.so" +#define PLUGIN_TEST_NAME "mytest" + + void PluginManagerTest::setUp(){ // Instanciate the plugin manager singleton _pm = ::sflphone::PluginManager::instance(); + handlePtr = NULL; + plugin = 0; } -void PluginManagerTest::testLoadPluginDirectory(){ - CPPUNIT_ASSERT(_pm->loadPlugins() == 0); +void PluginManagerTest::testLoadDynamicLibrary(){ + CPPUNIT_ASSERT(_pm->loadDynamicLibrary(PLUGIN_TEST_DIR) != NULL); +} + +void PluginManagerTest::testUnloadDynamicLibrary(){ + + handlePtr = _pm->loadDynamicLibrary(PLUGIN_TEST_DIR); + CPPUNIT_ASSERT(handlePtr != 0); + CPPUNIT_ASSERT(_pm->unloadDynamicLibrary(handlePtr) == 0 ); } -void PluginManagerTest::testLoadPlugin(){ - CPPUNIT_ASSERT(_pm->loadPlugins() == 0); - //CPPUNIT_ASSERT( _pm->isPluginLoaded("test") == NULL ); +void PluginManagerTest::testInstanciatePlugin(){ + + handlePtr = _pm->loadDynamicLibrary (PLUGIN_TEST_DIR); + CPPUNIT_ASSERT (handlePtr != 0); + CPPUNIT_ASSERT (_pm->instanciatePlugin (handlePtr, &plugin) == 0); + CPPUNIT_ASSERT (plugin!=NULL); +} + +void PluginManagerTest::testInitPlugin(){ + + handlePtr = _pm->loadDynamicLibrary (PLUGIN_TEST_DIR); + CPPUNIT_ASSERT (handlePtr != 0); + CPPUNIT_ASSERT (_pm->instanciatePlugin (handlePtr, &plugin) == 0); + CPPUNIT_ASSERT (plugin!=NULL); + CPPUNIT_ASSERT (plugin->initFunc(0) == 0); + CPPUNIT_ASSERT (plugin->getPluginName() == PLUGIN_TEST_NAME); } void PluginManagerTest::testRegisterPlugin(){ - // First load the default directory - _pm->loadPlugins(); - // Resolve the symbol + + handlePtr = _pm->loadDynamicLibrary (PLUGIN_TEST_DIR); + CPPUNIT_ASSERT (handlePtr != 0); + CPPUNIT_ASSERT (_pm->instanciatePlugin (handlePtr, &plugin) == 0); + CPPUNIT_ASSERT (_pm->isPluginLoaded (PLUGIN_TEST_NAME) == NULL); + CPPUNIT_ASSERT (_pm->registerPlugin (handlePtr, plugin) == 0); + CPPUNIT_ASSERT (_pm->isPluginLoaded (PLUGIN_TEST_NAME) == plugin); } void PluginManagerTest::tearDown(){ // Delete the plugin manager object delete _pm; _pm=0; + handlePtr = NULL; + if(plugin) + delete plugin; plugin = 0; } diff --git a/test/pluginmanagerTest.h b/test/pluginmanagerTest.h index 0cd92da42ce87ff50b2eb399975a68da9abd2f4f..857b265dec58b8386be8a73f2f933e4329a522ed 100644 --- a/test/pluginmanagerTest.h +++ b/test/pluginmanagerTest.h @@ -27,6 +27,7 @@ // Application import #include "plug-in/pluginmanager.h" +#include "plug-in/plugin.h" /* * @file pluginManagerTest.cpp @@ -38,12 +39,16 @@ class PluginManagerTest : public CppUnit::TestCase { + class Plugin; + /* * Use cppunit library macros to add unit test the factory */ CPPUNIT_TEST_SUITE( PluginManagerTest ); - CPPUNIT_TEST( testLoadPluginDirectory ); - CPPUNIT_TEST( testLoadPlugin ); + CPPUNIT_TEST( testLoadDynamicLibrary ); + CPPUNIT_TEST( testUnloadDynamicLibrary ); + CPPUNIT_TEST( testInstanciatePlugin ); + CPPUNIT_TEST( testInitPlugin ); CPPUNIT_TEST( testRegisterPlugin ); CPPUNIT_TEST_SUITE_END(); @@ -62,14 +67,20 @@ class PluginManagerTest : public CppUnit::TestCase { */ inline void tearDown(); - void testLoadPluginDirectory(); + void testLoadDynamicLibrary(); - void testLoadPlugin(); + void testUnloadDynamicLibrary(); + + void testInstanciatePlugin(); + + void testInitPlugin(); void testRegisterPlugin(); private: ::sflphone::PluginManager *_pm; + void *handlePtr; + ::sflphone::Plugin *plugin; }; /* Register our test module */