Skip to content

sgns::neoswarm::network::P2PNode

Manages a libp2p host for swarm task broadcasting and CRDT sync. More...

#include <p2p_node.hpp>

Public Classes

Name
struct Config
struct Impl

Public Types

Name
using std::function< void(const Task &task, const std::string &from_peer)> TaskHandler
using std::function< void(const std::string &crdt_data)> CRDTHandler

Public Functions

Name
P2PNode(std::shared_ptr< security::NodeIdentity > identity, Config cfg)
P2PNode(std::shared_ptr< security::NodeIdentity > identity)
~P2PNode()
outcome::result< void > Start()
Start the libp2p host and begin listening.
void Stop()
Stop the host and disconnect all peers.
bool IsRunning() const
std::string ListenAddress() const
std::string PeerId() const
void OnTask(TaskHandler handler)
Register a handler for incoming task broadcasts.
void OnCRDT(CRDTHandler handler)
Register a handler for incoming CRDT sync messages.
outcome::result< void > BroadcastTask(const Task & task)
Broadcast a task to all connected peers via GossipSub.
outcome::result< void > BroadcastCRDT(const std::string & crdt_data)
Broadcast a CRDT state update to all peers.
std::vector< std::string > ConnectedPeers() const
Get the list of currently connected peer IDs.

Detailed Description

class sgns::neoswarm::network::P2PNode;

Manages a libp2p host for swarm task broadcasting and CRDT sync.

Uses Noise protocol for encryption and Yamux for stream multiplexing. Falls back to a local stub when libp2p is not compiled in.

Public Types Documentation

using TaskHandler

using sgns::neoswarm::network::P2PNode::TaskHandler =  std::function<void( const Task& task, const std::string& from_peer )>;

using CRDTHandler

using sgns::neoswarm::network::P2PNode::CRDTHandler =  std::function<void( const std::string& crdt_data )>;

Public Functions Documentation

function P2PNode

P2PNode(
    std::shared_ptr< security::NodeIdentity > identity,
    Config cfg
)

function P2PNode

explicit P2PNode(
    std::shared_ptr< security::NodeIdentity > identity
)

function ~P2PNode

~P2PNode()

function Start

outcome::result< void > Start()

Start the libp2p host and begin listening.

Return: outcome::success or NetworkError.

function Stop

void Stop()

Stop the host and disconnect all peers.

function IsRunning

inline bool IsRunning() const

Return: True if the node is currently running.

function ListenAddress

std::string ListenAddress() const

Return: Our listen multiaddress (available after Start()).

function PeerId

std::string PeerId() const

Return: Our peer ID string.

function OnTask

inline void OnTask(
    TaskHandler handler
)

Register a handler for incoming task broadcasts.

Parameters:

  • handler Callback invoked when a task is received from a peer.

function OnCRDT

inline void OnCRDT(
    CRDTHandler handler
)

Register a handler for incoming CRDT sync messages.

Parameters:

  • handler Callback invoked when a CRDT update is received.

function BroadcastTask

outcome::result< void > BroadcastTask(
    const Task & task
)

Broadcast a task to all connected peers via GossipSub.

Parameters:

  • task Task to broadcast.

Return: outcome::success or NetworkError.

function BroadcastCRDT

outcome::result< void > BroadcastCRDT(
    const std::string & crdt_data
)

Broadcast a CRDT state update to all peers.

Parameters:

  • crdt_data Serialised CRDT state.

Return: outcome::success or NetworkError.

function ConnectedPeers

std::vector< std::string > ConnectedPeers() const

Get the list of currently connected peer IDs.

Return: Vector of peer ID strings.


Updated on 2026-10-09 at 03:59:42 +0000