-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yarprobotinterface pluginized + heavy fixes
- Loading branch information
Andrea Ruzzenenti
committed
Feb 7, 2018
1 parent
fae032e
commit 1e34b61
Showing
10 changed files
with
227 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (C) 2018 Istituto Italiano di Tecnologia (IIT) | ||
* Authors: Andrea Ruzzenenti <[email protected]> | ||
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT | ||
*/ | ||
#include <yarp/conf/api.h> | ||
#include <yarp/os/RFModuleFactory.h> | ||
#include <yarp/os/SharedLibraryClass.h> | ||
#include <@YARPPLUG_INCLUDE@> | ||
|
||
#ifdef YARP_STATIC_PLUGIN | ||
# define YARP_PLUGIN_IMPORT | ||
# define YARP_PLUGIN_EXPORT | ||
#else | ||
# define YARP_PLUGIN_IMPORT YARP_HELPER_DLL_IMPORT | ||
# define YARP_PLUGIN_EXPORT YARP_HELPER_DLL_EXPORT | ||
#endif | ||
|
||
|
||
#ifdef YARP_STATIC_PLUGIN | ||
YARP_PLUGIN_EXPORT void add_owned_@YARPPLUG_NAME@(const char *owner) { | ||
yarp::os::RFModuleFactory::AddModule("@YARPPLUG_NAME@", new @YARPPLUG_TYPE@); | ||
} | ||
#endif | ||
|
||
YARP_DEFINE_SHARED_SUBCLASS(@YARPPLUG_NAME@, @YARPPLUG_TYPE@, yarp::os::RFModule) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,126 @@ | |
* Authors: Andrea Ruzzenenti <[email protected]> | ||
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT | ||
*/ | ||
|
||
#include <yarp/os/YarpPluginSelector.h> | ||
#include <yarp/os/YarpPluginSettings.h> | ||
#include <yarp/os/YarpPlugin.h> | ||
#include <yarp/os/RFModuleFactory.h> | ||
#include <map> | ||
using namespace std; | ||
using namespace yarp::os; | ||
|
||
struct yarp::os::RFModuleFactory::Private | ||
class RFModuleSelector : public YarpPluginSelector | ||
{ | ||
virtual bool select(Searchable& options) override | ||
{ | ||
return options.check("type",Value("none")).asString() == "RFModule"; | ||
} | ||
}; | ||
|
||
|
||
|
||
class RFPlugin::RFPlugin_Private | ||
{ | ||
public: | ||
string name; | ||
YarpPlugin<RFModule> yarpPlugin; | ||
SharedLibraryClass<RFModule> sharedLibClass; | ||
RFModuleSelector selector; | ||
|
||
}; | ||
|
||
void RFPlugin::close() | ||
{ | ||
impl->sharedLibClass->stopModule(); | ||
} | ||
|
||
bool RFPlugin::isRunning() | ||
{ | ||
return !impl->sharedLibClass->isStopping(); | ||
} | ||
|
||
bool RFPlugin::open(const string& command) | ||
{ | ||
ResourceFinder rf; | ||
string name = command.substr(0, command.find(" ")); | ||
|
||
char* str = new char[command.size()]; | ||
memcpy(str, command.c_str(), command.size()); | ||
|
||
enum { kMaxArgs = 64 }; | ||
int argc = 0; | ||
char* argv[kMaxArgs]; | ||
|
||
char* p2 = strtok(str, " "); | ||
while (p2 && argc < kMaxArgs - 1) | ||
{ | ||
argv[argc++] = p2; | ||
p2 = strtok(0, " "); | ||
} | ||
argv[argc] = 0; | ||
|
||
|
||
rf.configure(argc, argv); | ||
delete str; | ||
|
||
RFModule* staticmodule{nullptr}; | ||
staticmodule = RFModuleFactory::GetInstance().GetModule(name); | ||
if (staticmodule) | ||
{ | ||
try | ||
{ | ||
if(staticmodule->configure(rf)) return false; | ||
staticmodule->runModuleThreaded(); | ||
return true; | ||
} | ||
catch (...) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
YarpPluginSettings settings; | ||
impl = new RFPlugin_Private; | ||
impl->name = name; | ||
impl->selector.scan(); | ||
|
||
settings.setPluginName(impl->name); | ||
settings.setVerboseMode(true); | ||
|
||
if (!settings.setSelector(impl->selector)) return false; | ||
if (!impl->yarpPlugin.open(settings)) return false; | ||
|
||
impl->sharedLibClass.open(*impl->yarpPlugin.getFactory()); | ||
|
||
if (!impl->sharedLibClass.isValid()) return false; | ||
|
||
settings.setLibraryMethodName(impl->yarpPlugin.getFactory()->getName(), settings.getMethodName()); | ||
settings.setClassInfo(impl->yarpPlugin.getFactory()->getClassName(), impl->yarpPlugin.getFactory()->getBaseClassName()); | ||
|
||
try | ||
{ | ||
impl->sharedLibClass.getContent().configure(rf); | ||
} | ||
catch ( ... ) | ||
{ | ||
return false; | ||
} | ||
|
||
bool&& ret = impl->sharedLibClass->configure(rf); | ||
if(ret) impl->sharedLibClass->runModuleThreaded(); | ||
return ret; | ||
} | ||
|
||
struct RFModuleFactory::Private | ||
{ | ||
map<string, RFModule*> delegates; | ||
}; | ||
|
||
RFModuleFactory::RFModuleFactory() : impl(new Private){} | ||
RFModuleFactory::RFModuleFactory() : impl(new Private) | ||
{ | ||
//add rfmodule here | ||
} | ||
|
||
RFModuleFactory& RFModuleFactory::GetInstance() | ||
{ | ||
static RFModuleFactory instance; | ||
|
@@ -26,13 +134,13 @@ void RFModuleFactory::AddModule(const string &name, RFModule* module) | |
GetInstance().impl->delegates[name] = module; | ||
} | ||
|
||
RFModule* RFModuleFactory::GetModule(const string& name) | ||
RFModule* RFModuleFactory::GetModule(const string name) | ||
{ | ||
if(impl->delegates.find(name) != impl->delegates.end()) | ||
{ | ||
return impl->delegates[name]; | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[plugin yarprobotinterface] | ||
type RFModule | ||
name yarprobotinterface | ||
library robotinterface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[plugin yarprobotinterface] | ||
type RFModule | ||
name yarprobotinterface | ||
library robotinterface |