discovery

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 21, 2019 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultNetID           = 3
	DefaultListenAddress   = "0.0.0.0:8483"
	PeerKeyFileName        = "peerKey"
	DefaultPort            = 8483
	DefaultListenInterface = "0.0.0.0"
	DefaultBucketSize      = 32
	DefaultBucketCount     = 32
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// ListenAddress is the network address where socket listen on, usually is the inner address
	// default value is "0.0.0.0:8483"
	ListenAddress string

	// PublicAddress is the network address can be access by other nodes, usually is the public Internet address
	PublicAddress string

	// DataDir is the directory to storing p2p data, if is null-string, will use memory as database
	DataDir string

	// PeerKey is to encrypt message, the corresponding public key use for NodeID, MUST NOT be revealed
	PeerKey string

	// BootNodes are roles as network entrance. Node can discovery more other nodes by send UDP query BootNodes,
	// but not create a TCP connection to BootNodes directly
	BootNodes []string

	// BootSeeds are the address where can query BootNodes, is a more flexible option than BootNodes
	BootSeeds []string

	// NetID is to mark which network our node in, nodes from different network can`t connect each other
	NetID int

	BucketCount int
	BucketSize  int
	// contains filtered or unexported fields
}

func (*Config) Ensure

func (cfg *Config) Ensure() (err error)

Ensure will set default value to missing fields and construct node. MUST be invoked before use. Will generate a random PeerKey and store in local file, `${DataDir}/peer.key`, if missing one.

func (*Config) Node

func (cfg *Config) Node() *vnode.Node

Node MUST NOT be invoked before Ensure

func (*Config) PrivateKey

func (cfg *Config) PrivateKey() ed25519.PrivateKey

type Discovery

type Discovery interface {
	Start() error
	Stop() error
	Delete(id vnode.NodeID, reason error)
	GetNodes(count int) []*vnode.Node
	Resolve(id vnode.NodeID) *vnode.Node
	AllNodes() []*vnode.Node
	SubscribeNode(receiver func(n *vnode.Node)) (subId int)
	Unsubscribe(subId int)
}

Discovery is the interface to discovery other node

func New

func New(cfg *Config, db NodeDB) Discovery

New create a Discovery implementation

type Finder

type Finder interface {
	Observer
	GetNodes(count int) []*vnode.Node
}

type Node

type Node struct {
	vnode.Node
	// contains filtered or unexported fields
}

type NodeDB

type NodeDB interface {
	StoreNode(node *vnode.Node) (err error)
	RemoveNode(ID vnode.NodeID)
	ReadNodes(count int, expiration time.Duration) []*vnode.Node
	RetrieveActiveAt(id vnode.NodeID) int64
	StoreActiveAt(id vnode.NodeID, v int64)
	RetrieveCheckAt(id vnode.NodeID) int64
	StoreCheckAt(id vnode.NodeID, v int64)
	Close() error
	Clean(expiration time.Duration)
}

type Observer

type Observer interface {
	Sub(sub Subscriber)
	UnSub(sub Subscriber)
}

type Request

type Request struct {
	Node  *vnode.Node `json:"node"`
	Count int         `json:"count"`
}

type Result

type Result struct {
	Code    int      `json:"code"`
	Message string   `json:"message"`
	Data    []string `json:"data"`
}

type Subscriber

type Subscriber interface {
	// Sub return the unique id
	Sub(receiver func(n *vnode.Node)) (subId int)
	// UnSub received the id returned by Sub
	UnSub(subId int)
}

Subscriber can be subscribed by observer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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