p2pnetwork

package
v0.0.0-...-1538d9d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2021 License: MIT Imports: 21 Imported by: 2

Documentation

Index

Constants

View Source
const (
	TrackerFileName = "trackers.p2p"
)

Variables

This section is empty.

Functions

func InitPeer

func InitPeer(infos NetworkInfos, prot string, ctx context.Context) (host.Host, error)

Types

type HttpTracker

type HttpTracker struct {
	Secure  bool
	Tracker NetworkInfos
}

func (HttpTracker) AddPeer

func (t HttpTracker) AddPeer(peerID, peerIP string, peerPort int) error

AddPeer: Request Tracker to add a peer

func (HttpTracker) IP

func (t HttpTracker) IP() string

IP: Return Tracker NetworkInfos.Ip (Ex: <192.168.0.31>)

func (HttpTracker) Peers

func (t HttpTracker) Peers() ([]PeerInfos, error)

Peers: Get peers from Tracker.

func (HttpTracker) Ping

func (t HttpTracker) Ping() error

Ping: Check if Tracker is alive

func (HttpTracker) Port

func (t HttpTracker) Port() int

Port: Return Tracker NetworkInfos.NWPort (Ex: <5001>)

func (HttpTracker) RemovePeer

func (t HttpTracker) RemovePeer(pid string) error

AddPeer: Request Tracker to remove a peer

func (HttpTracker) URL

func (t HttpTracker) URL() string

URL: Return Tracker URL like (Ex: <192.168.0.31:5001>)

type Network

type Network interface {

	// SendDatagram sends a datagram to the remote peer
	SendDatagram(d *protocol.Datagram, remote PeerID) error

	// Connect connects to the remote peer and creates any io resources necessary for the connection
	Connect(remote PeerID, protocol string) (*WrappedStream, error)

	// Disconnect disconnects from the remote peer and destroys any io resources created for the connection
	Disconnect(remote PeerID) error

	// ID returns the ID of this peer
	ID() PeerID

	// SetDatagramHandler sets the function that will be called on receipt of a new datagram
	// f gets called every time a new Datagram is received.
	SetDatagramHandler(f func(*protocol.Datagram, PeerID) error)

	// AddAddrs adds multiaddresses for the remote peer to this peer's store
	AddAddrs(id PeerID, addrs []ma.Multiaddr)

	// Addrs returns multiaddresses for this peer
	Addrs() []ma.Multiaddr

	// Close close the network
	Close() error

	// Peers return all connected peers
	Peers() []PeerID

	// RequestFileToPeers launch Have request to connected peers and return file size.
	RequestFileToPeers(file storage.FileHash, remoteStorage storage.PeerStorage) (int, error)
}

Network handles interactions with the underlying protocol

type NetworkInfos

type NetworkInfos struct {
	Ip     string
	IpPub  string
	NWPort int
}

func NewNetworkInfos

func NewNetworkInfos(ip string, port int) NetworkInfos

func (*NetworkInfos) IP

func (n *NetworkInfos) IP() string

func (*NetworkInfos) Port

func (n *NetworkInfos) Port() int

func (*NetworkInfos) PubIP

func (n *NetworkInfos) PubIP() string

func (*NetworkInfos) URL

func (n *NetworkInfos) URL() string

type P2PNetwork

type P2PNetwork struct {
	Host host.Host
	// contains filtered or unexported fields
}

func NewLibp2pNetwork

func NewLibp2pNetwork(infos NetworkInfos, prot string) (*P2PNetwork, error)

func (*P2PNetwork) AddAddrs

func (n *P2PNetwork) AddAddrs(remote PeerID, addrs []ma.Multiaddr)

func (*P2PNetwork) Addrs

func (n *P2PNetwork) Addrs() []ma.Multiaddr

func (*P2PNetwork) Close

func (n *P2PNetwork) Close() error

func (*P2PNetwork) Connect

func (n *P2PNetwork) Connect(id PeerID, protocol string) (*WrappedStream, error)

Connect creates a stream from p to the peer at id and sets a stream handler func (n *P2PNetwork) Connect(id PeerID) (*WrappedStream, error) {

func (*P2PNetwork) Disconnect

func (n *P2PNetwork) Disconnect(remote PeerID) error

func (*P2PNetwork) FirstPeer

func (n *P2PNetwork) FirstPeer() (PeerID, error)

func (*P2PNetwork) ID

func (n *P2PNetwork) ID() PeerID

func (*P2PNetwork) Peers

func (n *P2PNetwork) Peers() []PeerID

func (*P2PNetwork) RequestFileToPeers

func (n *P2PNetwork) RequestFileToPeers(file storage.FileHash, remoteStorage storage.PeerStorage) (int, error)

func (*P2PNetwork) SendDatagram

func (n *P2PNetwork) SendDatagram(d *protocol.Datagram, pid PeerID) (err error)

func (*P2PNetwork) SetDatagramHandler

func (n *P2PNetwork) SetDatagramHandler(handler func(*protocol.Datagram, PeerID) error)

type P2PPeerID

type P2PPeerID string

func (P2PPeerID) String

func (id P2PPeerID) String() string

type PeerID

type PeerID interface {
	String() string
}

PeerID identifies a peer

type PeerInfos

type PeerInfos struct {
	Idpeer   string
	Ippeer   string
	Portpeer int `json:"port"`
}

func (PeerInfos) ID

func (i PeerInfos) ID() string

func (PeerInfos) IP

func (i PeerInfos) IP() string

func (PeerInfos) Port

func (i PeerInfos) Port() string

type Tracker

type Tracker interface {
	IP() string
	URL() string
	Port() int
	Ping() error
	AddPeer(peerID, peerIP string, peerPort int) error
	Peers() ([]PeerInfos, error)
	RemovePeer(id string) error
}

func NewHttpTracker

func NewHttpTracker(trackerIP string, trackerPort int, secure bool) Tracker

NewHttpTracker Create a new htttp tracker Allows Node to request permanent otherPeers from http server

func ParseTrackerInfos

func ParseTrackerInfos(strorageDir string) ([]Tracker, error)

type WrappedStream

type WrappedStream struct {
	Stream inet.Stream
	Enc    *json.Encoder
	Dec    *json.Decoder
	W      *bufio.Writer
	R      *bufio.Reader
}

WrappedStream wraps a libp2p stream. We encode/decode whenever we write/read from a stream, so we can just carry the encoders and bufios with us

func WrapStream

func WrapStream(s inet.Stream) *WrappedStream

WrapStream takes a stream and complements it with r/w bufios and decoder/encoder. In order to write raw storage to the stream we can use wrap.w.Write(). To encode something into it we can wrap.enc.Encode(). Finally, we should wrap.w.Flush() to actually send the storage. Handling incoming storage works similarly with wrap.r.Read() for raw-reading and wrap.dec.Decode() to decode.

func (*WrappedStream) Close

func (s *WrappedStream) Close() error

Jump to

Keyboard shortcuts

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