Skip to content

Commit

Permalink
RFPlugin Factory Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Ruzzenenti committed Feb 13, 2018
1 parent 1e34b61 commit 3c2aa04
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmake/template/yarp_plugin_RFModule.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#ifdef YARP_STATIC_PLUGIN
YARP_PLUGIN_EXPORT void add_owned_@YARPPLUG_NAME@(const char *owner) {
yarp::os::RFModuleFactory::AddModule("@YARPPLUG_NAME@", new @YARPPLUG_TYPE@);
yarp::os::RFModuleFactory::AddModule("@YARPPLUG_NAME@", [](){return (RFModule*)new @YARPPLUG_TYPE@});
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion cmake/template/yarp_plugin_rfmodule.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#ifdef YARP_STATIC_PLUGIN
YARP_PLUGIN_EXPORT void add_owned_@YARPPLUG_NAME@(const char *owner) {
yarp::os::RFModuleFactory::AddModule("@YARPPLUG_NAME@", new @YARPPLUG_TYPE@);
yarp::os::RFModuleFactory::AddModule("@YARPPLUG_NAME@", [](){return (RFModule*)new @YARPPLUG_TYPE@});
}
#endif

Expand Down
5 changes: 5 additions & 0 deletions src/libYARP_OS/include/yarp/os/RFModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ class YARP_OS_API yarp::os::RFModule {
*/
virtual int runModuleThreaded(yarp::os::ResourceFinder &rf);

/**
* return the Thread unique identifier
*/
virtual int getThreadKey();

/**
* Configure the module, pass a ResourceFinder object to the module.
* This function can perform initialization including object creation and
Expand Down
12 changes: 10 additions & 2 deletions src/libYARP_OS/include/yarp/os/RFModuleFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ class YARP_OS_API RFPlugin
{
class RFPlugin_Private;
RFPlugin_Private* impl{nullptr};


public:
RFPlugin();

virtual ~RFPlugin();

virtual bool open(const std::string& command);

virtual void close();

virtual bool isRunning();

virtual std::string getCmd();

virtual std::string getAlias();

virtual int getThreadKey();
};

class YARP_OS_API RFModuleFactory
Expand All @@ -35,7 +43,7 @@ class YARP_OS_API RFModuleFactory
public:

static RFModuleFactory& GetInstance();
static void AddModule(const std::string& name, RFModule* module);
static void AddModule(const std::string& name, RFModule*(*moduleCreate)(void));
RFModule* GetModule(const std::string name);
};

Expand Down
5 changes: 4 additions & 1 deletion src/libYARP_OS/src/RFModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class RFModuleRespondHandler : public yarp::os::PortReader, public Thread {
}
};


bool RFModuleRespondHandler::read(ConnectionReader& connection) {
Bottle cmd, response;
if (!cmd.read(connection)) { return false; }
Expand Down Expand Up @@ -200,6 +199,10 @@ class RFModuleHelper {

static RFModule *module = nullptr;

int RFModule::getThreadKey()
{
return THREADED_HANDLER(implementation).getKey();
}

static void handler (int sig) {
YARP_UNUSED(sig);
Expand Down
93 changes: 66 additions & 27 deletions src/libYARP_OS/src/RFModuleFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,72 @@ class RFModuleSelector : public YarpPluginSelector
}
};



class RFPlugin::RFPlugin_Private
struct SharedRFPlugin
{
public:
string name;
YarpPlugin<RFModule> yarpPlugin;
SharedLibraryClass<RFModule> sharedLibClass;
RFModuleSelector selector;
};

struct RFPlugin::RFPlugin_Private
{
RFPlugin_Private() = default;

string alias;
string name;
string command;
int threadID{0};
SharedRFPlugin* shared{nullptr};

RFModule* module{nullptr};
~RFPlugin_Private()
{
delete shared;
}
};

RFPlugin::RFPlugin() :
impl(new RFPlugin_Private)
{
}

RFPlugin::~RFPlugin()
{
delete impl;
}

string RFPlugin::getCmd()
{
return impl->command;
}

string RFPlugin::getAlias()
{
return impl->alias;
}

int RFPlugin::getThreadKey()
{
return impl->module->getThreadKey();
}

void RFPlugin::close()
{
impl->sharedLibClass->stopModule();
impl->module->stopModule();
}

bool RFPlugin::isRunning()
{
return !impl->sharedLibClass->isStopping();
return !impl->module->isStopping();
}

bool RFPlugin::open(const string& command)
bool RFPlugin::open(const string& inCommand)
{
ResourceFinder rf;
string name = command.substr(0, command.find(" "));
string name = inCommand.substr(0, inCommand.find(" "));

char* str = new char[command.size()];
memcpy(str, command.c_str(), command.size());
char* str = new char[inCommand.size()];
memcpy(str, inCommand.c_str(), inCommand.size());

enum { kMaxArgs = 64 };
int argc = 0;
Expand All @@ -61,7 +98,7 @@ bool RFPlugin::open(const string& command)
}
argv[argc] = 0;


impl->command = inCommand;
rf.configure(argc, argv);
delete str;

Expand All @@ -71,8 +108,9 @@ bool RFPlugin::open(const string& command)
{
try
{
if(staticmodule->configure(rf)) return false;
if(!staticmodule->configure(rf)) return false;
staticmodule->runModuleThreaded();
impl->module = staticmodule;
return true;
}
catch (...)
Expand All @@ -82,40 +120,41 @@ bool RFPlugin::open(const string& command)
}

YarpPluginSettings settings;
impl = new RFPlugin_Private;
impl->shared = new SharedRFPlugin;
impl->name = name;
impl->selector.scan();
impl->shared->selector.scan();

settings.setPluginName(impl->name);
settings.setVerboseMode(true);

if (!settings.setSelector(impl->selector)) return false;
if (!impl->yarpPlugin.open(settings)) return false;
if (!settings.setSelector(impl->shared->selector)) return false;
if (!impl->shared->yarpPlugin.open(settings)) return false;

impl->sharedLibClass.open(*impl->yarpPlugin.getFactory());
impl->shared->sharedLibClass.open(*impl->shared->yarpPlugin.getFactory());

if (!impl->sharedLibClass.isValid()) return false;
if (!impl->shared->sharedLibClass.isValid()) return false;

settings.setLibraryMethodName(impl->yarpPlugin.getFactory()->getName(), settings.getMethodName());
settings.setClassInfo(impl->yarpPlugin.getFactory()->getClassName(), impl->yarpPlugin.getFactory()->getBaseClassName());
settings.setLibraryMethodName(impl->shared->yarpPlugin.getFactory()->getName(), settings.getMethodName());
settings.setClassInfo(impl->shared->yarpPlugin.getFactory()->getClassName(), impl->shared->yarpPlugin.getFactory()->getBaseClassName());

try
{
impl->sharedLibClass.getContent().configure(rf);
impl->shared->sharedLibClass.getContent().configure(rf);
}
catch ( ... )
{
return false;
}

bool&& ret = impl->sharedLibClass->configure(rf);
if(ret) impl->sharedLibClass->runModuleThreaded();
bool&& ret = impl->shared->sharedLibClass->configure(rf);
if(ret) impl->shared->sharedLibClass->runModuleThreaded();
impl->module = &(impl->shared->sharedLibClass.getContent());
return ret;
}

struct RFModuleFactory::Private
{
map<string, RFModule*> delegates;
map<string, RFModule*(*)(void)> delegates;
};

RFModuleFactory::RFModuleFactory() : impl(new Private)
Expand All @@ -129,7 +168,7 @@ RFModuleFactory& RFModuleFactory::GetInstance()
return instance;
}

void RFModuleFactory::AddModule(const string &name, RFModule* module)
void RFModuleFactory::AddModule(const string &name, RFModule*(*module)(void))
{
GetInstance().impl->delegates[name] = module;
}
Expand All @@ -138,7 +177,7 @@ RFModule* RFModuleFactory::GetModule(const string name)
{
if(impl->delegates.find(name) != impl->delegates.end())
{
return impl->delegates[name];
return impl->delegates[name]();
}

return nullptr;
Expand Down

0 comments on commit 3c2aa04

Please sign in to comment.