msg

package
v0.0.0-...-892988b Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2019 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknown = errors.New("unknown message type")

ErrUnknown the message type provided does not match any known types

Functions

func ParseClient

func ParseClient(src []byte) (Type, IMessage, error)

ParseClient attempts to coerce an incoming json message into it's respective IMessage implementing Message struct. Will return an error if the type cannot be found (ErrUnknown), or an error if the message validation is not met

func ParseServer

func ParseServer(src []byte) (Type, IMessage, error)

ParseServer attempts to coerce an incoming json message into it's respective IMessage implementing Message struct. Will return an error if the type cannot be found (ErrUnknown), or an error if the message validation is not met

func TypeToString

func TypeToString(mt Type) string

TypeToString converts an incoming message type to it's string enum equivalent

Types

type Ack

type Ack struct {
	*Message

	ID string `json:"id"`
}

Ack is a server echo response that a command message was received

type Add

type Add struct {
	*Message

	ID    string `json:"id"`
	Phase string `json:"phase"`
	Body  string `json:"body"` //hex encoded
}

Add tells the server to add a message to the mailbox for retrieval later. The body portion is hex encoded.

type Allocate

type Allocate struct {
	*Message
}

Allocate is the client asking for a nameplate to be reserved for them to use. The server should respond with an Allocated response

type Allocated

type Allocated struct {
	*Message

	Nameplate string `json:"nameplate"`
}

Allocated server response to the Allocate. Responds with the allocated nameplate

type Bind

type Bind struct {
	*Message

	AppID string `json:"appid"`
	Side  string `json:"side"`
}

Bind allows the client to declare itself to the server and should be the first message the client sends. No other client messages will be accepted until this one is received.

type Claim

type Claim struct {
	*Message

	Nameplate string `json:"nameplate"`
}

Claim allows the client to reserve an arbitrary nameplate of their choosing (given it is available)

type Claimed

type Claimed struct {
	*Message

	Mailbox string `json:"mailbox"`
}

Claimed is the server response to the Claim responding with the claimed mailbox

type Close

type Close struct {
	*Message

	Mood    string `json:"mood"`    //not sure what these values are actually
	Mailbox string `json:"mailbox"` //optional
}

Close tells the server that the client is closing, or terminating it's binding to the mailbox. It does not release the mailbox.

type Closed

type Closed struct {
	*Message
}

Closed confirms to the client that the server has closed their mailbox binding

type Echo

type Echo []byte

Echo wraps a []byte slice to fool the JSON encoder into not string encoding this value. This is used in message loopback, or echo-ing the original client message while maintaining the JSON format.

func (Echo) MarshalJSON

func (e Echo) MarshalJSON() ([]byte, error)

MarshalJSON turns this into JSON. For this instance, it is a strait copy so no data wrangling happens.

type Error

type Error struct {
	*Message

	Error string `json:"error"`
	Orig  Echo   `json:"orig"` //original message
}

Error is a server response to errors in client messages.

type IMessage

type IMessage interface {
	GetType() Type
	GetID() string
}

IMessage interface is used for tagging message types. Basically just wraps them up with a dummy function so reflection can grab them a bit easier

type List

type List struct {
	*Message
}

List is a request from the client to have the server reply with the available nameplates. The server may deny this request. The server fulfills this with the Nameplates

type MailboxMessage

type MailboxMessage struct {
	*Message

	Side  string `json:"side"`
	Phase string `json:"phase"`
	Body  string `json:"body"` //hex encoded
	MsgID string `json:"msg_id"`
}

MailboxMessage is the server pushing a mailbox message entry to the client The body field is hex encoded.

type Message

type Message struct {
	Type Type `json:"type"`

	ID         *string  `json:"id,omitempty"`
	ServerTime UnixTime `json:"server_tx,omitempty"`
}

Message base object for communication messages

func NewMessage

func NewMessage(mt Type) *Message

NewMessage returns a new Message object with the type filled in

func NewServerMessage

func NewServerMessage(mt Type) *Message

NewServerMessage returns a new Message object with the type and server time filled in

func (Message) GetID

func (m Message) GetID() string

GetID implements the IMessage interface, returning the client's message ID for acknowledgement

