Documentation
¶
Overview ¶
Package lunalink implements a LunaLink node (protocol revision 2.0): frame codec, MessagePack array encoding, durable outbox, ordered delivery and remote writes.
Index ¶
- Constants
- Variables
- func CRC16(data []byte) uint16
- func Decode(b []byte) (any, error)
- func DecodeError(p []byte) (code int64, msg string)
- func DecodeValue(dataType int, b []byte) any
- func EncodeAck(ackedSeq uint32) []byte
- func EncodeFrame(f Frame, computeCRC bool) []byte
- func EncodeGoodbye(reason string) []byte
- func EncodeHeartbeat(tsMs, pendingBatches, pendingValues, pendingBytes int64, oldestAgeSec float64, ...) []byte
- func EncodeHello(h Hello) []byte
- func EncodeItem(it Item) ([]byte, error)
- func EncodeTagDelta(items [][]byte, batchTsMs, nodeSeq int64, resync bool) []byte
- func EncodeTagWriteAck(seqRef uint32, status int, commandID string, hasCommand bool) []byte
- func EncodeValue(dataType int, v any) ([]byte, error)
- func StatusName(s int) string
- type Config
- type Encoder
- func (e *Encoder) Array(n int)
- func (e *Encoder) Bin(b []byte)
- func (e *Encoder) Bool(v bool)
- func (e *Encoder) Float32(v float32)
- func (e *Encoder) Float64(v float64)
- func (e *Encoder) Int(v int64)
- func (e *Encoder) Nil()
- func (e *Encoder) Raw(b []byte)
- func (e *Encoder) Str(s string)
- func (e *Encoder) Uint(v uint64)
- type Frame
- type GUID
- type Hello
- type HelloAck
- type Item
- type Node
- type OutboxEntry
- type Store
- func (s *Store) After(seq int64, n int) ([]OutboxEntry, error)
- func (s *Store) Close() error
- func (s *Store) CommandStatus(id string) (status int, found bool)
- func (s *Store) DeleteUpTo(seq int64) error
- func (s *Store) Enqueue(items [][]byte, tagIDs []GUID, batchTsMs int64) (int64, error)
- func (s *Store) Has(seq int64) bool
- func (s *Store) LastSequence() int64
- func (s *Store) Latest() [][]byte
- func (s *Store) SetCommandStatus(id string, status int) error
- func (s *Store) Stats() (batches, bytes, highest int64)
- type TagDeltaAck
- type WriteCmd
- type WriteHandler
Constants ¶
const ( Magic = 0x4C4C4E4B FrameVersion = 1 HeaderSize = 15 MaxPayloadSize = 4 * 1024 * 1024 FlagCompressed = 0x01 FlagRequiresAck = 0x02 )
Frame constants (spec section 3).
const ( MsgHello = 0x01 MsgHelloAck = 0x02 MsgTagDelta = 0x03 MsgTagWriteCmd = 0x04 MsgTagWriteAck = 0x05 MsgHeartbeat = 0x06 MsgHeartbeatAck = 0x07 MsgSnapshotReq = 0x08 MsgSnapshotResp = 0x09 MsgSubscribe = 0x0A MsgError = 0x0B MsgPing = 0x0C MsgPong = 0x0D MsgAck = 0x0E MsgTagDeltaAck = 0x0F MsgHistoryBatch = 0x10 MsgHistoryBatchAck = 0x11 MsgGoodbye = 0x12 MsgBrowseReq = 0x13 MsgBrowseResp = 0x14 MsgEventAck = 0x15 )
Message types (spec section 7).
const ( StatusUnknown = 0 StatusSucceeded = 1 StatusFailed = 2 StatusExpired = 3 )
Write status values (spec 7.6).
const ( TypeBool = 0 TypeInt16 = 1 TypeUInt16 = 2 TypeInt32 = 3 TypeUInt32 = 4 TypeFloat32 = 5 TypeFloat64 = 6 TypeString = 7 TypeEnum = 8 TypeJSON = 9 )
DataType values (spec 5.3). Types 10 to 13 need arrays-v1 and are not implemented by this node.
const ( QualityGood = 1 QualityBad = 2 QualityUncertain = 3 QualityNotConnected = 4 )
Quality values (spec 5.4).
Variables ¶
var ErrBadCRC = errors.New("lunalink: crc mismatch")
ErrBadCRC is returned for a frame whose CRC does not match. The frame was fully consumed.
var ErrBadMagic = errors.New("lunalink: bad magic")
ErrBadMagic is returned when a frame does not start with the magic. The stream cannot be resynchronised.
var ErrFrameTooLarge = errors.New("lunalink: frame too large")
ErrFrameTooLarge is returned when PayloadLen exceeds the receiver's limit.
var ErrMsgpack = errors.New("lunalink: invalid msgpack")
ErrMsgpack is returned for truncated or unsupported MessagePack input.
var ErrWriteUnknown = errors.New("lunalink: write outcome unknown")
ErrWriteUnknown marks a write whose outcome is unknown.
Functions ¶
func Decode ¶
Decode decodes one MessagePack value. Result types: nil, bool, int64, uint64 (only above MaxInt64), float64, string, []byte, []any. Maps are rejected (spec 4.1).
func DecodeError ¶
DecodeError decodes an Error payload.
func DecodeValue ¶
DecodeValue decodes ValueBytes: bool, int64, uint64 (UInt32/UInt16 give int64), float64 or string. Empty bytes decode to nil. Unknown types decode as UTF-8 text (spec 5.3).
func EncodeFrame ¶
EncodeFrame serialises a frame. When computeCRC is false the CRC field is 0x0000 (no-crc).
func EncodeGoodbye ¶
EncodeGoodbye encodes a Goodbye payload.
func EncodeHeartbeat ¶
func EncodeHeartbeat(tsMs, pendingBatches, pendingValues, pendingBytes int64, oldestAgeSec float64, connectedAtMs, reconnects int64) []byte
EncodeHeartbeat encodes a Heartbeat with keys 0 to 10 (the rest take defaults).
func EncodeTagDelta ¶
EncodeTagDelta builds a TagDelta payload from already encoded items.
func EncodeTagWriteAck ¶
EncodeTagWriteAck encodes a TagWriteAck payload.
func EncodeValue ¶
EncodeValue encodes v as ValueBytes for dataType (spec 5.3). A nil value gives empty bytes (null).
Types ¶
type Config ¶
type Config struct {
Addr string // master host:port
NodeID string // stable identity, required
NodeName string
Version string
AuthToken string
StorePath string // bbolt file, required
TLS *tls.Config // nil for plain TCP; MinVersion is forced to TLS 1.3
Window int // delivery window offered, 1 (default) to 256
MaxPayload int // max-payload offered, default 65536 (min 512)
AckTimeout time.Duration // default 5 s
ReconnectDelay time.Duration // default 5 s
CollapseAt int // resync when the backlog reaches this many batches, default 1024 (0 keeps default, <0 disables)
OnWrite WriteHandler // nil: every write fails
Logf func(format string, args ...any)
}
Config configures a Node.
type Encoder ¶
type Encoder struct{ B []byte }
Encoder is a minimal MessagePack writer. LunaLink only uses arrays, never maps (spec 4.1).
type GUID ¶
type GUID [16]byte
GUID is a tag identifier held in text order (RFC 4122 byte order). On the wire it uses .NET Guid.ToByteArray() order (spec 5.2): see Wire and GUIDFromWire.
func GUIDFromWire ¶
GUIDFromWire converts .NET order bytes to a GUID.
type Hello ¶
type Hello struct {
NodeID string
NodeName string
Version string
Capabilities []string
TimestampMs int64
AuthToken string // empty means nil
}
Hello is spec 7.1.
type HelloAck ¶
type HelloAck struct {
AssignedNodeID string
ServerTimeMs int64
HeartbeatInterval int64
Accepted bool
RejectReason string
Capabilities []string
SupportedVersions []int64
}
HelloAck is spec 7.2.
func DecodeHelloAck ¶
DecodeHelloAck decodes a HelloAck payload.
type Item ¶
type Item struct {
TagID GUID
Name string // optional
Value any // see EncodeValue
DataType int
Quality int
TimestampMs int64
}
Item is one tag value (TagDeltaItem, spec 5.5).
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is a LunaLink node. Publish values with Publish, run the connection loop with Run.
func (*Node) DropConnection ¶
func (n *Node) DropConnection()
DropConnection closes the current TCP connection without Goodbye (used to test recovery).
func (*Node) Publish ¶
Publish persists a batch in the outbox. It returns once the batch is durable. Large input is split into batches of at most 500 values.
type OutboxEntry ¶
OutboxEntry is one pending TagDelta payload.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the node's durable state in one bbolt file (pure Go): the outbox with its NodeSequence counter (spec section 8.1), the latest value of every tag (for resync baselines, section 8.4) and the write command results (section 9).
func (*Store) After ¶
func (s *Store) After(seq int64, n int) ([]OutboxEntry, error)
After returns up to n entries with sequence greater than seq, in ascending order.
func (*Store) CommandStatus ¶
CommandStatus returns the stored status of a write command.
func (*Store) DeleteUpTo ¶
DeleteUpTo removes every batch with sequence up to and including seq.
func (*Store) Enqueue ¶
Enqueue assigns the next NodeSequence, persists the batch and updates the latest values in one transaction. bbolt syncs to disk before Update returns.
func (*Store) LastSequence ¶
LastSequence returns the highest NodeSequence ever assigned.
func (*Store) SetCommandStatus ¶
SetCommandStatus persists the status of a write command.
type TagDeltaAck ¶
type TagDeltaAck struct {
NodeSequence int64
Accepted bool
ExpectedSequence int64
Duplicate bool
Error string
}
TagDeltaAck is spec 7.4.
func DecodeTagDeltaAck ¶
func DecodeTagDeltaAck(p []byte) (TagDeltaAck, error)
DecodeTagDeltaAck decodes a TagDeltaAck payload.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
lunalink-gateway
command
Command lunalink-gateway reads values from stdin and publishes them to a LunaLink master.
|
Command lunalink-gateway reads values from stdin and publishes them to a LunaLink master. |