Copyright Matus Chochlik. Distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
#include "lib_common_pki.hpp"
#include <set>
#include <thread>
namespace msgbus {
using shutdown_trigger_base =
service_composition<subscriber_discovery<shutdown_invoker<>>>;
class shutdown_trigger
: public main_ctx_object
, public shutdown_trigger_base {
using base = shutdown_trigger_base;
public:
, base{bus} {}
void is_alive(const subscriber_info&) final {}
void on_subscribed(
const subscriber_info&
info,
message_id sub_msg)
final {
_targets.insert(
info.endpoint_id);
this->bus().post_certificate(
info.endpoint_id);
}
}
void on_unsubscribed(
const subscriber_info&
info,
message_id sub_msg)
final {
_targets.erase(
info.endpoint_id);
}
}
void not_subscribed(
const subscriber_info&
info,
message_id sub_msg)
final {
log_info(
"target ${id} does not support shutdown")
_targets.erase(
info.endpoint_id);
}
}
void shutdown_all() {
for(auto id : _targets) {
this->shutdown_one(id);
}
}
private:
std::set<identifier_t> _targets{};
};
}
auto main(main_ctx& ctx) -> int {
msgbus::router_address
address{ctx};
msgbus::connection_setup conn_setup(ctx);
msgbus::endpoint bus{
EAGINE_ID(ShutdownEx), ctx};
bus.add_ca_certificate_pem(ca_certificate_pem(ctx));
bus.add_certificate_pem(msgbus_endpoint_certificate_pem(ctx));
msgbus::shutdown_trigger trgr{bus};
conn_setup.setup_connectors(trgr,
address);
timeout wait_done{std::chrono::seconds(30)};
while(!wait_done) {
trgr.update();
if(!trgr.process_all()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
trgr.shutdown_all();
wait_done.reset();
while(!wait_done) {
trgr.update();
if(!trgr.process_all()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
return 0;
}
}
#define EAGINE_ID(NAME)
Macro for constructing instances of eagine::identifier.
Definition: identifier.hpp:353
Common code is placed in this namespace.
Definition: eagine.hpp:21
#define EAGINE_MSG_ID(API, NAME)
Macro for instantiating objects of static_message_id.
Definition: message_id.hpp:148
@ info
Informational log entries.
@ endpoint
Message bus client endpoint.
main_ctx_object(identifier obj_id, main_ctx_parent parent) noexcept
Initialization from object id and parent.
Definition: main_ctx_object.hpp:77
auto log_info(string_view format) noexcept
Create a log message entry for information, with specified format.
Definition: logger.hpp:329
basic_address< false > address
Type alias for non-const memory address values.
Definition: address.hpp:203
auto bus() noexcept -> auto &
Returns a reference to the associated endpoint.
Definition: subscriber.hpp:38
@ message_id
The message type id has been verified.