morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::offline::NetworkMonitor Class Reference

Connectivity monitor that calls a probe on a timer and fires callbacks on state changes. More...

#include <network_monitor.hpp>

Public Types

using ProbeFunction = std::function<bool()>
 Returns true when the network is reachable.
using Callback = std::function<void()>
 Called on connectivity state change.
using Config = NetworkMonitorConfig
 Alias for the configuration struct.

Public Member Functions

 NetworkMonitor (::morph::exec::IoLoop &loop, ProbeFunction probe, Callback onOffline, Callback onOnline, Config cfg=Config{})
 Starts the monitor on loop.
 NetworkMonitor (ProbeFunction probe, Callback onOffline, Callback onOnline, Config cfg=Config{})
 Starts the monitor on a loop of its own, for a caller with no IoLoop to share. Natively that loop owns one thread.
 ~NetworkMonitor ()
 Stops the monitor. Once this returns, no probe or callback of this monitor will run again, and from any thread but the loop's none is running.
 NetworkMonitor (const NetworkMonitor &)=delete
NetworkMonitor & operator= (const NetworkMonitor &)=delete
 NetworkMonitor (NetworkMonitor &&)=delete
NetworkMonitor & operator= (NetworkMonitor &&)=delete
bool isOnline () const noexcept
 Returns true if the monitor currently considers the network reachable.
void stop ()
 Stops probing. Idempotent.

Detailed Description

Connectivity monitor that calls a probe on a timer and fires callbacks on state changes.

The probe runs every probeInterval on an exec::IoLoop's thread, as a loop timer. The monitor starts in the online state and transitions to offline only after failureThreshold consecutive failures. It returns to online after onlineThreshold consecutive successes.

The probe and both callbacks run on the loop's thread and must not block it: every socket and timer on the same loop waits while they run. A probe that needs a slow check should start it elsewhere and report the last result it has.

Cross-thread surface: isOnline(), an atomic read, and stop() and the destructor, which run their close on the loop and wait for it. Every other field is touched only on the loop.

The monitor is non-copyable and non-movable. Destroy it to stop monitoring.

Definition at line 52 of file network_monitor.hpp.

Member Typedef Documentation

◆ ProbeFunction

using morph::offline::NetworkMonitor::ProbeFunction = std::function<bool()>

Returns true when the network is reachable.

Definition at line 55 of file network_monitor.hpp.

◆ Callback

using morph::offline::NetworkMonitor::Callback = std::function<void()>

Called on connectivity state change.

Definition at line 58 of file network_monitor.hpp.

◆ Config

Alias for the configuration struct.

Definition at line 61 of file network_monitor.hpp.

Constructor & Destructor Documentation

◆ NetworkMonitor() [1/2]

morph::offline::NetworkMonitor::NetworkMonitor ( ::morph::exec::IoLoop & loop,
ProbeFunction probe,
Callback onOffline,
Callback onOnline,
Config cfg = Config{} )
inline

Starts the monitor on loop.

The monitor starts in the online state. The first failureThreshold consecutive probe failures must occur before onOffline is invoked. Callers that know the initial state is offline should use a probe that starts returning false immediately.

Parameters
loopThe application's loop, whose thread runs the probe and the callbacks. Borrowed: it must outlive this monitor.
probeCallable that tests connectivity. Must not throw (exceptions are swallowed) and must not block. Stored and invoked on the loop's thread for this monitor's whole lifetime, so anything the callable refers to must outlive the monitor.
onOfflineCalled on the loop's thread when the monitor goes offline. Retained on the same terms as probe.
onOnlineCalled on the loop's thread when the monitor comes back online. Retained on the same terms as probe.
cfgTuning parameters (interval, thresholds).

Definition at line 82 of file network_monitor.hpp.

◆ NetworkMonitor() [2/2]

morph::offline::NetworkMonitor::NetworkMonitor ( ProbeFunction probe,
Callback onOffline,
Callback onOnline,
Config cfg = Config{} )
inline

Starts the monitor on a loop of its own, for a caller with no IoLoop to share. Natively that loop owns one thread.

Parameters
probeAs for the other constructor.
onOfflineAs for the other constructor.
onOnlineAs for the other constructor.
cfgTuning parameters (interval, thresholds).

Definition at line 97 of file network_monitor.hpp.

◆ ~NetworkMonitor()

morph::offline::NetworkMonitor::~NetworkMonitor ( )
inline

Stops the monitor. Once this returns, no probe or callback of this monitor will run again, and from any thread but the loop's none is running.

May run anywhere, including inside this monitor's own probe or callback: the timer that called it keeps the loop-side state alive until it has returned.

Definition at line 113 of file network_monitor.hpp.

Member Function Documentation

◆ isOnline()

bool morph::offline::NetworkMonitor::isOnline ( ) const
inlinenodiscardnoexcept

Returns true if the monitor currently considers the network reachable.

Reads an atomic flag — safe to call from any thread at any time.

Returns
Current online state.

Definition at line 125 of file network_monitor.hpp.

◆ stop()

void morph::offline::NetworkMonitor::stop ( )
inline

Stops probing. Idempotent.

Runs on the loop: inline when called there (from the probe or a callback, which then finishes normally, and no further probe runs), posted and waited for from any other thread, so that once it returns no probe or callback is running.

Definition at line 133 of file network_monitor.hpp.


The documentation for this class was generated from the following file: