message

package
v0.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Package message provides insolar messaging protocol and serialization layer.

Message can be created with shortcut:

senderAddr, _ := node.NewAddress("127.0.0.1:1337")
receiverAddr, _ := node.NewAddress("127.0.0.1:1338")

sender := node.NewNode(senderAddr)
receiver := node.NewNode(receiverAddr)

msg := message.NewPingMessage(sender, receiver)

// do something with message

Or with builder:

builder := message.NewBuilder()

senderAddr, _ := node.NewAddress("127.0.0.1:1337")
receiverAddr, _ := node.NewAddress("127.0.0.1:1338")

sender := node.NewNode(senderAddr)
receiver := node.NewNode(receiverAddr)

msg := builder.
	Sender(sender).
	Receiver(receiver).
	Type(message.TypeFindNode).
	Request(&message.RequestDataFindNode{}).
	Build()

// do something with message

Message may be serialized:

msg := &message.Message{}
serialized, err := message.SerializeMessage(msg)

if err != nil {
	panic(err.Error())
}

fmt.Println(serialized)

And deserialized therefore:

var buffer bytes.Buffer

// Fill buffer somewhere

msg, err := message.DeserializeMessage(buffer)

if err != nil {
	panic(err.Error())
}

// do something with message

Index

Constants

View Source
const (
	// TypePing is message type for ping method.
	TypePing = messageType(iota + 1)
	// TypeStore is message type for store method.
	TypeStore
	// TypeFindNode is message type for FindNode method.
	TypeFindNode
	// TypeFindValue is message type for FindValue method.
	TypeFindValue
	// TypeRPC is message type for RPC method.
	TypeRPC
	// TypeRelay is message type for request target to be a relay
	TypeRelay
)
View Source
const (
	// Start - command start relay.
	Start = CommandType(iota + 1)
	// Stop - command stop relay.
	Stop
)

Variables

This section is empty.

Functions

func SerializeMessage

func SerializeMessage(q *Message) ([]byte, error)

SerializeMessage converts message to byte slice.

Types

type Builder

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

Builder allows lazy building of messages. Each operation returns new copy of a builder.

func NewBuilder

func NewBuilder() Builder

NewBuilder returns empty message builder.

func (Builder) Build

func (cb Builder) Build() (message *Message)

Build returns configured message.

func (Builder) Error

func (cb Builder) Error(err error) Builder

Error adds error description to message.

func (Builder) Receiver

func (cb Builder) Receiver(node *node.Node) Builder

Receiver sets message receiver.

func (Builder) Request

func (cb Builder) Request(request interface{}) Builder

Request adds request data to message.

func (Builder) Response

func (cb Builder) Response(response interface{}) Builder

Response adds response data to message

func (Builder) Sender

func (cb Builder) Sender(node *node.Node) Builder

Sender sets message sender.

func (Builder) Type

func (cb Builder) Type(messageType messageType) Builder

Type sets message type.

type CommandType

type CommandType int

CommandType - type for commands.

type Message

type Message struct {
	Sender    *node.Node
	Receiver  *node.Node
	Type      messageType
	RequestID RequestID

	Data       interface{}
	Error      error
	IsResponse bool
}

Message is DHT message object.

func DeserializeMessage

func DeserializeMessage(conn io.Reader) (*Message, error)

DeserializeMessage reads message from io.Reader.

func NewPingMessage

func NewPingMessage(sender, receiver *node.Node) *Message

NewPingMessage can be used as a shortcut for creating ping messages instead of message Builder.

func NewRelayMessage

func NewRelayMessage(command CommandType, sender, receiver *node.Node) *Message

NewRelayMessage uses for send a command to target node to make it as relay.

func (*Message) IsForMe

func (m *Message) IsForMe(origin node.Origin) bool

IsForMe checks if message is addressed to our node.

func (*Message) IsValid

func (m *Message) IsValid() (valid bool)

IsValid checks if message data is a valid structure for current message type.

type RequestDataFindNode

type RequestDataFindNode struct {
	Target []byte
}

RequestDataFindNode is data for FindNode request.

type RequestDataFindValue

type RequestDataFindValue struct {
	Target []byte
}

RequestDataFindValue is data for FindValue request.

type RequestDataRPC

type RequestDataRPC struct {
	Method string
	Args   [][]byte
}

RequestDataRPC is data for RPC request.

type RequestDataStore

type RequestDataStore struct {
	Data       []byte
	Publishing bool // Whether or not we are the original publisher.
}

RequestDataStore is data for Store request.

type RequestID

type RequestID uint64

RequestID is 64 bit unsigned int request id.

type RequestRelay

type RequestRelay struct {
	Command CommandType
}

RequestRelay is data for relay request (commands: start/stop relay).

type ResponseDataFindNode

type ResponseDataFindNode struct {
	Closest []*node.Node
}

ResponseDataFindNode is data for FindNode response.

type ResponseDataFindValue

type ResponseDataFindValue struct {
	Closest []*node.Node
	Value   []byte
}

ResponseDataFindValue is data for FindValue response.

type ResponseDataRPC

type ResponseDataRPC struct {
	Success bool
	Result  []byte
	Error   string
}

ResponseDataRPC is data for RPC response.

type ResponseDataStore

type ResponseDataStore struct {
	Success bool
}

ResponseDataStore is data for Store response.

type ResponseRelay

type ResponseRelay struct {
	Success bool
	State   relay.State
}

ResponseRelay is data for relay request response

Jump to

Keyboard shortcuts

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