net

package
v0.0.0-...-84d53aa Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2019 License: GPL-3.0 Imports: 35 Imported by: 0

README

net

Documentation

Index

Constants

View Source
const (
	DefaultConnMgrHighWater    = 900
	DefaultConnMgrLowWater     = 600
	DefaultConnMgrGracePeriod  = time.Second * 20
	DefaultMaxReadConcurrency  = 100
	DefaultMaxWriteConcurrency = 100
)

DefaultConnMgrHighWater is the default value for the connection managers values from "ipfs/go-ipfs-config"

View Source
const (
	MaxMessageSize = inet.MessageSizeMax
	StreamTTL      = 10 * time.Second
)

Network system parameters

View Source
const (
	MessagePriorityHigh = iota
	MessagePriorityNormal
	MessagePriorityLow
)

Message Priority.

View Source
const (
	DefaultPrivateKeyFile  = "network.key"
	DefaultBootstrapPeriod = 30 * time.Second
	DefaultCacheFile       = "network.cache"
	DefaultCachePeriod     = 3 * time.Minute
)

Default Config

View Source
const (
	MedProtocolID    = "/med/1.0.0"
	MedDHTProtocolID = "/med/kad/1.0.0"
)

Protocols

View Source
const (
	MessageWeightZero = MessageWeight(0)
	MessageWeightNewTx
	MessageWeightNewBlock = MessageWeight(0.5)
	MessageWeightRouteTable
	MessageWeightChainChunks
	MessageWeightChainChunkData
)

const

Variables

View Source
var (
	ErrNoPeersToSendMessage = errors.New("filtered peer connection does not existed")
	ErrContextDone          = errors.New("context is done")
)

Error types

View Source
var (
	ErrListenPortIsNotAvailable = errors.New("listen port is not available")
	ErrConfigLackNetWork        = errors.New("config.conf should have network")
)

Errors

Functions

func LoadNetworkKeyFromFile

func LoadNetworkKeyFromFile(path string) (libcrypto.PrivKey, error)

LoadNetworkKeyFromFile load network priv key from file.

func LoadOrNewNetworkKey

func LoadOrNewNetworkKey(cfg *medletpb.Config) (libcrypto.PrivKey, error)

LoadOrNewNetworkKey load network priv key from file or create new one.

func MarshalNetworkKey

func MarshalNetworkKey(key libcrypto.PrivKey) (string, error)

MarshalNetworkKey marshal network key.

func PeerInfoToProto

func PeerInfoToProto(p peerstore.PeerInfo) *netpb.PeerInfo

PeerInfoToProto convert peerinfo to peerinfo for config file

func PeersToProto

func PeersToProto(peers ...peerstore.PeerInfo) []*netpb.PeerInfo

PeersToProto convert peers to peer list for config

func UnmarshalNetworkKey

func UnmarshalNetworkKey(data string) (libcrypto.PrivKey, error)

UnmarshalNetworkKey unmarshal network key.

Types

type BloomFilter

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

BloomFilter manages bloom filter

func NewBloomFilter

func NewBloomFilter(m uint, k uint, maxCount int) *BloomFilter

NewBloomFilter returns BloomFilter

func (*BloomFilter) HasKey

func (bf *BloomFilter) HasKey(key string) bool

HasKey use bloom filter to check if the key exists quickly

func (*BloomFilter) HasRecvMessage

func (bf *BloomFilter) HasRecvMessage(msg *SendMessage) bool

HasRecvMessage use bloom filter sender check if the key exists quickly

func (*BloomFilter) RecordKey

func (bf *BloomFilter) RecordKey(key string)

RecordKey add key to bloom filter.

func (*BloomFilter) RecordRecvMessage

func (bf *BloomFilter) RecordRecvMessage(msg *RecvMessage)

RecordRecvMessage records received message

type BootstrapConfig

type BootstrapConfig struct {
	MinConnThreshold  uint32
	Period            time.Duration
	ConnectionTimeout time.Duration
	BootstrapPeers    []pstore.PeerInfo // seed host
}

BootstrapConfig is a struct for bootstrapping node

func NewBootstrapConfig

func NewBootstrapConfig(cfg *medletpb.Config) (bConfig *BootstrapConfig, err error)

NewBootstrapConfig return new BootstrapConfig

type ChainSyncPeersFilter

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

ChainSyncPeersFilter will filter some peers randomly

func (*ChainSyncPeersFilter) Filter

