Skip to content

Commit

Permalink
yarprobotinterface pluginized + heavy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Ruzzenenti committed Feb 7, 2018
1 parent fae032e commit 1e34b61
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmake/YarpPlugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ macro(YARP_ADD_PLUGIN_YARPDEV_EXECUTABLE exename bundle_name)
set(YARP_CODE_PRE)
set(YARP_CODE_POST)
else()
set(YARP_CODE_PRE "YARP_DECLARE_DEVICES(${bundle_name})")
set(YARP_CODE_POST " YARP_REGISTER_DEVICES(${bundle_name})")
set(YARP_CODE_PRE "YARP_DECLARE_PLUGINS(${bundle_name})")
set(YARP_CODE_POST " YARP_REGISTER_PLUGINS(${bundle_name})")
endif()
configure_file("${YARP_MODULE_DIR}/template/yarp_plugin_yarpdev_main.cpp.in"
"${CMAKE_CURRENT_BINARY_DIR}/${bundle_name}_yarpdev.cpp" @ONLY)
Expand Down
26 changes: 26 additions & 0 deletions cmake/template/yarp_plugin_rfmodule.cpp.in
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)
19 changes: 19 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ add_subdirectory(devices)
add_subdirectory(carriers)
add_subdirectory(libYARP_init)

option(CREATE_RFMODULE_LIBRARY_MODULES "compile rfmodule also as plugins" OFF)

if(YARP_COMPILE_EXECUTABLES)
# idl compilers (thrift and ros)
add_subdirectory(idls)

# executables

yarp_begin_plugin_library(yarprfmod)
add_subdirectory(yarpserver)
add_subdirectory(yarp)
add_subdirectory(yarprun)
Expand All @@ -47,6 +51,21 @@ if(YARP_COMPILE_EXECUTABLES)
add_subdirectory(yarpmanager-console)
add_subdirectory(yarplogger-console)
add_subdirectory(yarpdatadumper)
yarp_end_plugin_library(yarprfmod)

add_library(YARP::yarprfmod ALIAS yarprfmod)
install(TARGETS yarprfmod
EXPORT YARP
COMPONENT runtime
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

if (YARP_LINK_PLUGINS)
set_property(GLOBAL APPEND PROPERTY YARP_LIBS yarprfmod ${yarprfmod_LIBRARIES})
endif()

set_property(TARGET yarprfmod PROPERTY FOLDER "Plugins/RFModules")

# Qt5 GUIs
add_subdirectory(yarpview)
Expand Down
18 changes: 17 additions & 1 deletion src/libYARP_OS/include/yarp/os/RFModuleFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/


#include <yarp/os/RFModule.h>
#include <string>

namespace yarp {
namespace os {

class YARP_OS_API RFPlugin
{
class RFPlugin_Private;
RFPlugin_Private* impl{nullptr};


public:

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

virtual void close();

virtual bool isRunning();
};

class YARP_OS_API RFModuleFactory
{
private:
Expand All @@ -20,7 +36,7 @@ class YARP_OS_API RFModuleFactory

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

}
Expand Down
118 changes: 113 additions & 5 deletions src/libYARP_OS/src/RFModuleFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}


7 changes: 7 additions & 0 deletions src/libYARP_init/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ if(YARP_LINK_PLUGINS)
target_link_libraries(YARP_init PRIVATE YARP::yarpmod)
target_link_libraries(YARP_init PRIVATE YARP::YARP_dev)
endif()

if(CREATE_RFMODULE_LIBRARY_MODULES)
set_property(SOURCE src/CustomInit.cpp
APPEND PROPERTY COMPILE_DEFINITIONS
PLUGIN_INIT_FUNCTION3=add_yarprfmod_plugins)
target_link_libraries(YARP_init PRIVATE YARP::yarprfmod)
endif()
endif()

install(TARGETS YARP_init
Expand Down
6 changes: 6 additions & 0 deletions src/libYARP_init/src/CustomInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ extern "C" YARP_IMPORT void PLUGIN_INIT_FUNCTION();
#ifdef PLUGIN_INIT_FUNCTION2
extern "C" YARP_IMPORT void PLUGIN_INIT_FUNCTION2();
#endif
#ifdef PLUGIN_INIT_FUNCTION3
extern "C" YARP_IMPORT void PLUGIN_INIT_FUNCTION3();
#endif

extern "C" void yarpCustomInit() {
#ifdef PLUGIN_INIT_FUNCTION
Expand All @@ -27,6 +30,9 @@ extern "C" void yarpCustomInit() {
#ifdef PLUGIN_INIT_FUNCTION2
PLUGIN_INIT_FUNCTION2();
#endif
#ifdef PLUGIN_INIT_FUNCTION3
PLUGIN_INIT_FUNCTION3();
#endif
}

#include <yarp/os/impl/LogForwarder.h>
Expand Down
29 changes: 29 additions & 0 deletions src/yarprobotinterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,42 @@ if(CREATE_YARPROBOTINTERFACE)
set(yarprobotinterface_IDLS
yarprobotinterface.thrift)


include(YarpIDL)
yarp_idl_to_dir(${yarprobotinterface_IDLS}
"${CMAKE_CURRENT_SOURCE_DIR}/idl_generated_code"
yarprobotinterface_IDLS_SRCS
yarprobotinterface_IDLS_HDRS
yarprobotinterface_IDLS_INCLUDE_DIRS)
include_directories("${yarprobotinterface_IDLS_INCLUDE_DIRS}")

yarp_prepare_plugin(yarprobotinterface
CATEGORY rfmodule
TYPE RobotInterface::Module
INCLUDE Module.h
EXTRA_CONFIG CODE=""
DEPENDS "CREATE_RFMODULE_LIBRARY_MODULES")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
yarp_add_plugin(robotinterface ${yarprobotinterface_SRCS}
${yarprobotinterface_HDRS}
${yarprobotinterface_IDLS_SRCS}
${yarprobotinterface_IDLS_HDRS})

target_link_libraries(robotinterface
YARP::YARP_OS
YARP::YARP_dev
YARP::YARP_init
${TinyXML_LIBRARIES})

yarp_install(TARGETS robotinterface
EXPORT YARP
COMPONENT runtime
LIBRARY DESTINATION ${YARP_DYNAMIC_PLUGINS_INSTALL_DIR}
ARCHIVE DESTINATION ${YARP_STATIC_PLUGINS_INSTALL_DIR})
yarp_install(FILES yarprobotinterface.ini
COMPONENT runtime
DESTINATION ${YARP_PLUGIN_MANIFESTS_INSTALL_DIR})
set_property(TARGET robotinterface PROPERTY FOLDER "Plugins/RFModules")

add_executable(yarprobotinterface ${yarprobotinterface_SRCS}
${yarprobotinterface_HDRS}
Expand Down
4 changes: 4 additions & 0 deletions src/yarprobotinterface/RobotInterface.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[plugin yarprobotinterface]
type RFModule
name yarprobotinterface
library robotinterface
4 changes: 4 additions & 0 deletions src/yarprobotinterface/yarprobotinterface.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[plugin yarprobotinterface]
type RFModule
name yarprobotinterface
library robotinterface

0 comments on commit 1e34b61

Please sign in to comment.