rotor
Event loop friendly C++ actor micro-framework
 
Loading...
Searching...
No Matches
registry.h
1#pragma once
2
3//
4// Copyright (c) 2019-2023 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
5//
6// Distributed under the MIT Software License
7//
8
9#include "plugin_base.h"
10#include "link_client.h"
11#include "rotor/error_code.h"
12#include <string>
13#include <unordered_map>
14
15#if defined(_MSC_VER)
16#pragma warning(push)
17#pragma warning(disable : 4251)
18#endif
19
20namespace rotor::plugin {
21
35struct ROTOR_API registry_plugin_t : public plugin_base_t {
36 using plugin_base_t::plugin_base_t;
37
39 enum class phase_t { discovering, linking };
40
44 struct ROTOR_API discovery_task_t {
46 using callback_t = std::function<void(phase_t phase, const extended_error_ptr_t &)>;
47
49 enum class state_t { PASSIVE, DISCOVERING, LINKING, OPERATIONAL, CANCELLING };
50
52 discovery_task_t &link(bool operational_only_ = true) noexcept {
53 link_on_discovery = true;
54 operational_only = operational_only_;
55 return *this;
56 }
57
59 template <typename Callback> void callback(Callback &&cb) noexcept {
60 task_callback = std::forward<Callback>(cb);
61 }
62
64 template <typename T, typename... Args> auto access(Args... args) noexcept;
65
66 private:
67 discovery_task_t(registry_plugin_t &plugin_, address_ptr_t *address_, std::string service_name_, bool delayed_)
68 : plugin{&plugin_}, address(address_), service_name{service_name_}, delayed{delayed_},
69 state{state_t::PASSIVE} {}
70 operator bool() const noexcept { return address; }
71
72 void do_discover() noexcept;
73 void on_discovery(address_ptr_t *service_addr, const extended_error_ptr_t &ec) noexcept;
74 bool do_cancel() noexcept;
75 void post_discovery(const extended_error_ptr_t &ec) noexcept;
76
77 // allow implicit copy-assignment operator
78 registry_plugin_t *plugin;
79 address_ptr_t *address;
80 std::string service_name;
81 bool delayed;
82 state_t state;
83 request_id_t request_id = 0;
84 callback_t task_callback;
85 bool link_on_discovery = false;
86 bool operational_only = false;
87
88 friend struct registry_plugin_t;
89 };
90
92 static const std::type_index class_identity;
93
94 const std::type_index &identity() const noexcept override;
95
96 void activate(actor_base_t *actor) noexcept override;
97
99 virtual void on_registration(message::registration_response_t &) noexcept;
100
102 virtual void on_discovery(message::discovery_response_t &) noexcept;
103
105 virtual void on_future(message::discovery_future_t &message) noexcept;
106
112 virtual void register_name(const std::string &name, const address_ptr_t &address) noexcept;
113
124 virtual discovery_task_t &discover_name(const std::string &name, address_ptr_t &address,
125 bool delayed = false) noexcept;
126
127 bool handle_shutdown(message::shutdown_request_t *message) noexcept override;
128 bool handle_init(message::init_request_t *message) noexcept override;
129
131 template <typename T> auto &access() noexcept;
132
134 using discovery_map_t = std::unordered_map<std::string, discovery_task_t>;
135
136 private:
137 enum class state_t { REGISTERING, LINKING, OPERATIONAL, UNREGISTERING };
138 struct register_info_t {
139 address_ptr_t address;
140 state_t state;
141 };
142 using register_map_t = std::unordered_map<std::string, register_info_t>;
143 using names_t = std::vector<std::string>;
144 using aliases_map_t = std::unordered_map<address_ptr_t, names_t>;
145
146 enum plugin_state_t : std::uint32_t {
147 CONFIGURED = 1 << 0,
148 LINKING = 1 << 1,
149 LINKED = 1 << 2,
150 };
151 std::uint32_t plugin_state = 0;
152
153 register_map_t register_map;
154 discovery_map_t discovery_map;
155 aliases_map_t aliases_map;
156
157 void link_registry() noexcept;
158 void on_link(const extended_error_ptr_t &ec) noexcept;
159 bool has_registering() noexcept;
160 virtual void continue_init(const error_code_t &possible_ec, const extended_error_ptr_t &root_ec) noexcept;
161};
162
163} // namespace rotor::plugin
164
165#if defined(_MSC_VER)
166#pragma warning(pop)
167#endif
intrusive_ptr_t< address_t > address_ptr_t
intrusive pointer for address
Definition address.hpp:57
intrusive_ptr_t< extended_error_t > extended_error_ptr_t
intrusive pointer to extended error type
Definition extended_error.h:25
state_t
state of actor in rotor
Definition state.h:12
std::size_t request_id_t
timer identifier type in the scope of the actor
Definition forward.hpp:34
universal primitive of concurrent computation
Definition actor_base.h:47
base class for all actor plugins
Definition plugin_base.h:23
helper class to invoke callback upon address discovery
Definition registry.h:44
void callback(Callback &&cb) noexcept
discovery progress callback setter
Definition registry.h:59
state_t
stat of the discovery task
Definition registry.h:49
discovery_task_t & link(bool operational_only_=true) noexcept
sets that linking should be performed on operational-only discovered address
Definition registry.h:52
std::function< void(phase_t phase, const extended_error_ptr_t &)> callback_t
callback for the discovery progress
Definition registry.h:46
auto access(Args... args) noexcept
generic non-public methods accessor
handy access to registry_t, for name registration and discovery
Definition registry.h:35
phase_t
phase for each discovery task: discovering or linking
Definition registry.h:39
std::unordered_map< std::string, discovery_task_t > discovery_map_t
service name to task mapping
Definition registry.h:134