diff --git a/src/adapter.hpp b/src/adapter.hpp index 5a86e17..3101ab8 100644 --- a/src/adapter.hpp +++ b/src/adapter.hpp @@ -1,5 +1,6 @@ #pragma once +#include "src/tuple.hpp" #include #include #include @@ -16,23 +17,25 @@ using namespace boost::callable_traits; -template - requires( - // Encoder return type is same as Decoder argument type, encoder and decoder have only 1 arg - std::tuple_size_v> == 1 && std::is_same_v, std::remove_cvref_t>>> && - std::tuple_size_v> == 1 && std::is_same_v, std::remove_cvref_t>>>) +template class Adapter; +template + requires(std::tuple_size_v> == 1 && // Decoder has only 1 argument + std::is_same_v, std::remove_cvref_t>>> && // Encoder return type is same with decoder argument + std::tuple_size_v> == 1 && // Encoder accepts only one argument + std::is_same_v, std::remove_cvref_t>>> // Decoder return type is same with encoder argument + ) -class Adapter : public AdapterBase>> { +class Adapter> : public AdapterBase>, CallbackRetTypeTag, CbkAargs...> { public: using encoder_type_t = Encoder; using decoder_type_t = Decoder; - using adapter_type_t = Adapter; - using callback_arg_type_t = std::tuple_element_t<0, boost::callable_traits::args_t>; - using base_t = AdapterBase; - using callback_type_t = base_t::callback_type_t; - Adapter(const std::string &name, std::pair &&fns) - : AdapterBase(name), mc_enc_(std::forward(fns.first)), mc_dec_(std::forward(fns.second)) {} + using callback_arg_type_t = std::tuple_element_t<0, args_t>; + using base_t = AdapterBase; + + Adapter(const std::string &name, std::tuple &&fns) + : AdapterBase(name), mc_enc_(std::forward(std::get<0u>(fns))), + mc_dec_(std::forward(std::get<1u>(fns))) {} inline const auto &encoder() const { return mc_enc_; } inline const auto &decoder() const { return mc_dec_; } @@ -42,5 +45,5 @@ public: private: const Encoder mc_enc_; const Decoder mc_dec_; - mutable callback_type_t m_callback_; + mutable typename base_t::callback_type_t m_callback_; }; diff --git a/src/adapter_base.hpp b/src/adapter_base.hpp index 57b02e4..de64803 100644 --- a/src/adapter_base.hpp +++ b/src/adapter_base.hpp @@ -1,10 +1,36 @@ #pragma once +#include "utils.hpp" #include +#include +#include + +using namespace boost::signals2; + +template class AdapterBase { + using cbk_ret_type_t_ = typename CallbackRetTypeTag::type; + + // If return type is not void we combine all callback ivocation results to std::vector + class CollectAllCombiner_ { + public: + using result_type = std::conditional_t, std::vector, std::false_type>; + + template result_type operator()(InputIterator first, InputIterator last) const { + result_type results; + if constexpr (!std::is_same_v) { + for (; first != last; ++first) { + results.push_back(*first); + } + } + + return results; + } + }; -template class AdapterBase { public: - using callback_type_t = boost::signals2::signal; + // Use combiner if return type is not void + using signature_t = std::conditional_t, void(const InType &, CbkAargs &&...), cbk_ret_type_t_(const InType &, CbkAargs &&...)>; + using callback_type_t = std::conditional_t, signal, signal>; AdapterBase(const std::string &name) : mc_name_(name) {} @@ -15,4 +41,3 @@ protected: private: const std::string mc_name_; }; - diff --git a/src/adapter_factory.hpp b/src/adapter_factory.hpp index f78e49f..1309de3 100644 --- a/src/adapter_factory.hpp +++ b/src/adapter_factory.hpp @@ -1,7 +1,68 @@ #pragma once -#include "adapter.hpp" +#include +#include +#include -template auto makeAdapter(const std::string &name, std::pair &&fns) { - return std::make_unique>(name, std::forward>(fns)); -} +#include "adapter.hpp" +#include "src/tuple.hpp" +using namespace boost::callable_traits; + +template class AdapterBuilder; +template class AdapterBuilderNamed; + +template <> class AdapterBuilder<> { +public: + template AdapterBuilder encodeDataBy(Encoder &&encoder) { return AdapterBuilder(std::forward(encoder)); } +}; + +template class AdapterBuilder { +public: + AdapterBuilder(Encoder &&encoder) : m_encoder_ref_(std::forward(encoder)) {} + template AdapterBuilder decodeDataBy(Decoder &&decoder) { + return AdapterBuilder(std::forward(m_encoder_ref_), std::forward(decoder)); + } + +private: + Encoder &&m_encoder_ref_; +}; + +template class AdapterBuilder { +public: + AdapterBuilder(Encoder &&encoder, Decoder &&decoder) : m_encoder_ref_(std::forward(encoder)), m_decoder_ref_(std::forward(decoder)) {} + template AdapterBuilder withCallbackSignature() { + return AdapterBuilder(std::forward(m_encoder_ref_), std::forward(m_decoder_ref_)); + } + +private: + Encoder &&m_encoder_ref_; + Decoder &&m_decoder_ref_; +}; + +template class AdapterBuilder { +public: + AdapterBuilder(Encoder &&encoder, Decoder &&decoder) : m_encoder_ref_(std::forward(encoder)), m_decoder_ref_(std::forward(decoder)) {} + AdapterBuilderNamed withName(const std::string &name) { + return AdapterBuilderNamed(std::forward(m_encoder_ref_), std::forward(m_decoder_ref_), name); + } + +private: + Encoder &&m_encoder_ref_; + Decoder &&m_decoder_ref_; +}; + +template class AdapterBuilderNamed { +public: + AdapterBuilderNamed(Encoder &&encoder, Decoder &&decoder, const std::string &name) + : m_encoder_ref_(std::forward(encoder)), m_decoder_ref_(std::forward(decoder)), mc_name_(name) {} + + auto finalize() { + return std::make_unique>, typename tp::tuple_tail>::type>>( + mc_name_, std::make_tuple(std::forward(m_encoder_ref_), std::forward(m_decoder_ref_), tag_s>{})); + } + +private: + const std::string &mc_name_; + Encoder &&m_encoder_ref_; + Decoder &&m_decoder_ref_; +}; diff --git a/src/module.hpp b/src/module.hpp index c20823b..8deac6e 100644 --- a/src/module.hpp +++ b/src/module.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "port_base.hpp" #include "tuple.hpp" diff --git a/src/port.hpp b/src/port.hpp index 2ace523..74504d1 100644 --- a/src/port.hpp +++ b/src/port.hpp @@ -5,18 +5,32 @@ #include #include #include +#include +#include +#include #include "port_base.hpp" #include "ports.hpp" +#include "src/port_types.hpp" #include "tuple.hpp" using namespace boost::callable_traits; template class Port; template + requires( + (std::is_same_v>::base_t::callback_type_t::result_type> && + ...) && + ((std::tuple_size_v> == + std::tuple_size_v>::callback_type_t::signature_type>>) && + ...)) + class Port, std::tuple> : public PortBase...>>> { public: + static constexpr auto callback_args_num = std::tuple_size_v>::callback_type_t::signature_type>>; + using this_t = Port, std::tuple>; using port_data_type_t = std::tuple_element_t<0, std::tuple...>>; + using callback_aargs_t = tp::tuple_tail>::callback_type_t::signature_type>>::type; Port(enum port_types_e pt, const std::string &name, const std::string &endpoint, zmq::context_t &zmq_ctx, std::tuple...> &&adapters, std::tuple &&args) @@ -26,39 +40,48 @@ public: mc_adapters_([&](std::index_sequence) { return std::make_tuple([&]() { using adapter_type_t = std::remove_cvref_t(adapters))>; - using adapter_input_type_t = return_type_t; + using adapter_input_type_t = std::remove_cvref_t>; + using adapter_callback_type_t = std::remove_cvref_t; - return std::make_tuple(std::get(adapters)->name(), std::hash()(std::get(adapters)->name()), - typeid(std::remove_cvref_t).hash_code(), std::forward>(std::get(adapters))); + // fmt::print("Adding callback: name: {}, namehash: {}, typehash: {}, cbk_typehash: {}, cbk_type: {}\r\n", std::get(adapters)->name(), + // std::hash()(std::get(adapters)->name()), typeid(adapter_input_type_t).hash_code(), typeid(adapter_callback_type_t).hash_code(), + // type_name()); + return std::make_tuple(std::get(adapters)->name(), std::hash()(std::get(adapters)->name()), typeid(adapter_input_type_t).hash_code(), + typeid(adapter_callback_type_t).hash_code(), std::forward>(std::get(adapters))); }.template operator()()...); }(std::make_index_sequence{})) { std::apply([&, this](auto &&...args) { init_impl_(pt, zmq_ctx, std::move(endpoint), std::forward(args)...); }, std::forward(args)); } + ~Port() override { stop(); } + void stop() const override { - this->stop__(); - mc_impl_->close(); + stop__(); + m_impl__->close(); } + inline const auto &adapters() const { return mc_adapters_; } + protected: + void stop__() const override { m_impl__->stop_source().request_stop(); } void send__(const std::string &addr, const void *data, size_t size, size_t hash) const override { const void *adapter_ptr = nullptr; tp::for_each(mc_adapters_, [&](const auto &e) { - const auto &[adapter_name, adapter_namehash, adapter_typehash, adapter] = e; + const auto &[adapter_name, adapter_namehash, adapter_typehash, adapter_cbk_typehash, adapter] = e; if (adapter_typehash == hash) { using adapter_in_type_t = std::remove_cvref_tdecoder())>>; adapter_ptr = &adapter; - typename PortImplBase::port_payload_s payload = { + typename PortImplBase>::port_payload_s payload = { .typehash = hash, - .data = adapter->encoder()(*reinterpret_cast(data)), + .data = {adapter->encoder()(*reinterpret_cast(data))}, }; msgpack::sbuffer buf; msgpack::pack(buf, payload); - mc_impl_->send(addr, buf); + m_impl__->send(addr, buf); } }); @@ -67,20 +90,21 @@ protected: } } - void *get_adapter__(const std::string &name, size_t namehash, size_t typehash) const override final { + void *get_adapter__(const std::string &name, size_t namehash, size_t typehash, size_t cbk_typehash) const override final { void *ret = nullptr; tp::for_each(mc_adapters_, [&](auto &a) { if (!ret) { - auto &[adapter_name, adapter_namehash, adapter_typehash, adapter] = a; - if (adapter_typehash == typehash && adapter_namehash == namehash) { + auto &[adapter_name, adapter_namehash, adapter_typehash, adapter_cbk_typehash, adapter] = a; + if (adapter_typehash == typehash && adapter_namehash == namehash && adapter_cbk_typehash == cbk_typehash) { ret = reinterpret_cast(adapter.get()); } } }); if (!ret) { - throw std::runtime_error(fmt::format("No adapter '{}' in port '{}'\r\n", name, this->name())); + throw std::runtime_error( + fmt::format("No such callback in adapter '{}' in port '{}' (namehash: #{}, typehash: #{}, cbk_typehash: #{})\r\n", name, this->name(), namehash, typehash, cbk_typehash)); } return ret; @@ -88,20 +112,47 @@ protected: private: using base_t_ = PortBase; - using cbk_type_t_ = std::function; + using cbk_return_type_t_ = typename std::tuple_element_t<0, std::tuple>::base_t::callback_type_t::result_type; - mutable std::tuple>...> mc_adapters_; - mutable std::unique_ptr> mc_impl_; + template class PortImplCallback_; + template class PortImplCallback_> { + public: + PortImplCallback_(const Port *port) : mc_port_(port) {} + using type_t = std::function; - void listen__(std::stop_token st) const override { mc_impl_->listen(st); } + cbk_return_type_t_ operator()(const port_data_type_t &data, size_t hash, Aargs &&...callback_args) const { + std::conditional_t, cbk_return_type_t_, std::false_type> ret; + + tp::for_each(mc_port_->mc_adapters_, [&](const auto &e) { + const auto &[adapter_name, adapter_namehash, adapter_typehash, adapter_cbk_typehash, adapter] = e; + if (adapter_typehash == hash) { + if constexpr (std::is_void_vcallback())>, decltype(adapter->decoder()(data)), Aargs &&...>>) { + adapter->callback()(adapter->decoder()(data), std::forward(callback_args)...); + } else { + ret = adapter->callback()(adapter->decoder()(data), std::forward(callback_args)...); + } + } + }); + + if constexpr (!std::is_void_v) { + return ret; + } + } + + private: + const Port *mc_port_; + }; + + mutable std::unique_ptr>> m_impl__; + mutable std::tuple>...> mc_adapters_; template void init_impl_(enum port_types_e pt, ImplArgs &&...args) const { using enum port_types_e; static constexpr auto make_null_impl_pair = []() consteval { if constexpr (port_type == UNKNOWN) { - return std::make_pair(port_type, static_cast *>(nullptr)); + return std::make_pair(port_type, static_cast> *>(nullptr)); } else { - return std::make_pair(port_type, static_cast *>(nullptr)); + return std::make_pair(port_type, static_cast> *>(nullptr)); } }; @@ -116,20 +167,8 @@ private: if (type == pt) { using impl_type_t = std::remove_pointer_t; - if constexpr (std::is_constructible_v) { - mc_impl_ = std::make_unique( - // Args - std::forward(args)..., - - // Callback - [this](const port_data_type_t &data, size_t hash) { - tp::for_each(mc_adapters_, [&](const auto &e) { - const auto &[adapter_name, adapter_namehash, adapter_typehash, adapter] = e; - if (adapter_typehash == hash) { - adapter->callback()(adapter->decoder()(data)); - } - }); - }); + if constexpr (std::is_constructible_v &&>) { + m_impl__ = std::make_unique(this, std::forward(args)..., PortImplCallback_(this)); } } }); diff --git a/src/port_base.hpp b/src/port_base.hpp index 760c909..98812dc 100644 --- a/src/port_base.hpp +++ b/src/port_base.hpp @@ -1,8 +1,12 @@ #pragma once -#include -#include +#include "src/tuple.hpp" +#include +#include #include +#include + +#define ZMQ_BUILD_DRAFT_API #include #define FMT_HEADER_ONLY @@ -13,6 +17,8 @@ #include "port_types.hpp" #include "utils.hpp" +using namespace boost::callable_traits; + template class PortBase { class AddressedPort_ { public: @@ -31,6 +37,8 @@ public: PortBase(enum port_types_e port_type, const std::string &name, const std::string &endpoint, zmq::context_t &zmq_ctx) : mc_type_(port_type), mc_name_(name), mc_endpoint_(endpoint), mc_name_hash_(std::hash()(name)), mc_endpoint_hash_(std::hash()(endpoint)) {} + virtual ~PortBase() = default; + inline const auto &type() const { return mc_type_; } inline const auto &name() const { return mc_name_; } inline const auto &endpoint() const { return mc_endpoint_; } @@ -44,23 +52,38 @@ public: return *this; } - void listen() const { listen__(m_ss_.get_token()); }; + // void listen() const { listen__(m_ss_.get_token()); }; virtual void stop() const = 0; - template auto &callback(const std::string &name) const { - return (*static_cast *>(get_adapter__(name, std::hash()(name), typeid(std::remove_cvref_t).hash_code()))).callback(); + template auto &callback(const std::string &name) const { + return GetCallbackHelper_>::type>(this).template operator()(name); } protected: - void stop__() const { m_ss_.request_stop(); } - + virtual void stop__() const = 0; virtual void send__(const std::string &addr, const void *data, size_t size, size_t type_hash) const = 0; - virtual void listen__(std::stop_token) const = 0; - virtual void *get_adapter__(const std::string &name, size_t namehash, size_t typehash) const = 0; + virtual void *get_adapter__(const std::string &name, size_t namehash, size_t typehash, size_t cbk_typehash) const = 0; private: const enum port_types_e mc_type_; const std::string mc_name_, mc_endpoint_; const size_t mc_name_hash_, mc_endpoint_hash_; - mutable std::stop_source m_ss_; + + template class GetCallbackHelper_; + template class GetCallbackHelper_> { + public: + GetCallbackHelper_(const PortBase *port) : mc_port_(port) {} + + template auto &operator()(const std::string &name) const { + using ret_type_t = std::remove_cvref_t>>; + using arg_type_t = std::tuple_element_t<0, args_t>>; + return (*static_cast, Aargs...> *>( + mc_port_->get_adapter__(name, std::hash()(name), typeid(arg_type_t).hash_code(), + typeid(typename AdapterBase, Aargs...>::callback_type_t).hash_code()))) + .callback(); + } + + private: + const PortBase *mc_port_; + }; }; diff --git a/src/port_dealer_impl.hpp b/src/port_dealer_impl.hpp index 012632a..5c44d47 100644 --- a/src/port_dealer_impl.hpp +++ b/src/port_dealer_impl.hpp @@ -7,16 +7,18 @@ #include #include -template class PortImpl : public PortImplBase { +template class PortImpl : public PortImplBase { public: - PortImpl(zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) : PortImplBase(zmq_ctx, endpoint, std::forward(callback)) {} - void listen(std::stop_token st) const override { + PortImpl(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) + : PortImplBase(port, zmq_ctx, endpoint, std::forward(callback)) {} + + // Send to socket depending on implementation + void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; + +private: + void listen__(std::stop_token st) const override { while (!st.stop_requested()) { std::this_thread::sleep_for(std::chrono::milliseconds(100u)); } } - - // Send to socket depending on implementation - void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; }; - diff --git a/src/port_impl_base.hpp b/src/port_impl_base.hpp index 375c964..92aa41f 100644 --- a/src/port_impl_base.hpp +++ b/src/port_impl_base.hpp @@ -4,23 +4,24 @@ #include #include #include + +#define ZMQ_BUILD_DRAFT_API #include -template class PortImplBase { +template class PortImplBase { using port_data_type_t_ = std::remove_cvref_t>>; public: struct port_payload_s { size_t typehash; - port_data_type_t_ data; + std::vector data; MSGPACK_DEFINE(typehash, data); }; - PortImplBase(zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) : mc_endpoint__(endpoint), m_ctx__(zmq_ctx), mc_cbk__(callback) {} - + PortImplBase(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) + : mc_endpoint__(endpoint), m_ctx__(zmq_ctx), mc_cbk__(callback), mc_port__(port) {} virtual ~PortImplBase() = default; - virtual void listen(std::stop_token st) const = 0; virtual void send(const std::string &addr, const msgpack::sbuffer &data) const = 0; void close() { m_sock__.close(); @@ -30,11 +31,18 @@ public: } }; + inline auto &stop_source() { return m_ss_; } + protected: + static constexpr auto sc_recv_timeout_ms__ = 1'000u; mutable std::future m_listener_thread__; mutable zmq::socket_t m_sock__; zmq::context_t &m_ctx__; const std::string mc_endpoint__; const Callback mc_cbk__; + const Port *mc_port__; + virtual void listen__(std::stop_token st) const = 0; +private: + mutable std::stop_source m_ss_; }; diff --git a/src/port_pair_impl.hpp b/src/port_pair_impl.hpp index be1e06b..b49b224 100644 --- a/src/port_pair_impl.hpp +++ b/src/port_pair_impl.hpp @@ -7,15 +7,18 @@ #include #include -template class PortImpl : public PortImplBase { +template class PortImpl : public PortImplBase { public: - PortImpl(zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) : PortImplBase(zmq_ctx, endpoint, std::forward(callback)) {} - void listen(std::stop_token st) const override { + PortImpl(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) + : PortImplBase(port, zmq_ctx, endpoint, std::forward(callback)) {} + + // Send to socket depending on implementation + void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; + +private: + void listen__(std::stop_token st) const override { while (!st.stop_requested()) { std::this_thread::sleep_for(std::chrono::milliseconds(100u)); } } - - // Send to socket depending on implementation - void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; }; diff --git a/src/port_publisher_impl.hpp b/src/port_publisher_impl.hpp index 9e40d5b..9b29c81 100644 --- a/src/port_publisher_impl.hpp +++ b/src/port_publisher_impl.hpp @@ -7,17 +7,16 @@ #include #include -template class PortImpl final : public PortImplBase { +template class PortImpl final : public PortImplBase { public: - PortImpl(zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) : PortImplBase(zmq_ctx, endpoint, std::forward(callback)) { + PortImpl(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) + : PortImplBase(port, zmq_ctx, endpoint, std::forward(callback)) { this->m_sock__ = zmq::socket_t(this->m_ctx__, zmq::socket_type::pub); this->m_sock__.bind(this->mc_endpoint__); } ~PortImpl() override {} - void listen(std::stop_token st) const override { throw std::runtime_error("Can't listen on PUBLISHER pattern socket"); } - // Send to socket depending on implementation void send(const std::string &addr, const msgpack::sbuffer &data) const override { try { @@ -27,4 +26,7 @@ public: fmt::print("ZMQ error: {1} ({0})\r\n", err.num(), err.what()); } }; + +private: + void listen__(std::stop_token st) const override { throw std::runtime_error("Can't listen on PUBLISHER pattern socket"); } }; diff --git a/src/port_pull_impl.hpp b/src/port_pull_impl.hpp index cec7730..a2a391f 100644 --- a/src/port_pull_impl.hpp +++ b/src/port_pull_impl.hpp @@ -2,20 +2,58 @@ #include "port_impl_base.hpp" #include "port_types.hpp" +#include +#include #define FMT_HEADER_ONLY #include #include -template class PortImpl : public PortImplBase { +template class PortImpl : public PortImplBase { public: - PortImpl(zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) : PortImplBase(zmq_ctx, endpoint, std::forward(callback)) {} - void listen(std::stop_token st) const override { - while (!st.stop_requested()) { - std::this_thread::sleep_for(std::chrono::milliseconds(100u)); - } + using base_t = PortImplBase; + PortImpl(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) + : PortImplBase(port, zmq_ctx, endpoint, std::forward(callback)) { + this->m_sock__ = zmq::socket_t(this->m_ctx__, zmq::socket_type::pull); + this->m_sock__.connect(this->mc_endpoint__); + + listen__(this->stop_source().get_token()); } // Send to socket depending on implementation - void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; + void send(const std::string &addr, const msgpack::sbuffer &data) const override { throw std::runtime_error("Can't send anything on PULL pattern socket"); }; + +private: + void listen__(std::stop_token st) const override { + this->m_listener_thread__ = std::async( + std::launch::async, + [this](std::stop_token st) { + zmq::poller_t poller; + poller.add(this->m_sock__, zmq::event_flags::pollin); + + while (!st.stop_requested()) { + std::vector> events(1u); + size_t num_events = poller.wait_all(events, std::chrono::milliseconds(base_t::sc_recv_timeout_ms__)); + + for (int32_t i = 0; i < num_events; ++i) { + zmq::message_t msg; + + this->m_sock__.recv(msg, zmq::recv_flags::dontwait).and_then([&](const auto &res) { + typename base_t::port_payload_s payload; + + msgpack::sbuffer buf; + buf.write(reinterpret_cast(msg.data()), msg.size()); + const auto &[typehash, batch] = msgpack::unpack(buf.data(), buf.size()).get().convert(payload); + + for (const auto &data : batch) { + this->mc_cbk__(data, typehash); + } + + return std::optional(res); + }); + } + } + }, + st); + } }; diff --git a/src/port_push_impl.hpp b/src/port_push_impl.hpp index dd0c794..61c2f89 100644 --- a/src/port_push_impl.hpp +++ b/src/port_push_impl.hpp @@ -7,15 +7,23 @@ #include #include -template class PortImpl : public PortImplBase { +template class PortImpl : public PortImplBase { public: - PortImpl(zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) : PortImplBase(zmq_ctx, endpoint, std::forward(callback)) {} - void listen(std::stop_token st) const override { - while (!st.stop_requested()) { - std::this_thread::sleep_for(std::chrono::milliseconds(100u)); - } + PortImpl(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) + : PortImplBase(port, zmq_ctx, endpoint, std::forward(callback)) { + this->m_sock__ = zmq::socket_t(this->m_ctx__, zmq::socket_type::push); + this->m_sock__.bind(this->mc_endpoint__); } // Send to socket depending on implementation - void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; + void send(const std::string &addr, const msgpack::sbuffer &data) const override { + try { + this->m_sock__.send(zmq::message_t(data.data(), data.size()), zmq::send_flags::none); + } catch (const zmq::error_t &err) { + fmt::print("ZMQ error: {1} ({0})\r\n", err.num(), err.what()); + } + }; + +private: + void listen__(std::stop_token st) const override { throw std::runtime_error("Can't listen on PUSH pattern socket"); } }; diff --git a/src/port_rep_impl.hpp b/src/port_rep_impl.hpp index 60a018f..303d5f6 100644 --- a/src/port_rep_impl.hpp +++ b/src/port_rep_impl.hpp @@ -1,22 +1,90 @@ #pragma once -#include "port_impl_base.hpp" -#include "port_types.hpp" +#include +#include +#include +#include #define FMT_HEADER_ONLY #include #include -template class PortImpl : public PortImplBase { +#include "port_impl_base.hpp" +#include "port_types.hpp" +#include "tuple.hpp" + +template class PortImpl : public PortImplBase { public: - PortImpl(zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) : PortImplBase(zmq_ctx, endpoint, std::forward(callback)) {} - void listen(std::stop_token st) const override { - while (!st.stop_requested()) { - std::this_thread::sleep_for(std::chrono::milliseconds(100u)); - } + using base_t = PortImplBase; + PortImpl(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) + : PortImplBase(port, zmq_ctx, endpoint, std::forward(callback)) { + this->m_sock__ = zmq::socket_t(this->m_ctx__, zmq::socket_type::rep); + this->m_sock__.bind(this->mc_endpoint__); + + listen__(this->stop_source().get_token()); } // Send to socket depending on implementation - void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; -}; + void send(const std::string &addr, const msgpack::sbuffer &data) const override { throw std::runtime_error("Can't send anything on REPLY pattern socket"); }; +private: + void listen__(std::stop_token st) const override { + this->m_listener_thread__ = std::async( + std::launch::async, + [this](std::stop_token st) { + zmq::poller_t poller; + poller.add(this->m_sock__, zmq::event_flags::pollin); + + while (!st.stop_requested()) { + std::vector> events(1u); + size_t num_events = poller.wait_all(events, std::chrono::milliseconds(base_t::sc_recv_timeout_ms__)); + + for (int32_t i = 0; i < num_events; ++i) { + zmq::message_t msg; + + this->m_sock__.recv(msg, zmq::recv_flags::dontwait).and_then([&](const auto &res) { + typename base_t::port_payload_s payload; + + msgpack::sbuffer buf; + buf.write(reinterpret_cast(msg.data()), msg.size()); + const auto &[typehash, batch] = msgpack::unpack(buf.data(), buf.size()).get().convert(payload); + + for (const auto &data : batch) { + if constexpr (!std::is_void_vmc_cbk__)>>>) { + auto reply_data = this->mc_cbk__(data, typehash); + for (const auto &d : reply_data) { + using adapter_in_type_t = std::remove_cvref_t; + + size_t typehash = typeid(adapter_in_type_t).hash_code(); + + tp::for_each(this->mc_port__->adapters(), [&](const auto &e) { + const auto &[adapter_name, adapter_namehash, adapter_typehash, adapter_cbk_typehash, adapter] = e; + if constexpr (std::is_same_vencoder())>>>, + adapter_in_type_t>) { + if (adapter_typehash == typehash) { + + typename base_t::port_payload_s payload = { + .typehash = typeid(d).hash_code(), + .data = {adapter->encoder()(d)}, + }; + + msgpack::sbuffer buf; + msgpack::pack(buf, payload); + this->m_sock__.send(zmq::message_t(buf.data(), buf.size()), zmq::send_flags::none); + } + } + }); + } + } else { + std::runtime_error("Callback of REPLY pattern socket should return non-void value"); + } + } + + return std::optional(res); + }); + } + } + }, + st); + } +}; diff --git a/src/port_req_impl.hpp b/src/port_req_impl.hpp index 2a505d1..3ed2651 100644 --- a/src/port_req_impl.hpp +++ b/src/port_req_impl.hpp @@ -7,16 +7,38 @@ #include #include -template class PortImpl : public PortImplBase { +template class PortImpl : public PortImplBase { public: - PortImpl(zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) : PortImplBase(zmq_ctx, endpoint, std::forward(callback)) {} - void listen(std::stop_token st) const override { - while (!st.stop_requested()) { - std::this_thread::sleep_for(std::chrono::milliseconds(100u)); - } + using base_t = PortImplBase; + PortImpl(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) + : PortImplBase(port, zmq_ctx, endpoint, std::forward(callback)) { + this->m_sock__ = zmq::socket_t(this->m_ctx__, zmq::socket_type::req); + this->m_sock__.connect(this->mc_endpoint__); } - // Send to socket depending on implementation - void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; -}; + void send(const std::string &addr, const msgpack::sbuffer &data) const override { + try { + zmq::message_t reply; + this->m_sock__.send(zmq::message_t(data.data(), data.size()), zmq::send_flags::none); + this->m_sock__.recv(reply, zmq::recv_flags::none).and_then([&](const auto &res) { + typename base_t::port_payload_s payload; + + msgpack::sbuffer buf; + buf.write(reinterpret_cast(reply.data()), reply.size()); + const auto &[typehash, batch] = msgpack::unpack(buf.data(), buf.size()).get().convert(payload); + + for (const auto &data : batch) { + this->mc_cbk__(data, typehash); + } + + return std::optional(res); + }); + } catch (const zmq::error_t &err) { + fmt::print("ZMQ error: {1} ({0})\r\n", err.num(), err.what()); + } + }; + +private: + void listen__(std::stop_token st) const override { throw std::runtime_error("Can't listen on REQUEST pattern socket"); } +}; diff --git a/src/port_router_impl.hpp b/src/port_router_impl.hpp index 4c5e431..54471d8 100644 --- a/src/port_router_impl.hpp +++ b/src/port_router_impl.hpp @@ -7,16 +7,18 @@ #include #include -template class PortImpl : public PortImplBase { +template class PortImpl : public PortImplBase { public: - PortImpl(zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) : PortImplBase(zmq_ctx, endpoint, std::forward(callback)) {} - void listen(std::stop_token st) const override { + PortImpl(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, Callback &&callback) + : PortImplBase(port, zmq_ctx, endpoint, std::forward(callback)) {} + + // Send to socket depending on implementation + void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; + +private: + void listen__(std::stop_token st) const override { while (!st.stop_requested()) { std::this_thread::sleep_for(std::chrono::milliseconds(100u)); } } - - // Send to socket depending on implementation - void send(const std::string &addr, const msgpack::sbuffer &data) const override {}; }; - diff --git a/src/port_subscriber_impl.hpp b/src/port_subscriber_impl.hpp index 6d23d79..fe201f9 100644 --- a/src/port_subscriber_impl.hpp +++ b/src/port_subscriber_impl.hpp @@ -2,48 +2,30 @@ #include "port_impl_base.hpp" #include "port_types.hpp" +#include +#include +#include +#include +#include +#include #define FMT_HEADER_ONLY #include #include -template class PortImpl : public PortImplBase { +template class PortImpl : public PortImplBase { public: - using base_t = PortImplBase; - PortImpl(zmq::context_t &zmq_ctx, const std::string &endpoint, const std::list &topics, Callback &&callback) - : PortImplBase(zmq_ctx, endpoint, std::forward(callback)), mc_topics_(topics) { + using base_t = PortImplBase; + PortImpl(const Port *port, zmq::context_t &zmq_ctx, const std::string &endpoint, const std::list &topics, Callback &&callback) + : PortImplBase(port, zmq_ctx, endpoint, std::forward(callback)), mc_topics_(topics) { this->m_sock__ = zmq::socket_t(this->m_ctx__, zmq::socket_type::sub); this->m_sock__.connect(this->mc_endpoint__); for (const auto &topic : mc_topics_) { this->m_sock__.set(zmq::sockopt::subscribe, topic); } - } - void listen(std::stop_token st) const override { - this->m_listener_thread__ = std::async( - std::launch::async, - [this](std::stop_token st) { - while (!st.stop_requested()) { - zmq::message_t msg; - - this->m_sock__.recv(msg, zmq::recv_flags::dontwait).and_then([&](const auto &res) { - this->m_sock__.recv(msg).and_then([&](const auto &res) { - typename base_t::port_payload_s payload; - - msgpack::sbuffer buf; - buf.write(reinterpret_cast(msg.data()), msg.size()); - const auto [typehash, data] = msgpack::unpack(buf.data(), buf.size()).get().convert(payload); - - this->mc_cbk__(data, typehash); - return std::optional(res); - }); - - return std::optional(res); - }); - } - }, - st); + listen__(this->stop_source().get_token()); } // Send to socket depending on implementation @@ -51,4 +33,43 @@ public: private: const std::list mc_topics_; + + void listen__(std::stop_token st) const override { + this->m_listener_thread__ = std::async( + std::launch::async, + [this](std::stop_token st) { + zmq::poller_t poller; + poller.add(this->m_sock__, zmq::event_flags::pollin); + + while (!st.stop_requested()) { + std::vector> events(1u); + size_t num_events = poller.wait_all(events, std::chrono::milliseconds(base_t::sc_recv_timeout_ms__)); + + for (int32_t i = 0; i < num_events; ++i) { + zmq::message_t msg; + + this->m_sock__.recv(msg, zmq::recv_flags::dontwait).and_then([&](const auto &res) { + std::string envelope = std::string(static_cast(msg.data()), msg.size()); + + this->m_sock__.recv(msg).and_then([&](const auto &res) { + typename base_t::port_payload_s payload; + + msgpack::sbuffer buf; + buf.write(reinterpret_cast(msg.data()), msg.size()); + const auto &[typehash, batch] = msgpack::unpack(buf.data(), buf.size()).get().convert(payload); + + for (const auto &data : batch) { + this->mc_cbk__(data, typehash, envelope); + } + + return std::optional(res); + }); + + return std::optional(res); + }); + } + } + }, + st); + } }; diff --git a/src/templates.cpp b/src/templates.cpp index 76237fe..f252aa1 100644 --- a/src/templates.cpp +++ b/src/templates.cpp @@ -1,18 +1,17 @@ #include -#include +#include #include #include #include #include #include -#include - #define FMT_HEADER_ONLY #include #include #include "adapter_factory.hpp" +#include "codecs.hpp" #include "module_factory.hpp" #include "port_factory.hpp" @@ -21,19 +20,22 @@ using env_data_type_t = std::vector; void subscriber_entry(int32_t argc, char **argv, char **envp, const std::unordered_map *> &ports) { const auto &subscriber = *ports.at("subscriber_port"); // Get subscriber port - auto &int_cbk = subscriber.callback("int-vector-int"); - auto &string_cbk = subscriber.callback("string-vector-string"); - auto &double_cbk = subscriber.callback("double-vector-double"); + auto &int_cbk = subscriber.callback("int-vector-int"); + auto &string_cbk = subscriber.callback("string-vector-string"); + auto &double_cbk = subscriber.callback("double-vector-double"); // Connect callbacks - int_cbk.connect([](const int32_t &i) -> void { fmt::print("SUBSCRIBER: got data: {} of integer type\r\n", i); }); - string_cbk.connect([](const std::string &s) -> void { fmt::print("SUBSCRIBER: got data: {} of string type\r\n", s); }); - double_cbk.connect([](const double &d) -> void { fmt::print("SUBSCRIBER: got data: {} of double type\r\n", d); }); + int_cbk.connect([](const int32_t &i, const std::string &addr) { + fmt::print("SUBSCRIBER socket: got data: {} of {} type from address: {}\r\n", i, type_name>(), addr); + }); - // Listen for 1 second - subscriber.listen(); - std::this_thread::sleep_for(std::chrono::milliseconds(1000u)); - subscriber.stop(); + string_cbk.connect([](const std::string &s, const std::string &addr) { + fmt::print("SUBSCRIBER socket: got data: {} of {} type from address: {}\r\n", s, type_name>(), addr); + }); + + double_cbk.connect([](const double &d, const std::string &addr) { + fmt::print("SUBSCRIBER socket: got data: {} of {} type from address: {}\r\n", d, type_name>(), addr); + }); } // Publisher module entrypoint @@ -47,67 +49,126 @@ void publisher_entry(int32_t argc, char **argv, char **envp, const std::unordere publisher["topic3"] << 1 << 2 << double{3.f} << std::string("test"); } -struct codecs_s { - struct encoders_s { - static inline const auto from_int = [](const int32_t &i) -> env_data_type_t { - auto str = std::to_string(i); // Convert to string first - return {str.begin(), str.end()}; // String to byte array - }; +// Publisher module entrypoint +void pusher_entry(int32_t argc, char **argv, char **envp, const std::unordered_map *> &ports) { + const auto &publisher = *ports.at("push_port"); // Get publisher port + publisher << 1 << 2 << double{3.f} << std::string("test"); +} - static inline const auto from_string = [](const std::string &s) -> env_data_type_t { return {s.begin(), s.end()}; }; - static inline const auto from_double = [](const double &d) -> env_data_type_t { - auto str = std::to_string(d); - return {str.begin(), str.end()}; - }; - } encoders; +void puller_entry(int32_t argc, char **argv, char **envp, const std::unordered_map *> &ports) { + const auto &puller = *ports.at("pull_port"); // Get subscriber port - struct decoders_s { - static inline const auto to_int = [](const env_data_type_t &s) -> int32_t { - int32_t ret; - auto str = std::string(s.begin(), s.end()); - if (std::from_chars(str.c_str(), str.c_str() + str.size(), ret).ec == std::errc{}) { - return ret; - } + auto &int_cbk = puller.callback("int-vector-int"); + auto &string_cbk = puller.callback("string-vector-string"); + auto &double_cbk = puller.callback("double-vector-double"); - throw std::runtime_error(fmt::format("Invalid convert from {} to integer type", str)); - }; // Byte array to int + // Connect callbacks + int_cbk.connect([](const int32_t &i) { fmt::print("PULL socket: got data: {} of {} type\r\n", i, type_name>()); }); + string_cbk.connect([](const std::string &s) { fmt::print("PULL socket: got data: {} of {} type\r\n", s, type_name>()); }); + double_cbk.connect([](const double &d) { fmt::print("PULL socket: got data: {} of {} type\r\n", d, type_name>()); }); +} - static inline const auto to_string = [](const env_data_type_t &i) -> std::string { return std::string(i.begin(), i.end()); }; - static inline const auto to_double = [](const env_data_type_t &s) -> double { - double ret; - auto str = std::string(s.begin(), s.end()); - if (std::from_chars(str.c_str(), str.c_str() + str.size(), ret).ec == std::errc{}) { - return ret; - } +void req_entry(int32_t argc, char **argv, char **envp, const std::unordered_map *> &ports) { + const auto &req = *ports.at("req_port"); // Get publisher port + // static_assert(std::is_same_v, void>, ""); + auto &int_cbk = req.callback("int-vector-int"); + auto &string_cbk = req.callback("string-vector-string"); + auto &double_cbk = req.callback("double-vector-double"); - throw std::runtime_error(fmt::format("Invalid convert from {} to double type", str)); - }; - } decoders; -} codecs; + // Connect callbacks + int_cbk.connect([](const int32_t &i) { fmt::print("REQUEST socket: got data: {} of {} type\r\n", i, type_name>()); }); + string_cbk.connect([](const std::string &s) { fmt::print("REQUEST socket: got data: {} of {} type\r\n", s, type_name>()); }); + double_cbk.connect([](const double &d) { fmt::print("REQUEST socket: got data: {} of {} type\r\n", d, type_name>()); }); + + req << 1 << 2 << double{3.f} << std::string("test"); +} + +void rep_entry(int32_t argc, char **argv, char **envp, const std::unordered_map *> &ports) { + const auto &rep = *ports.at("rep_port"); // Get subscriber port + + auto &int_cbk = rep.callback("int-vector-int"); + auto &string_cbk = rep.callback("string-vector-string"); + auto &double_cbk = rep.callback("double-vector-double"); + + // Connect callbacks + int_cbk.connect([](const int32_t &i) -> std::string { + fmt::print("REPLY socket: got data: {} of {} type\r\n", i, type_name>()); + // Handle data ... + return fmt::format("'Handle request: {} of type: {}'", i, type_name>()); + }); + + string_cbk.connect([](const std::string &s) -> std::string { + fmt::print("REPLY socket: got data: {} of {} type\r\n", s, type_name>()); + // Handle data ... + return fmt::format("'Handle request: {} of type: {}'", s, type_name>()); + }); + + double_cbk.connect([](const double &d) -> std::string { + fmt::print("REPLY socket: got data: {} of {} type\r\n", d, type_name>()); + // Handle data ... + return fmt::format("'Handle request: {} of type: {}'", d, type_name>()); + }); +} int main(int argc, char *argv[], char *envp[]) { using enum port_types_e; + codecs_s codecs; zmq::context_t zmq_ctx; // Use common context because both modules have in-process ports, but working in different threads // Make module that contains only 1 port working on PUBLISHER pattern + // TODO: merge all builders to one complex "Module" builder auto publisher_module = makeModule(argc, argv, envp, "publisher_module", zmq_ctx, std::tuple{ - makePort(PUB, "publisher_port", "inproc://point" /* This port will publish messages here */, zmq_ctx, + makePort(PUB, "publisher_port", "inproc://PUB-SUB" /* This port will publish messages here */, zmq_ctx, std::tuple{ - makeAdapter("int-vector-int", std::pair{codecs.encoders.from_int, codecs.decoders.to_int}), - makeAdapter("string-vector-string", std::pair{codecs.encoders.from_string, codecs.decoders.to_string}), - makeAdapter("double-vector-double", std::pair{codecs.encoders.from_double, codecs.decoders.to_double}), + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_int) + .decodeDataBy(&codecs.decoders.to_int) + .withCallbackSignature() + .withName("int-vector-int") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_string) + .decodeDataBy(&codecs.decoders.to_string) + .withCallbackSignature() + .withName("string-vector-string") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_double) + .decodeDataBy(&codecs.decoders.to_double) + .withCallbackSignature() + .withName("double-vector-double") + .finalize(), }), }); // Make module that contains only 1 port working on SUBSCRIBER pattern auto subscriber_module = makeModule(argc, argv, envp, "subscriber_module", zmq_ctx, std::tuple{ - makePort(SUB, "subscriber_port", "inproc://point" /* this port will read data here */, zmq_ctx, + makePort(SUB, "subscriber_port", "inproc://PUB-SUB" /* this port will read data here */, zmq_ctx, std::tuple{ - makeAdapter("int-vector-int", std::pair{codecs.encoders.from_int, codecs.decoders.to_int}), - makeAdapter("string-vector-string", std::pair{codecs.encoders.from_string, codecs.decoders.to_string}), - makeAdapter("double-vector-double", std::pair{codecs.encoders.from_double, codecs.decoders.to_double}), + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_int) + .decodeDataBy(&codecs.decoders.to_int) + .withCallbackSignature() + .withName("int-vector-int") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_string) + .decodeDataBy(&codecs.decoders.to_string) + .withCallbackSignature() + .withName("string-vector-string") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_double) + .decodeDataBy(&codecs.decoders.to_double) + .withCallbackSignature() + .withName("double-vector-double") + .finalize(), }, // This type of port requires arguments - topics to subscribe @@ -117,7 +178,129 @@ int main(int argc, char *argv[], char *envp[]) { }), }); - publisher_module->run(publisher_entry); // Publish data + auto pusher_module = makeModule(argc, argv, envp, "pusher_module", zmq_ctx, + std::tuple{ + makePort(PUSH, "push_port", "inproc://PUSH-PULL" /* This port will publish messages here */, zmq_ctx, + std::tuple{ + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_int) + .decodeDataBy(&codecs.decoders.to_int) + .withCallbackSignature() + .withName("int-vector-int") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_string) + .decodeDataBy(&codecs.decoders.to_string) + .withCallbackSignature() + .withName("string-vector-string") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_double) + .decodeDataBy(&codecs.decoders.to_double) + .withCallbackSignature() + .withName("double-vector-double") + .finalize(), + }), + }); + + auto puller_module = makeModule(argc, argv, envp, "puller_module", zmq_ctx, + std::tuple{ + makePort(PULL, "pull_port", "inproc://PUSH-PULL" /* This port will publish messages here */, zmq_ctx, + std::tuple{ + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_int) + .decodeDataBy(&codecs.decoders.to_int) + .withCallbackSignature() + .withName("int-vector-int") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_string) + .decodeDataBy(&codecs.decoders.to_string) + .withCallbackSignature() + .withName("string-vector-string") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_double) + .decodeDataBy(&codecs.decoders.to_double) + .withCallbackSignature() + .withName("double-vector-double") + .finalize(), + }), + }); + + auto req_module = makeModule(argc, argv, envp, "req_module", zmq_ctx, + std::tuple{ + makePort(REQ, "req_port", "inproc://REQ-REP" /* This port will publish messages here */, zmq_ctx, + std::tuple{ + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_int) + .decodeDataBy(&codecs.decoders.to_int) + .withCallbackSignature() + .withName("int-vector-int") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_string) + .decodeDataBy(&codecs.decoders.to_string) + .withCallbackSignature() + .withName("string-vector-string") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_double) + .decodeDataBy(&codecs.decoders.to_double) + .withCallbackSignature() + .withName("double-vector-double") + .finalize(), + }), + }); + + auto rep_module = makeModule(argc, argv, envp, "rep_module", zmq_ctx, + std::tuple{ + makePort(REP, "rep_port", "inproc://REQ-REP" /* This port will publish messages here */, zmq_ctx, + std::tuple{ + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_int) + .decodeDataBy(&codecs.decoders.to_int) + .withCallbackSignature() + .withName("int-vector-int") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_string) + .decodeDataBy(&codecs.decoders.to_string) + .withCallbackSignature() + .withName("string-vector-string") + .finalize(), + + AdapterBuilder() + .encodeDataBy(&codecs.encoders.from_double) + .decodeDataBy(&codecs.decoders.to_double) + .withCallbackSignature() + .withName("double-vector-double") + .finalize(), + }), + }); + + fmt::print("\r\nPUB-SUB test:\r\n"); subscriber_module->run(subscriber_entry); // Subscribe and get data + publisher_module->run(publisher_entry); // Publish data + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + fmt::print("\r\nPUSH-PULL test:\r\n"); + pusher_module->run(pusher_entry); + puller_module->run(puller_entry); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + fmt::print("\r\nREQ-REP test:\r\n"); + rep_module->run(rep_entry); + req_module->run(req_entry); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + fmt::print("DONE!\r\n"); + return 0; } diff --git a/src/tuple.hpp b/src/tuple.hpp index fa90908..5f0b8f6 100644 --- a/src/tuple.hpp +++ b/src/tuple.hpp @@ -117,4 +117,10 @@ template auto tuple_zip(Head &&head, Tail &&...tail) { return detail::tuple_zip_impl(std::forward(head), std::forward(tail)..., std::make_index_sequence>>()); } + +// TAIL +template struct tuple_tail; +template struct tuple_tail> { + using type = std::tuple; +}; // }; // namespace tp diff --git a/src/utils.hpp b/src/utils.hpp index 40b13ae..ace5292 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -2,6 +2,9 @@ #include +template class Template> struct is_specialization : std::false_type {}; +template