relay

package
v0.0.0-...-c2cee1d Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2019 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanNowPure

func CleanNowPure() error

CleanNowPure runs the cleaning operation without actually spinning up the service resources

func Initialize

func Initialize() error

Initialize sets-up the relay servers initial systems

func LogDebug

func LogDebug(c *Client, args ...interface{})

LogDebug is a convenience wrapper for logging usage statistics given the relay server settings

func LogDebugf

func LogDebugf(c *Client, fmt string, args ...interface{})

LogDebugf is a convenience wrapper for logging usage statistics given the relay server settings

func LogErr

func LogErr(c *Client, msg string, err error)

LogErr is a convenience wrapper for logging errors with usage statistics

func LogError

func LogError(c *Client, fmt string, args ...interface{})

LogError is a convenience wrapper for logging errors with usage statistics

func LogErrorf

func LogErrorf(c *Client, fmt string, args ...interface{})

LogErrorf is a convenience wrapper for logging errors with usage statistics

func LogInfo

func LogInfo(c *Client, args ...interface{})

LogInfo is a convenience wrapper for logging usage statistics given the relay server settings

func LogInfof

func LogInfof(c *Client, fmt string, args ...interface{})

LogInfof is a convenience wrapper for logging usage statistics given the relay server settings

func LogWarn

func LogWarn(c *Client, fmt string, args ...interface{})

LogWarn is a convenience wrapper for logging warnings with usage statistics

func LogWarnf

func LogWarnf(c *Client, fmt string, args ...interface{})

LogWarnf is a convenience wrapper for logging warnings with usage statistics

func Shutdown

func Shutdown(ctx context.Context) error

Shutdown performs the graceful shutdown of the relay server using the provided context

func Start

func Start()

Start spins up the relay server as a coroutine

Types

type Application

type Application struct {
	ID string

	Mailboxes map[string]*Mailbox
}

Application holds the data for interacting with an individual applications usage with the relay server. All mailboxes are broken down into their parent apps so that a wider variety of client apps can exist on one server without conflicting with each others protocols

func NewApplication

func NewApplication(id string) (Application, error)

NewApplication creates a new application container and returns it as a pointer, or error if something failed.

func (Application) AddMailbox

func (a Application) AddMailbox(id string, forNameplate bool, side string) error

AddMailbox creates a new mailbox in the application

func (Application) AllocateNameplate

func (a Application) AllocateNameplate(side string) (string, error)

AllocateNameplate generates a new nameplate ID and associates with a mailbox. The returned value is the nameplate ID

func (Application) ClaimNameplate

func (a Application) ClaimNameplate(name, side string) (string, error)

ClaimNameplate claims a nameplate and it's respective mailbox. Returns the mailbox ID, or an error if one occured

func (*Application) Cleanup

func (a *Application) Cleanup(since int64) error

Cleanup updates and removes mailboxes and nameplates as needed via timeouts.

func (Application) FindNameplate

func (a Application) FindNameplate() (string, error)

FindNameplate attempts to find an available nameplate to return back for clients to use

func (*Application) Free

func (a *Application) Free()

Free is called when the service is closing and the final death-throws should be performed

func (Application) FreeMailbox

func (a Application) FreeMailbox(id string)

FreeMailbox removes a mailbox listing from the application memory. Does not remove it from the database

func (Application) GetNameplateIDs

func (a Application) GetNameplateIDs() ([]string, error)

GetNameplateIDs returns all the nameplate IDs used by the current application. This should only be allowed if the config option AllowList is true.

func (Application) OpenMailbox

func (a Application) OpenMailbox(id, side string) (*Mailbox, error)

OpenMailbox marks the mailbox as opened

func (Application) ReleaseNameplate

func (a Application) ReleaseNameplate(name, side string) error

ReleaseNameplate removes the claim on a nameplates side. If no other claims are on the nameplate, then the whole thing is cleared out

func (Application) StillInUse

func (a Application) StillInUse() bool

StillInUse returns true if the application (by ID) is still being used, or registered, in the database. If it is not, then it's safe to delete it during cleaning

type Client

type Client struct {
	App       *Application
	Side      string
	Nameplate string
	Mailbox   *Mailbox

	Allocated bool
	Claimed   bool
	Released  bool
	Listening bool
	Closed    bool
	// contains filtered or unexported fields
}

Client wraps up the websocket connection with a sending buffer and functions for transfering messages

func (*Client) Close

func (c *Client) Close()

Close terminates the client connection and cleans up resources it had bound.

func (*Client) HandleAdd

func (c *Client) HandleAdd(m msg.Add) error

HandleAdd command from the client to add a message to the opened mailbox of this client. Adding messages will also trigger broadcasts for any listners currently on the service for the specified mailbox. Which of course means, it echos back immediately

func (*Client) HandleAllocate

func (c *Client) HandleAllocate(m msg.Allocate) error

HandleAllocate command is received from client when they want to allocate, or reserve, a nameplate slot for message transfer. Clients can only allocate 1 during a connection. Allocate generates a new nameplate and returns it

func (*Client) HandleBind

func (c *Client) HandleBind(m msg.Bind) error

HandleBind handles bind messages.

