7#include "spdlog/spdlog.h"
10#include "synapse-app-sdk/middleware/conversions.hpp"
17enum class PublisherType : uint8_t {
36 DataPublisher(
const PublisherType& publisher_type, zmq::context_t& zmq_context)
37 : publisher_type_(publisher_type),
38 zmq_context_(zmq_context),
39 publisher_socket_(zmq_context_, zmq::socket_type::pub) {}
46 publisher_socket_.close();
55 bool setup(
const std::string& endpoint) {
57 publisher_socket_.bind(endpoint);
60 }
catch (
const std::exception& e) {
61 spdlog::error(
"Failed to bind data publisher to: {}, why: {}", endpoint, e.what());
78 spdlog::warn(
"Could not publish, did you connect?");
82 const std::string kControllerZMQTopic =
"controller/output";
83 zmq::message_t topic_message(kControllerZMQTopic.begin(), kControllerZMQTopic.end());
84 const auto topic_send_ret = publisher_socket_.send(topic_message, zmq::send_flags::sndmore);
85 if (!topic_send_ret.has_value() || topic_send_ret.value() == 0) {
86 spdlog::error(
"Failed to publish topic data for controller node");
91 zmq::message_t out_message = protobuf_to_zmq_message(message);
92 if (out_message.size() == 0) {
93 spdlog::error(
"Failed to convert protobuf to zmq message for sending");
97 auto result = publisher_socket_.send(out_message, zmq::send_flags::dontwait);
98 return result.has_value();
103 PublisherType publisher_type_;
106 std::atomic<bool> connected_{
false};
109 zmq::context_t& zmq_context_;
112 zmq::socket_t publisher_socket_;
~DataPublisher()
Destructor - ensures socket is closed.
Definition data_publisher.hpp:44
bool setup(const std::string &endpoint)
Sets up the publisher with the specified endpoint.
Definition data_publisher.hpp:55
DataPublisher(const PublisherType &publisher_type, zmq::context_t &zmq_context)
Constructor for DataPublisher.
Definition data_publisher.hpp:36
bool try_publish(const T &message)
Non-blocking publish of a message.
Definition data_publisher.hpp:76