Back to Site
Loading...
Searching...
No Matches
librats::Node Class Referencefinal

#include <node.h>

Inheritance diagram for librats::Node:
[legend]
Collaboration diagram for librats::Node:
[legend]

Public Member Functions

 Node (NodeConfig config)
 Construct a node from its configuration (see NodeConfig).
 
 ~Node () override
 Stops the node if still running, then releases all resources.
 
 Node (const Node &)=delete
 
Nodeoperator= (const Node &)=delete
 
template<class T >
T * add_subsystem (std::unique_ptr< T > subsystem)
 Attach a subsystem (DHT, GossipSub, PingService…).
 
bool start ()
 Bring the node up: open the listener (if enabled), start the reactor pool, then start every attached subsystem.
 
void stop ()
 Tear the node down: stop subsystems (reverse order), close all connections, and join the reactor pool.
 
const PeerIdlocal_id () const noexcept override
 Our self-certifying peer identity (the public key peers authenticate).
 
uint16_t listen_port () const noexcept override
 The bound listen port (the actual port when the config requested 0).
 
const std::string & protocol_name () const noexcept
 Application protocol identity bound into the handshake (see NodeConfig).
 
const std::string & protocol_version () const noexcept
 
EventBusevents () noexcept
 
ServiceRegistryservices () noexcept
 
void connect (const Address &address) override
 Dial a peer.
 
void connect (const std::string &host, uint16_t port)
 Dial a peer.
 
size_t peer_count () const noexcept
 Number of currently-established peers.
 
std::vector< PeerInfopeers () const override
 Snapshot of all established peers (id, addresses, direction, timing).
 
std::optional< Peerpeer (const PeerId &id)
 Handle to a connected peer by id, or std::nullopt if not connected.
 
std::vector< Addressobserved_addresses () const
 Our own addresses as remote peers reported observing us at — their observed IP paired with our listen port.
 
size_t max_peers () const noexcept
 
void set_max_peers (size_t n) noexcept
 
bool peer_limit_reached () const noexcept
 
void send (const PeerId &to, std::string_view channel, ByteView payload)
 Send raw bytes to one peer on a named channel.
 
void broadcast (std::string_view channel, ByteView payload)
 Send raw bytes on a named channel to every connected peer.
 
void on_peer_connected (PeerNetwork::PeerEventHandler cb) override
 Subscribe to peer-connected events. The handler runs on a reactor thread.
 
void on_peer_disconnected (PeerNetwork::PeerDisconnectHandler cb) override
 Subscribe to peer-disconnected events. The handler runs on a reactor thread.
 
void on_dial_failed (PeerNetwork::DialFailedHandler cb) override
 Subscribe to failed-outbound-dial events. The handler runs on a reactor thread.
 
void on (std::string_view channel, MessageRouter::Handler cb)
 Register a handler for inbound messages on a named channel.
 
template<class T >
T * subsystem () noexcept
 
MessageJsonjson () noexcept
 The JSON messaging module if one was attached (add_subsystem<MessageJson>), else nullptr.
 
void send (const PeerId &to, MessageType type, ByteView payload) override
 
void broadcast (MessageType type, ByteView payload) override
 
std::vector< PeerIdconnected_peers () const override
 
void on (MessageType type, PeerNetwork::MessageHandler cb) override
 
- Public Member Functions inherited from librats::PeerNetwork
virtual ~PeerNetwork ()=default
 

Friends

class Peer
 

Additional Inherited Members

- Public Types inherited from librats::PeerNetwork
using MessageHandler = std::function< void(const Peer &, ByteView)>
 
using PeerEventHandler = std::function< void(const Peer &)>
 
using PeerDisconnectHandler = std::function< void(const PeerId &)>
 
using DialFailedHandler = std::function< void(const Address &)>
 

Detailed Description

Definition at line 70 of file node.h.

Constructor & Destructor Documentation

◆ Node() [1/2]

librats::Node::Node ( NodeConfig  config)
explicit

Construct a node from its configuration (see NodeConfig).

This only loads the identity and prepares the layers; no socket is opened until start().

◆ ~Node()

librats::Node::~Node ( )
override

Stops the node if still running, then releases all resources.

◆ Node() [2/2]

librats::Node::Node ( const Node )
delete

Member Function Documentation

◆ add_subsystem()

template<class T >
T * librats::Node::add_subsystem ( std::unique_ptr< T >  subsystem)
inline

Attach a subsystem (DHT, GossipSub, PingService…).

Call before start(); the node owns it (single ownership), gives it a PeerNetwork on start(), and stops it on stop(). Returns a non-owning pointer to the just-added subsystem so the caller can drive its API without a separate get()/move dance — valid for the node's lifetime: auto* files = node.add_subsystem(std::make_unique<FileTransfer>("./dl"));

Definition at line 88 of file node.h.

References subsystem().

◆ broadcast() [1/2]

void librats::Node::broadcast ( MessageType  type,
ByteView  payload 
)
overridevirtual

Implements librats::PeerNetwork.

◆ broadcast() [2/2]

void librats::Node::broadcast ( std::string_view  channel,
ByteView  payload 
)

Send raw bytes on a named channel to every connected peer.

◆ connect() [1/2]

void librats::Node::connect ( const Address address)
overridevirtual

Dial a peer.

Non-blocking: the connection (TCP + handshake) completes asynchronously and surfaces via on_peer_connected. A duplicate or self-connection is detected and dropped after the handshake.

Implements librats::PeerNetwork.

◆ connect() [2/2]

void librats::Node::connect ( const std::string &  host,
uint16_t  port 
)

Dial a peer.

