command

package
v0.0.0-...-fdfa60e Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderControlEcho         = 0x00
	HeaderControlPauseStream  = 0x01
	HeaderControlResumeStream = 0x02
)

Control signal types

View Source
const (
	StreamHeaderMaxLength = 0x1fff
	StreamHeaderMaxMarker = 0x07
)

Stream header consts

View Source
const (
	HeaderMaxData = 0x3f
)

Consts

View Source
const (
	MaxCommandID = 0x0f
)

Consts

Variables

View Source
var (
	ErrHandlerUnknownHeaderType = errors.New(
		"unknown command header type")

	ErrHandlerControlMessageTooLong = errors.New(
		"control message was too long")

	ErrHandlerInvalidControlMessage = errors.New(
		"invalid control message")
)

Errors

View Source
var (
	ErrStreamsInvalidStreamID = errors.New(
		"stream ID is invalid")

	ErrStreamsStreamOperateInactiveStream = errors.New(
		"specified stream was inactive for operation")

	ErrStreamsStreamClosingInactiveStream = errors.New(
		"closing an inactive stream is not allowed")

	ErrStreamsStreamReleasingInactiveStream = errors.New(
		"releasing an inactive stream is not allowed")
)

Errors

View Source
var (
	ErrCommandRunUndefinedCommand = errors.New(
		"undefined Command")
)

Errors

View Source
var (
	ErrFSMMachineClosed = errors.New(
		"FSM Machine is already closed, it cannot do anything but be released")
)

Errors

Functions

This section is empty.

Types

type Builder

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

Builder builds a command

func Register

func Register(name string, c Command, p configuration.PresetReloader) Builder

Register builds a Builder for registration

type Command

type Command func(
	l log.Logger,
	w StreamResponder,
	cfg Configuration,
) FSMMachine

Command represents a command handler machine builder

type Commander

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

Commander command control

func New

func New(cs Commands) Commander

New creates a new Commander

func (Commander) New

func (c Commander) New(
	cfg Configuration,
	receiver rw.FetchReader,
	sender io.Writer,
	senderLock *sync.Mutex,
	receiveDelay time.Duration,
	sendDelay time.Duration,
	l log.Logger,
) (Handler, error)

New Adds a new client

type Commands

type Commands [MaxCommandID + 1]Builder

Commands contains data of all commands

func (Commands) Reconfigure

func (c Commands) Reconfigure(
	p []configuration.Preset,
) ([]configuration.Preset, error)

Reconfigure lets commands reset configuration

func (*Commands) Register

func (c *Commands) Register(
	id byte,
	name string,
	cb Command,
	ps configuration.PresetReloader,
)

Register registers a new command

func (Commands) Run

func (c Commands) Run(
	id byte,
	l log.Logger,
	w StreamResponder,
	cfg Configuration,
) (FSM, error)

Run creates command executer

type Configuration

type Configuration struct {
	Dial        network.Dial
	DialTimeout time.Duration
}

Configuration contains configuration data needed to run command

type FSM

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

FSM state machine control

func (*FSM) Switch

func (f *FSM) Switch(s FSMState)

Switch switch to specificied State for the next tick

type FSMError

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

FSMError Represents an error from FSM

func NoFSMError

func NoFSMError() FSMError

NoFSMError return a FSMError that represents a success operation

func ToFSMError

func ToFSMError(e error, c StreamError) FSMError

ToFSMError converts error to FSMError

func (FSMError) Code

func (e FSMError) Code() StreamError

Code return the error code

func (FSMError) Error

func (e FSMError) Error() string

Error return the error message

func (FSMError) Succeed

func (e FSMError) Succeed() bool

Succeed returns whether or not current error represents a succeed operation

type FSMMachine

type FSMMachine interface {
	// Bootup boots up the machine
	Bootup(r *rw.LimitedReader, b []byte) (FSMState, FSMError)

	// Close stops the machine and get it ready for release.
	//
	// NOTE: Close function is responsible in making sure the HeaderClose signal
	//       is sent before it returns.
	//       (It may not need to send the header by itself, but it have to
	//       make sure the header is sent)
	Close() error

	// Release shuts the machine down completely and release it's resources
	Release() error
}

