p2p

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2021 License: Apache-2.0 Imports: 34 Imported by: 0

README

1.tls

Documentation

Index

Constants

View Source
const (
	DefaultStrategy           FilterStrategy = "DefaultStrategy"
	BucketsStrategy                          = "BucketsStrategy"
	NearestBucketStrategy                    = "NearestBucketStrategy"
	BucketsWithFactorStrategy                = "BucketsWithFactorStrategy"
	CorePeersStrategy                        = "CorePeersStrategy"
)

supported filter strategies

View Source
const (
	MessageVersion1 = "1.0.0"
	MessageVersion2 = "2.0.0"
	MessageVersion3 = "3.0.0"
)

define message versions

Variables

View Source
var (
	ErrSubscriber     = errors.New("subscribe error")
	ErrRegistered     = errors.New("subscriber already registered")
	ErrMessageEmpty   = errors.New("message empty")
	ErrMessageHandled = errors.New("message handled")
	ErrStreamNil      = errors.New("stream is nil")
	ErrNotRegister    = errors.New("message not register")
)
View Source
var (
	ErrMessageChecksum   = errors.New("verify checksum error")
	ErrMessageDecompress = errors.New("decompress error")
	ErrMessageUnmarshal  = errors.New("message unmarshal error")
)
View Source
var (
	ErrHandlerError    = errors.New("handler error")
	ErrResponseNil     = errors.New("handler response is nil")
	ErrStreamSendError = errors.New("send response error")
	ErrChannelBlock    = errors.New("channel block")
)
View Source
var (
	// Metrics is the default instance of metrics. It is intended
	// to be used in conjunction the default Prometheus metrics
	// registry.
	Metrics = newMetrics()
)

Functions

func Checksum

func Checksum(msg *pb.XuperMessage) uint32

Checksum calculate checksum of message

func Compress

func Compress(msg *pb.XuperMessage) *pb.XuperMessage

Compressed compress msg

func Decompress

func Decompress(msg *pb.XuperMessage) ([]byte, error)

Decompress decompress msg

func GenerateKeyPairWithPath

func GenerateKeyPairWithPath(path string) error

GenerateKeyPairWithPath generate xuper net key pair

func GenerateNetKeyFromPemKey

func GenerateNetKeyFromPemKey(path string) error

GenerateNetKeyFromPemKey get net private key from pem format private key

func GeneratePemKeyFromNetKey

func GeneratePemKeyFromNetKey(path string) error

GeneratePemKeyFromNetKey get pem format private key from net private key

func GenerateUniqueRandList

func GenerateUniqueRandList(size int, max int) []int

GenerateUniqueRandList get a random unique number list

func GetKeyPairFromPath

func GetKeyPairFromPath(path string) (crypto.PrivKey, error)

GetKeyPairFromPath get xuper net key from file path

func GetPeerIDByAddress

func GetPeerIDByAddress(peerAddr string) (peer.ID, error)

GetPeerIDByAddress return peer ID corresponding to peerAddr

func GetPeerIDFromPath

func GetPeerIDFromPath(path string) (string, error)

GetPeerIDFromPath return peer id of given private key path

func GetPemKeyPairFromPath

func GetPemKeyPairFromPath(path string) (crypto.PrivKey, error)

GetPemKeyPairFromPath get xuper pem private key from file path

func GetRespMessageType

func GetRespMessageType(msgType pb.XuperMessage_MessageType) pb.XuperMessage_MessageType

GetRespMessageType get the message type

func MessageKey

func MessageKey(msg *pb.XuperMessage) string

func NewMessage

func NewMessage(typ pb.XuperMessage_MessageType, message proto.Message, opts ...MessageOption) *pb.XuperMessage

NewMessage create P2P message instance with given params

func NewTLS

func NewTLS(path, serviceName string) (credentials.TransportCredentials, error)

func Unmarshal

func Unmarshal(msg *pb.XuperMessage, message proto.Message) error

