p2p

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

README

go-p2p

It's the assembly of libp2p to build a working network layer for p2p applications.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	HostName:         "127.0.0.1",
	Port:             7000,
	ExternalHostName: "",
	ExternalPort:     7000,
	SecureIO:         false,
	Gossip:           false,
	ConnectTimeout:   time.Minute,
	MasterKey:        "",
	PrivateKey:       "",
	ConnLowWater:     200,
	ConnHighWater:    500,
	ConnGracePeriod:  0,
}

DefaultConfig is a set of default configs

Functions

func EnsureIPv4

func EnsureIPv4(ipOrHost string) (string, error)

EnsureIPv4 returns an IPv4 address regardless the input is a IPv4 address or host name. If the host name has multiple IPv4 address be associated, a random one will be returned.

func GetBroadcastMsg

func GetBroadcastMsg(ctx context.Context) (*pubsub.Message, bool)

GetBroadcastMsg retrieves *pubsub.Message from broadcast message context.

func GetUnicastStream

func GetUnicastStream(ctx context.Context) (net.Stream, bool)

GetUnicastStream retrieves net.Stream from unicast request context.

func Logger

func Logger() *zap.Logger

Logger returns the logger

func SetLogger

func SetLogger(l *zap.Logger)

SetLogger sets the logger

Types

type Config

type Config struct {
	HostName         string        `yaml:"hostName"`
	Port             int           `yaml:"port"`
	ExternalHostName string        `yaml:"externalHostName"`
	ExternalPort     int           `yaml:"externalPort"`
	SecureIO         bool          `yaml:"secureIO"`
	Gossip           bool          `yaml:"gossip"`
	ConnectTimeout   time.Duration `yaml:"connectTimeout"`
	MasterKey        string        `yaml:"masterKey"`
	PrivateKey       string
	ConnLowWater     int           `yaml:"connLowWater"`
	ConnHighWater    int           `yaml:"connHighWater"`
	ConnGracePeriod  time.Duration `yaml:"connGracePeriod"`
	Bootnode         bool
}

Config enumerates the configs required by a host

type HandleBroadcast

type HandleBroadcast func(ctx context.Context, data []byte) error

HandleBroadcast defines the callback function triggered when a broadcast message reaches a host

type Host

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

Host is the main struct that represents a host that communicating with the rest of the P2P networks

func NewHost

func NewHost(ctx context.Context, options ...Option) (*Host, error)

NewHost constructs a host struct

func (*Host) AddBroadcastPubSub

func (h *Host) AddBroadcastPubSub(topic string, callback HandleBroadcast) error

AddBroadcastPubSub adds a broadcast topic that the host will pay attention to. This need to be called before using Connect/JoinOverlay. Otherwise, pubsub may not be aware of the existing overlay topology

func (*Host) AddUnicastPubSub

func (h *Host) AddUnicastPubSub(topic string, handler network.StreamHandler) error

AddUnicastPubSub adds a unicast topic that the host will pay attention to

func (*Host) Addresses

func (h *Host) Addresses() []multiaddr.Multiaddr

Addresses returns the multi address

func (*Host) Broadcast

func (h *Host) Broadcast(topic string, data []byte) error

Broadcast sends a message to the hosts who subscribe the topic

func (*Host) Close

func (h *Host) Close() error

Close closes the host

func (*Host) Connect

func (h *Host) Connect(ctx context.Context, target peer.AddrInfo) error

Connect connects a peer.

func (*Host) ConnectWithMultiaddr

func (h *Host) ConnectWithMultiaddr(ctx context.Context, ma multiaddr.Multiaddr) error

ConnectWithMultiaddr connects a peer given the multi address

func (*Host) DHT added in v0.4.0

func (h *Host) DHT() *dht.IpfsDHT

DHT returns the DHT of the host.

func (*Host) Discover added in v0.4.0

func (h *Host) Discover(ctx context.Context, dht *dht.IpfsDHT, rendezvous string)

Discover discovers peers on the network.

func (*Host) HostIdentity

func (h *Host) HostIdentity() string

HostIdentity returns the host identity string

func (*Host) Info

func (h *Host) Info() peer.AddrInfo

Info returns host's peer info.

func (*Host) JoinOverlay

func (h *Host) JoinOverlay(ctx context.Context)

JoinOverlay triggers the host to join the DHT overlay

func (*Host) Neighbors

func (h *Host) Neighbors(ctx context.Context) ([]peer.AddrInfo, error)

Neighbors returns the closest peer addresses

func (*Host) OverlayIdentity

func (h *Host) OverlayIdentity() string

OverlayIdentity returns the overlay identity string

func (*Host) PeerInfo added in v1.1.0

func (h *Host) PeerInfo() peerstore.Peerstore

PeerInfo returns the peer info for the node.

func (*Host) Unicast

func (h *Host) Unicast(ctx context.Context, target peer.AddrInfo, topic string, data []byte) (network.Stream, error)

Unicast sends a message to a peer on the given address. The returned stream can be used for reading but closed for writing when finished, the caller must close or reset the stream.

type HostOperator added in v0.3.3

type HostOperator interface {
	AddBroadcastPubSub(topic string, callback HandleBroadcast) error
	AddUnicastPubSub(topic string, callback network.StreamHandler) error
	Addresses() []multiaddr.Multiaddr
	Broadcast(topic string, data []byte) error
	Close() error
	ConnectWithMultiaddr(ctx context.Context, ma multiaddr.Multiaddr) error
	Connect(ctx context.Context, target peer.AddrInfo) error
	Discover(ctx context.Context, dht *dht.IpfsDHT, rendezvous string)
	DHT() *dht.IpfsDHT
	HostIdentity() string
	Info() peer.AddrInfo
	JoinOverlay(ctx context.Context)
	Neighbors(ctx context.Context) ([]peer.AddrInfo, error)
	OverlayIdentity() string
	Unicast(ctx context.Context, target peer.AddrInfo, topic string, data []byte) (network.Stream, error)
	PeerInfo() peerstore.Peerstore
}

HostOperator defines the peer to peer functionality available

type Option

type Option func(cfg *Config) error

Option defines the option function to modify the config for a host

func BootNode added in v0.4.0

func BootNode() Option

BootNode determines whether this libp2p host is a bootnode.

func ConnectTimeout

func ConnectTimeout(timout time.Duration) Option

ConnectTimeout is the option to override the connect timeout

func ExternalHostName

func ExternalHostName(externalHostName string) Option

ExternalHostName is the option to set the host name or IP address seen from external

func ExternalPort

func ExternalPort(externalPort int) Option

ExternalPort is the option to set the port number seen from external

func Gossip

func Gossip() Option

Gossip is to indicate using gossip protocol

func HostName

func HostName(hostName string) Option

HostName is the option to override the host name or IP address

func MasterKey

func MasterKey(masterKey string) Option

MasterKey is to determine network identifier

func Port

func Port(port int) Option

Port is the option to override the port number

func PrivateKey added in v0.3.1

func PrivateKey(privateKey string) Option

PrivateKey is to determine private key to use for addressing

func SecureIO

func SecureIO() Option

SecureIO is to indicate using secured I/O

func WithConnectionManagerConfig added in v0.2.3

func WithConnectionManagerConfig(lo, hi int, grace time.Duration) Option

WithConnectionManagerConfig set configuration for connection manager.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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