Non-blocking: the connection (TCP + handshake) completes asynchronously and surfaces via on_peer_connected. A duplicate or self-connection is detected and dropped after the handshake.

◆ connected_peers()

std::vector< PeerId > librats::Node::connected_peers ( ) const
overridevirtual

Implements librats::PeerNetwork.

◆ events()

EventBus & librats::Node::events ( )
inlinenoexcept

Definition at line 116 of file node.h.

◆ json()

MessageJson * librats::Node::json ( )
noexcept

The JSON messaging module if one was attached (add_subsystem<MessageJson>), else nullptr.

Convenience over subsystem<MessageJson>(); defined in node.cpp.

◆ listen_port()

uint16_t librats::Node::listen_port ( ) const
inlineoverridevirtualnoexcept

The bound listen port (the actual port when the config requested 0).

Implements librats::PeerNetwork.

Definition at line 107 of file node.h.

◆ local_id()

const PeerId & librats::Node::local_id ( ) const
inlineoverridevirtualnoexcept

Our self-certifying peer identity (the public key peers authenticate).

Implements librats::PeerNetwork.

Definition at line 105 of file node.h.

◆ max_peers()

size_t librats::Node::max_peers ( ) const
inlinenoexcept

Definition at line 140 of file node.h.

◆ observed_addresses()

std::vector< Address > librats::Node::observed_addresses ( ) const

Our own addresses as remote peers reported observing us at — their observed IP paired with our listen port.

De-duplicated and bounded; populated as peers send their identify message. Useful for NAT awareness / advertising.

◆ on() [1/2]

void librats::Node::on ( MessageType  type,
PeerNetwork::MessageHandler  cb 
)
inlineoverridevirtual

Implements librats::PeerNetwork.

Definition at line 185 of file node.h.

◆ on() [2/2]

void librats::Node::on ( std::string_view  channel,
MessageRouter::Handler  cb 
)
inline

Register a handler for inbound messages on a named channel.

Additive: multiple handlers may coexist. The handler runs on a reactor thread.

Definition at line 167 of file node.h.

◆ on_dial_failed()

void librats::Node::on_dial_failed ( PeerNetwork::DialFailedHandler  cb)
inlineoverridevirtual

Subscribe to failed-outbound-dial events. The handler runs on a reactor thread.

Implements librats::PeerNetwork.

Definition at line 164 of file node.h.

◆ on_peer_connected()

void librats::Node::on_peer_connected ( PeerNetwork::PeerEventHandler  cb)
inlineoverridevirtual

Subscribe to peer-connected events. The handler runs on a reactor thread.

Implements librats::PeerNetwork.

Definition at line 160 of file node.h.

◆ on_peer_disconnected()

void librats::Node::on_peer_disconnected ( PeerNetwork::PeerDisconnectHandler  cb)
inlineoverridevirtual

Subscribe to peer-disconnected events. The handler runs on a reactor thread.

Implements librats::PeerNetwork.

Definition at line 162 of file node.h.

◆ operator=()

Node & librats::Node::operator= ( const Node )
delete

◆ peer()

std::optional< Peer > librats::Node::peer ( const PeerId id)

Handle to a connected peer by id, or std::nullopt if not connected.

◆ peer_count()

size_t librats::Node::peer_count ( ) const
inlinenoexcept

Number of currently-established peers.

Definition at line 128 of file node.h.

◆ peer_limit_reached()

bool librats::Node::peer_limit_reached ( ) const
inlinenoexcept

Definition at line 142 of file node.h.

◆ peers()

std::vector< PeerInfo > librats::Node::peers ( ) const
inlineoverridevirtual

Snapshot of all established peers (id, addresses, direction, timing).

Implements librats::PeerNetwork.

Definition at line 130 of file node.h.

◆ protocol_name()

const std::string & librats::Node::protocol_name ( ) const
inlinenoexcept

Application protocol identity bound into the handshake (see NodeConfig).

Definition at line 110 of file node.h.

References librats::NodeConfig::protocol_name.

◆ protocol_version()

const std::string & librats::Node::protocol_version ( ) const
inlinenoexcept

Definition at line 111 of file node.h.

References librats::NodeConfig::protocol_version.

◆ send() [1/2]

void librats::Node::send ( const PeerId to,
MessageType  type,
ByteView  payload 
)
overridevirtual

Implements librats::PeerNetwork.

◆ send() [2/2]

void librats::Node::send ( const PeerId to,
std::string_view  channel,
ByteView  payload 
)

Send raw bytes to one peer on a named channel.

Non-blocking; the payload is copied into the peer's send queue. No-op if the peer is not connected.

Parameters
todestination peer id
channelapplication channel name (interned to a 16-bit id)
payloadmessage bytes (copied)

◆ services()

ServiceRegistry & librats::Node::services ( )
inlinenoexcept

Definition at line 117 of file node.h.

◆ set_max_peers()

void librats::Node::set_max_peers ( size_t  n)
inlinenoexcept

Definition at line 141 of file node.h.

◆ start()

bool librats::Node::start ( )

Bring the node up: open the listener (if enabled), start the reactor pool, then start every attached subsystem.

Register callbacks and attach subsystems BEFORE calling this.

Returns
true on success; false if the listener could not bind.

◆ stop()

void librats::Node::stop ( )

Tear the node down: stop subsystems (reverse order), close all connections, and join the reactor pool.

Safe to call once; idempotent.

◆ subsystem()

template<class T >
T * librats::Node::subsystem ( )
inlinenoexcept

Definition at line 173 of file node.h.

Referenced by add_subsystem().

Friends And Related Symbol Documentation

◆ Peer

friend class Peer
friend

Definition at line 188 of file node.h.


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