room

package
v0.0.0-...-47ad847 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2016 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package room provides a framework for REx servers and clients to communicate using arbitrary messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BestAddr

func BestAddr() string

BestAddr returns the best address at which a server should bind. It attempts to locate the interface address for the local network. When no address can be found the discovery server may just attempt to bind to the default address ":0".

func Broadcast

func Broadcast(ctx context.Context, content Content) error

Broadcast sends a broadcast event to all clients connected to the Bus associated with ctx.

Types

type Bus

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

Bus is the communication bus for a Server.

func NewBus

func NewBus(ctx context.Context, handlers ...MsgHandler) *Bus

NewBus initializes and returns a new Bus.

func (*Bus) AddHandler

func (b *Bus) AddHandler(h MsgHandler)

AddHandler changes the bus message handler.

func (*Bus) Event

func (b *Bus) Event(c Content) error

Event broadcasts an event to all Subscription.

func (*Bus) Message

func (b *Bus) Message(session string, c Content) error

Message is called by a subscriber to signal back to the bus owner via b.handler.

func (*Bus) Subscribe

func (b *Bus) Subscribe(start int) *Subscription

Subscribe returns a new Subscription that new events from b.

func (*Bus) Unsubscribe

func (b *Bus) Unsubscribe(s *Subscription)

Unsubscribe removes s from the recipients of b's events. After Unsubscribe returns no further events will be received in calls to s.Next().

type Client

type Client struct {
	Host    string
	Port    int
	Handler EventHandler
	HTTP    *http.Client
	Now     func() *roomtime.Time
	Session string
}

Client is an interface to a remote REx server.

func NewClient

func NewClient(h EventHandler) *Client

NewClient allocates and returns a new client with its Handler set to h.

func NewClientRestore

func NewClientRestore(h EventHandler, session string) *Client

NewClientRestore behaves like NewClient but sets the client's Session to session as well.

func (*Client) CreateSession

func (c *Client) CreateSession(ctx context.Context, name string) error

CreateSession initializes c.Session by registering an identifier with the remote bus. If the value is already set no registration is performed.

func (*Client) Run

func (c *Client) Run(ctx context.Context, start int) (next int, err error)

Run processes events received from the remote bus.

func (*Client) Send

func (c *Client) Send(ctx context.Context, content Content) error

Send sends a message to the remote server using the given session identifier.

type Codec

type Codec interface {
	// EncodeMessage serializes v and returns the []byte or any error.
	EncodeMessage(v Msg) []byte
	// DecodeMessage deserializes b into a Msg and returns it with any error.
	DecodeMessage(b []byte) (Msg, error)

	// DecodeEvent deserializes b into an Event and returns it with any error.
	DecodeEvent(b []byte) (Event, error)
	// EncodeEvent serializes v and returns the []byte or any error.
	EncodeEvent(v Event) []byte
}

Codec performs serialization for room data.

type Content

type Content interface {
	// Data returns the event content is a slice of bytes (even if the content
	// is stored as a string).  Applications should use whichever of Data or
	// Text methods they need and any translation necessary will happen as fast
	// as possible.
	Data() []byte

	// Text is like Data but returns a string representation of event data and
	// may be optmized to do that.
	Text() string
}

Content is application data that is transmitted over a bus.

func Bytes

func Bytes(b []byte) Content

Bytes returns a Content containing the b as its data.

func String

func String(s string) Content

String returns a Content containing the s as its data.

type Event

type Event interface {
	Index() uint64

	Time() *roomtime.Time

	Content
}

Event is a broadcast message from the server to all clients. Unlike Msg an event does not have an associated session identifier because it is intended for all clients.

type EventHandler

type EventHandler interface {
	HandleEvent(context.Context, *Client, Event)
}

EventHandler is part of the client's event loop.

type Msg

type Msg interface {
	// Session returns an identifier for the client (session) that originated
	// the message.
	Session() string

	Time() *roomtime.Time

	Content
}

Msg is a piece of data originating from a client application.

type MsgHandler

type MsgHandler interface {
	HandleMessage(ctx context.Context, msg Msg)
}

MsgHandler is a bus message handler.

type Room

type Room struct {
	Name    string
	Service string
}

Room represents a single shared enivornment managed by a server. The service is advertised using mDNS an must conform to the format specified in RFC 6763 Section 7. The Name may contain any unicode text excluding ASCII control characters but is recommended to not contain '\n' bytes for display purposes. An mDNS instance identifier will be generated from the given name, the time and the process identifier.

type Server

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

Server is a server used by a TV application to run a game or collaborative procedure.

func NewServer

func NewServer(config *ServerConfig) *Server

NewServer initializes a new server, but does not start serving clients.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the string address the bus is listening on for HTTP requests.

func (*Server) Event

func (s *Server) Event(c Content)

Event broadcasts c to all connected clients, giving it the next unused event index.

func (*Server) Room

func (s *Server) Room() *Room

Room returns the Room s is hosting.

func (*Server) Run

func (s *Server) Run() error

Run binds to a random port, begins broadcasting service metadata using mDNS, and begins streaming client events and dispatching client messages. Typically, Run never returns a value. If any critical error is encountered it will be returned.

Run is equivalent to calling Start, followed by s.Wait.

func (*Server) Start

func (s *Server) Start() error

Start binds the server to a port and beigns allowing clients to connect. Start must not be called more than once.

func (*Server) Wait

func (s *Server) Wait() error

Wait returns when the server has terminated. Wait must not be called more than once.

type ServerConfig

type ServerConfig struct {
	Room *Room
	Bus  *Bus

	// Addr is an optional address to bind.  If empty, the address of ":0" will
	// be used.
	Addr string
}

ServerConfig controls how a server advertises itself to potential clients as well as miscelaneous communication behaviors.

type Subscription

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

Subscription represents a remote client that needs to receive messages from a Bus.

func (*Subscription) Event

func (s *Subscription) Event() Event

Event returns the last received Event.

func (*Subscription) Next

func (s *Subscription) Next(timeout <-chan time.Time) (ok bool)

Next waits for the next event to be received over the channel and returns it. If the subscription is terminated, or values is received over timeout Next will return a value value. Otherwise Next returns true and Event will return the event received.

Directories

Path Synopsis
Package roomdisco provides discovery for room servers, allowing clients to automatically find servers.
Package roomdisco provides discovery for room servers, allowing clients to automatically find servers.

Jump to

Keyboard shortcuts

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