Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
socket_monitor.hpp
Go to the documentation of this file.
1/*
2 ____ _ _ __ __ _ _
3 / ___| ___ ___| | _____| |_| \/ | ___ _ __ (_) |_ ___ _ __
4 \___ \ / _ \ / __| |/ / _ \ __| |\/| |/ _ \| '_ \| | __/ _ \| '__|
5 ___) | (_) | (__| < __/ |_| | | | (_) | | | | | || (_) | |
6 |____/ \___/ \___|_|\_\___|\__|_| |_|\___/|_| |_|_|\__\___/|_|
7
8Wraps zmq_socket_monitor()/zmq::monitor_t so callers can turn connection
9lifecycle guesswork into fact: a real ZMQ_EVENT_CONNECTED instead of a blind
10sleep, a real ZMQ_EVENT_HANDSHAKE_FAILED_AUTH instead of a bare receive
11timeout (ZMQ_DEVELOPMENT.md ยง2.1). Purely local observation -- nothing on the
12wire changes, so using this has no version impact.
13
14Author(s): Paolo Bosetti
15*/
16#pragma once
17
18#include <chrono>
19#include <cstdint>
20#include <memory>
21#include <optional>
22#include <string>
23
24#include <zmq.hpp>
25
26namespace Mads {
27
48
53enum class LinkStatus {
54 Unknown,
56 Up,
57 Down,
58};
59
62struct LinkState {
76 std::string last_event_address;
79 uint64_t drops = 0;
82 uint64_t recoveries = 0;
85 std::optional<std::chrono::steady_clock::time_point> changed_at;
95 uint64_t accept_failures = 0;
96};
97
115public:
118 SocketMonitor(const SocketMonitor &) = delete;
120
128 void start(zmq::socket_t &socket, int events = ZMQ_EVENT_ALL);
129
143 void attach(zmq::socket_t &socket, int events = ZMQ_EVENT_ALL);
144
151 zmq::socket_ref pollable() const;
152
163
175 void stop();
176
187 bool wait_connected(std::chrono::milliseconds timeout);
188
202 bool wait_handshake_succeeded(std::chrono::milliseconds timeout);
203
206
218
221 std::string last_event_address() const;
222
223private:
224 class Impl;
225 std::unique_ptr<Impl> _impl;
226};
227
228} // namespace Mads
One monitor per monitored socket. start() must be called before the socket's connect()/bind(): libzmq...
void attach(zmq::socket_t &socket, int events=ZMQ_EVENT_ALL)
Attaches to socket without starting any thread, leaving the caller to drive the monitor: include poll...
void stop()
Stops the monitoring thread (if start() created one) and detaches from the socket (zmq_socket_monitor...
void process_pending()
Consumes the events pollable() has signalled, updating last_event()/state() and waking the wait_*() c...
std::string last_event_address() const
void start(zmq::socket_t &socket, int events=ZMQ_EVENT_ALL)
Starts monitoring socket on a private inproc:// endpoint and a dedicated thread. A second call before...
SocketMonitor & operator=(const SocketMonitor &)=delete
SocketMonitor(const SocketMonitor &)=delete
LinkState state() const
A consistent snapshot of the link's current status, the event that produced it, and how often it has ...
bool wait_connected(std::chrono::milliseconds timeout)
Blocks until a Connected or HandshakeSucceeded event is observed, or timeout elapses....
zmq::socket_ref pollable() const
The monitor's own PAIR socket, for inclusion in the caller's zmq::poll(). Its handle() is null until ...
LinkEvent last_event() const
The most recent event observed (None if none yet).
bool wait_handshake_succeeded(std::chrono::milliseconds timeout)
Blocks until a ZMTP handshake completes one way or the other, or timeout elapses. Unlike wait_connect...
Definition agent.hpp:67
@ Down
the peer went away, or the handshake was refused
@ Up
the ZMTP handshake completed and has not been undone since
std::optional< std::chrono::steady_clock::time_point > changed_at
LinkEvent last_handshake
std::string last_event_address
The peer address libzmq reported with that event ("" if none yet).
LinkEvent last_event
The most recent event, whatever it was.