mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 06:06:10 -04:00
- Added basic infrastructure to plugin system in libretroshare and retroshare-gui.
- ported LinksCloud to a new plugin, and removed it from main sources Next moves: - add gui for managing plugins - handle windows compilation git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4275 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
3925a7114a
commit
a2c91a7924
28 changed files with 4676 additions and 243 deletions
25
libretroshare/src/plugins/pluginclasses.h
Normal file
25
libretroshare/src/plugins/pluginclasses.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
|
||||
#include <dbase/cachestrapper.h>
|
||||
|
||||
// The following class abstracts the construction of a cache service. The user only has to
|
||||
// supply RS with a type ID. If the ID is already in use, RS will complain.
|
||||
//
|
||||
class RsCacheService: public CacheSource, public CacheStore, public p3Config
|
||||
{
|
||||
public:
|
||||
RsCacheService(uint16_t type,uint32_t config_type,uint32_t tick_delay_in_seconds) ;
|
||||
|
||||
uint32_t tickDelay() const { return _tick_delay_in_seconds ; }
|
||||
virtual void tick() {}
|
||||
|
||||
// Functions from p3config
|
||||
//
|
||||
virtual RsSerialiser *setupSerialiser() { return NULL ; }
|
||||
virtual bool saveList(bool&, std::list<RsItem*>&) { return false ;}
|
||||
virtual bool loadList(std::list<RsItem*>&) { return false ;}
|
||||
|
||||
private:
|
||||
uint32_t _tick_delay_in_seconds ;
|
||||
};
|
||||
|
205
libretroshare/src/plugins/pluginmanager.cc
Normal file
205
libretroshare/src/plugins/pluginmanager.cc
Normal file
|
@ -0,0 +1,205 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "pluginmanager.h"
|
||||
#include <dirent.h>
|
||||
#include <util/folderiterator.h>
|
||||
#include <ft/ftserver.h>
|
||||
#include <dbase/cachestrapper.h>
|
||||
#include <retroshare/rsplugin.h>
|
||||
#include <retroshare/rsfiles.h>
|
||||
#include <pqi/pqiservice.h>
|
||||
#include <plugins/pluginclasses.h>
|
||||
|
||||
// lets disable the plugin system for now, as it's unfinished.
|
||||
#define WINDOWS_SYS
|
||||
#ifndef WINDOWS_SYS
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
std::string RsPluginManager::_plugin_entry_symbol ;
|
||||
std::string RsPluginManager::_local_cache_dir ;
|
||||
std::string RsPluginManager::_remote_cache_dir ;
|
||||
ftServer *RsPluginManager::_ftserver = NULL ;
|
||||
p3ConnectMgr *RsPluginManager::_connectmgr = NULL ;
|
||||
|
||||
typedef RsPlugin *(*RetroSharePluginEntry)(void) ;
|
||||
RsPluginHandler *rsPlugins ;
|
||||
|
||||
void RsPluginManager::setCacheDirectories(const std::string& local_cache, const std::string& remote_cache)
|
||||
{
|
||||
_local_cache_dir = local_cache ;
|
||||
_remote_cache_dir = remote_cache ;
|
||||
}
|
||||
|
||||
bool RsPluginManager::acceptablePluginName(const std::string& name)
|
||||
{
|
||||
// Needs some windows specific code here
|
||||
//
|
||||
#ifdef WINDOWS_SYS
|
||||
return name.size() > 4 && !strcmp(name.c_str()+name.size()-3,".dll") ;
|
||||
#else
|
||||
return name.size() > 3 && !strcmp(name.c_str()+name.size()-3,".so") ;
|
||||
#endif
|
||||
}
|
||||
|
||||
void RsPluginManager::loadPlugins(const std::vector<std::string>& plugin_directories)
|
||||
{
|
||||
_plugin_entry_symbol = "RETROSHARE_PLUGIN_provide" ;
|
||||
|
||||
// 0 - get the list of files to read
|
||||
|
||||
for(uint32_t i=0;i<plugin_directories.size();++i)
|
||||
{
|
||||
librs::util::FolderIterator dirIt(plugin_directories[i]);
|
||||
if(!dirIt.isValid())
|
||||
{
|
||||
std::cerr << "Plugin directory : " << plugin_directories[i] << " does not exist." << std::endl ;
|
||||
return ;
|
||||
}
|
||||
|
||||
while(dirIt.readdir())
|
||||
{
|
||||
std::string fname;
|
||||
dirIt.d_name(fname);
|
||||
std::string fullname = plugin_directories[i] + "/" + fname;
|
||||
|
||||
if(!acceptablePluginName(fullname))
|
||||
continue ;
|
||||
|
||||
std::cerr << "Found plugin " << fullname << std::endl;
|
||||
std::cerr << " Loading plugin..." << std::endl;
|
||||
|
||||
loadPlugin(fullname) ;
|
||||
}
|
||||
dirIt.closedir();
|
||||
}
|
||||
|
||||
std::cerr << "Loaded a total of " << _plugins.size() << " plugins." << std::endl;
|
||||
}
|
||||
|
||||
bool RsPluginManager::loadPlugin(const std::string& plugin_name)
|
||||
{
|
||||
#ifndef WINDOWS_SYS
|
||||
std::cerr << " Loading plugin " << plugin_name << std::endl;
|
||||
|
||||
// The following choice is somewhat dangerous since the program can stop when a symbol can
|
||||
// not be resolved. However, this is the only way to bind a single .so for both the
|
||||
// interface and command line executables.
|
||||
|
||||
int link_mode = RTLD_NOW | RTLD_GLOBAL ; // RTLD_NOW
|
||||
//int link_mode = RTLD_GLOBAL ;
|
||||
|
||||
// Warning: this temporary vector is necessary, because linking with a .so that would include BundleManager.h
|
||||
// is going to call the initialization of the static members of bundleManager a second time, and therefore
|
||||
// will erase whatever is already initialized. So I first open all libraries, then fill the vectors.
|
||||
//
|
||||
void *handle = dlopen(plugin_name.c_str(),link_mode) ;
|
||||
|
||||
if(handle == NULL)
|
||||
{
|
||||
std::cerr << " Cannot open plugin: " << dlerror() << std::endl ;
|
||||
return false ;
|
||||
}
|
||||
|
||||
void *pf = dlsym(handle,_plugin_entry_symbol.c_str()) ;
|
||||
|
||||
if(pf == NULL)
|
||||
std::cerr << dlerror() << std::endl ;
|
||||
else
|
||||
std::cerr << " Added function entry for symbol " << _plugin_entry_symbol << std::endl ;
|
||||
|
||||
RsPlugin *p = ( (*(RetroSharePluginEntry)pf)() ) ;
|
||||
|
||||
if(p == NULL)
|
||||
{
|
||||
std::cerr << " Plugin entry function " << _plugin_entry_symbol << " returns NULL ! It should return an object of type RsPlugin* " << std::endl;
|
||||
return false ;
|
||||
}
|
||||
_plugins.push_back(p) ;
|
||||
|
||||
if(link_mode & RTLD_LAZY)
|
||||
{
|
||||
std::cerr << " Symbols have been linked in LAZY mode. This means that undefined symbols may" << std::endl ;
|
||||
std::cerr << " crash your program any time." << std::endl ;
|
||||
}
|
||||
#else
|
||||
std::cerr << " Plugin load is yet unimplemented under windows." << std::endl;
|
||||
#endif
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
||||
p3ConnectMgr *RsPluginManager::getConnectMgr() const
|
||||
{
|
||||
assert(_connectmgr != NULL) ;
|
||||
return _connectmgr ;
|
||||
}
|
||||
ftServer *RsPluginManager::getFileServer() const
|
||||
{
|
||||
assert(_ftserver != NULL) ;
|
||||
return _ftserver ;
|
||||
}
|
||||
const std::string& RsPluginManager::getLocalCacheDir() const
|
||||
{
|
||||
assert(!_local_cache_dir.empty()) ;
|
||||
return _local_cache_dir ;
|
||||
}
|
||||
const std::string& RsPluginManager::getRemoteCacheDir() const
|
||||
{
|
||||
assert(!_remote_cache_dir.empty()) ;
|
||||
return _remote_cache_dir ;
|
||||
}
|
||||
void RsPluginManager::slowTickPlugins(time_t seconds)
|
||||
{
|
||||
for(uint32_t i=0;i<_plugins.size();++i)
|
||||
if(_plugins[i]->rs_cache_service() != NULL && (seconds % _plugins[i]->rs_cache_service()->tickDelay() ))
|
||||
{
|
||||
std::cerr << " ticking plugin " << _plugins[i]->getPluginName() << std::endl;
|
||||
_plugins[i]->rs_cache_service()->tick() ;
|
||||
}
|
||||
}
|
||||
|
||||
void RsPluginManager::registerCacheServices()
|
||||
{
|
||||
std::cerr << " Registering cache services." << std::endl;
|
||||
|
||||
for(uint32_t i=0;i<_plugins.size();++i)
|
||||
if(_plugins[i]->rs_cache_service() != NULL)
|
||||
{
|
||||
rsFiles->getCacheStrapper()->addCachePair(CachePair(_plugins[i]->rs_cache_service(),_plugins[i]->rs_cache_service(),CacheId(_plugins[i]->rs_service_id(), 0))) ;
|
||||
std::cerr << " adding new cache pair for plugin " << _plugins[i]->getPluginName() << ", with RS_ID " << _plugins[i]->rs_service_id() << std::endl ;
|
||||
}
|
||||
}
|
||||
|
||||
void RsPluginManager::registerClientServices(p3ServiceServer *pqih)
|
||||
{
|
||||
std::cerr << " Registering pqi services." << std::endl;
|
||||
|
||||
for(uint32_t i=0;i<_plugins.size();++i)
|
||||
if(_plugins[i]->rs_pqi_service() != NULL)
|
||||
{
|
||||
pqih->addService(_plugins[i]->rs_pqi_service()) ;
|
||||
std::cerr << " Added pqi service for plugin " << _plugins[i]->getPluginName() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void RsPluginManager::addConfigurations(p3ConfigMgr *ConfigMgr)
|
||||
{
|
||||
std::cerr << " Registering configuration files." << std::endl;
|
||||
|
||||
for(uint32_t i=0;i<_plugins.size();++i)
|
||||
if(_plugins[i]->configurationFileName().length() > 0)
|
||||
{
|
||||
ConfigMgr->addConfiguration(_plugins[i]->configurationFileName(), _plugins[i]->rs_cache_service());
|
||||
std::cerr << " Added configuration for plugin " << _plugins[i]->getPluginName() << ", with file " << _plugins[i]->configurationFileName() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
RsCacheService::RsCacheService(uint16_t service_type,uint32_t config_type,uint32_t tick_delay)
|
||||
: CacheSource(service_type, true, rsPlugins->getFileServer()->getCacheStrapper(), rsPlugins->getLocalCacheDir()),
|
||||
CacheStore (service_type, true, rsPlugins->getFileServer()->getCacheStrapper(), rsPlugins->getFileServer()->getCacheTransfer(), rsPlugins->getRemoteCacheDir()),
|
||||
p3Config(config_type), // CONFIG_TYPE_RANK_LINK
|
||||
_tick_delay_in_seconds(tick_delay)
|
||||
{
|
||||
}
|
||||
|
48
libretroshare/src/plugins/pluginmanager.h
Normal file
48
libretroshare/src/plugins/pluginmanager.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <retroshare/rsplugin.h>
|
||||
|
||||
class p3ConfigMgr ;
|
||||
class p3ServiceServer ;
|
||||
class p3ConnectMgr ;
|
||||
|
||||
class RsPluginManager: public RsPluginHandler
|
||||
{
|
||||
public:
|
||||
RsPluginManager() {}
|
||||
virtual ~RsPluginManager() {}
|
||||
|
||||
virtual int nbPlugins() const { return _plugins.size() ; }
|
||||
virtual RsPlugin *plugin(int i) { return _plugins[i] ; }
|
||||
|
||||
virtual void slowTickPlugins(time_t sec) ;
|
||||
virtual void addConfigurations(p3ConfigMgr *cfgMgr) ;
|
||||
virtual const std::string& getLocalCacheDir() const ;
|
||||
virtual const std::string& getRemoteCacheDir() const ;
|
||||
virtual ftServer *getFileServer() const ;
|
||||
virtual p3ConnectMgr *getConnectMgr() const ;
|
||||
|
||||
static void setPluginEntrySymbol(const std::string& s) { _plugin_entry_symbol = s ; }
|
||||
static bool acceptablePluginName(const std::string& s) ;
|
||||
static void setCacheDirectories(const std::string& local,const std::string& remote) ;
|
||||
static void setFileServer(ftServer *ft) { _ftserver = ft ; }
|
||||
static void setConnectMgr(p3ConnectMgr *cm) { _connectmgr = cm ; }
|
||||
|
||||
void loadPlugins(const std::vector<std::string>& plugin_directories) ;
|
||||
|
||||
void registerCacheServices() ;
|
||||
void registerClientServices(p3ServiceServer *pqih) ;
|
||||
private:
|
||||
bool loadPlugin(const std::string& shared_library_name) ;
|
||||
|
||||
std::vector<RsPlugin *> _plugins ;
|
||||
|
||||
static std::string _plugin_entry_symbol ;
|
||||
static std::string _remote_cache_dir ;
|
||||
static std::string _local_cache_dir ;
|
||||
static ftServer *_ftserver ;
|
||||
static p3ConnectMgr *_connectmgr ;
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue