swarm

package
v0.0.0-...-bafe949 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2022 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

package swarm implements a connection muxer with a pair of channels to synchronize all network communication.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDialBackoff = errors.New("dial backoff")
	ErrDialFailed  = errors.New("dial attempt failed")
	ErrDialToSelf  = errors.New("dial to self attempted")
)
View Source
var DialTimeout time.Duration = time.Second * 10

DialTimeout is the amount of time each dial attempt has. We can think about making this larger down the road, or putting more granular timeouts (i.e. within each subcomponent of Dial)

View Source
var PSTransport pst.Transport

Functions

This section is empty.

Types

type Conn

type Conn ps.Conn

a Conn is a simple wrapper around a ps.Conn that also exposes some of the methods from the underlying conn.Conn. There's **five** "layers" to each connection:

  • 0. the net.Conn - underlying net.Conn (TCP/UDP/UTP/etc)
  • 1. the manet.Conn - provides multiaddr friendly Conn
  • 2. the conn.Conn - provides Peer friendly Conn (inc Secure channel)
  • 3. the peerstream.Conn - provides peerstream / spdysptream happiness
  • 4. the Conn - abstracts everyting out, exposing only key parts of underlying layers

(I know, this is kinda crazy. it's more historical than a good design. though the layers do build up pieces of functionality. and they're all just io.RW :) )

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) LocalMultiaddr

func (c *Conn) LocalMultiaddr() ma.Multiaddr

LocalMultiaddr is the Multiaddr on this side

func (*Conn) LocalPeer

func (c *Conn) LocalPeer() peer.ID

LocalPeer is the Peer on our side of the connection

func (*Conn) LocalPrivateKey

func (c *Conn) LocalPrivateKey() ic.PrivKey

LocalPrivateKey is the public key of the peer on this side

func (*Conn) NewStream

func (c *Conn) NewStream() (inet.Stream, error)

NewStream returns a new Stream from this connection

func (*Conn) NewSwarmStream

func (c *Conn) NewSwarmStream() (*Stream, error)

NewSwarmStream returns a new Stream from this connection

func (*Conn) RawConn

func (c *Conn) RawConn() conn.Conn

func (*Conn) RemoteMultiaddr

func (c *Conn) RemoteMultiaddr() ma.Multiaddr

RemoteMultiaddr is the Multiaddr on the remote side

func (*Conn) RemotePeer

func (c *Conn) RemotePeer() peer.ID

RemotePeer is the Peer on the remote side

func (*Conn) RemotePublicKey

func (c *Conn) RemotePublicKey() ic.PubKey

RemotePublicKey is the public key of the peer on the remote side

func (*Conn) StreamConn

func (c *Conn) StreamConn() *ps.Conn

func (*Conn) String

func (c *Conn) String() string

type ConnHandler

type ConnHandler func(*Conn)

ConnHandler is called when new conns are opened from remote peers. See peerstream.ConnHandler

type Network

type Network Swarm

Network implements the inet.Network interface. It is simply a swarm, with a few different functions to implement inet.Network.

func NewNetwork

func NewNetwork(ctx context.Context, listen []ma.Multiaddr, local peer.ID,
	peers peer.Peerstore, bwc metrics.Reporter) (*Network, error)

NewNetwork constructs a new network and starts listening on given addresses.

func (*Network) Close

func (n *Network) Close() error

Close calls the ContextCloser func

func (*Network) ClosePeer

func (n *Network) ClosePeer(p peer.ID) error

ClosePeer connection to peer

func (*Network) Connectedness

func (n *Network) Connectedness(p peer.ID) inet.Connectedness

Connectedness returns a state signaling connection capabilities For now only returns Connected || NotConnected. Expand into more later.

func (*Network) Conns

func (n *Network) Conns() []inet.Conn

Conns returns the connected peers

func (*Network) ConnsToPeer

func (n *Network) ConnsToPeer(p peer.ID) []inet.Conn

ConnsToPeer returns the connections in this Netowrk for given peer.

func (*Network) CtxGroup

func (n *Network) CtxGroup() ctxgroup.ContextGroup

CtxGroup returns the network's ContextGroup

func (*Network) DialPeer

func (n *Network) DialPeer(ctx context.Context, p peer.ID) (inet.Conn, error)

DialPeer attempts to establish a connection to a given peer. Respects the context.

func (*Network) InterfaceListenAddresses

func (n *Network) InterfaceListenAddresses() ([]ma.Multiaddr, error)

InterfaceListenAddresses returns a list of addresses at which this network listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to use the known local interfaces.

func (*Network) Listen

func (n *Network) Listen(addrs ...ma.Multiaddr) error

Listen tells the network to start listening on given multiaddrs.

func (*Network) ListenAddresses

func (n *Network) ListenAddresses() []ma.Multiaddr

ListenAddresses returns a list of addresses at which this network listens.

func (*Network) LocalPeer

func (n *Network) LocalPeer() peer.ID

LocalPeer the network's LocalPeer

func (*Network) NewStream

func (n *Network) NewStream(p peer.ID) (inet.Stream, error)

NewStream returns a new stream to given peer p. If there is no connection to p, attempts to create one.

func (*Network) Notify

func (n *Network) Notify(f inet.Notifiee)

Notify signs up Notifiee to receive signals when events happen

func (*Network) Peers

func (n *Network) Peers() []peer.ID

Peers returns the connected peers

func (*Network) Peerstore

func (n *Network) Peerstore() peer.Peerstore

Peers returns the connected peers

func (*Network) SetConnHandler

func (n *Network) SetConnHandler(h inet.ConnHandler)

SetConnHandler sets the conn handler on the Network. This operation is threadsafe.

func (*Network) SetStreamHandler

func (n *Network) SetStreamHandler(h inet.StreamHandler)

SetHandler sets the protocol handler on the Network's Muxer. This operation is threadsafe.

func (*Network) StopNotify

func (n *Network) StopNotify(f inet.Notifiee)

StopNotify unregisters Notifiee fromr receiving signals

func (*Network) String

func (n *Network) String() string

String returns a string representation of Network.

func (*Network) Swarm

func (n *Network) Swarm() *Swarm

Swarm returns the network's peerstream.Swarm

type Stream

type Stream ps.Stream

a Stream is a wrapper around a ps.Stream that exposes a way to get our Conn and Swarm (instead of just the ps.Conn and ps.Swarm)

func (*Stream) Close

func (s *Stream) Close() error

Close closes the stream, indicating this side is finished with the stream.

func (*Stream) Conn

func (s *Stream) Conn() inet.Conn

Conn returns the Conn associated with this Stream, as an inet.Conn

func (*Stream) Read

func (s *Stream) Read(p []byte) (n int, err error)

Read reads bytes from a stream.

func (*Stream) Stream

func (s *Stream) Stream() *ps.Stream

Stream returns the underlying peerstream.Stream

func (*Stream) SwarmConn

func (s *Stream) SwarmConn() *Conn

SwarmConn returns the Conn associated with this Stream, as a *Conn

func (*Stream) Write

func (s *Stream) Write(p []byte) (n int, err error)

Write writes bytes to a stream, flushing for each call.

type Swarm

type Swarm struct {
	// contains filtered or unexported fields
}

Swarm is a connection muxer, allowing connections to other peers to be opened and closed, while still using the same Chan for all communication. The Chan sends/receives Messages, which note the destination or source Peer.

Uses peerstream.Swarm

func NewSwarm

func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
	local peer.ID, peers peer.Peerstore, bwc metrics.Reporter) (*Swarm, error)

NewSwarm constructs a Swarm, with a Chan.

func (*Swarm) Close

func (s *Swarm) Close() error

Close stops the Swarm.

func (*Swarm) CloseConnection

func (s *Swarm) CloseConnection(p peer.ID) error

CloseConnection removes a given peer from swarm + closes the connection

func (*Swarm) Connections

func (s *Swarm) Connections() []*Conn

Connections returns a slice of all connections.

func (*Swarm) ConnectionsToPeer

func (s *Swarm) ConnectionsToPeer(p peer.ID) []*Conn

ConnectionsToPeer returns all the live connections to p

func (*Swarm) CtxGroup

func (s *Swarm) CtxGroup() ctxgroup.ContextGroup

CtxGroup returns the Context Group of the swarm

func (*Swarm) Dial

func (s *Swarm) Dial(ctx context.Context, p peer.ID) (*Conn, error)

Dial connects to a peer.

The idea is that the client of Swarm does not need to know what network the connection will happen over. Swarm can use whichever it choses. This allows us to use various transport protocols, do NAT traversal/relay, etc. to achive connection.

func (*Swarm) InterfaceListenAddresses

func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error)

InterfaceListenAddresses returns a list of addresses at which this swarm listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to use the known local interfaces.

func (*Swarm) Listen

func (s *Swarm) Listen(addrs ...ma.Multiaddr) error

CtxGroup returns the Context Group of the swarm

func (*Swarm) ListenAddresses

func (s *Swarm) ListenAddresses() []ma.Multiaddr

ListenAddresses returns a list of addresses at which this swarm listens.

func (*Swarm) LocalPeer

func (s *Swarm) LocalPeer() peer.ID

LocalPeer returns the local peer swarm is associated to.

func (*Swarm) NewStreamWithPeer

func (s *Swarm) NewStreamWithPeer(p peer.ID) (*Stream, error)

NewStreamWithPeer creates a new stream on any available connection to p

func (*Swarm) Notify

func (s *Swarm) Notify(f inet.Notifiee)

Notify signs up Notifiee to receive signals when events happen

func (*Swarm) Peers

func (s *Swarm) Peers() []peer.ID

Peers returns a copy of the set of peers swarm is connected to.

func (*Swarm) SetConnHandler

func (s *Swarm) SetConnHandler(handler ConnHandler)

SetConnHandler assigns the handler for new connections. See peerstream. You will rarely use this. See SetStreamHandler

func (*Swarm) SetStreamHandler

func (s *Swarm) SetStreamHandler(handler inet.StreamHandler)

SetStreamHandler assigns the handler for new streams. See peerstream.

func (*Swarm) StopNotify

func (s *Swarm) StopNotify(f inet.Notifiee)

StopNotify unregisters Notifiee fromr receiving signals

func (*Swarm) StreamSwarm

func (s *Swarm) StreamSwarm() *ps.Swarm

StreamSwarm returns the underlying peerstream.Swarm

func (*Swarm) StreamsWithPeer

func (s *Swarm) StreamsWithPeer(p peer.ID) []*Stream

StreamsWithPeer returns all the live Streams to p

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL