Skip to content

Commit

Permalink
changed from linktime to dlopen
Browse files Browse the repository at this point in the history
  • Loading branch information
skucheria committed Jul 18, 2019
1 parent 26fcfc2 commit c7347e2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
2 changes: 2 additions & 0 deletions rclcpp_components/cmake/rclcpp_components_register_node.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ macro(rclcpp_components_register_node target)
_rclcpp_components_register_package_hook()
if(WIN32)
set(_path "bin")
set(LIBRARY_NAME ${target}.dll)
else()
set(_path "lib")
set(LIBRARY_NAME lib${target}.so)
endif()
set(_RCLCPP_COMPONENTS__NODES
"${_RCLCPP_COMPONENTS__NODES}${component};${_path}/$<TARGET_FILE_NAME:${target}>\n")
Expand Down
100 changes: 89 additions & 11 deletions rclcpp_components/src/node_main.cpp.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
// // Copyright 2016 Open Source Robotics Foundation, Inc.
// //
// // Licensed under the Apache License, Version 2.0 (the "License");
// // you may not use this file except in compliance with the License.
// // You may obtain a copy of the License at
// //
// // http://www.apache.org/licenses/LICENSE-2.0
// //
// // Unless required by applicable law or agreed to in writing, software
// // distributed under the License is distributed on an "AS IS" BASIS,
// // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// // See the License for the specific language governing permissions and
// // limitations under the License.

// #include <memory>
// #include <string>
// #include <vector>

// #include "class_loader/class_loader.hpp"
// #include "rclcpp/rclcpp.hpp"
// #include "rclcpp_components/node_factory.hpp"
// #include "rclcpp_components/node_factory_template.hpp"

// #define NODE_MAIN_LOGGER_NAME "node_main"

// int main(int argc, char * argv[])
// {
// // Force flush of the stdout buffer.
// setvbuf(stdout, NULL, _IONBF, BUFSIZ);

// auto args = rclcpp::init_and_remove_ros_arguments(argc, argv);
// rclcpp::Logger logger = rclcpp::get_logger(NODE_MAIN_LOGGER_NAME);
// rclcpp::executors::SingleThreadedExecutor exec;
// rclcpp::NodeOptions options;
// options.arguments(args);
// std::vector<class_loader::ClassLoader *> loaders;
// std::vector<rclcpp_components::NodeInstanceWrapper> node_wrappers;
// std::vector<std::string> libraries = {
// // all classes from libraries linked by the linker (rather then dlopen)
// // are registered under the library_path ""
// "",
// };
// std::string className = "rclcpp_components::NodeFactoryTemplate<@CLASS_NAME@>";
// for (const auto & library : libraries) {
// RCLCPP_DEBUG(logger, "Loading library %s", library.c_str());
// auto loader = new class_loader::ClassLoader(library);
// auto classes = loader->getAvailableClasses<rclcpp_components::NodeFactory>();
// for (auto clazz : classes) {
// std::string name = clazz.c_str();
// if (!(name.compare(className))) {
// RCLCPP_DEBUG(logger, "Instantiate class %s", clazz.c_str());
// auto node_factory = loader->createInstance<rclcpp_components::NodeFactory>(clazz);
// auto wrapper = node_factory->create_node_instance(options);
// auto node = wrapper.get_node_base_interface();
// node_wrappers.push_back(wrapper);
// exec.add_node(node);
// }
// }
// loaders.push_back(loader);
// }

// exec.spin();

// for (auto wrapper : node_wrappers) {
// exec.remove_node(wrapper.get_node_base_interface());
// }
// node_wrappers.clear();
// rclcpp::shutdown();
// return 0;
// }

// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,34 +92,39 @@
#include "rclcpp_components/node_factory.hpp"
#include "rclcpp_components/node_factory_template.hpp"

#define NODE_MAIN_LOGGER_NAME "node_main"
#define NODE_MAIN_LOGGER_NAME "dlopen_composition"

int main(int argc, char * argv[])
{
// Force flush of the stdout buffer.
setvbuf(stdout, NULL, _IONBF, BUFSIZ);

auto args = rclcpp::init_and_remove_ros_arguments(argc, argv);
// if (argc < 2) {
// fprintf(stderr, "Requires at least one argument to be passed with the library to load\n");
// return 1;
// }
rclcpp::init(argc, argv);
rclcpp::Logger logger = rclcpp::get_logger(NODE_MAIN_LOGGER_NAME);
rclcpp::executors::SingleThreadedExecutor exec;
rclcpp::NodeOptions options;
options.arguments(args);
std::vector<class_loader::ClassLoader *> loaders;
std::vector<rclcpp_components::NodeInstanceWrapper> node_wrappers;
std::vector<std::string> libraries = {
// all classes from libraries linked by the linker (rather then dlopen)
// are registered under the library_path ""
"",
};

std::vector<std::string> libraries;
// for (int i = 1; i < argc; ++i) {
// libraries.push_back(argv[i]);
// }
std::string libraryName = "@LIBRARY_NAME@";
libraries.push_back(libraryName);
std::string className = "rclcpp_components::NodeFactoryTemplate<@CLASS_NAME@>";
for (const auto & library : libraries) {
RCLCPP_DEBUG(logger, "Loading library %s", library.c_str());
for (auto library : libraries) {
RCLCPP_INFO(logger, "Load library %s", library.c_str());
auto loader = new class_loader::ClassLoader(library);
auto classes = loader->getAvailableClasses<rclcpp_components::NodeFactory>();
for (auto clazz : classes) {
std::string name = clazz.c_str();
if (!(name.compare(className))) {
RCLCPP_DEBUG(logger, "Instantiate class %s", clazz.c_str());
RCLCPP_INFO(logger, "Instantiate class %s", clazz.c_str());
auto node_factory = loader->createInstance<rclcpp_components::NodeFactory>(clazz);
auto wrapper = node_factory->create_node_instance(options);
auto node = wrapper.get_node_base_interface();
Expand All @@ -65,6 +141,8 @@ int main(int argc, char * argv[])
exec.remove_node(wrapper.get_node_base_interface());
}
node_wrappers.clear();

rclcpp::shutdown();

return 0;
}

0 comments on commit c7347e2

Please sign in to comment.