func (Message) GetType

func (m Message) GetType() Type

GetType implements the IMessage interface

type NameplateEntry

type NameplateEntry struct {
	ID string `json:"id"`
}

NameplateEntry is an individual entry in the nameplate response, identified by it's ID

type Nameplates

type Nameplates struct {
	*Message

	Nameplates []NameplateEntry `json:"nameplates"`
}

Nameplates is the server response to a clients list request (List), and responds with the available nameplates

type Open

type Open struct {
	*Message

	Mailbox string `json:"mailbox"`
}

Open binds the client to a mailbox. Can only be called once per connection. After this point, old messages are transmited as well as listening for updates to the mailbox in which it will push new ones to the client

type Ping

type Ping struct {
	*Message
	Ping int `json:"ping"`
}

Ping is a protocol specific ping/pong test from client to server. Ping is sent from client

type Pong

type Pong struct {
	*Message
	Pong int `json:"pong"`
}

Pong is a protocol specific ping/pong test from client to server. Pong is sent from server in response

type Release

type Release struct {
	*Message

	Nameplate string `json:"nameplate"` //optional
}

Release tells the server that the client is done with their nameplate/mailbox. The Nameplate field is optional, but should match the same one from Claim, or what is allocated to the client.

type Released

type Released struct {
	*Message
}

Released is the servers confirmation that it has released the clients nameplate/mailbox as instructed from the Release

type Type

type Type int

Type enumerates the available message types accepted

const (
	TypeUnknown Type = iota
	TypeAck
	TypePing
	TypePong

	TypeWelcome
	TypeBind
	TypeList
	TypeNameplates
	TypeAllocate
	TypeAllocated
	TypeClaim
	TypeClaimed
	TypeRelease
	TypeReleased
	TypeOpen
	TypeMessage
	TypeAdd
	TypeClose
	TypeClosed

	TypeError //Important! keep this one last
)

Available message type enumerations

func DetectType

func DetectType(src []byte) (Type, error)

DetectType takes an incoming JSON string as bytes and attempts to find the messages type field by unmarshalling it into a dummy object. Surprisingly, this was faster then RegExp.

func TypeFromString

func TypeFromString(str string) Type

TypeFromString converts an incoming string to it's Type equivalent, or TypeUnknown if no matches

func (*Type) FromString

func (mt *Type) FromString(str string)

FromString converts the incoming string into the message type it matches, or TypeUnknown if no matches

func (Type) MarshalText

func (mt Type) MarshalText() ([]byte, error)

MarshalText converts the enum into a string/text representation

func (Type) String

func (mt Type) String() string

func (*Type) UnmarshalText

func (mt *Type) UnmarshalText(src []byte) error

UnmarshalText converts the string representation, back into the enum

type UnixTime

type UnixTime struct {
	Time *time.Time
}

UnixTime wraps a time.Time pointer so that we can control the json encoding/decoding of the value. The wormhole protocol has it's time values in unix seconds since epoch.

func NewUnixTime

func NewUnixTime() UnixTime

NewUnixTime returns a new UnixTime object filled to time.Now()

func (UnixTime) MarshalJSON

func (t UnixTime) MarshalJSON() ([]byte, error)

MarshalJSON reads the json

func (*UnixTime) UnmarshalJSON

func (t *UnixTime) UnmarshalJSON(src []byte) error

UnmarshalJSON writes the json

type Welcome

type Welcome struct {
	*Message

	Info WelcomeInfo `json:"welcome"`
}

Welcome used for the server to introduce itself to clients when they connect

type WelcomeInfo

type WelcomeInfo struct {
	//MOTD Message Of The Day. Will be displayed
	//to connecting clients when received
	MOTD *string `json:"motd,omitempty"`

	//Error displays an error message to clients
	//on connection and they will close immediately.
	//Used to anounce service termination
	Error *string `json:"error,omitempty"`

	//Version advertises a new CLI client version to
	//the clients
	Version *string `json:"current_cli_version,omitempty"`
}

WelcomeInfo wraps up the welcome information for servers to respond to clients with. This is JSON encoded so the type is provided for convenience.

Jump to

Keyboard shortcuts

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