service

package
v0.0.0-...-eadad4f Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2020 License: MIT Imports: 17 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultLocalTransportPublish

func DefaultLocalTransportPublish(tp *LocalTransport,
	network, chat, node string, payload interface{}) error

func DispatchMsg

func DispatchMsg(rcv Receiver, rawMsg []byte) error

DispatchMsg dispatches a raw input message to the receiver (service)

func MakeID

func MakeID(reqID string) string

reqID can be an ID that the new ID will be in response to, or empty.

func Uint64ID

func Uint64ID(dest *strings.Builder, n uint64) int

Uint64ID writes an ID string to dest based on n. Returns the number of bytes written.

Types

type ClientStateInfo

type ClientStateInfo struct {
	Network       stdchat.NetworkStateInfo
	Subscriptions []stdchat.SubscriptionStateInfo
}

type LocalTransport

type LocalTransport struct {
	Protocol       string
	PublishHandler func(tp *LocalTransport,
		network, chat, node string, payload interface{}) error
	WebServer
}

LocalTransport is a service transport for local use. All fields optional.

func (*LocalTransport) Advertise

func (tp *LocalTransport) Advertise() error

func (*LocalTransport) GetProtocol

func (tp *LocalTransport) GetProtocol() string

func (*LocalTransport) Publish

func (tp *LocalTransport) Publish(network, chat, node string, payload interface{}) error

func (*LocalTransport) PublishError

func (tp *LocalTransport) PublishError(id string, network string, err error) error

type MultiTransport

type MultiTransport struct {
	Protocol string
	WebServer
	// contains filtered or unexported fields
}

MultiTransport relays messages to zero or more other transports. It is thread safe.

func (*MultiTransport) AddTransport

func (tp *MultiTransport) AddTransport(transport Transporter)

func (*MultiTransport) Advertise

func (tp *MultiTransport) Advertise() error

func (*MultiTransport) Close

func (tp *MultiTransport) Close() error

func (*MultiTransport) GetProtocol

func (tp *MultiTransport) GetProtocol() string

func (*MultiTransport) Publish

func (tp *MultiTransport) Publish(network, chat, node string, payload interface{}) error

Publish will be called on all the added transports, errors will be collected into a *MultiTransportError if more than one error, *SingleTransportError if just one error, or nil if no errors.

func (*MultiTransport) PublishError

func (tp *MultiTransport) PublishError(id string, network string, err error) error

func (*MultiTransport) RemoveTransport

func (tp *MultiTransport) RemoveTransport(transport Transporter)

type MultiTransportError

type MultiTransportError struct {
	Errors []SingleTransportError
}

func (*MultiTransportError) Error

func (err *MultiTransportError) Error() string

type MultiTransporter

type MultiTransporter interface {
	Transporter
	AddTransport(transport Transporter)
	RemoveTransport(transport Transporter)
}

type Networker

type Networker interface {
	io.Closer
	Receiver
	Logout(reason string) error                 // Same as Close() with logout reason.
	Start(ctx context.Context, id string) error // id = request id
	NetworkID() string
	ConnID() string // empty if no connections.
	Context() context.Context
	Closed() bool
	GetStateInfo() ClientStateInfo
}

Networker is a client implementation for a network.

type NewClientFunc

type NewClientFunc = func(svc *Service, remote, userID, auth string, values stdchat.ValuesInfo) (Networker, error)

type Receiver

type Receiver interface {
	Handler(msg *stdchat.ChatMsg)
	CmdHandler(msg *stdchat.CmdMsg)
}

Receiver can receive input messages and commands.

type Service

type Service struct {
	Verbose bool // verbose output to log.Print/Printf
	// contains filtered or unexported fields
}

Service is a service.

func NewService

func NewService(tp Transporter, newClient NewClientFunc) *Service

NewService creates a new service. newClient must be set to a function, a lock will be acquired during newClient. The client eventually needs to call OnClientClosed when done.

func (*Service) CheckArgs

func (svc *Service) CheckArgs(n int, msg *stdchat.CmdMsg) bool

