socket

package
v0.0.0-...-947ae56 Latest Latest
Warning

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

Go to latest
Published: May 26, 2021 License: MIT Imports: 15 Imported by: 3

Documentation

Index

Constants

View Source
const (
	HELLO_MAJOR = 2
	HELLO_MINOR = 0
)
View Source
const (
	ServerAckSuccess        = 0
	ServerAckBadRequest     = 1
	ServerAckUnauthorized   = 2
	ServerAckNotFound       = 3
	ServerAckGeneralFailure = 4
	ServerAckTimeout        = 5
)
View Source
const (
	NoEventId                          EventId = 0
	ClientHelloEventId                         = 1
	ServerHelloEventId                         = 2
	ChannelFilterEventId                       = 3
	ServerAckEventId                           = 4
	Reserved5EventId                           = 5
	BusPowerStatusUpdateEventId                = 6
	BusPowerEventId                            = 7
	ChannelUpdateRequestEventId                = 8
	ChannelUpdateEventId                       = 9
	ChannelListRequestEventId                  = 10
	ChannelListEventId                         = 11
	NodeUpdateRequestEventId                   = 12
	NodeUpdateEventId                          = 13
	NodeListRequestEventId                     = 14
	NodeListEventId                            = 15
	NodeFirmwareEventId                        = 16
	NodeFirmwareDownloadRequestEventId         = 17
	ReservedEvent16EventId                     = 18
	NodeFirmwareProgressEventId                = 19
	NodeRebootRequestEventId                   = 20
	BusPowerStatusUpdateRequestEventId         = 21
	DeviceInformationRequestEventId            = 22
	DeviceInformationEventId                   = 23
	SystemPropertiesRequestEventId             = 24
	SystemPropertiesEventId                    = 25
	EventIdCount                               = 26
)

Variables

View Source
var (
	ErrorServerAckBadRequest     = errors.New("Bad request")
	ErrorServerAckUnauthorized   = errors.New("Unauthorized")
	ErrorServerAckNotFound       = errors.New("Not found")
	ErrorServerAckGeneralFailure = errors.New("General failure")
	ErrorServerAckTimeout        = errors.New("Timeout")
	ErrorServerAckUndefined      = errors.New("Undefined")
)
View Source
var ErrorMissingData error = errors.New("Missing data for value decoder")
View Source
var EventNameMap map[string]EventId
View Source
var EventNames = [EventIdCount]string{
	"no-event",
	"client-hello-event",
	"server-hello-event",
	"channel-subscribe-event",
	"server-ack-event",
	"reserved5-event",
	"bus-power-status-update-event",
	"bus-power-event",
	"channel-update-request-event",
	"channel-update-event",
	"channel-list-request-event",
	"channel-list-event",
	"node-update-request-event",
	"node-update-event",
	"node-list-request-event",
	"node-list-event",
	"node-firmware-upload-event",
	"node-firmware-download-request-event",
	"node-firmware-download-event",
	"node-firmware-progress-event",
	"node-reboot-request-event",
	"bus-power-status-update-request-event",
	"device-information-request-event",
	"device-information-event",
	"system-properties-request-event",
	"system-properties-event",
}
View Source
var Terminate = errors.New("Client termination request")
View Source
var TimeoutError = errors.New("Event response timeout")

Functions

func DecodeTime

func DecodeTime(src []byte) time.Time

func DecodeUint16

func DecodeUint16(src []byte) uint16

func DecodeUint32

func DecodeUint32(src []byte) uint32

func DecodeUint64

func DecodeUint64(src []byte) uint64

func EncodeEvent

func EncodeEvent(w io.Writer, e Eventer) error

func EncodeTime

func EncodeTime(dest []byte, t time.Time)

func EncodeUint16

func EncodeUint16(dest []byte, u uint16)

func EncodeUint32

func EncodeUint32(dest []byte, u uint32)

func EncodeUint64

