server

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 11, 2015 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package server implements a chat server for websocket access.

Index

Constants

View Source
const (
	ChatReqTypeSetNickname = 101 + iota
	ChatReqTypeGetNickname
	ChatReqTypeListRooms
	ChatReqTypeJoin
	ChatReqTypeListNames
	ChatReqTypeHide
	ChatReqTypeUnhide
	ChatReqTypeMsg
	ChatReqTypeLeave
)
View Source
const (
	ChatRspTypeSetNickname = 101 + iota
	ChatRspTypeGetNickname
	ChatRspTypeListRooms
	ChatRspTypeJoin
	ChatRspTypeListNames
	ChatRspTypeHide
	ChatRspTypeUnhide
	ChatRspTypeMsg
	ChatRspTypeLeave
)
View Source
const (
	ChatRspTypeErrRoomMandatory = 1001 + iota
	ChatRspTypeErrMaxRoomsReached
	ChatRspTypeErrRoomUnavailable
	ChatRspTypeErrNicknameMandatory
	ChatRspTypeErrAlreadyJoined
	ChatRspTypeErrNicknameUsed
	ChatRspTypeErrHiddenNickname
	ChatRspTypeErrUnknownReq
)
View Source
const (
	DefaultHostname = "localhost" // The hostname of the server.
	DefaultPort     = 6660        // Port to receive requests: see IANA Port Numbers.
	DefaultProfPort = 0           // Profiler port to receive requests. *
	DefaultMaxConns = 0           // Maximum number of connections allowed. *
	DefaultMaxRooms = 0           // Maximum number of chat rooms allowed. *
	DefaultMaxIdle  = 0           // Maximum idle seconds per user connection. *
	DefaultMaxProcs = 0           // Maximum number of computer processors to utilize. *

)

Variables

This section is empty.

Functions

func PrintUsageAndExit

func PrintUsageAndExit()

PrintUsageAndExit is used to print out command line options.

func PrintVersionAndExit

func PrintVersionAndExit()

PrintVersionAndExit prints the version of the server then exits.

Types

type ChatLogger

type ChatLogger struct {
	*logger.Logger
}

ChatLogger is an enhancement over the base logger for application logging patterns.

func ChatLoggerNew

func ChatLoggerNew() *ChatLogger

ChatLoggerNew is a factory function that returns a new ChatLogger instance.

func (*ChatLogger) LogConnect

func (l *ChatLogger) LogConnect(r *http.Request)

LogConnect is used to log request information when the client first connects to the server.

func (*ChatLogger) LogError

func (l *ChatLogger) LogError(addr string, msg string)

LogError is used to record misc session error information between server and client.

func (*ChatLogger) LogSession

func (l *ChatLogger) LogSession(tp string, addr string, msg string)

LogSession is used to record information received during the client's session.

type ChatManager added in v0.1.2

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

ChatManager represents a control hub of chat rooms and chatters for the server.

func ChatManagerNew added in v0.1.2

func ChatManagerNew(maxr int, maxi int, l *ChatLogger) *ChatManager

ChatManagerNew is a factory function that returns a new instance of a chat manager.

func (*ChatManager) MaxIdle added in v0.1.2

func (m *ChatManager) MaxIdle() int

MaxIdle returns the current maximum idle time for a connection.

func (*ChatManager) MaxRooms added in v0.1.2

func (m *ChatManager) MaxRooms() int

MaxRooms returns the current maximum number of rooms allowed on the server.

func (*ChatManager) SetMaxIdle added in v0.1.2

func (m *ChatManager) SetMaxIdle(maxi int)

SetMaxIdle sets the maximum idle time for a connection.

func (*ChatManager) SetMaxRooms added in v0.1.2

func (m *ChatManager) SetMaxRooms(maxr int)

SetMaxRooms sets the maximum number of rooms allowed on the server.

type ChatRequest

