Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yarp rfmodule plugin #1532

Merged
merged 5 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ install(FILES ${YARP_CMAKE_FILES}

# Templates used by modules
set(YARP_TEMPLATE_FILES template/yarp_plugin_carrier.cpp.in
template/yarp_plugin_RFModule.cpp.in
template/yarp_plugin_device.cpp.in
template/yarp_plugin_portmonitor.cpp.in
template/yarp_plugin_library.cpp.in
Expand Down
29 changes: 29 additions & 0 deletions cmake/template/yarp_plugin_RFModule.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/

#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@", [](){return (yarp::os::RFModule*)new @YARPPLUG_TYPE@;});
}
#endif

YARP_DEFINE_SHARED_SUBCLASS(@YARPPLUG_NAME@, @YARPPLUG_TYPE@, yarp::os::RFModule)
4 changes: 4 additions & 0 deletions doc/release/v3_2_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ New Features
### extern
* rplidar sdk updated to 1.9.0

#### RFModules

* Added the possibility to compile `RFModule`s as plugin and load them runtime in a single process context.

### GUIs

#### `yarpmanager`
Expand Down
27 changes: 27 additions & 0 deletions example/RFModulePlugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
# All rights reserved.
#
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license. See the accompanying LICENSE file for details.

cmake_minimum_required(VERSION 3.5)
project(myModules)

# Enable C++11
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED 11)

# Must be set before find_package(YARP)
set(BUILD_SHARED_LIBS ON)

find_package(YARP REQUIRED)

yarp_begin_plugin_library(myModules)

add_subdirectory(testmoduleFoo)
add_subdirectory(testmoduleBar)

yarp_end_plugin_library(myModules)

add_subdirectory(moduleLoader)
10 changes: 10 additions & 0 deletions example/RFModulePlugin/moduleLoader/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
# All rights reserved.
#
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license. See the accompanying LICENSE file for details.

set(SRC main.cpp)
add_executable(moduleLoader ${SRC})
target_link_libraries(moduleLoader YARP::YARP_OS
YARP::YARP_init)
39 changes: 39 additions & 0 deletions example/RFModulePlugin/moduleLoader/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/

#include <yarp/os/RFPlugin.h>
#include <yarp/os/Time.h>
#include <memory>

using namespace yarp::os;

int main()
{
std::unique_ptr<RFPlugin> pluginBar(new RFPlugin);

if (!pluginBar->open("myModuleBar"))
{
printf("failed to open myModuleBar\n");
return 1;
}

std::unique_ptr<RFPlugin> pluginFoo(new RFPlugin);

if (!pluginFoo->open("myModuleFoo"))
{
printf("failed to open myModuleFoo\n");
return 1;
}

while (true)
{
Time::delay(0.3);
}

return 0;
}
23 changes: 23 additions & 0 deletions example/RFModulePlugin/testmoduleBar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
# All rights reserved.
#
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license. See the accompanying LICENSE file for details.

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set(HDRS module.h)

yarp_prepare_plugin(myModuleBar
CATEGORY rfmodule
TYPE moduleBar
INCLUDE module.h)

if(ENABLE_myModules_myModuleBar)
yarp_add_plugin(module_Bar ${HDRS})
target_link_libraries(module_Bar YARP::YARP_OS)
endif()

#create the executable target
add_executable(myModuleBarExe main.cpp ${SRC} ${HDRS})
target_link_libraries(myModuleBarExe YARP::YARP_OS YARP::YARP_init)
16 changes: 16 additions & 0 deletions example/RFModulePlugin/testmoduleBar/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/

#include <module.h>
#include <yarp/os/Network.h>
int main(int argc, char** argv)
{
yarp::os::Network yarp;
moduleBar module;
return module.runModule();
}
27 changes: 27 additions & 0 deletions example/RFModulePlugin/testmoduleBar/module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/


#include <yarp/os/RFModule.h>
#include <yarp/os/Time.h>
#include <yarp/os/LogStream.h>