func EncodeUint64(dest []byte, u uint64)

func ReturnErrorOrContinue

func ReturnErrorOrContinue(c *EventConn, e error) error

func ReturnErrorOrTerminate

func ReturnErrorOrTerminate(c *EventConn, e error) error

Types

type BaseEvent

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

Event

func CreateEvent

func CreateEvent(eid EventId) BaseEvent

func (BaseEvent) Id

func (e BaseEvent) Id() EventId

func (BaseEvent) MsgId

func (e BaseEvent) MsgId() uint16

func (*BaseEvent) SetMsgId

func (e *BaseEvent) SetMsgId(mid uint16)

func (BaseEvent) String

func (e BaseEvent) String() string

type BusPowerEvent

type BusPowerEvent struct {
	BaseEvent
	PowerOn bool
}

func NewBusPowerEvent

func NewBusPowerEvent(power_on bool) *BusPowerEvent

func (BusPowerEvent) Pack

func (bp BusPowerEvent) Pack() ([]byte, error)

func (*BusPowerEvent) Unpack

func (bp *BusPowerEvent) Unpack(b []byte) error

type BusPowerStatusUpdateEvent

type BusPowerStatusUpdateEvent struct {
	BaseEvent
	Status *device.PowerStatus
}

func NewBusPowerStatusUpdateEvent

func NewBusPowerStatusUpdateEvent(status *device.PowerStatus) *BusPowerStatusUpdateEvent

func (BusPowerStatusUpdateEvent) Pack

func (bps BusPowerStatusUpdateEvent) Pack() ([]byte, error)

func (BusPowerStatusUpdateEvent) String

func (bps BusPowerStatusUpdateEvent) String() string

func (*BusPowerStatusUpdateEvent) Unpack

func (bps *BusPowerStatusUpdateEvent) Unpack(b []byte) error

type BusPowerStatusUpdateRequestEvent

type BusPowerStatusUpdateRequestEvent struct {
	EmptyEvent
}

func NewBusPowerStatusUpdateRequestEvent

func NewBusPowerStatusUpdateRequestEvent() *BusPowerStatusUpdateRequestEvent

type ChannelFilterEvent

type ChannelFilterEvent struct {
	BaseEvent
	Channels map[nocan.ChannelId]bool
}

func NewChannelFilterEvent

func NewChannelFilterEvent(chans ...nocan.ChannelId) *ChannelFilterEvent

func (*ChannelFilterEvent) Add

func (sl *ChannelFilterEvent) Add(chans ...nocan.ChannelId)

func (*ChannelFilterEvent) Includes

func (sl *ChannelFilterEvent) Includes(id nocan.ChannelId) bool

func (*ChannelFilterEvent) Pack

func (sl *ChannelFilterEvent) Pack() ([]byte, error)

func (*ChannelFilterEvent) Remove

func (sl *ChannelFilterEvent) Remove(chans ...nocan.ChannelId)

func (*ChannelFilterEvent) String

func (sl *ChannelFilterEvent) String() string

func (*ChannelFilterEvent) Unpack

func (sl *ChannelFilterEvent) Unpack(b []byte) error

type ChannelListEvent

type ChannelListEvent struct {
	BaseEvent `json:"-"`
	Channels  []*ChannelUpdateEvent `json:"channels"`
}

ChannelListEvent

func NewChannelListEvent

func NewChannelListEvent() *ChannelListEvent

func (*ChannelListEvent) Append

func (cl *ChannelListEvent) Append(cu *ChannelUpdateEvent)

func (*ChannelListEvent) Pack

func (cl *ChannelListEvent) Pack() ([]byte, error)

func (ChannelListEvent) String

func (cl ChannelListEvent) String() string

func (*ChannelListEvent) Unpack

func (cl *ChannelListEvent) Unpack(b []byte) error

type ChannelListRequestEvent

type ChannelListRequestEvent struct {
	EmptyEvent
}