func (*Client) HandleClaim

func (c *Client) HandleClaim(m msg.Claim) error

HandleClaim command from client when they want to claim a specific nameplate instead of auto-generating one for them. Clients can only allocate 1 during a connection.

func (*Client) HandleClose

func (c *Client) HandleClose(m msg.Close) error

HandleClose command from the client to close it's connection to an opened mailbox. The "mailbox" field is optional, but if supplied must match the currently open one.

func (*Client) HandleList

func (c *Client) HandleList(m msg.List) error

HandleList handles list commands from the client who would like to know the available nameplates. This is optional for whether the server will allow it via the AllowList relay server configuration option. If this option is not available, an empty list is returned back to the client

func (*Client) HandleOpen

func (c *Client) HandleOpen(m msg.Open) error

HandleOpen command from the client to open the specified mailbox (by ID) for reading. Will also bind the listeners for event callbacks.

func (*Client) HandlePing

func (c *Client) HandlePing(m msg.Ping)

HandlePing handles ping messages and responds back with the matching Pong message

func (*Client) HandleRelease

func (c *Client) HandleRelease(m msg.Release) error

HandleRelease command from client when they want to release their hold, or side, of a nameplate. They can provide the nameplate as a means of double checking but the current client one is inferred. If they do supply it, it must match. This command must come after claim.

func (Client) IsBound

func (c Client) IsBound() bool

IsBound returns true if the client has already bound to the server

func (*Client) OnConnect

func (c *Client) OnConnect()

OnConnect is called when the client has successfully been registered to the server

func (*Client) OnMessage

func (c *Client) OnMessage(src []byte)

OnMessage called when a message from the client is received and it needs to be handled/processed. From this point we have a message as only the bytes and need to handle it appropriately. We need to do all steps and validation here as necessary. So this will be broken down into the message types.

type Mailbox

type Mailbox struct {
	ID    string
	AppID string
	// contains filtered or unexported fields
}

Mailbox holds an association with an application as well as its on ID. Here the client messages are stored for retrieval later and transmitting between users

func NewMailbox

func NewMailbox(id, appID string) *Mailbox

NewMailbox returns a new mailbox address with the provided information

func (*Mailbox) AddListener

func (m *Mailbox) AddListener(listener MailboxListener, stopCallback MailboxListenerStop) int

AddListener registers a callback for mailbox messages and returns an integer handle for de-registration

func (*Mailbox) AddMessage

func (m *Mailbox) AddMessage(msg MailboxMessage) error

AddMessage inserts a new message into the mailbox

func (*Mailbox) Close

func (m *Mailbox) Close(side string, mood string) error

Close registers the mailbox as closed

func (*Mailbox) Delete

func (m *Mailbox) Delete() error

Delete removes the mailbox from the database

func (*Mailbox) GetMessages

func (m *Mailbox) GetMessages() ([]MailboxMessage, error)

GetMessages returns the messages currently present in this mailbox

func (*Mailbox) HasListeners

func (m *Mailbox) HasListeners() bool

HasListeners returns true if there are any listeners registered

func (*Mailbox) Open

func (m *Mailbox) Open(side string) error

Open registers an open side on the mailbox

func (*Mailbox) RemoveAllListeners

func (m *Mailbox) RemoveAllListeners()

RemoveAllListeners calls the stop callback on each listener, and then clears all the listners

func (*Mailbox) RemoveListener

func (m *Mailbox) RemoveListener(handle int)

RemoveListener removes a previously registered listener by it's given handle

func (*Mailbox) Touch

func (m *Mailbox) Touch() error

Touch updates the db timestamp for this mailbox

type MailboxListener

type MailboxListener func(MailboxMessage)

MailboxListener is a callback function that receives new mailbox messages when they are added to the one the listener has subscribed too.

type MailboxListenerStop

type MailboxListenerStop func()

MailboxListenerStop is another callback function like MailboxListener, which is used in conjunction. When the service needs to remove a mailbox with an attached listener, this callback is called to alert the listener that they are going to loose their hook.

type MailboxMessage

type MailboxMessage struct {
	ID        string
	AppID     string
	MailboxID string
	Side      string
	Phase     string
	Body      string
	ServerRX  int64
}

MailboxMessage is an individual entry within a parent Mailbox

type Service

type Service struct {
	Welcome msg.WelcomeInfo

	Apps map[string]Application
}

Service encompases the actual relay service for use by clients. Essentially most of this package handles the actual network connections and message handling. This object is the actual implementation (or at least the start of it)

func NewService

func NewService() (*Service, error)

NewService initializes the relay service object and returns it as a pointer, if we can not start one then nil, error is returned instead

func (*Service) CleanApps

func (s *Service) CleanApps(since int64) error

CleanApps iterates the apps registered to the service and runs the cleaining process on each one

func (Service) GetAllApps

func (s Service) GetAllApps() ([]string, error)

GetAllApps returns all the application IDs in memory, and in the database. I will admit, this function was A LOT shorter in the Python version.

func (*Service) GetApp

func (s *Service) GetApp(id string) *Application

GetApp finds an application registered with the relay service. If not found, it will create and initialize the object for it

Jump to

Keyboard shortcuts

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