server

package
v0.0.0-...-3aa0b6b Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2014 License: MIT Imports: 12 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BroadcastBit = "64 bit"
View Source
var BroadcastVersion = "0.1.0"
View Source
var Delims = []byte("\r\n")
View Source
var LogoHeader = `` /* 158-byte string literal not displayed */
View Source
var NullBulk = []byte("-1")

Functions

This section is empty.

Types

type Backend

type Backend interface {
	Load() error
	Unload() error
}

type BroadcastContext

type BroadcastContext struct {
	Commands    map[string]Handler  // commands is a map of all the available commands executable by the server
	CommandHelp map[string]Command  // command help includes name, description and usage
	Events      chan BroadcastEvent // events for the context of the broadcast server
	ClientSize  int                 // number of connected clients
}

func NewBroadcastContext

func NewBroadcastContext() *BroadcastContext

func (*BroadcastContext) Help

func (ctx *BroadcastContext) Help() (map[string]Command, error)

func (*BroadcastContext) Register

func (ctx *BroadcastContext) Register(cmd string, handler Handler)

Register will bind a particular byte/mark to a specific command handler (thus registering command handlers)

func (*BroadcastContext) RegisterCommand

func (ctx *BroadcastContext) RegisterCommand(cmd Command, handler Handler)

RegisterCommand takes a simple command structure and handler to assign both the help info and the handler itself

func (*BroadcastContext) RegisterHelp

func (ctx *BroadcastContext) RegisterHelp(cmd Command)

RegisterHelp will only register that the command exists in some form (without a handler which may be processed another way)

func (*BroadcastContext) Status

func (ctx *BroadcastContext) Status() (*BroadcastServerStatus, error)

Status will return the current status of this system

type BroadcastEvent

type BroadcastEvent struct {
	Level   string // level represents a string used for the event level or type
	Message string // message associated with the event occurring
	Err     error  // error potentially associated with this event
	Buf     []byte // buffer of a potential runtime stack
}

BroadcastEvent represents a simple construct for when 'things' occur in the application on certain levels

type BroadcastServer

type BroadcastServer struct {
	Closed  bool                // closed is the boolean for when the application has already been closed
	Quit    chan struct{}       // quit is a simple channel signal for when the application quits
	Events  chan BroadcastEvent // events is a channel for when emitted data occurs in the application
	Name    string              // canonical name of the broadcast server
	Version string              // version of the broadcast server
	Header  string              // header for the broadcast server
	// contains filtered or unexported fields
}

BroadcastServer represents a construct for the application as a whole including the various address, protocol, network listener, connected clients, and overall server state that can be used for either reporting, or communicating with services.

func Listen

func Listen(port int, host string) (*BroadcastServer, error)

Listen will use the given address parameters to construct a simple server that listens for incoming clients

func ListenProtocol

func ListenProtocol(port int, host string, protocol BroadcastServerProtocol) (*BroadcastServer, error)

ListenProtocol uses the address parameters and the specified protocol to construct the broadcast server

func (*BroadcastServer) AcceptConnections

func (app *BroadcastServer) AcceptConnections()

AcceptConnections will use the network listener for incoming clients in order to handle those connections in an async manner. This will setup routines for both reading and writing to a connected client

func (*BroadcastServer) Address

func (app *BroadcastServer) Address() string

Address will return a string representation of the full server address (i.e. host:port)

func (*BroadcastServer) Close

func (app *BroadcastServer) Close()

Close will end any open network connections, issue last minute commands and flush any transient data

func (*BroadcastServer) GetClient

func (app *BroadcastServer) GetClient(id string) (ProtocolClient, bool)

func (*BroadcastServer) Help

func (app *BroadcastServer) Help() (map[string]Command, error)

Help will output the current context help commands

func (*BroadcastServer) LoadBackend

func (app *BroadcastServer) LoadBackend(backend Backend) error

Load will load the backend service

func (*BroadcastServer) Register

func (app *BroadcastServer) Register(cmd string, handler Handler)

Register will bind a particular byte/mark to a specific command handler (thus registering command handlers)

func (*BroadcastServer) RegisterCommand

func (app *BroadcastServer) RegisterCommand(cmd Command, handler Handler)