func NewChannelListRequestEvent

func NewChannelListRequestEvent() *ChannelListRequestEvent

type ChannelStatus

type ChannelStatus byte

ChannelUpdate

const (
	CHANNEL_CREATED ChannelStatus = iota
	CHANNEL_UPDATED
	CHANNEL_DESTROYED
	CHANNEL_NOT_FOUND
)

func (ChannelStatus) String

func (cs ChannelStatus) String() string

type ChannelUpdateEvent

type ChannelUpdateEvent struct {
	BaseEvent
	ChannelId   nocan.ChannelId
	ChannelName string
	Status      ChannelStatus
	Value       []byte
	UpdatedAt   time.Time
}

func NewChannelUpdateEvent

func NewChannelUpdateEvent(chan_name string, chan_id nocan.ChannelId, status ChannelStatus, value []byte, updated_at time.Time) *ChannelUpdateEvent

func NewChannelUpdateEventEmpty

func NewChannelUpdateEventEmpty() *ChannelUpdateEvent

func (*ChannelUpdateEvent) MarshalJSON

func (cu *ChannelUpdateEvent) MarshalJSON() ([]byte, error)

func (*ChannelUpdateEvent) Pack

func (cu *ChannelUpdateEvent) Pack() ([]byte, error)

func (*ChannelUpdateEvent) PackedLength

func (cu *ChannelUpdateEvent) PackedLength() int

func (ChannelUpdateEvent) String

func (cu ChannelUpdateEvent) String() string

func (*ChannelUpdateEvent) Unpack

func (cu *ChannelUpdateEvent) Unpack(value []byte) error

type ChannelUpdateRequestEvent

type ChannelUpdateRequestEvent struct {
	BaseEvent
	ChannelId   nocan.ChannelId
	ChannelName string
}

func NewChannelUpdateRequestEvent

func NewChannelUpdateRequestEvent(chan_name string, chan_id nocan.ChannelId) *ChannelUpdateRequestEvent

func (*ChannelUpdateRequestEvent) Pack

func (cu *ChannelUpdateRequestEvent) Pack() ([]byte, error)

func (*ChannelUpdateRequestEvent) PackedLength

func (cu *ChannelUpdateRequestEvent) PackedLength() int

func (ChannelUpdateRequestEvent) String

func (cu ChannelUpdateRequestEvent) String() string

func (*ChannelUpdateRequestEvent) Unpack

func (cu *ChannelUpdateRequestEvent) Unpack(value []byte) error

type ClientDescriptor

type ClientDescriptor struct {
	Id              uint
	Server          *Server
	Conn            *sscp.Conn
	OutputChan      chan Eventer
	TerminationChan chan struct{}
	ChannelFilter   *ChannelFilterEvent
	Connected       bool
	Next            *ClientDescriptor
	LastMsgId       uint16
}

ClientDescriptor represents a single connection from an external client through TCP/IP

func (*ClientDescriptor) Name

func (c *ClientDescriptor) Name() string

func (*ClientDescriptor) SendAck

func (c *ClientDescriptor) SendAck(ack byte) error

func (*ClientDescriptor) SendEvent

func (c *ClientDescriptor) SendEvent(event Eventer) error

type ClientHelloEvent

type ClientHelloEvent struct {
	BaseEvent
	Tool         string
	VersionMajor byte
	VersionMinor byte
}

func NewClientHelloEvent

func NewClientHelloEvent(tool string, major byte, minor byte) *ClientHelloEvent

func (*ClientHelloEvent) Pack

func (ch *ClientHelloEvent) Pack() ([]byte, error)

func (*ClientHelloEvent) Unpack

func (ch *ClientHelloEvent) Unpack(b []byte) error

type DeviceInformationEvent

type DeviceInformationEvent struct {
	BaseEvent
	Information *device.Information
}

func NewDeviceInformationEvent

