socket

package
v2.0.0-...-814a870 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StatusOk means that command was successfully processed.
	StatusOk = 0
	// StatusErr means that command was not processed,
	// please check the `Response.Error` for details.
	StatusErr = 13
	// StatusInternalErr means than command was not sent or decoding of response failed.
	StatusInternalErr = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	Name    string
	Handler ActionFunc
}

Action is a pair of command name and command handler.

type ActionFunc

type ActionFunc func(request Request) Response

ActionFunc is a specified handler of the socket command.

type Client

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

Client provides the ability to communicate over the socket with some application running `Server`.

func NewClient

func NewClient(socketName string) *Client

NewClient returns new `Client`.

func (Client) Send

func (client Client) Send(request Request) (*Response, error)

Send tries to send a command in the `Request` through the socket to the `Server` and process the `Response`.

type Request

type Request struct {
	Action string          `json:"ActionFunc"`
	Args   json.RawMessage `json:"args"`
}

Request is a pair of command name and command arguments.

type Response

type Response struct {
	Status int             `json:"status"`
	Error  string          `json:"error,omitempty"`
	Data   json.RawMessage `json:"data"`
}

Response is the result of executing the command handler.

func NewResponse

func NewResponse(status int, data interface{}, errorStr string) Response

NewResponse correctly fills `Response` with passed arguments.

type Server

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

Server is a handler that opens a `net.Socket` and accepts commands and writes responses in JSON format.

func NewServer

func NewServer(socketName string, actions ...Action) *Server

NewServer creates a new server with some actions.

func (*Server) Errors

func (sw *Server) Errors() <-chan error

Errors returns a channel with errors.

func (*Server) Serve

func (sw *Server) Serve(ctx context.Context) (err error)

Serve creates the UNIX socket and starts listening for incoming commands. When command accepted server tries to decode message into `Request`. In case when the server has the handler for `Request` command it executes and writes a response in JSON format to the socket.

func (*Server) SetHandler

func (sw *Server) SetHandler(name string, action ActionFunc)

SetHandler adds new or replaces the command (action) handler.

Jump to

Keyboard shortcuts

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