func (filter *ChainSyncPeersFilter) Filter(peers []peer.ID) []peer.ID

Filter implements PeerFilterAlgorithm interface

func (*ChainSyncPeersFilter) SetExcludedPIDs

func (filter *ChainSyncPeersFilter) SetExcludedPIDs(excludedPIDs map[string]struct{})

SetExcludedPIDs set excludedPIDs

type Dispatcher

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

Dispatcher a message dispatcher service.

func NewDispatcher

func NewDispatcher() *Dispatcher

NewDispatcher create Dispatcher instance.

func (*Dispatcher) Deregister

func (dp *Dispatcher) Deregister(subscribers ...*Subscriber)

Deregister deregister subscribers.

func (*Dispatcher) ReceivedMessageCh

func (dp *Dispatcher) ReceivedMessageCh() chan<- Message

ReceivedMessageCh return dispatcher's received message channel

func (*Dispatcher) Register

func (dp *Dispatcher) Register(subscribers ...*Subscriber)

Register register subscribers.

func (*Dispatcher) Start

func (dp *Dispatcher) Start(ctx context.Context)

Start start message dispatch goroutine.

type MedService

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

MedService service for medibloc p2p network

func NewNetService

func NewNetService(cfg *medletpb.Config) (*MedService, error)

NewNetService create netService

func (*MedService) Broadcast

func (ms *MedService) Broadcast(msgType string, data []byte, priority int)

Broadcast message.

func (*MedService) ClosePeer

func (ms *MedService) ClosePeer(peerID string, reason error)

ClosePeer close connection

func (*MedService) Deregister

func (ms *MedService) Deregister(subscribers ...*Subscriber)

Deregister Deregister the subscribers.

func (*MedService) Node

func (ms *MedService) Node() *Node

Node return the peer host

func (*MedService) Register

func (ms *MedService) Register(subscribers ...*Subscriber)

Register register the subscribers.

func (*MedService) RequestAndResponse

func (ms *MedService) RequestAndResponse(ctx context.Context, query Query, f MessageCallback, filter PeerFilterAlgorithm) (bool, []error)

RequestAndResponse set id to query and send to peers

func (*MedService) SendMessageToPeer

func (ms *MedService) SendMessageToPeer(msgType string, data []byte, priority int, peerID string)

SendMessageToPeer send message to a peer.

func (*MedService) SendMessageToPeers

func (ms *MedService) SendMessageToPeers(msgType string, data []byte, priority int, filter PeerFilterAlgorithm) []string

SendMessageToPeers send message to peers.

func (*MedService) SendPbMessageToPeer

func (ms *MedService) SendPbMessageToPeer(msgType string, pb proto.Message, priority int, peerID string)

SendPbMessageToPeer send protobuf message to peer

func (*MedService) SendPbMessageToPeers

func (ms *MedService) SendPbMessageToPeers(msgType string, pb proto.Message, priority int, filter PeerFilterAlgorithm) []string

SendPbMessageToPeers send protobuf messages to filtered peers

func (*MedService) Start

func (ms *MedService) Start(ctx context.Context) error

Start start p2p manager.

type Message

type Message interface {
	MessageType() string
	MessageFrom() string
	Data() []byte
	Hash() string
}

Message interface for message.

type MessageCallback

type MessageCallback func(q Query, message Message) error

MessageCallback is a function for handle response message

type MessageWeight

type MessageWeight float64

MessageWeight float64

type Node

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

Node the host can be used as both the client and the server

func NewNode

func NewNode(cfg *medletpb.Config, recvMessageCh chan<- Message) (*Node, error)

NewNode return new Node according to the config.

func (*Node) Addrs

func (node *Node) Addrs() []multiaddr.Multiaddr

Addrs returns opened addrs (w/o p2p-circuit)

func (*Node) Bootstrap

func (node *Node) Bootstrap()

Bootstrap run bootstrap

func (*Node) BroadcastMessage

func (node *Node) BroadcastMessage(msgType string, data []byte, priority int)

BroadcastMessage broadcast message.

func (*Node) ClosePeer

func (node *Node) ClosePeer(peerID string, reason error)

ClosePeer close the connection to peer

func (*Node) Connected

func (node *Node) Connected() []peer.ID

Connected returns connected peer ids

func (*Node) DHT

func (node *Node) DHT() *dht.IpfsDHT

DHT returns distributed hashed table

func (*Node) DHTSync

func (node *Node) DHTSync()