func NewDeviceInformationEvent(di *device.Information) *DeviceInformationEvent

func (DeviceInformationEvent) Pack

func (ie DeviceInformationEvent) Pack() ([]byte, error)

func (DeviceInformationEvent) String

func (ie DeviceInformationEvent) String() string

func (*DeviceInformationEvent) Unpack

func (ie *DeviceInformationEvent) Unpack(b []byte) error

type DeviceInformationRequestEvent

type DeviceInformationRequestEvent struct {
	EmptyEvent
}

func NewDeviceInformationRequestEvent

func NewDeviceInformationRequestEvent() *DeviceInformationRequestEvent

type EmptyEvent

type EmptyEvent struct {
	BaseEvent
}

func (EmptyEvent) Pack

func (x EmptyEvent) Pack() ([]byte, error)

func (*EmptyEvent) Unpack

func (c *EmptyEvent) Unpack(b []byte) error

type EventCallback

type EventCallback func(*EventConn, Eventer) error

type EventConn

type EventConn struct {
	Conn       *sscp.Conn
	Addr       string
	ClientName string
	AuthToken  string
	Connected  bool
	AutoRedial bool
	MsgId      uint16
	Callbacks  map[EventId]EventCallback

	Mutex sync.Mutex
	// contains filtered or unexported fields
}

EventConn

func NewEventConn

func NewEventConn(addr string, client_name string, auth string) *EventConn

func (*EventConn) Close

func (conn *EventConn) Close() error

func (*EventConn) Connect

func (conn *EventConn) Connect() error

func (*EventConn) EnableAutoRedial

func (conn *EventConn) EnableAutoRedial() *EventConn

func (*EventConn) OnConnect

func (conn *EventConn) OnConnect(cb func(*EventConn) error)

func (*EventConn) OnEvent

func (conn *EventConn) OnEvent(eid EventId, cb EventCallback)

func (*EventConn) Send

func (conn *EventConn) Send(request Eventer) error

func (*EventConn) SendAsync

func (conn *EventConn) SendAsync(request Eventer, cb func(*EventConn, error) error)

func (*EventConn) Terminate

func (conn *EventConn) Terminate() error

func (*EventConn) WaitTermination

func (conn *EventConn) WaitTermination(duration time.Duration) error

type EventHandler

type EventHandler func(*ClientDescriptor, Eventer) error

type EventId

type EventId byte

func LookupEventByName

func LookupEventByName(name string) EventId

func (EventId) String

func (eid EventId) String() string

type EventRequest

type EventRequest struct {
	ResponseCallback func(*EventConn, error) error
	CreatedAt        time.Time
}

type Eventer

type Eventer interface {
	Id() EventId
	MsgId() uint16
	SetMsgId(uint16)
	Pack() ([]byte, error)
	Unpack([]byte) error
	String() string
}

func DecodeEvent

func DecodeEvent(r io.Reader) (Eventer, error)

type FirmwareBlock

type FirmwareBlock struct {
	Offset uint32
	Data   []byte
}

type NodeFirmwareDownloadRequestEvent

type NodeFirmwareDownloadRequestEvent struct {
	BaseEvent
	NodeId nocan.NodeId
}

func NewNodeFirmwareDownloadRequestEvent

func NewNodeFirmwareDownloadRequestEvent(nid nocan.NodeId) *NodeFirmwareDownloadRequestEvent

func (NodeFirmwareDownloadRequestEvent) Pack

func (*NodeFirmwareDownloadRequestEvent) Unpack

func (ndr *NodeFirmwareDownloadRequestEvent) Unpack(b []byte) error

type NodeFirmwareEvent

type NodeFirmwareEvent struct {
	BaseEvent
	NodeId   nocan.NodeId
	Download bool
	Limit    uint32
	Code     []FirmwareBlock
}

func NewNodeFirmwareEvent

func NewNodeFirmwareEvent(id nocan.NodeId) *NodeFirmwareEvent

