From 80dd8500c358641b1c11f6012e6d708b4ad74f84 Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nwpope@utu.fi> Date: Tue, 9 Apr 2019 09:48:14 +0300 Subject: [PATCH] Add broadcast to universe --- net/cpp/include/ftl/net/universe.hpp | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/net/cpp/include/ftl/net/universe.hpp b/net/cpp/include/ftl/net/universe.hpp index a60785654..5e2584f94 100644 --- a/net/cpp/include/ftl/net/universe.hpp +++ b/net/cpp/include/ftl/net/universe.hpp @@ -58,6 +58,14 @@ class Universe { template <typename F> void bind(const std::string &name, F func); + /** + * Subscribe a function to a resource. The subscribed function is + * triggered whenever that resource is published to. It is akin to + * RPC broadcast (no return value) to a subgroup of peers. + */ + template <typename F> + bool subscribe(const std::string &res, F func); + /** * Send a non-blocking RPC call with no return value to all connected * peers. @@ -65,6 +73,15 @@ class Universe { template <typename... ARGS> void broadcast(const std::string &name, ARGS... args); + /** + * Send a non-blocking RPC call with no return value to all subscribers + * of a resource. There may be no subscribers. + */ + template <typename... ARGS> + void publish(const std::string &res, ARGS... args); + + // TODO(nick) Add find_one, find_all, call_any ... + private: void _run(); int _setDescriptors(); @@ -82,8 +99,26 @@ class Universe { std::vector<ftl::net::Peer*> peers_; ftl::UUID id_; ftl::net::Dispatcher disp_; + + // std::map<std::string, std::vector<ftl::net::Peer*>> subscriptions_; }; +//------------------------------------------------------------------------------ + +template <typename F> +void Universe::bind(const std::string &name, F func) { + disp_.bind(name, func, + typename ftl::internal::func_kind_info<F>::result_kind(), + typename ftl::internal::func_kind_info<F>::args_kind()); +} + +template <typename... ARGS> +void Universe::broadcast(const std::string &name, ARGS... args) { + for (auto p : peers_) { + p->send(name, args...); + } +} + }; // namespace net }; // namespace ftl -- GitLab