24#ifndef GOBY_MIDDLEWARE_TOOL_PUBLISH_SUBSCRIBE_TOOL_H
25#define GOBY_MIDDLEWARE_TOOL_PUBLISH_SUBSCRIBE_TOOL_H
35#include <google/protobuf/text_format.h>
37#include "dccl/dynamic_protobuf_manager.h"
74 std::string::size_type slash_pos = spec.find(
'/');
75 if (slash_pos == std::string::npos)
81 else if (spec.find(
"protobuf.") != std::string::npos)
90 type = spec.substr(slash_pos + 1);
102template <
typename InterprocessType,
typename PublishConfig>
104 const std::string& tool_name)
118 auto pb_msg = dccl::DynamicProtobufManager::new_protobuf_message<
119 std::shared_ptr<google::protobuf::Message>>(type);
120 google::protobuf::TextFormat::Parser parser;
121 goby::util::FlexOStreamErrorCollector error_collector(cfg.value());
122 parser.RecordErrorsTo(&error_collector);
123 parser.AllowPartialMessage(
false);
124 parser.ParseFromString(cfg.value(), pb_msg.get());
131 interprocess.template publish_dynamic<
139 auto j = nlohmann::json::parse(cfg.value());
140 if (type.empty() || type ==
"nlohmann::json")
142 interprocess.template publish_dynamic<nlohmann::json>(j,
group);
173template <
typename InterprocessType,
typename SubscribeConfig>
175 std::map<
int, std::unique_ptr<goby::middleware::log::LogPlugin>>& plugins,
176 const std::string& internal_group_regex =
"")
180 if (cfg.has_scheme())
187 std::make_unique<goby::middleware::log::ProtobufPlugin>();
189 std::make_unique<goby::middleware::log::DCCLPlugin>();
191 std::make_unique<goby::middleware::log::JSONPlugin>();
193 interprocess.subscribe_regex(
194 [&cfg, &plugins, internal_group_regex](
const std::vector<unsigned char>& bytes,
int scheme,
195 const std::string& type,
198 if (!internal_group_regex.empty())
200 std::regex exclude_pattern(internal_group_regex);
201 if (std::regex_match(std::string(group), exclude_pattern) &&
202 !cfg.include_internal_groups())
207 std::string debug_text;
209 auto plugin = plugins.find(log_entry.scheme());
210 if (plugin == plugins.end())
212 debug_text = std::string(
"Message of " + std::to_string(bytes.size()) +
" bytes");
218 debug_text = plugin->second->debug_text_message(log_entry);
222 debug_text =
"Unable to parse message of " +
223 std::to_string(log_entry.data().size()) +
224 " bytes. Reason: " + e.what();
229 std::cout <<
scheme <<
" | " <<
group <<
" | " << type <<
" | "
230 << goby::time::convert<boost::posix_time::ptime>(log_entry.timestamp())
231 <<
" | " << debug_text << std::endl;
233 schemes, cfg.type_regex(), cfg.group_regex());
241template <
typename ImplementationTag,
typename PublishConfig>
253 publish_time_(
std::chrono::steady_clock::now() +
254 std::chrono::duration_cast<
std::chrono::steady_clock::duration>(
255 std::chrono::duration<double>(this->
cfg().settle_seconds())))
257 _publish_once_settled();
265 _publish_once_settled();
266 else if (std::chrono::steady_clock::now() >= quit_time_)
273 void _publish_once_settled()
275 if (std::chrono::steady_clock::now() < publish_time_)
279 std::string(
"goby ") + Traits::name +
" publish");
281 quit_time_ = std::chrono::steady_clock::now() + std::chrono::seconds(1);
284 const std::chrono::steady_clock::time_point publish_time_;
285 std::chrono::steady_clock::time_point quit_time_;
286 bool published_{
false};
294template <
typename ImplementationTag,
typename SubscribeConfig>
306 std::string(
"goby::") + Traits::name +
"::_internal.*");
311 std::map<int, std::unique_ptr<goby::middleware::log::LogPlugin>> plugins_;
void quit(int return_value=0)
Requests a clean exit.
const Config & app_cfg()
Accesses configuration object passed at launch.
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Implements an Application for a two layer middleware setup ([ intervehicle [ interprocess ] ]) based ...
InterProcessPortal & interprocess()
const Config & cfg() const
goby::util::logger::GroupSetter group(std::string n)
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
The global namespace for the Goby project.
util::FlexOstream glog
Access the Goby logger through this object.
static int from_string(const std::string &s)
Convert from a string to a marshalling scheme id.
Class for parsing and serializing a given marshalling scheme. Must be specialized for a particular sc...
Maps an ImplementationTag to the portal that implements it, so that generic code can be written again...