9 std::string message(data.begin(), data.end());
10 std::shared_ptr<rapidjson::Document> doc(
new rapidjson::Document);
11 doc->Parse(message.c_str());
13 if (doc->HasParseError()) {
14 ROS_ERROR(
"invalid json message: %s", message.c_str());
15 ROS_ERROR(
"error location: %ld", doc->GetErrorOffset());
17 rapidjson::GetParseError_En(doc->GetParseError()));
22 (*plugin)->Execute(doc);
27 std::shared_ptr<rapidjson::Document> doc) {
29 Writer<StringBuffer> writer(buffer);
31 std::string
s(buffer.GetString(), buffer.GetSize());
32 std::vector<uint8_t> data(
s.begin(),
s.end());
38 udp::endpoint target_socket,
46 std::make_shared<std::function<
void(std::vector<uint8_t>)>>(std::bind(
53 (*plugin)->Shutdown();
57 ROS_DEBUG(
"Succeeded in unloading all plugins");
63 return {
false, 0,
"invalid config parameters"};
66 auto callback = std::make_shared<
67 std::function<void(std::shared_ptr<rapidjson::Document>)>>(std::bind(
70 for (rapidjson::Value::ConstValueIterator itr =
config_->Begin();
72 if (!itr->HasMember(
"plugin_name"))
continue;
73 std::string plugin_name = (*itr)[
"plugin_name"].GetString();
78 std::shared_ptr<rapidjson::Document> parameter(
new rapidjson::Document);
79 parameter->CopyFrom(*itr, parameter->GetAllocator());
80 plugin->Initialize(std::move(parameter), callback);
82 ROS_DEBUG(
"plugin %s has been loaded successfully", plugin_name.c_str());
85 std::ostringstream stream;
86 stream <<
"Failed to load " << plugin_name << ex.what();
87 std::string message = stream.str();
88 char* data = (
char*)malloc(strlen(message.c_str()) + 1);
89 ROS_ERROR(
"plugin load error: %s", message.c_str());
90 strcpy(data, message.c_str());
92 return {
false, 0, (
const char*)data};
99 return {
true,
socket_->Port(),
""};
104 Component<fruit::Annotated<JsonAnnotation, PluginRouterFactory>>
106 return createComponent()
boost::shared_ptr< T > createInstance(const std::string &lookup_name)
Component< fruit::Annotated< JsonAnnotation, PluginRouterFactory > > getJsonPluginRouterComponent()
std::vector< boost::shared_ptr< skyway_plugin::SkyWayJsonPlugin > > plugins_
std::function< std::unique_ptr< Socket >(udp::endpoint, std::shared_ptr< std::function< void(std::vector< uint8_t >)> >)> SocketFactory
void observe_socket(std::vector< uint8_t > data)
pluginlib::ClassLoader< skyway_plugin::SkyWayJsonPlugin > plugin_loader_
std::unique_ptr< Socket > socket_
std::shared_ptr< rapidjson::Document > config_
void observe_plugins(std::shared_ptr< rapidjson::Document > document)
virtual PluginResult TryStart() override
JsonPluginRouter()=delete
Component< SocketFactory > getUdpSocketComponent()
udp::endpoint target_socket_
virtual uint16_t Port() override