func (*NodeFirmwareEvent) AppendBlock

func (nf *NodeFirmwareEvent) AppendBlock(offset uint32, data []byte)

func (*NodeFirmwareEvent) ConfigureAsDownload

func (nf *NodeFirmwareEvent) ConfigureAsDownload() *NodeFirmwareEvent

func (*NodeFirmwareEvent) ConfigureAsUpload

func (nf *NodeFirmwareEvent) ConfigureAsUpload() *NodeFirmwareEvent

func (*NodeFirmwareEvent) Pack

func (nf *NodeFirmwareEvent) Pack() ([]byte, error)

func (*NodeFirmwareEvent) Unpack

func (nf *NodeFirmwareEvent) Unpack(b []byte) error

type NodeFirmwareProgressEvent

type NodeFirmwareProgressEvent struct {
	BaseEvent
	NodeId           nocan.NodeId
	Progress         ProgressReport
	BytesTransferred uint32
}

func NewNodeFirmwareProgressEvent

func NewNodeFirmwareProgressEvent(id nocan.NodeId) *NodeFirmwareProgressEvent

func (*NodeFirmwareProgressEvent) MarkAsFailed

func (*NodeFirmwareProgressEvent) MarkAsSuccess

func (*NodeFirmwareProgressEvent) Pack

func (nfp *NodeFirmwareProgressEvent) Pack() ([]byte, error)

func (NodeFirmwareProgressEvent) String

func (nfp NodeFirmwareProgressEvent) String() string

func (*NodeFirmwareProgressEvent) Unpack

func (nfp *NodeFirmwareProgressEvent) Unpack(b []byte) error

func (*NodeFirmwareProgressEvent) Update

func (nfp *NodeFirmwareProgressEvent) Update(progress ProgressReport, transferred uint32) *NodeFirmwareProgressEvent

type NodeListEvent

type NodeListEvent struct {
	BaseEvent
	Nodes []*NodeUpdateEvent `json:"nodes"`
}

func NewNodeListEvent

func NewNodeListEvent() *NodeListEvent

func (*NodeListEvent) Append

func (nl *NodeListEvent) Append(nu *NodeUpdateEvent)

func (*NodeListEvent) Exclude

func (nl *NodeListEvent) Exclude(id nocan.NodeId) bool

func (*NodeListEvent) Pack

func (nl *NodeListEvent) Pack() ([]byte, error)

func (NodeListEvent) String

func (nl NodeListEvent) String() string

func (*NodeListEvent) Unpack

func (nl *NodeListEvent) Unpack(b []byte) error

type NodeListRequestEvent

type NodeListRequestEvent struct {
	EmptyEvent
}

func NewNodeListRequestEvent

func NewNodeListRequestEvent() *NodeListRequestEvent

type NodeRebootRequestEvent

type NodeRebootRequestEvent struct {
	BaseEvent
	RebootId byte
}

func NewNodeRebootRequestEvent

func NewNodeRebootRequestEvent(nid nocan.NodeId, force bool) *NodeRebootRequestEvent

func (*NodeRebootRequestEvent) Forced

func (nr *NodeRebootRequestEvent) Forced() bool

func (NodeRebootRequestEvent) NodeId

func (nr NodeRebootRequestEvent) NodeId() nocan.NodeId

func (NodeRebootRequestEvent) Pack

func (nr NodeRebootRequestEvent) Pack() ([]byte, error)

func (NodeRebootRequestEvent) String

func (nr NodeRebootRequestEvent) String() string

func (*NodeRebootRequestEvent) Unpack

func (nr *NodeRebootRequestEvent) Unpack(b []byte) error

type NodeUpdateEvent

type NodeUpdateEvent struct {
	BaseEvent `json:"-"`
	NodeId    nocan.NodeId     `json:"id"`
	State     models.NodeState `json:"state"`
	Udid      models.Udid8     `json:"udid"`
	LastSeen  time.Time        `json:"last_seen"`
}