FSMMachine State machine

type FSMState

type FSMState func(f *FSM, r *rw.LimitedReader, h StreamHeader, b []byte) error

FSMState represents a state of a machine

type Handler

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

Handler client stream control

func (*Handler) Handle

func (e *Handler) Handle() error

Handle starts handling

type HandlerCancelSignal

type HandlerCancelSignal chan struct{}

HandlerCancelSignal signals the cancel of the entire handling proccess

type Header byte

Header Packet Type

const (
	// 00------: Control signals
	// Remaing bits: Data length
	//
	// Format:
	//   0011111 [63 bytes long data] - 63 bytes of control data
	//
	HeaderControl Header = 0x00

	// 01------: Bidirectional stream data
	// Remaining bits: Stream ID
	// Followed by: Parameter or data
	//
	// Format:
	//   0111111 [Command parameters / data] - Open/use stream 63 to execute
	//                                         command or transmit data
	HeaderStream Header = 0x40

	// 10------: Close stream
	// Remaining bits: Stream ID
	//
	// Format:
	//   1011111 - Close stream 63
	//
	// WARNING: The requester MUST NOT send any data to this stream once this
	//          header is sent.
	//
	// WARNING: The receiver MUST reply with a Completed header to indicate
	//          the success of the Close action. Until a Completed header is
	//          replied, all data from the sender must be proccessed as normal.
	HeaderClose Header = 0x80

	// 11------: Stream has been closed/completed in respond to client request
	// Remaining bits: Stream ID
	//
	// Format:
	//   1111111 - Stream 63 is completed
	//
	// WARNING: This header can ONLY be send in respond to a Close header
	//
	// WARNING: The sender of this header MUST NOT send any data to the stream
	//          once this header is sent until this stream been re-opened by a
	//          Data header
	HeaderCompleted Header = 0xc0
)

Packet Types

func (Header) Data

func (p Header) Data() byte

Data returns the data of current Packet header

func (Header) IsStreamControl

func (p Header) IsStreamControl() bool

IsStreamControl returns true when the header is for stream control, false when otherwise

func (*Header) Set

func (p *Header) Set(data byte)

Set set a new value of the Header

func (Header) String

func (p Header) String() string

Set set a new value of the Header

func (Header) Type

func (p Header) Type() Header

Type get packet type

type StreamError

type StreamError uint16

StreamError Stream Error signal

const (
	StreamErrorCommandUndefined      StreamError = 0x01
	StreamErrorCommandFailedToBootup StreamError = 0x02
)

Error signals

type StreamHeader

type StreamHeader [2]byte

StreamHeader contains data of the stream header

func (StreamHeader) Length

func (s StreamHeader) Length() uint16

Length returns the data length of the stream

func (StreamHeader) Marker

func (s StreamHeader) Marker() byte

Marker returns the header marker data

func (*StreamHeader) Set

func (s *StreamHeader) Set(marker byte, n uint16)

Set sets the stream header

type StreamInitialSignalSender

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

StreamInitialSignalSender sends stream initial signal

func (*StreamInitialSignalSender) Signal

func (s *StreamInitialSignalSender) Signal(
	errno StreamError, success bool) error

Signal send signal

type StreamResponder

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

StreamResponder sends data through stream

func (StreamResponder) HeaderSize

func (w StreamResponder) HeaderSize() int

HeaderSize returns the size of header

func (StreamResponder) Send

func (w StreamResponder) Send(marker byte, data []byte, buf []byte) error

Send sends data. Data will be automatically segmentated if it's too long to fit into one data package or buffer space

func (StreamResponder) SendManual

func (w StreamResponder) SendManual(marker byte, data []byte) error

SendManual sends the data without automatical segmentation. It will construct the data package directly using the given `data` buffer, that is, the first n bytes of the given `data` will be used to setup headers. It is the caller's

responsibility to leave n bytes of space so no meaningful data will be over

written. The number n can be acquired by calling .HeaderSize() method.

func (StreamResponder) Signal

func (w StreamResponder) Signal(signal Header) error

Signal sends a signal

Jump to

Keyboard shortcuts

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