wire

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: Apache-2.0 Imports: 13 Imported by: 20

Documentation

Overview

Package wire contains the basic wire communication infrastructure like wire message en- and decoding. The actual (de)serialization functions are found in package pkg/io.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(msg Msg, w io.Writer) error

Encode encodes a message into an io.Writer. It also encodes the message type whereas the Msg.Encode implementation is assumed not to write the type.

func RegisterDecoder added in v0.4.0

func RegisterDecoder(t Type, decoder func(io.Reader) (Msg, error))

RegisterDecoder sets the decoder of messages of Type `t`.

func RegisterExternalDecoder added in v0.4.0

func RegisterExternalDecoder(t Type, decoder func(io.Reader) (Msg, error), name string)

RegisterExternalDecoder sets the decoder of messages of external type `t`. This is like RegisterDecoder but for message types not part of the Perun wire protocol and thus not known natively. This can be used by users of the framework to create additional message types and send them over the same peer connection. It also comes in handy to register types for testing.

func TestMsg added in v0.4.0

func TestMsg(t *testing.T, msg Msg)

TestMsg performs generic tests on a wire.Msg object.

Types

type Account added in v0.4.0

type Account = wallet.Account

Account is a node's permanent Perun identity, which is used to establish authenticity within the Perun peer-to-peer network. For now, it is just a stub.

type Address added in v0.4.0

type Address = wallet.Address

Address is a Perun node's public Perun address, which is used as a permanent identity within the Perun peer-to-peer network. For now, it is just a stub.

func DecodeAddress added in v0.4.0

func DecodeAddress(r stdio.Reader) (Address, error)

DecodeAddress decodes a peer address.

type Addresses added in v0.4.0

type Addresses = wallet.Addresses

Addresses is a helper type for encoding and decoding address slices in situations where the length of the slice is known.

type AddressesWithLen added in v0.4.0

type AddressesWithLen = wallet.AddressesWithLen

AddressesWithLen is a helper type for encoding and decoding address slices of unknown length.

type AuthResponseMsg added in v0.4.0

type AuthResponseMsg struct {
}

AuthResponseMsg is the response message in the peer authentication protocol.

This will be expanded later to contain signatures.

func (*AuthResponseMsg) Decode added in v0.4.0

func (m *AuthResponseMsg) Decode(r io.Reader) (err error)

Decode decodes an AuthResponseMsg from an io.Reader.

func (*AuthResponseMsg) Encode added in v0.4.0

func (m *AuthResponseMsg) Encode(w io.Writer) error

Encode encodes this AuthResponseMsg into an io.Writer.

func (*AuthResponseMsg) Type added in v0.4.0

func (m *AuthResponseMsg) Type() Type

Type returns AuthResponse.

type Bus added in v0.4.0

type Bus interface {
	Publisher

	// SubscribeClient should route all messages with clientAddr as recipient to
	// the provided Consumer. Every address may only be subscribed to once.
	SubscribeClient(c Consumer, clientAddr Address) error
}

A Bus is a central message bus over which all clients of a channel network communicate. It is used as the transport layer abstraction for the client.Client.

type Cache added in v0.4.0

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

Cache is a message cache. The default value is a valid empty cache.

func (*Cache) Cache added in v0.4.0

func (c *Cache) Cache(ctx context.Context, p Predicate)

Cache is a message cache. The default value is a valid empty cache.

func (*Cache) Flush added in v0.4.0

func (c *Cache) Flush()

Flush empties the message cache and removes all predicates.

func (*Cache) Get added in v0.4.0

func (c *Cache) Get(p Predicate) []*Envelope

Get retrieves all messages from the cache that match the predicate. They are removed from the Cache.

func (*Cache) Put added in v0.4.0

func (c *Cache) Put(e *Envelope) bool

Put puts the message into the cache if it matches any active predicate. If it matches several predicates, it is still only added once to the cache.

func (*Cache) Size added in v0.4.0

func (c *Cache) Size() int

Size returns the number of messages held in the message cache.

type Cacher added in v0.4.0

type Cacher interface {
	// Cache should enable the caching of messages
	Cache(context.Context, Predicate)
}

