aln

package module
v0.0.0-...-cbea110 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FRAME_CF_LENGTH = 2
	FRAME_END       = 0xC0
	FRAME_ESC       = 0xDB
	FRAME_END_T     = 0xDC
	FRAME_ESC_T     = 0xDD
)

Packet framing

View Source
const (
	CF_HAMMING1  = 0x8000
	CF_HAMMING2  = 0x4000
	CF_HAMMING3  = 0x2000
	CF_HAMMING4  = 0x1000
	CF_HAMMING5  = 0x0800
	CF_NETSTATE  = 0x0400
	CF_SERVICE   = 0x0200
	CF_SRCADDR   = 0x0100
	CF_DESTADDR  = 0x0080
	CF_NEXTADDR  = 0x0040
	CF_SEQNUM    = 0x0020
	CF_ACKBLOCK  = 0x0010
	CF_CONTEXTID = 0x0008
	CF_DATATYPE  = 0x0004
	CF_DATA      = 0x0002
	CF_CRC       = 0x0001
)

Control Flag bits (Hamming encoding consumes 5 bits, leaving 11)

View Source
const (
	CF_FIELD_SIZE           = 2   // INT16U
	NETSTATE_FIELD_SIZE     = 1   // INT08U enumerated
	SERVICE_FIELD_SIZE_MAX  = 256 // string
	SRCADDR_FIELD_SIZE_MAX  = 256 // string
	DESTADDR_FIELD_SIZE_MAX = 256 // string
	NEXTADDR_FIELD_SIZE_MAX = 256 // string
	SEQNUM_FIELD_SIZE       = 2   // INT16U
	ACKBLOCK_FIELD_SIZE     = 4   // INT32U
	CONTEXTID_FIELD_SIZE    = 2   // INT16U
	DATATYPE_FIELD_SIZE     = 1   // INT08U
	DATALENGTH_FIELD_SIZE   = 2   // INT16U
	CRC_FIELD_SIZE          = 4   // INT32U
)

Packet header field sizes

limits

View Source
const (
	NET_ROUTE   = byte(0x01) // packet contains route entry
	NET_SERVICE = byte(0x02) // packet contains service entry
	NET_QUERY   = byte(0x03) // packet is a request for content
	NET_ERROR   = byte(0xFF) // packet is an peer error message
)

LinkState value enumerations (TODO support mesh routing)

View Source
const (
	STATE_BUFFERING = 0
	STATE_ESCAPED   = 1
)

Packet parsing state enumeration

Variables

This section is empty.

Functions

func CFHamDecode

func CFHamDecode(value uint16) uint16

CFHamDecode decodes the CF using a modified Hamming (15,11).

It will fix one error, if only one error occures, not very good for burst errors.
* This is a SEC (single error correction) which means it has no BED (bit error detection) to save on size.
* The returned value will be, if fixed properly, the same 11 bits that were sent into the encoder.
* Bits 0xF800 will be zero.
* Based off of the following Hamming (15,11) matrix...
* H[16,4]  = [[1011,1000,1110,1000],  0x171D
*             [1101,1011,0010,0100],  0x24DB
*             [1110,1101,1000,0010],  0x41B7
*             [1111,0110,0100,0001]]; 0x826F

func CFHamEncode

func CFHamEncode(value uint16) uint16

CFHamEncode encodes the CF using a modified Hamming (15,11).

Returns the encoded 11 bit CF as a 15 bit codeword.
* Only the 0x07FF bits are  aloud to be on for the input all others will be ignored.
* Based off of the following Hamming (15,11) matrix...
* G[16,11] = [[1000,0000,0000,1111],  0x800F
*             [0100,0000,0000,0111],  0x4007
*             [0010,0000,0000,1011],  0x200B
*             [0001,0000,0000,1101],  0x100D
*             [0000,1000,0000,1110],  0x080E
*             [0000,0100,0000,0011],  0x0403
*             [0000,0010,0000,0101],  0x0205
*             [0000,0001,0000,0110],  0x0106
*             [0000,0000,1000,1010],  0x008A
*             [0000,0000,0100,1001],  0x0049
*             [0000,0000,0010,1100]]; 0x002C

func CRC32

func CRC32(pdata []byte) uint32

CRC32 is a 32 bit Cyclic Redundancy Check

func HeaderFieldOffset

func HeaderFieldOffset(controlFlags uint16, field uint16, frame []byte) uint16

func HeaderLength

func HeaderLength(controlFlags uint16, frame []byte) uint16

func IntXOR

func IntXOR(n uint16) uint16

IntXOR is a modified "Sparse Ones Bit Count".

              Instead of counting the ones it just determines