class moduleBar : public yarp::os::RFModule
{
virtual bool configure(yarp::os::ResourceFinder &r)
{
return true;
}

virtual bool updateModule() override
{
yarp::os::Time::delay(1);
yDebug() << "Updated module Bar";
return true;
}
};
4 changes: 4 additions & 0 deletions example/RFModulePlugin/testmoduleBar/myModuleBar.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[plugin myModuleBar]
type rfmodule
name myModuleBar
library module_Bar
23 changes: 23 additions & 0 deletions example/RFModulePlugin/testmoduleFoo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
# All rights reserved.
#
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license. See the accompanying LICENSE file for details.

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set(HDRS module.h)

yarp_prepare_plugin(myModuleFoo
CATEGORY rfmodule
TYPE moduleFoo
INCLUDE module.h)

if(ENABLE_myModules_myModuleFoo)
yarp_add_plugin(module_Foo ${HDRS})
target_link_libraries(module_Foo YARP::YARP_OS)
endif()

#create the executable target
add_executable(myModuleFooExe main.cpp ${SRC} ${HDRS})
target_link_libraries(myModuleFooExe YARP::YARP_OS YARP::YARP_init)
16 changes: 16 additions & 0 deletions example/RFModulePlugin/testmoduleFoo/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/

#include <module.h>
#include <yarp/os/Network.h>
int main(int argc, char** argv)
{
yarp::os::Network yarp;
moduleFoo module;
return module.runModule();
}
26 changes: 26 additions & 0 deletions example/RFModulePlugin/testmoduleFoo/module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/

#include <yarp/os/RFModule.h>
#include <yarp/os/Time.h>
#include <yarp/os/LogStream.h>

class moduleFoo : public yarp::os::RFModule
{
virtual bool configure(yarp::os::ResourceFinder &r)
{
return true;
}

virtual bool updateModule() override
{
yarp::os::Time::delay(1);
yDebug() << "Updated module Foo";
return true;
}
};
4 changes: 4 additions & 0 deletions example/RFModulePlugin/testmoduleFoo/myModuleFoo.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[plugin myModuleFoo]
type rfmodule
name myModuleFoo
library module_Foo
5 changes: 5 additions & 0 deletions src/libYARP_OS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ set(YARP_OS_HDRS include/yarp/os/AbstractCarrier.h
include/yarp/os/ResourceFinder.h
include/yarp/os/ResourceFinderOptions.h
include/yarp/os/RFModule.h
include/yarp/os/RFPlugin.h

include/yarp/os/RosNameSpace.h
include/yarp/os/Route.h
include/yarp/os/RpcClient.h
Expand Down Expand Up @@ -190,6 +192,7 @@ set(YARP_OS_IMPL_HDRS include/yarp/os/impl/AuthHMAC.h
include/yarp/os/impl/PortCorePackets.h
include/yarp/os/impl/PortCoreUnit.h
include/yarp/os/impl/Protocol.h
include/yarp/os/impl/RFModuleFactory.h
include/yarp/os/impl/Runnable.h
include/yarp/os/impl/SocketTwoWayStream.h
include/yarp/os/impl/SplitString.h
Expand Down Expand Up @@ -297,6 +300,8 @@ set(YARP_OS_SRCS src/AbstractCarrier.cpp
src/ResourceFinder.cpp
src/ResourceFinderOptions.cpp
src/RFModule.cpp
src/RFModuleFactory.cpp
src/RFPlugin.cpp
src/RosNameSpace.cpp
src/Route.cpp
src/RpcClient.cpp
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 @@ -129,6 +129,11 @@ class YARP_OS_API 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
60 changes: 60 additions & 0 deletions src/libYARP_OS/include/yarp/os/RFPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2006-2019 Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/

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

#ifndef YARP_OS_RFPLUGIN_H
#define YARP_OS_RFPLUGIN_H

namespace yarp {
namespace os {

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

public:
RFPlugin();

virtual ~RFPlugin();

/**
* opens the plugin
* @param command a string containing the equivalent command line
* @return true if opened succesfully
*/
virtual bool open(const std::string& command);

/**
* closes the plugin
*/
virtual void close();

/**
* check if the plugin is running
*/
virtual bool isRunning();

/**
* get the command line used to open the plugin
*/
virtual std::string getCmd();

/**
* get the thread id
*/
virtual int getThreadKey();
};


}
}

#endif // YARP_OS_RFPLUGIN_H
Loading