message

package
v0.0.3 Latest Latest
Warning

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

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

Documentation

Overview

Package message provides network 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
	// TypeAuth is message type for authentication between nodes.
	TypeAuth
	// TypeCheckOrigin is message to check originality of some node.
	TypeCheckOrigin
	// TypeObtainIP is message to get itself IP from another node.
	TypeObtainIP
	// TypeRelayOwnership is message to say all other nodes that current node have a static IP.
	TypeRelayOwnership
	// TypeKnownOuterNodes is message to say how much outer nodes current node know.
	TypeKnownOuterNodes
)
View Source
const (
	// Unknown - unknown command.
	Unknown = CommandType(iota + 1)
	// StartRelay - command start relay.
	StartRelay
	// StopRelay - command stop relay.
	StopRelay
	// BeginAuth - begin authentication.
	BeginAuth
	// RevokeAuth - revoke authentication.
	RevokeAuth
)

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
	RemoteAddress string

	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 NewAuthMessage added in v0.0.3

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

NewAuthMessage uses for starting authentication.

func NewCheckOriginMessage added in v0.0.3

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

NewCheckOriginMessage uses for check originality.

func NewKnownOuterNodesMessage added in v0.0.3

func NewKnownOuterNodesMessage(sender, receiver *node.Node, nodes int) *Message

NewKnownOuterNodesMessage uses to notify all nodes in home subnet about known outer nodes.

func NewObtainIPMessage added in v0.0.3

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

NewObtainIPMessage uses for get self IP.

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 NewRelayOwnershipMessage added in v0.0.3

func NewRelayOwnershipMessage(sender, receiver *node.Node, ready bool) *Message

NewRelayOwnershipMessage uses for relay ownership request.

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 RequestAuth added in v0.0.3

type RequestAuth struct {
	Command CommandType
}

RequestAuth is data for authentication.

type RequestCheckOrigin added in v0.0.3

type RequestCheckOrigin struct {
}

RequestCheckOrigin is data to check originality.

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 RequestKnownOuterNodes added in v0.0.3

type RequestKnownOuterNodes struct {
	ID         string // origin ID
	OuterNodes int    // number of known outer nodes
}

RequestKnownOuterNodes is data to notify home subnet about known outer nodes.

type RequestObtainIP added in v0.0.3

type RequestObtainIP struct {
}

RequestObtainIP is data to obtain a self IP.

type RequestRelay

type RequestRelay struct {
	Command CommandType
}

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

type RequestRelayOwnership added in v0.0.3

type RequestRelayOwnership struct {
	Ready bool
}

RequestRelayOwnership is data to notify that current node can be a relay.

type ResponseAuth added in v0.0.3

type ResponseAuth struct {
	Success       bool
	AuthUniqueKey []byte
}

ResponseAuth is data for authentication request response.

type ResponseCheckOrigin added in v0.0.3

type ResponseCheckOrigin struct {
	AuthUniqueKey []byte
}

ResponseCheckOrigin is data for check originality request response.

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 ResponseKnownOuterNodes added in v0.0.3

type ResponseKnownOuterNodes struct {
	ID         string //	id of node in which more known outer nodes
	OuterNodes int    // number of known outer nodes
}

ResponseKnownOuterNodes is data to answer if origin node know more outer nodes.

type ResponseObtainIP added in v0.0.3

type ResponseObtainIP struct {
	IP string
}

ResponseObtainIP is data for get a IP of requesting node.

type ResponseRelay

type ResponseRelay struct {
	State relay.State
}

ResponseRelay is data for relay request response.

type ResponseRelayOwnership added in v0.0.3

type ResponseRelayOwnership struct {
	Accepted bool
}

ResponseRelayOwnership is data to response to relay ownership request.

Jump to

Keyboard shortcuts

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