swarm/pss: Message handler refactor (#18169)

This commit is contained in:
lash
2018-11-26 13:52:04 +01:00
committed by Anton Evangelatov
parent ca228569e4
commit 197d609b9a
10 changed files with 644 additions and 109 deletions

View File

@ -159,9 +159,39 @@ func (msg *PssMsg) String() string {
}
// Signature for a message handler function for a PssMsg
//
// Implementations of this type are passed to Pss.Register together with a topic,
type Handler func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error
type HandlerFunc func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error
type handlerCaps struct {
raw bool
prox bool
}
// Handler defines code to be executed upon reception of content.
type handler struct {
f HandlerFunc
caps *handlerCaps
}
// NewHandler returns a new message handler
func NewHandler(f HandlerFunc) *handler {
return &handler{
f: f,
caps: &handlerCaps{},
}
}
// WithRaw is a chainable method that allows raw messages to be handled.
func (h *handler) WithRaw() *handler {
h.caps.raw = true
return h
}
// WithProxBin is a chainable method that allows sending messages with full addresses to neighbourhoods using the kademlia depth as reference
func (h *handler) WithProxBin() *handler {
h.caps.prox = true
return h
}
// the stateStore handles saving and loading PSS peers and their corresponding keys
// it is currently unimplemented