messenger

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2018 License: Apache-2.0 Imports: 20 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:

infra.ChainRequest        -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.ChainReq
infra.Chain               -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.Chain
infra.TRCRequest          -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.TRCReq
infra.TRC                 -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.TRC
infra.PathSegmentRequest  -> ctrl.SignedPld/ctrl.Pld/path_mgmt.SegReq
infra.PathSegmentReply    -> ctrl.SignedPld/ctrl.Pld/path_mgmt.SegReply
infra.ChainIssueRequest   -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.ChainIssReq
infra.ChainIssueReply     -> ctrl.SignedPld/ctrl.Pld/cert_mgmt.ChainIssRep

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(infra.ChainRequest, MyCustomHandler)
msger.AddHandler(infra.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.NewChainPushHandler
trust.*Store.NewTRCPushHandler

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 (
	DefaultHandlerTimeout = 10 * time.Second
)

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 Config added in v0.1.1

type Config struct {
	// HandlerTimeout is the amount of time allocated to the processing of a
	// received message. This includes the time needed to verify the signature
	// and the execution of a registered handler (if one exists). If the
	// timeout is 0, the default is used.
	HandlerTimeout time.Duration
	// DisableSignatureVerification can be set to true to disable the
	// verification of the top level signature in received signed control
	// payloads.
	DisableSignatureVerification bool
}

Config can be used to customize the behavior of the Messenger.

type Counter

type Counter uint64

func (*Counter) Next

func (c *Counter) Next() uint64

Next is a concurrency-safe generator of unique request IDs for the messenger.

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,
	config *Config) *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 infra.MessageType, 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) GetPathSegs

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

GetPathSegs asks the server at the remote address for the path segments that satisfy 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) RequestChainIssue added in v0.1.1

func (m *Messenger) RequestChainIssue(ctx context.Context, msg *cert_mgmt.ChainIssReq, a net.Addr,
	id uint64) (*cert_mgmt.ChainIssRep, error)

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) SendChainIssueReply added in v0.1.1

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

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.

func (*Messenger) UpdateSigner added in v0.1.1

func (m *Messenger) UpdateSigner(signer ctrl.Signer, types []infra.MessageType)

UpdateSigner enables signing of messages with signer. Only the messages in types are signed, the rest are left with a null signature. If types is nil, only the signer is updated and the existing internal list of types is unchanged. An empty slice of types disables signing for all messages.

func (*Messenger) UpdateVerifier added in v0.1.1

func (m *Messenger) UpdateVerifier(verifier ctrl.SigVerifier)

UpdateVerifier enables verifying of messages with verifier.

FIXME(scrye): Verifiers are usually bound to a trust store to which the messenger already holds a reference. We should decouple the trust store from either one or the other.

type MockAddress

type MockAddress struct{}

func (*MockAddress) Network

func (f *MockAddress) Network() string

func (*MockAddress) String

func (f *MockAddress) String() string

type MockMessenger

type MockMessenger struct {
	TRCs   map[addr.ISD]*trc.TRC
	Chains map[addr.IA]*cert.Chain
}

func (*MockMessenger) AddHandler

func (m *MockMessenger) AddHandler(msgType infra.MessageType, h infra.Handler)

func (*MockMessenger) CloseServer

func (m *MockMessenger) CloseServer() error

func (*MockMessenger) GetCertChain

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

func (*MockMessenger) GetPathSegs

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

func (*MockMessenger) GetTRC

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

func (*MockMessenger) ListenAndServe

func (m *MockMessenger) ListenAndServe()

func (*MockMessenger) RecvMsg

func (*MockMessenger) RequestChainIssue added in v0.1.1

func (m *MockMessenger) RequestChainIssue(ctx context.Context, msg *cert_mgmt.ChainIssReq,
	a net.Addr, id uint64) (*cert_mgmt.ChainIssRep, error)

func (*MockMessenger) SendCertChain

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

func (*MockMessenger) SendChainIssueReply added in v0.1.1

func (m *MockMessenger) SendChainIssueReply(ctx context.Context, msg *cert_mgmt.ChainIssRep,
	a net.Addr, id uint64) error

func (*MockMessenger) SendTRC

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

Jump to

Keyboard shortcuts

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