type ChatRequest struct {
	Who      *Chatter `json:"-"`        // The chatter who is issuing the request.
	RoomName string   `json:"roomName"` // The name of the room to receive the request.
	ReqType  int      `json:"reqType"`  // The command type ex: join, leave, send.
	Content  string   `json:"content"`  // Any message or text to interpret with the request.
}

ChatRequest is a structure for commands sent for processing from the client.

func ChatRequestNew

func ChatRequestNew(c *Chatter, room string, reqt int, cont string) (*ChatRequest, error)

ChatMessageNew is a factory method that returns a new chat room message instance.

func (*ChatRequest) String

func (r *ChatRequest) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type ChatResponse

type ChatResponse struct {
	RoomName string   `json:"roomName"` // The room name where the response originated.
	RspType  int      `json:"rspType"`  // The response type ex: join, leave, send.
	Content  string   `json:"content"`  // Any message text or other content for the client.
	List     []string `json:"list"`     // A list of entries returned with the response.
}

ChatResponse is a structure for JSON responses sent back to the client.

func ChatResponseNew

func ChatResponseNew(name string, rspt int, cont string, l []string) (*ChatResponse, error)

ChatResponseNew is a factory method that returns a new chat room message instance.

func (*ChatResponse) String

func (r *ChatResponse) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type ChatRoom

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

ChatRoom represents a hub of chatters where messages can be exchanged.

func ChatRoomNew

func ChatRoomNew(name string, d chan bool, cl *ChatLogger, g *sync.WaitGroup) *ChatRoom

ChatRoomNew is a factory function that returns a new instance of a chat room.

func (*ChatRoom) ChatRoomStatsNew added in v0.1.1

func (r *ChatRoom) ChatRoomStatsNew() *ChatRoomStats

ChatRoomStatsNew returns status information on the room.

func (*ChatRoom) Name added in v0.1.3

func (r *ChatRoom) Name() string

Name returns the current name of the room.

func (*ChatRoom) Run

func (r *ChatRoom) Run()

Run is the main routine that is evoked in background to accept commands to the room.

func (*ChatRoom) SetName added in v0.1.3

func (r *ChatRoom) SetName(name string)

SetName sets the name of the room.

type ChatRoomChatterStat

type ChatRoomChatterStat struct {
	Nickname   string `json:"nickname"`   // The nickname of the chatter.
	RemoteAddr string `json:"remoteAddr"` // The remote IP and port of the chatter.
}

type ChatRoomStats

type ChatRoomStats struct {
	Name     string                 `json:"name"`     // The name of the room.
	Start    time.Time              `json:"start"`    // The start time of the room.
	LastReq  time.Time              `json:"lastReq"`  // The last request time to the room.
	LastRsp  time.Time              `json:"lastRsp"`  // The last response time from the room.
	ReqCount uint64                 `json:"reqcount"` // Total requests received.
	RspCount uint64                 `json:"rspCount"` // Total responses sent.
	Chatters []*ChatRoomChatterStat `json:"chatters"` // Stats on chatters in the room
}

ChatRoomStats is a simple structure for returning statistic information on the room.

type Chatter

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

Chatter is a wrapper around a connection that represents one chat client on the server.

func ChatterNew

func ChatterNew(cm *ChatManager, w *websocket.Conn, l *ChatLogger) *Chatter

ChatterNew is a factory function that returns a new Chatter instance

func (*Chatter) ChatterStatsNew added in v0.1.1

func (c *Chatter) ChatterStatsNew() *ChatterStats

ChatterStatsNew returns status information on the chatter.

func (*Chatter) Nickname added in v0.1.1

func (c *Chatter) Nickname() string

Nickname returns the raw nickname for the chatter.

func (*Chatter) Run

func (c *Chatter) Run()

Run starts the event loop that manages the sending and receiving of information to the client.

type ChatterStats