*               if there was an odd or even count of bits by XORing the int.
* ARGUMENTS   : This is a 32 bit number to count the ones of.
* RETURNS     : Return 0x0 or 0x1.

Types

type AddressType

type AddressType string

AddressType of all ELP packets

type Channel

type Channel interface {
	Send(*Packet) error
	Receive(PacketCallback, OnCloseCallback)
	Close()
}

Channel sends one packet at a time by some mechanism

func OpenTCPChannel

func OpenTCPChannel(hostname string, port int) (Channel, error)

OpenTCPChannel attempts to create an ELP wrapped socket connection to a remote host

type ChannelHost

type ChannelHost interface {
	OnAccept(func(Channel))
}

type LimitedChannel

type LimitedChannel struct {
	CanSend func(*Router, *Packet) (bool, error)
	// contains filtered or unexported fields
}

LimitedChannel wraps a pair of chan primatives; intended for development and testing

func NewLimitedChannel

func NewLimitedChannel(ch Channel, router *Router, canSend func(*Router, *Packet) (bool, error)) *LimitedChannel

NewLimitedChannel wraps another channel with a new LimitedChannel

func (*LimitedChannel) Close

func (lc *LimitedChannel) Close()

Close releases the Recieve go routine; no other cleanup required

func (*LimitedChannel) Receive

func (lc *LimitedChannel) Receive(onPacket PacketCallback, onClose OnCloseCallback)

Receive starts a go routine to call onPacket

func (*LimitedChannel) Send

func (lc *LimitedChannel) Send(packet *Packet) error

Send transmits immediately

type LocalChannel

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

LocalChannel wraps a pair of chan primatives; intended for development and testing

func NewLocalChannel

func NewLocalChannel() *LocalChannel

NewLocalChannel allocates the chans of a new LocalChannel

func (*LocalChannel) Close

func (lc *LocalChannel) Close()

Close releases the Recieve go routine; no other cleanup required

func (*LocalChannel) FlippedChannel

func (lc *LocalChannel) FlippedChannel() *LocalChannel

FlippedChannel returns the channel from the reverse persepective

func (*LocalChannel) Receive

func (lc *LocalChannel) Receive(onPacket PacketCallback, onClose OnCloseCallback)

Receive starts a go routine to call onPacket

func (*LocalChannel) Send

func (lc *LocalChannel) Send(packet *Packet) error

Send transmits immediately

type NodeLoad

type NodeLoad struct {
	Load     uint16
	LastSeen time.Time
}

type NodeLoadMap

type NodeLoadMap map[AddressType]NodeLoad // map[adress]NodeLoad

type OnCloseCallback

type OnCloseCallback func(Channel)

type Packet

type Packet struct {
	ControlFlags uint16      `bson:"cf" json:"cf"`
	NetState     byte        `bson:"net" json:"net,omitempty"`
	Service      string      `bson:"srv" json:"srv,omitempty"`
	SrcAddr      AddressType `bson:"src" json:"src,omitempty"`
	DestAddr     AddressType `bson:"dst" json:"dst,omitempty"`
	NextAddr     AddressType `bson:"nxt" json:"nxt,omitempty"`
	SeqNum       uint16      `bson:"seq" json:"seq,omitempty"`
	AckBlock     uint32      `bson:"ack" json:"ack,omitempty"`
	ContextID    uint16      `bson:"ctx" json:"ctx,omitempty"`
	DataType     uint8       `bson:"typ" json:"typ,omitempty"`
	DataSize     uint16      `bson:"sz" json:"sz,omitempty"`
	Data         []byte      `bson:"data" json:"data,omitempty"`
	CrcSum       uint32      `bson:"-" json:"-"`
}

func NewPacket

func NewPacket() *Packet

func ParsePacket

func ParsePacket(packetBuffer []byte) (*Packet, error)

ParsePacket makes a structured packet from an unframed buffer

func (*Packet) Copy

func (p *Packet) Copy() (*Packet, error)

func (*Packet) SetControlFlags

func (p *Packet) SetControlFlags()

func (*Packet) ToBytes

func (p *Packet) ToBytes() ([]byte, error)

ToBytes returns an unframed packet buffer

func (*Packet) ToFrameBytes

func (p *Packet) ToFrameBytes() ([]byte, error)

ToFrameBytes returns a byte array with starting delimiter and may contain escape chars

func (*Packet) ToJsonString

func (p *Packet) ToJsonString() string

func (*Packet) Write

func (p *Packet) Write(w io.Writer) (n int, err error)

Write a framed packet to the writer

type PacketCallback

type PacketCallback func(*Packet) bool

type Parser

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

func NewParser