func NewNodeUpdateEvent

func NewNodeUpdateEvent() *NodeUpdateEvent

func NewNodeUpdateEventWithParams

func NewNodeUpdateEventWithParams(id nocan.NodeId, state models.NodeState, udid models.Udid8, last_seen time.Time) *NodeUpdateEvent

func (*NodeUpdateEvent) Pack

func (nu *NodeUpdateEvent) Pack() ([]byte, error)

func (NodeUpdateEvent) String

func (nu NodeUpdateEvent) String() string

func (*NodeUpdateEvent) Unpack

func (nu *NodeUpdateEvent) Unpack(b []byte) error

type NodeUpdateRequestEvent

type NodeUpdateRequestEvent struct {
	BaseEvent
	NodeId nocan.NodeId
}

func NewNodeUpdateRequestEvent

func NewNodeUpdateRequestEvent(node_id nocan.NodeId) *NodeUpdateRequestEvent

func (NodeUpdateRequestEvent) Pack

func (nu NodeUpdateRequestEvent) Pack() ([]byte, error)

func (NodeUpdateRequestEvent) String

func (nu NodeUpdateRequestEvent) String() string

func (*NodeUpdateRequestEvent) Unpack

func (nu *NodeUpdateRequestEvent) Unpack(b []byte) error

type ProgressReport

type ProgressReport byte
const (
	ProgressSuccess ProgressReport = 0xFE
	ProgressFailed  ProgressReport = 0xFF
)

func (ProgressReport) String

func (pr ProgressReport) String() string

type Server

type Server struct {
	Mutex     sync.Mutex
	AuthToken string
	// contains filtered or unexported fields
}

func NewServer

func NewServer() *Server

func (*Server) Broadcast

func (s *Server) Broadcast(event Eventer, exclude_client *ClientDescriptor)

func (*Server) DeleteClient

func (s *Server) DeleteClient(c *ClientDescriptor) bool

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(addr string, auth_token string) error

func (*Server) NewClient

func (s *Server) NewClient(conn *sscp.Conn) *ClientDescriptor

func (*Server) RegisterHandler

func (s *Server) RegisterHandler(eid EventId, fn EventHandler)

type ServerAckEvent

type ServerAckEvent struct {
	BaseEvent
	Code byte
}

func NewServerAckEvent

func NewServerAckEvent(val byte) *ServerAckEvent

func (*ServerAckEvent) Pack

func (sa *ServerAckEvent) Pack() ([]byte, error)

func (ServerAckEvent) String

func (sa ServerAckEvent) String() string

func (ServerAckEvent) ToError

func (sa ServerAckEvent) ToError() error

func (*ServerAckEvent) Unpack

func (sa *ServerAckEvent) Unpack(value []byte) error

type ServerHelloEvent

type ServerHelloEvent struct {
	ClientHelloEvent
}

func NewServerHelloEvent

func NewServerHelloEvent(tool string, major byte, minor byte) *ServerHelloEvent

type SystemPropertiesEvent

type SystemPropertiesEvent struct {
	BaseEvent
	Properties *properties.Properties
}

func NewSystemPropertiesEvent

func NewSystemPropertiesEvent(props *properties.Properties) *SystemPropertiesEvent

func (SystemPropertiesEvent) Pack

func (sp SystemPropertiesEvent) Pack() ([]byte, error)

func (SystemPropertiesEvent) String

func (sp SystemPropertiesEvent) String() string

func (*SystemPropertiesEvent) Unpack

func (sp *SystemPropertiesEvent) Unpack(b []byte) error

type SystemPropertiesRequestEvent

type SystemPropertiesRequestEvent struct {
	EmptyEvent
}

func NewSystemPropertiesRequestEvent

func NewSystemPropertiesRequestEvent() *SystemPropertiesRequestEvent

Jump to

Keyboard shortcuts

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