RegisterCommand takes a simple command structure and handler to assign both the help info and the handler itself

func (*BroadcastServer) RegisterHelp

func (app *BroadcastServer) RegisterHelp(cmd Command)

RegisterHelp will only register that the command exists in some form (without a handler which may be processed another way)

func (*BroadcastServer) Status

func (app *BroadcastServer) Status() (*BroadcastServerStatus, error)

Status will return the current state of the system and process

type BroadcastServerProtocol

type BroadcastServerProtocol interface {
	HandleConnection(conn *net.TCPConn) (ProtocolClient, error)
	RunClient(client ProtocolClient)
	Initialize(ctx *BroadcastContext) error
	Name() string
}

type BroadcastServerStatus

type BroadcastServerStatus struct {
	NumGoroutines int               // number of go-routines running
	NumCpu        int               // number of cpu's running
	NumCgoCall    int64             // number of cgo calls
	Memory        *runtime.MemStats // memory statistics running
	NumClients    int               // number of connected clients
}

type BufferClient

type BufferClient struct {
	sync.Mutex

	Reader *bufio.Reader
	Writer *bufio.Writer
}

func (*BufferClient) Flush

func (client *BufferClient) Flush() error

func (*BufferClient) ParseBool

func (client *BufferClient) ParseBool(b []byte) (bool, error)

func (*BufferClient) ParseByte

func (client *BufferClient) ParseByte(b []byte) (byte, error)

func (*BufferClient) ParseError

func (client *BufferClient) ParseError(b []byte) (error, error)

func (*BufferClient) ParseFloat64

func (client *BufferClient) ParseFloat64(b []byte) (float64, error)

func (*BufferClient) ParseInt64

func (client *BufferClient) ParseInt64(b []byte) (int64, error)

func (*BufferClient) ParseString

func (client *BufferClient) ParseString(b []byte) (string, error)

func (*BufferClient) ReadBulkPayload

func (client *BufferClient) ReadBulkPayload() ([][]byte, error)

ReadBulkPayload is a format where the bulk arguments are described as a set of payload arguments where each payload is read by ReadPayload and finally returned as an array of byte arrays.

func (*BufferClient) ReadInterface

func (client *BufferClient) ReadInterface() (interface{}, error)

ReadInterface will read the described payloads as the appropriate interpreted typed-syntax for which they describe and return it as an interface{}. The protocol is described through the above prefixed delimiters through the use of various interface related methods (i.e. WriteString, WriteInt64...etc). ReadInterface is the generic use case implemented for broadcast-clients when needing to return errors, integers or payloads that aren't in the pure form of bytes for easy interpretation.

func (*BufferClient) ReadLine

func (client *BufferClient) ReadLine() ([]byte, error)

func (*BufferClient) ReadLineInvariant

func (client *BufferClient) ReadLineInvariant() ([]byte, error)

func (*BufferClient) ReadPayload

func (client *BufferClient) ReadPayload() ([]byte, error)

ReadPayload is a formatted read off of a buffer client where the payload is described by the $bytelength\r\n[...bytes...]\r\n

func (*BufferClient) WriteArray

func (client *BufferClient) WriteArray(args []interface{}) error

func (*BufferClient) WriteBool

func (client *BufferClient) WriteBool(b bool) error

func (*BufferClient) WriteBulk

func (client *BufferClient) WriteBulk(data [][]byte) error

func (*BufferClient) WriteByte

func (client *BufferClient) WriteByte(b byte) error

func (*BufferClient) WriteBytes

func (client *BufferClient) WriteBytes(b []byte) error

func (*BufferClient) WriteCommand

func (client *BufferClient) WriteCommand(cmd string, args []interface{}) error

func (*BufferClient) WriteError

func (client *BufferClient) WriteError(e error) error

func (*BufferClient) WriteFloat64

func (client *BufferClient) WriteFloat64(n float64) error

func (*BufferClient) WriteInt64

func (client *BufferClient) WriteInt64(n int64) error

func (*BufferClient) WriteInterface

func (client *BufferClient) WriteInterface(arg interface{}) error

func (*BufferClient) WriteJson

func (client *BufferClient) WriteJson(arg interface{}) error

func (*BufferClient) WriteLen