func NewParser(cb PacketCallback) *Parser

func (*Parser) Clear

func (p *Parser) Clear()

Clear resets the state of the parser following a frame

func (*Parser) IngestStream

func (p *Parser) IngestStream(buffer []byte) bool

type RemoteNodeInfo

type RemoteNodeInfo struct {
	Address  AddressType // target addres to communicate with
	NextHop  AddressType // next routing service node for address
	Channel  Channel     // specific channel that is hosting next hop
	Cost     uint16      // cost of using this route, generally a hop count
	LastSeen time.Time   // routes should decay with a few missed updates
}

type RemoteNodeMap

type RemoteNodeMap map[AddressType]*RemoteNodeInfo

type Router

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

Router manages a set of channels and packet handlers

func NewRouter

func NewRouter(address AddressType) *Router

NewRouter instantiates an applications ELP Router

func (*Router) AddChannel

func (r *Router) AddChannel(channel Channel)

AddChannel initializes

func (*Router) Address

func (r *Router) Address() AddressType

func (*Router) ExportRouteTable

func (r *Router) ExportRouteTable() []*Packet

ExportRouteTable generates a set of packets that another router can read

func (*Router) ExportServiceTable

func (r *Router) ExportServiceTable() []*Packet

ExportServiceTable composes a list of packets encoding the service table of this node

func (*Router) NumChannels

func (r *Router) NumChannels() int

func (*Router) RegisterContextHandler

func (r *Router) RegisterContextHandler(packetHandler func(*Packet)) uint16

RegisterContextHandler returns a contextID. Services must respond with the same contextID to reach the correct response handler.

func (*Router) RegisterService

func (r *Router) RegisterService(service string, onPacket func(*Packet))

RegisterService binds a handler to a service and shares the info with neighbors

func (*Router) ReleaseContext

func (r *Router) ReleaseContext(ctxID uint16)

ReleaseContext frees memory associated with a context

func (*Router) RemoveChannel

func (r *Router) RemoveChannel(channel Channel)

func (*Router) SelectService

func (r *Router) SelectService(service string) AddressType

SelectService returns the address of the service with the lowest load, or zero

func (*Router) Send

func (r *Router) Send(p *Packet) error

Send is the core routing function of Router. A packet is either expected

locally by a registered Node, or on a multi-hop route to it's destination.

func (*Router) ServiceAddresses

func (r *Router) ServiceAddresses(service string) []AddressType

ServiceAddresses returns all the addresses that advertise a specific service

func (*Router) ShareNetState

func (r *Router) ShareNetState()

ShareNetState broadcasts the local routing table

func (*Router) UnregisterService

func (r *Router) UnregisterService(service string)

UnregisterService releases the handler for a service

type ServiceLoadMap

type ServiceLoadMap map[string]NodeLoadMap // map[service][address]NodeLoad

type SslChannelHost

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

implements ChannelHost

func NewSslChannelHost

func NewSslChannelHost(name string, port int, keyFile, certFile string) *SslChannelHost

func (*SslChannelHost) Listen

func (host *SslChannelHost) Listen(onConnect func(Channel))

Listen for incoming connections. Blocks indefinetly.

type TCPChannel

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

func NewTCPChannel

func NewTCPChannel(conn net.Conn) *TCPChannel

NewTCPChannel creates a new channel around an existing connection

func (*TCPChannel) Close

func (ch *TCPChannel) Close()

Close .

func (*TCPChannel) Receive

func (ch *TCPChannel) Receive(onPacket PacketCallback, onClose OnCloseCallback)

Receive deserializes packets from it's socket

func (*TCPChannel) Send

func (ch *TCPChannel) Send(p *Packet) error

Send serializes a packet frame through the socket

type TcpChannelHost

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

implements ChannelHost

func NewTcpChannelHost

func NewTcpChannelHost(name string, port int) *TcpChannelHost

func (*TcpChannelHost) Listen

func (host *TcpChannelHost) Listen(onConnect func(Channel))

Listen for incoming connections. Blocks indefinetly.

type WebSocketChannel

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

func NewWebSocketChannel

func NewWebSocketChannel(conn *websocket.Conn) *WebSocketChannel

NewWebSocketChannel creates a new channel around an existing connection

func (*WebSocketChannel) Close

func (ch *WebSocketChannel) Close()

Close .

func (*WebSocketChannel) Receive

func (ch *WebSocketChannel) Receive(onPacket PacketCallback, onClose OnCloseCallback)

Receive deserializes packets from it's socket

func (*WebSocketChannel) Send

func (ch *WebSocketChannel) Send(p *Packet) error

Send serializes a packet frame through the socket

Jump to

Keyboard shortcuts

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