Documentation ¶
Index ¶
- Constants
- func CFHamDecode(value uint16) uint16
- func CFHamEncode(value uint16) uint16
- func CRC32(pdata []byte) uint32
- func HeaderFieldOffset(controlFlags uint16, field uint16, frame []byte) uint16
- func HeaderLength(controlFlags uint16, frame []byte) uint16
- func IntXOR(n uint16) uint16
- type AddressType
- type Channel
- type ChannelHost
- type LimitedChannel
- type LocalChannel
- type NodeLoad
- type NodeLoadMap
- type OnCloseCallback
- type Packet
- type PacketCallback
- type Parser
- type RemoteNodeInfo
- type RemoteNodeMap
- type Router
- func (r *Router) AddChannel(channel Channel)
- func (r *Router) Address() AddressType
- func (r *Router) ExportRouteTable() []*Packet
- func (r *Router) ExportServiceTable() []*Packet
- func (r *Router) NumChannels() int
- func (r *Router) RegisterContextHandler(packetHandler func(*Packet)) uint16
- func (r *Router) RegisterService(service string, onPacket func(*Packet))
- func (r *Router) ReleaseContext(ctxID uint16)
- func (r *Router) RemoveChannel(channel Channel)
- func (r *Router) SelectService(service string) AddressType
- func (r *Router) Send(p *Packet) error
- func (r *Router) ServiceAddresses(service string) []AddressType
- func (r *Router) ShareNetState()
- func (r *Router) UnregisterService(service string)
- type ServiceLoadMap
- type SslChannelHost
- type TCPChannel
- type TcpChannelHost
- type WebSocketChannel
Constants ¶
const ( FRAME_CF_LENGTH = 2 FRAME_END = 0xC0 FRAME_ESC = 0xDB FRAME_END_T = 0xDC FRAME_ESC_T = 0xDD )
Packet framing
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)
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
const ( MAX_HEADER_SIZE = CF_FIELD_SIZE + NETSTATE_FIELD_SIZE + SERVICE_FIELD_SIZE_MAX + SRCADDR_FIELD_SIZE_MAX + DESTADDR_FIELD_SIZE_MAX + NEXTADDR_FIELD_SIZE_MAX + SEQNUM_FIELD_SIZE + ACKBLOCK_FIELD_SIZE + CONTEXTID_FIELD_SIZE + DATATYPE_FIELD_SIZE + DATALENGTH_FIELD_SIZE MAX_DATA_SIZE = 4096 MAX_PACKET_SIZE = MAX_HEADER_SIZE + MAX_DATA_SIZE + CRC_FIELD_SIZE )
limits
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)
const ( STATE_BUFFERING = 0 STATE_ESCAPED = 1 )
Packet parsing state enumeration
Variables ¶
This section is empty.
Functions ¶
func CFHamDecode ¶
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 ¶
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 HeaderFieldOffset ¶
func HeaderLength ¶
Types ¶
type Channel ¶
type Channel interface { Send(*Packet) error Receive(PacketCallback, OnCloseCallback) Close() }
Channel sends one packet at a time by some mechanism
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 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 ParsePacket ¶
ParsePacket makes a structured packet from an unframed buffer
func (*Packet) SetControlFlags ¶
func (p *Packet) SetControlFlags()
func (*Packet) ToFrameBytes ¶
ToFrameBytes returns a byte array with starting delimiter and may contain escape chars
func (*Packet) ToJsonString ¶
type PacketCallback ¶
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 ¶
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) Address ¶
func (r *Router) Address() AddressType
func (*Router) ExportRouteTable ¶
ExportRouteTable generates a set of packets that another router can read
func (*Router) ExportServiceTable ¶
ExportServiceTable composes a list of packets encoding the service table of this node
func (*Router) NumChannels ¶
func (*Router) RegisterContextHandler ¶
RegisterContextHandler returns a contextID. Services must respond with the same contextID to reach the correct response handler.
func (*Router) RegisterService ¶
RegisterService binds a handler to a service and shares the info with neighbors
func (*Router) ReleaseContext ¶
ReleaseContext frees memory associated with a context
func (*Router) RemoveChannel ¶
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 ¶
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 ¶
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) 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) 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