string_plugin_router.cpp
Go to the documentation of this file.
1 //
2 // Created by nakakura on 22/08/25.
3 //
4 
5 #include "string_plugin_router.h"
6 
7 //===== private =====
8 void StringPluginRouter::observe_socket(std::vector<uint8_t> data) {
9  std::string message(data.begin(), data.end());
10 
11  for (auto plugin = plugins_.begin(); plugin != plugins_.end(); ++plugin) {
12  (*plugin)->Execute(message);
13  }
14 }
15 
16 void StringPluginRouter::observe_plugins(std::string message) {
17  std::vector<uint8_t> data(message.begin(), message.end());
18  socket_->SendData(data);
19 }
20 
21 //===== public =====
23  std::shared_ptr<rapidjson::Document> config, udp::endpoint target_socket,
24  SocketFactory factory)
25  : plugin_loader_("skyway", "skyway_plugin::SkyWayStringPlugin"),
26  config_(std::move(config)),
27  target_socket_(target_socket) {
28  // Socketからのcallbackを与えてSocketを生成`
29  socket_ = factory(
31  std::make_shared<std::function<void(std::vector<uint8_t>)>>(std::bind(
32  &StringPluginRouter::observe_socket, this, std::placeholders::_1)));
33 }
34 
36  if (socket_) socket_->Stop();
37  for (auto plugin = plugins_.rbegin(); plugin != plugins_.rend(); ++plugin) {
38  (*plugin)->Shutdown();
39  plugin->reset();
40  }
41 
42  ROS_DEBUG("Succeeded in unloading all plugins");
43 }
44 
45 // try startにして、errorを返せるようにする
47  // plugin情報の配列を与えられていない場合は開始できない
48  if (!config_->IsArray()) {
49  return {false, 0, "invalid config parameters"};
50  }
51 
52  auto callback = std::make_shared<std::function<void(std::string)>>(std::bind(
53  &StringPluginRouter::observe_plugins, this, std::placeholders::_1));
54 
55  for (rapidjson::Value::ConstValueIterator itr = config_->Begin();
56  itr != config_->End(); ++itr) {
57  if (!itr->HasMember("plugin_name")) continue;
58  std::string plugin_name = (*itr)["plugin_name"].GetString();
59 
60  try {
62  plugin_loader_.createInstance(plugin_name.c_str());
63  std::shared_ptr<rapidjson::Document> parameter(new rapidjson::Document);
64  parameter->CopyFrom(*itr, parameter->GetAllocator());
65  plugin->Initialize(std::move(parameter), callback);
66  ROS_DEBUG("plugin %s has been loaded successfully", plugin_name.c_str());
67  plugins_.push_back(plugin);
68  } catch (pluginlib::PluginlibException& ex) {
69  // pluginがopenできなかったらここでreturnする
70  std::ostringstream stream;
71  stream << "Failed to load " << plugin_name << ex.what();
72  std::string message = stream.str();
73  char* data = (char*)malloc(strlen(message.c_str()) + 1);
74  ROS_ERROR("plugin load error: %s", message.c_str());
75  strcpy(data, message.c_str());
76  return {false, 0, (const char*)data};
77  }
78  }
79 
80  // ここでsocket startするとデータが流れ始める
81  socket_->Start();
82 
83  return {true, socket_->Port(), ""};
84 }
85 
86 uint16_t StringPluginRouter::Port() { return socket_->Port(); }
87 
88 Component<fruit::Annotated<StringAnnotation, PluginRouterFactory>>
90  return createComponent()
91  .bind<fruit::Annotated<StringAnnotation, PluginRouter>,
93  .install(getUdpSocketComponent);
94 }
StringPluginRouter::TryStart
virtual PluginResult TryStart() override
Definition: string_plugin_router.cpp:46
string_plugin_router.h
getUdpSocketComponent
Component< SocketFactory > getUdpSocketComponent()
Definition: udp_socket.cpp:87
StringPluginRouter::plugin_loader_
pluginlib::ClassLoader< skyway_plugin::SkyWayStringPlugin > plugin_loader_
Definition: string_plugin_router.h:22
StringPluginRouter::observe_plugins
void observe_plugins(std::string message)
Definition: string_plugin_router.cpp:16
boost::shared_ptr
StringPluginRouter::config_
std::shared_ptr< rapidjson::Document > config_
Definition: string_plugin_router.h:26
StringPluginRouter::factory
SocketFactory factory
Definition: string_plugin_router.h:36
StringPluginRouter::Port
virtual uint16_t Port() override
Definition: string_plugin_router.cpp:86
pluginlib::PluginlibException
getStringPluginRouterComponent
Component< fruit::Annotated< StringAnnotation, PluginRouterFactory > > getStringPluginRouterComponent()
Definition: string_plugin_router.cpp:89
ROS_DEBUG
#define ROS_DEBUG(...)
StringPluginRouter::StringPluginRouter
StringPluginRouter()=delete
Definition: string_plugin_router.cpp:22
StringPluginRouter::observe_socket
void observe_socket(std::vector< uint8_t > data)
Definition: string_plugin_router.cpp:8
SocketFactory
std::function< std::unique_ptr< Socket >(udp::endpoint, std::shared_ptr< std::function< void(std::vector< uint8_t >)> >)> SocketFactory
Definition: udp_socket.h:69
pluginlib::ClassLoader::createInstance
boost::shared_ptr< T > createInstance(const std::string &lookup_name)
std
StringPluginRouter::target_socket_
udp::endpoint target_socket_
Definition: string_plugin_router.h:24
ROS_ERROR
#define ROS_ERROR(...)
StringPluginRouter::plugins_
std::vector< boost::shared_ptr< skyway_plugin::SkyWayStringPlugin > > plugins_
Definition: string_plugin_router.h:23
StringPluginRouter::socket_
std::unique_ptr< Socket > socket_
Definition: string_plugin_router.h:25
PluginResult
Definition: plugin_router.h:14
StringPluginRouter
Definition: string_plugin_router.h:20
StringPluginRouter::~StringPluginRouter
~StringPluginRouter()
Definition: string_plugin_router.cpp:35


skyway
Author(s): Toshiya Nakakura
autogenerated on Thu Oct 26 2023 02:42:21