messenger

package
v0.0.0-...-c8fc2fc Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package messenger contains the default implementation for interface infra.Messenger. Sent and received messages must be one of the supported types below.

The following message types are valid messages. At the moment, no signature verification is performed on the SignedPld.

ChainRequest -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.ChainReq
Chain        -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.Chain
TRCRequest   -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.TRCReq
TRC          -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.TRC
PathRequest  -> ctrl.SignedPld/ctrl.Pld/path_mgmt.SegReq

The word "reliable" in method descriptions means a reliable protocol is used to deliver that message.

To start processing messages received via the Messenger, call ListenAndServe. The method runs in the current goroutine, and spawns new goroutines to handle each received message:

msger := New(...)
msger.ListenAndServe()

ListenAndServe will log errors for all received messages. To process messages, handlers need to be registered. Handlers allow different infrastructure servers to choose which requests they service, and to exploit shared functionality. One handler can be registered for each message type, identified by its msgType string:

msger.AddHandler(ChainRequest, MyCustomHandler)
msger.AddHandler(TRCRequest, MyOtherCustomHandler)

Each handler runs indepedently (i.e., without any synchronization) until completion. Goroutines inherit a reference to the Messenger via the infra.MessengerContextKey context key. This allows handlers to directly send network messages.

Some default handlers are already implemented; for more information, see their package documentation:

trust.*Store.NewChainReqHandler
trust.*Store.NewTRCReqHandler
trust.*Store.NewPushChainHandler
trust.*Store.NewPushTRCHandler

Shut down the server and any running handlers using CloseServer():

msger.CloseServer()

CloseServer() does not do graceful shutdown of the handlers and does not close the Messenger itself.

Index

Constants

View Source
const (
	ChainRequest = "ChainRequest"
	Chain        = "Chain"
	TRCRequest   = "TRCRequest"
	TRC          = "TRC"
)

Variables

View Source
var DefaultAdapter = &Adapter{}

Default adapter

Functions

This section is empty.

Types

type Adapter

type Adapter struct{}

Adapter implements disp.MessageAdapter for ctrl.SignedPld.

func (*Adapter) MsgKey

func (a *Adapter) MsgKey(msg proto.Cerealizable) string

func (*Adapter) MsgToRaw

func (a *Adapter) MsgToRaw(msg proto.Cerealizable) (common.RawBytes, error)

func (*Adapter) RawToMsg

func (a *Adapter) RawToMsg(b common.RawBytes) (proto.Cerealizable, error)

type Messenger

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

Messenger exposes the API for sending and receiving CtrlPld messages.

func New

func New(dispatcher *disp.Dispatcher, store infra.TrustStore, logger log.Logger) *Messenger

New creates a new Messenger that uses dispatcher for sending and receiving messages, and trustStore as crypto information database.

func (*Messenger) AddHandler

func (m *Messenger) AddHandler(msgType string, handler infra.Handler)

AddHandler registers a handler for msgType.

func (*Messenger) CloseServer

func (m *Messenger) CloseServer() error

CloseServer stops any running ListenAndServe functions, and cancels all running handlers. The server's Messenger layer is not closed.

func (*Messenger) GetCertChain

func (m *Messenger) GetCertChain(ctx context.Context, msg *cert_mgmt.ChainReq,
	a net.Addr, id uint64) (*cert_mgmt.Chain, error)

GetCertChain sends a cert_mgmt.ChainReq to address a, blocks until it receives a reply and returns the reply.

func (*Messenger) GetPaths

func (m *Messenger) GetPaths(ctx context.Context, msg *path_mgmt.SegReq,
	a net.Addr, id uint64) (*path_mgmt.SegReply, error)

GetPaths asks the server at the remote address for the paths specified by msg, and returns a verified reply.

func (*Messenger) GetTRC

func (m *Messenger) GetTRC(ctx context.Context, msg *cert_mgmt.TRCReq,
	a net.Addr, id uint64) (*cert_mgmt.TRC, error)

GetTRC sends a cert_mgmt.TRCReq request to address a, blocks until it receives a reply and returns the reply.

func (*Messenger) ListenAndServe

func (m *Messenger) ListenAndServe()

ListenAndServe starts listening and serving messages on srv's Messenger interface. The function runs in the current goroutine. Multiple ListenAndServe methods can run in parallel.

func (*Messenger) SendCertChain

func (m *Messenger) SendCertChain(ctx context.Context, msg *cert_mgmt.Chain, a net.Addr,
	id uint64) error

SendCertChain sends a reliable cert_mgmt.Chain to address a.

func (*Messenger) SendTRC

func (m *Messenger) SendTRC(ctx context.Context, msg *cert_mgmt.TRC, a net.Addr, id uint64) error

SendTRC sends a reliable cert_mgmt.TRC to address a.

Jump to

Keyboard shortcuts

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