A Cacher has the Cache method to enable caching of messages.

type Consumer added in v0.4.0

type Consumer interface {
	// The producer calls OnClose() to unregister the Consumer after it is
	// closed.
	sync.OnCloser
	// Put is called by the emitter when relaying a message.
	Put(*Envelope)
}

Consumer consumes messages fed into it via Put().

type Envelope added in v0.4.0

type Envelope struct {
	Sender    Address // Sender of the message.
	Recipient Address // Recipient of the message.
	// Msg contained in this Envelope. Not embedded so Envelope doesn't implement Msg.
	Msg Msg
}

An Envelope encapsulates a message with routing information, that is, the sender and intended recipient.

func (*Envelope) Decode added in v0.4.0

func (env *Envelope) Decode(r io.Reader) (err error)

Decode decodes an Envelope from an io.Reader.

func (*Envelope) Encode added in v0.4.0

func (env *Envelope) Encode(w io.Writer) error

Encode encodes an Envelope into an io.Writer.

type LocalBus added in v0.4.0

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

LocalBus is a bus that only sends message in the same process.

func NewLocalBus added in v0.4.0

func NewLocalBus() *LocalBus

NewLocalBus creates a new local bus, which only targets receivers that lie within the same process.

func (*LocalBus) Publish added in v0.4.0

func (h *LocalBus) Publish(ctx context.Context, e *Envelope) error

Publish implements wire.Bus.Publish. It returns only once the recipient received the message or the context times out.

func (*LocalBus) SubscribeClient added in v0.4.0

func (h *LocalBus) SubscribeClient(c Consumer, receiver Address) error

SubscribeClient implements wire.Bus.SubscribeClient. There can only be one subscription per receiver address. When the Consumer closes, its subscription is removed.

type Msg added in v0.4.0

type Msg interface {
	// Type returns the message's type.
	Type() Type
	// encoding of payload. Type byte should not be encoded.
	perunio.Encoder
}

Msg is the top-level abstraction for all messages sent between Perun nodes.

func Decode

func Decode(r io.Reader) (Msg, error)

Decode decodes a message from an io.Reader.

func NewAuthResponseMsg added in v0.4.0

func NewAuthResponseMsg(_ Account) Msg

NewAuthResponseMsg creates an authentication response message.

type PingMsg added in v0.4.0

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

PingMsg is a ping request. It contains the time at which it was sent, so that the recipient can also measure the time it took to transmit the ping request.

func NewPingMsg added in v0.4.0

func NewPingMsg() *PingMsg

NewPingMsg creates a new Ping message.

func (*PingMsg) Decode added in v0.4.0

func (m *PingMsg) Decode(reader io.Reader) error

func (PingMsg) Encode added in v0.4.0

func (m PingMsg) Encode(writer io.Writer) error

func (*PingMsg) Type added in v0.4.0

func (m *PingMsg) Type() Type

Type returns Ping.

type PongMsg added in v0.4.0

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

PongMsg is the response to a ping message. It contains the time at which it was sent, so that the recipient knows how long the ping request took to be transmitted, and how quickly the response was sent.

func NewPongMsg added in v0.4.0

func NewPongMsg() *PongMsg

NewPongMsg creates a new Pong message.

func (*PongMsg) Decode added in v0.4.0

func (m *PongMsg) Decode(reader io.Reader) error

func (PongMsg) Encode added in v0.4.0

func (m PongMsg) Encode(writer io.Writer) error

func (*PongMsg) Type added in v0.4.0

func (m *PongMsg) Type() Type

Type returns Pong.

type Predicate added in v0.4.0

type Predicate = func(*Envelope) bool

A Predicate defines a message filter.

type Publisher added in v0.4.0

type Publisher interface {
	// Publish should return nil when the message was delivered (outgoing) or is
	// guaranteed to be eventually delivered (cached), depending on the goal of the
	// implementation.
	Publish(context.Context, *Envelope) error
}

A Publisher allows to publish a message in a messaging network.

type Receiver added in v0.4.0

type Receiver struct {
	sync.Closer
	// contains filtered or unexported fields
}