CheckArgs ensures the CmdMsg has at least n args, if so returns true; otherwise returns false and publishes an error.

func (*Service) Close

func (svc *Service) Close() error

func (*Service) Closed

func (svc *Service) Closed() bool

func (*Service) CmdHandler

func (svc *Service) CmdHandler(msg *stdchat.CmdMsg)

func (*Service) Context

func (svc *Service) Context() context.Context

func (*Service) GenericError

func (svc *Service) GenericError(err error)

func (*Service) GetClientByNetwork

func (svc *Service) GetClientByNetwork(networkID string) Networker

func (*Service) GetClients

func (svc *Service) GetClients() []Networker

func (*Service) GetStateInfo

func (svc *Service) GetStateInfo() ServiceStateInfo

func (*Service) Handler

func (svc *Service) Handler(msg *stdchat.ChatMsg)

func (*Service) Login

func (svc *Service) Login(remote, userID, auth string, values stdchat.ValuesInfo, id string) (Networker, error)

func (*Service) Logout

func (svc *Service) Logout(logoutID, reason string, values stdchat.ValuesInfo, id string) error

func (*Service) OnClientClosed

func (svc *Service) OnClientClosed(client Networker)

OnClientClosed is to be called by the Client implementation when done. Panics if client.Closed() returns false.

func (*Service) Protocol

func (svc *Service) Protocol() string

func (*Service) Transporter

func (svc *Service) Transporter() Transporter

type ServiceStateInfo

type ServiceStateInfo struct {
	Protocol      stdchat.ProtocolStateInfo
	Networks      []stdchat.NetworkStateInfo
	Subscriptions []stdchat.SubscriptionStateInfo
}

type Servicer

type Servicer interface {
	io.Closer
	Receiver
	GenericError(err error) // An error from outside, such as during msg dispatch.
	GetClients() []Networker
	GetClientByNetwork(networkID string) Networker
	Protocol() string
	Context() context.Context
	Closed() bool
	GetStateInfo() ServiceStateInfo
}

Servicer represents a service.

type SingleTransportError

type SingleTransportError struct {
	Transport Transporter
	Err       error
}

func (*SingleTransportError) Error

func (err *SingleTransportError) Error() string

func (*SingleTransportError) Unwrap

func (err *SingleTransportError) Unwrap() error

type Transporter

type Transporter interface {
	io.Closer

	GetProtocol() string

	// Advertise the service is up,
	// this needs to be called before clients can do anything.
	Advertise() error
	// Publish a message.
	// network and chat can be empty.
	// node is the final part in the topic name, as in chat/Protocol/node
	// e.g. to publish a protocol msg, use Publish("", "", "my-info", payload)
	Publish(network, chat, node string, payload interface{}) error

	// id should be the ID of the request (if a request), which can be empty.
	// network can be empty if a protocol msg.
	PublishError(id string, network string, err error) error

	// Full URL returned.
	// pathSuffix is the last part of the URL path, such as userA/foo4.png
	// handler is set to a HTTP client handler to serve the headers+content.
	// It is resonable that a directory is served.
	// Use StopServeURL to stop serving.
	ServeURL(network, pathSuffix string, handler http.Handler) (string, error)

	// Cancel serving a URL.
	// If this is the last served URL, the web service may shut down.
	StopServeURL(network, pathSuffix string)
}

Transporter is a chat service transport.

type WebServer

type WebServer struct {
	PublicURL string // uses localhost and a random port if empty.
	BindAddr  string // parses from PublicURL if empty.
	// contains filtered or unexported fields
}

func (*WebServer) Close

func (ws *WebServer) Close() error

func (*WebServer) FindHandler

func (ws *WebServer) FindHandler(path string) http.Handler

func (*WebServer) ServeHTTP

func (ws *WebServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*WebServer) ServeURL

func (ws *WebServer) ServeURL(network, pathSuffix string, handler http.Handler) (string, error)

func (*WebServer) StopServeURL

func (ws *WebServer) StopServeURL(network, pathSuffix string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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