DHTSync run dht bootstrap

func (*Node) EstablishedPeersCount

func (node *Node) EstablishedPeersCount() int32

EstablishedPeersCount return handShakeSucceed steam count. Depreciated

func (*Node) PeersCount

func (node *Node) PeersCount() int32

PeersCount return stream count. Depreciated

func (*Node) SaveCache

func (node *Node) SaveCache()

SaveCache save host's peerstore to cache file

func (*Node) SendMessageToPeer

func (node *Node) SendMessageToPeer(msgType string, data []byte, priority int, peerID string)

SendMessageToPeer send message to a peer.

func (*Node) SendMessageToPeers

func (node *Node) SendMessageToPeers(msgType string, data []byte, priority int, filter PeerFilterAlgorithm) (peers []string)

SendMessageToPeers send messages to filtered peers

func (*Node) Start

func (node *Node) Start(ctx context.Context) error

Start host & route table discovery

type PeerFilterAlgorithm

type PeerFilterAlgorithm interface {
	Filter([]peer.ID) []peer.ID
}

PeerFilterAlgorithm is the algorithm used to filter peers

type Query

type Query interface {
	Hash() []byte
	MessageType() string
	ProtoBuf() proto.Message
	SetID(string)
}

Query is interface for send request/response message

type RandomPeerFilter

type RandomPeerFilter struct {
	N int
}

RandomPeerFilter will filter a peer randomly

func (*RandomPeerFilter) Filter

func (filter *RandomPeerFilter) Filter(peers []peer.ID) []peer.ID

Filter implements PeerFilterAlgorithm interface

type RecvMessage

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

RecvMessage is a struct for received message

func (*RecvMessage) Data

func (m *RecvMessage) Data() []byte

Data returns data

func (*RecvMessage) Hash

func (m *RecvMessage) Hash() string

Hash returns hash

func (*RecvMessage) MessageFrom

func (m *RecvMessage) MessageFrom() string

MessageFrom returns peer id of message sender

func (*RecvMessage) MessageType

func (m *RecvMessage) MessageType() string

MessageType returns message type

type SendMessage

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

SendMessage is a struct for sending message

func (*SendMessage) Bytes

func (m *SendMessage) Bytes() []byte

Bytes returns bytes

func (*SendMessage) Data

func (m *SendMessage) Data() []byte

Data returns data

func (*SendMessage) Hash

func (m *SendMessage) Hash() string

Hash returns hash

func (*SendMessage) MessageType

func (m *SendMessage) MessageType() string

MessageType returns message type

func (*SendMessage) SetReceiver

func (m *SendMessage) SetReceiver(receiver peer.ID)

SetReceiver sets receiver

type Service

type Service interface {
	Start(ctx context.Context) error

	Node() *Node

	// dispatcher
	Register(...*Subscriber)
	Deregister(...*Subscriber)

	// host
	SendMessageToPeer(msgType string, data []byte, priority int, peerID string)
	SendMessageToPeers(msgType string, data []byte, priority int, filter PeerFilterAlgorithm) []string
	Broadcast(msgType string, data []byte, priority int)
	ClosePeer(peerID string, reason error)
	SendPbMessageToPeer(msgType string, pb proto.Message, priority int, peerID string)
	SendPbMessageToPeers(msgType string, pb proto.Message, priority int, filter PeerFilterAlgorithm) []string
	RequestAndResponse(ctx context.Context, query Query, f MessageCallback, filter PeerFilterAlgorithm) (bool, []error)
}

Service net Service interface

type Subscriber

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

Subscriber subscriber.

func NewSubscriber

func NewSubscriber(id interface{}, msgChan chan Message, doFilter bool, msgType string, weight MessageWeight) *Subscriber

NewSubscriber return new Subscriber instance.

func (*Subscriber) DoFilter

func (s *Subscriber) DoFilter() bool

DoFilter return doFilter

func (*Subscriber) ID

func (s *Subscriber) ID() interface{}

ID return id.

func (*Subscriber) MessageChan

func (s *Subscriber) MessageChan() chan Message

MessageChan return msgChan.

func (*Subscriber) MessageType

func (s *Subscriber) MessageType() string

MessageType return msgTypes.

func (*Subscriber) MessageWeight

func (s *Subscriber) MessageWeight() MessageWeight

MessageWeight return weight of msgType

Directories

Path Synopsis
Package netpb is a generated protocol buffer package.
Package netpb is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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