type ChatterStats struct {
	Nickname   string    `json:"nickname"`   // The nickname of the chatter.
	RemoteAddr string    `json:"remoteAddr"` // The remote IP and port of the chatter.
	Start      time.Time `json:"start"`      // The start time of the chatter.
	LastReq    time.Time `json:"lastReq"`    // The last request time from the chatter.
	LastRsp    time.Time `json:"lastRsp"`    // The last response time to the chatter.
	ReqCount   uint64    `json:"reqcount"`   // Total requests received.
	RspCount   uint64    `json:"rspCount"`   // Total responses sent.
}

ChatterStats is a simple structure for returning statistic information on the chatter.

type Info

type Info struct {
	Version  string `json:"version"`      // Version of the server.
	Name     string `json:"name"`         // The name of the server.
	Hostname string `json:"hostname"`     // The hostname of the server.
	UUID     string `json:"UUID"`         // Unique ID of the server.
	Port     int    `json:"port"`         // Port the server is listening on.
	ProfPort int    `json:"profPort"`     // Profiler port the server is listening on.
	MaxConns int    `json:"maxConns"`     // The maximum concurrent clients accepted.
	MaxRooms int    `json:"maxRooms"`     // The maximum number of chat rooms allowed.
	MaxIdle  int    `json:"maxIdle"`      // The maximum client idle time in seconds before disconnect.
	Debug    bool   `json:"debugEnabled"` // Is debugging enabled on the server.
}

Info provides basic config information to/about the running server.

func InfoNew

func InfoNew(opts ...func(*Info)) *Info

InfoNew is a factory function that returns a new instance of Info. opts is an optional list of functions that initialize the structure

func (*Info) String

func (i *Info) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type Options

type Options struct {
	Name     string `json:"name"`         // The name of the server.
	Hostname string `json:"hostname"`     // The hostname of the server.
	Port     int    `json:"port"`         // The default port of the server.
	ProfPort int    `json:"profPort"`     // The profiler port of the server.
	MaxConns int    `json:"maxConns"`     // The maximum concurrent clients accepted.
	MaxRooms int    `json:"maxRooms"`     // The maximum number of chat rooms allowed.
	MaxIdle  int    `json:"maxIdle"`      // The maximum client idle time in seconds before disconnect.
	MaxProcs int    `json:"maxProcs"`     // The maximum number of processor cores available.
	Debug    bool   `json:"debugEnabled"` // Is debugging enabled in the application or server.
}

Options represents parameters that are passed to the application to be used in constructing the server.

func (*Options) String

func (o *Options) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

type Server

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

Server is the main structure that represents a server instance.

func New

func New(ops *Options) *Server

New is a factory function that returns a new server instance.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown takes down the server gracefully back to an initialize state.

func (*Server) Start

func (s *Server) Start() error

Start spins up the server to accept incoming connections.

func (*Server) StartProfiler

func (s *Server) StartProfiler()

StartProfiler is called to enable dynamic profiling.

type Stats

type Stats struct {
	Start        time.Time                   `json:"startTime"`    // The start time of the server.
	ReqCount     int64                       `json:"reqCount"`     // How many requests came in to the server.
	ReqBytes     int64                       `json:"reqBytes"`     // Size of the requests in bytes.
	RouteStats   map[string]map[string]int64 `json:"routeStats"`   // How many requests/bytes came into each route.
	ChatterStats []*ChatterStats             `json:"chatterStats"` // Statistics about each logged in chatter.
	RoomStats    []*ChatRoomStats            `json:"roomStats"`    // How many requests etc came into each room.
}

Stats contains runtime statistics for the server.

func StatsNew

func StatsNew(opts ...func(*Stats)) *Stats

StatsNew is a factory function that returns a new instance of statistics. options is an optional list of functions that initialize the structure

func (*Stats) IncrReqStats

func (s *Stats) IncrReqStats(b int64)

IncrReqStats increments the stats totals for the server.

func (*Stats) IncrRouteStats

func (s *Stats) IncrRouteStats(path string, rb int64)

IncrRouteStats increments the stats totals for the route.

func (*Stats) String

func (s *Stats) String() string

String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.

Jump to

Keyboard shortcuts

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