Receiver is a helper object that can subscribe to different message categories from multiple peers. Receivers must only be used by a single execution context at a time. If multiple contexts need to access a peer's messages, then multiple receivers have to be created.

func NewReceiver added in v0.4.0

func NewReceiver() *Receiver

NewReceiver creates a new receiver.

func (*Receiver) Next added in v0.4.0

func (r *Receiver) Next(ctx context.Context) (*Envelope, error)

Next returns a channel to the next message.

func (*Receiver) Put added in v0.4.0

func (r *Receiver) Put(e *Envelope)

Put puts a new message into the queue.

type Relay added in v0.4.0

type Relay struct {
	sync.Closer
	// contains filtered or unexported fields
}

Relay handles (un)registering Consumers for a message Relay's messages.

func NewRelay added in v0.4.0

func NewRelay() *Relay

NewRelay returns a new Relay which logs unhandled messages.

func (*Relay) Cache added in v0.4.0

func (p *Relay) Cache(ctx context.Context, predicate Predicate)

Cache enables caching of messages that don't match any consumer. They are only cached if they match the given predicate, within the given context.

func (*Relay) Close added in v0.4.0

func (p *Relay) Close() error

Close closes the relay.

func (*Relay) Put added in v0.4.0

func (p *Relay) Put(e *Envelope)

Put puts an Envelope in the relay.

func (*Relay) SetDefaultMsgHandler added in v0.4.0

func (p *Relay) SetDefaultMsgHandler(handler func(*Envelope))

SetDefaultMsgHandler sets the default message handler.

func (*Relay) Subscribe added in v0.4.0

func (p *Relay) Subscribe(c Consumer, predicate Predicate) error

Subscribe adds a Consumer to the subscriptions. If the Consumer is already subscribed, Subscribe panics. If the producer is closed, Subscribe returns an error. Otherwise, Subscribe returns nil.

type ShutdownMsg added in v0.4.0

type ShutdownMsg struct {
	Reason string
}

ShutdownMsg is sent when orderly shutting down a connection.

func (*ShutdownMsg) Decode added in v0.4.0

func (m *ShutdownMsg) Decode(r io.Reader) error

Decode implements msg.Decode.

func (*ShutdownMsg) Encode added in v0.4.0

func (m *ShutdownMsg) Encode(w io.Writer) error

Encode implements msg.Encode.

func (*ShutdownMsg) Type added in v0.4.0

func (m *ShutdownMsg) Type() Type

Type implements msg.Type.

type Subscriber added in v0.4.0

type Subscriber interface {
	// Subscribe adds a Consumer to the subscriptions.
	// If the Consumer is already subscribed, Subscribe panics.
	Subscribe(Consumer, Predicate) error
}

A Subscriber allows to subscribe Consumers, which will receive messages that match a predicate.

type Type added in v0.4.0

type Type uint8

Type is an enumeration used for (de)serializing messages and identifying a message's Type.

const (
	Ping Type = iota
	Pong
	Shutdown
	AuthResponse
	LedgerChannelProposal
	ChannelProposalAcc
	ChannelProposalRej
	ChannelUpdate
	ChannelUpdateAcc
	ChannelUpdateRej
	ChannelSync
	LastType // upper bound on the message types of the Perun wire protocol
)

Enumeration of message categories known to the Perun framework.

func (Type) String added in v0.4.0

func (t Type) String() string

String returns the name of a message type if it is valid and name known or otherwise its numerical representation.

func (Type) Valid added in v0.4.0

func (t Type) Valid() bool

Valid checks whether a decoder is known for the type.

Directories

Path Synopsis
net
Package net contains the abstract communication logic between peers.
Package net contains the abstract communication logic between peers.
simple
Package simple contains simplistic implementation for the wire.Dialer and wire.Listener interfaces.
Package simple contains simplistic implementation for the wire.Dialer and wire.Listener interfaces.
test
Package test contains the testing types for wire/net.
Package test contains the testing types for wire/net.
Package test contains implementations of the peer interfaces that are useful for testing.
Package test contains implementations of the peer interfaces that are useful for testing.

Jump to

Keyboard shortcuts

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