func (client *BufferClient) WriteLen(prefix byte, n int) error

WriteLen will write the given prefix and integer to the command line

func (*BufferClient) WriteNull

func (client *BufferClient) WriteNull() error

func (*BufferClient) WriteString

func (client *BufferClient) WriteString(s string) error

/ WriteString will write the length of the string followed by the string data

type Command

type Command struct {
	Name        string // name of the command
	Description string // description of the command
	Usage       string // example usage of the command
	FireForget  bool   // true to ignore responses, false to wait for a response
}

Command describes a command handler with name, description, usage

type DefaultBroadcastServerProtocol

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

func NewDefaultBroadcastServerProtocol

func NewDefaultBroadcastServerProtocol() *DefaultBroadcastServerProtocol

func (*DefaultBroadcastServerProtocol) HandleConnection

func (p *DefaultBroadcastServerProtocol) HandleConnection(conn *net.TCPConn) (ProtocolClient, error)

HandleConnection will create several routines for handling a new network connection to the broadcast server. This method will create a simple client, spawn both write and read routines where appropriate, handle disconnects, and finalize the client connection when the server is disposing

func (*DefaultBroadcastServerProtocol) Initialize

func (*DefaultBroadcastServerProtocol) Name

func (*DefaultBroadcastServerProtocol) RunClient

func (p *DefaultBroadcastServerProtocol) RunClient(client ProtocolClient)

Run will begin reading from the buffer reader until the client has either disconnected or any other panic routine has occured as a result of this routine, when we receive data, the client's data handler function should accomodate callback routines.

type Handler

type Handler func(interface{}, ProtocolClient) error

Handler is the actual function declaration that is provided argument data, client, and server

type NetworkClient

type NetworkClient struct {
	BufferClient

	Addr         string        // remote address identifier
	Closed       bool          // closed boolean identifier
	Conn         *net.TCPConn  // network connection associated with this client
	Quit         chan struct{} // channel for when the client exits
	RequestError chan error    // channel for request errors
}

func NewNetworkClient

func NewNetworkClient(conn *net.TCPConn) (*NetworkClient, error)

func NewNetworkClientSize

func NewNetworkClientSize(conn *net.TCPConn, bufferSize int) (*NetworkClient, error)

func (NetworkClient) Address

func (netClient NetworkClient) Address() string

func (NetworkClient) Close

func (netClient NetworkClient) Close()

Close will shutdown any latent network connections and clear the client out

func (*NetworkClient) Initialize

func (client *NetworkClient) Initialize(conn *net.TCPConn, bufferSize int)

func (NetworkClient) IsClosed

func (netClient NetworkClient) IsClosed() bool

func (*NetworkClient) RequestErrorChan

func (client *NetworkClient) RequestErrorChan() chan error

func (NetworkClient) WaitExit

func (netClient NetworkClient) WaitExit() chan struct{}

type ProtocolClient

type ProtocolClient interface {
	Close()
	IsClosed() bool
	Address() string
	WaitExit() chan struct{}

	Initialize(conn *net.TCPConn, bufferSize int)
	Flush() error

	WriteLen(prefix byte, n int) error
	WriteString(s string) error
	WriteByte(b byte) error
	WriteBytes(b []byte) error
	WriteInt64(n int64) error
	WriteFloat64(n float64) error
	WriteBool(b bool) error
	WriteError(e error) error
	WriteNull() error
	WriteBulk(data [][]byte) error
	WriteInterface(arg interface{}) error
	WriteArray(args []interface{}) error
	WriteJson(arg interface{}) error
	WriteCommand(cmd string, args []interface{}) error

	ReadInterface() (interface{}, error)
	ReadLine() ([]byte, error)
	ReadLineInvariant() ([]byte, error)
	ReadPayload() ([]byte, error)
	ReadBulkPayload() ([][]byte, error)

	ParseByte(b []byte) (byte, error)
	ParseString(b []byte) (string, error)
	ParseInt64(b []byte) (int64, error)
	ParseFloat64(b []byte) (float64, error)
	ParseBool(b []byte) (bool, error)
	ParseError(b []byte) (error, error)
	RequestErrorChan() chan error
}

Jump to

Keyboard shortcuts

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