Unmarshal unmarshal msgInfo

func VerifyChecksum

func VerifyChecksum(msg *pb.XuperMessage) bool

VerifyChecksum verify the checksum of message

func VerifyMessageType

func VerifyMessageType(request *pb.XuperMessage, response *pb.XuperMessage, peerID string) bool

VerifyMessageType 用于带返回的请求场景下验证收到的消息是否为预期的消息

Types

type Dispatcher

type Dispatcher interface {
	Register(sub Subscriber) error
	UnRegister(sub Subscriber) error

	// Dispatch dispatch message to registered subscriber
	Dispatch(*pb.XuperMessage, Stream) error
}

Dispatcher

func NewDispatcher

func NewDispatcher(ctx *nctx.NetCtx) Dispatcher

type FilterStrategy

type FilterStrategy string

FilterStrategy defines the supported filter strategies

type HandleFunc

type HandleFunc func(xctx.XContext, *pb.XuperMessage) (*pb.XuperMessage, error)

type MessageOption

type MessageOption func(*pb.XuperMessage)

func WithBCName

func WithBCName(bcname string) MessageOption

func WithErrorType

func WithErrorType(errorType pb.XuperMessage_ErrorType) MessageOption

func WithLogId

func WithLogId(logid string) MessageOption

WithLogId set message logId

func WithVersion

func WithVersion(version string) MessageOption

type Option

type Option struct {
	Filters   []FilterStrategy
	Addresses []string
	PeerIDs   []string
	Accounts  []string

	WhiteList map[string]bool

	Percent float32 // percent wait for return
}

func Apply

func Apply(optFunc []OptionFunc) *Option

Apply apply OptionFunc

type OptionFunc

type OptionFunc func(*Option)

OptionFunc define single Option function for send message

func WithAccounts

func WithAccounts(accounts []string) OptionFunc

func WithAddresses

func WithAddresses(addresses []string) OptionFunc

WithAddresses add target peer addresses to message Option

func WithFilter

func WithFilter(filters []FilterStrategy) OptionFunc

WithFilter add filter strategies to message Option

func WithPeerIDs

func WithPeerIDs(peerIDs []string) OptionFunc

WithPeerIDs add target peer IDs to message Option

func WithPercent

func WithPercent(percent float32) OptionFunc

WithPercent add percentage to message Option

func WithWhiteList

func WithWhiteList(whiteList map[string]bool) OptionFunc

WithWhiteList add whiteList

type Server

type Server interface {
	Init(*nctx.NetCtx) error
	Start()
	Stop()

	NewSubscriber(pb.XuperMessage_MessageType, interface{}, ...SubscriberOption) Subscriber
	Register(Subscriber) error
	UnRegister(Subscriber) error

	SendMessage(xctx.XContext, *pb.XuperMessage, ...OptionFunc) error
	SendMessageWithResponse(xctx.XContext, *pb.XuperMessage, ...OptionFunc) ([]*pb.XuperMessage, error)

	Context() *nctx.NetCtx

	PeerInfo() pb.PeerInfo
}

P2P is the p2p server interface

type Stream

type Stream interface {
	Send(*pb.XuperMessage) error
}

Stream send p2p response message

type Subscriber

type Subscriber interface {
	GetMessageType() pb.XuperMessage_MessageType
	Match(*pb.XuperMessage) bool
	HandleMessage(xctx.XContext, *pb.XuperMessage, Stream) error
}

Subscriber is the interface for p2p message subscriber

func NewSubscriber

func NewSubscriber(ctx *nctx.NetCtx, typ pb.XuperMessage_MessageType,
	v interface{}, opts ...SubscriberOption) Subscriber

type SubscriberOption

type SubscriberOption func(*subscriber)

func WithFilterBCName

func WithFilterBCName(bcName string) SubscriberOption

func WithFilterFrom

func WithFilterFrom(from string) SubscriberOption

Jump to

Keyboard shortcuts

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