alfred

package
v0.0.0-...-0eeec4f Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2015 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package A.L.F.R.E.D. contains functionality resembling that of the C variant, to be found here: http://git.open-mesh.org/alfred.git/ This is a pure Go reimplementation.

Index

Constants

View Source
const (
	ALFRED_IFNAMSIZ          = 16
	ALFRED_VERSION           = 0
	ALFRED_PORT              = 0x4242
	ALFRED_MAX_RESERVED_TYPE = 64
	ALFRED_MAX_PAYLOAD       = 65535 - 20
)
View Source
const (
	ALFRED_PUSH_DATA        = 0
	ALFRED_ANNOUNCE_MASTER  = 1
	ALFRED_REQUEST          = 2
	ALFRED_STATUS_TXEND     = 3
	ALFRED_STATUS_ERROR     = 4
	ALFRED_MODESWITCH       = 5
	ALFRED_CHANGE_INTERFACE = 6
)

packet types

View Source
const (
	ALFRED_MODESWITCH_SLAVE  = 0
	ALFRED_MODESWITCH_MASTER = 1
)

operation modes

View Source
const (
	// slave mode: regularly forward local data
	SERVER_MODE_SLAVE = iota
	// master mode: receive and cache data from slaves, local data, and sync it all to all other known masters
	// announce presence via broadcasts
	SERVER_MODE_MASTER
	// special mode not present in reference implementation:
	// announce presence via unicast to other known masters
	// receive sync data from them, but only propagate local
	// data to one of them (like a slave server)
	SERVER_MODE_STEALTH_MASTER
)
View Source
const PACKETTYPE_ALL = 0

for requests, this means to return data of all packettypes

Variables

View Source
var ErrParseMAC = errors.New("error parsing MAC address")
View Source
var ErrProtocol = errors.New("Bad data received from A.L.F.R.E.D. server")
View Source
var ErrRead = errors.New("cannot read data")
View Source
var ErrStatus = errors.New("A.L.F.R.E.D. server reported an error")
View Source
var ErrTooLarge = errors.New("data chunk is too large to fit into packet")
View Source
var ErrUnknownType = errors.New("unknown data type")
View Source
var NullHardwareAddr = HardwareAddr([]byte{0, 0, 0, 0, 0, 0})

Functions

This section is empty.

Types

type AnnounceMasterV0

type AnnounceMasterV0 struct {
	Header *TLV
}

Header-only packet sent by an A.L.F.R.E.D. server to announce that it is an available master server and ready to receive data from slave servers or to be queried for collected data

func NewAnnounceMasterV0

func NewAnnounceMasterV0() *AnnounceMasterV0

func (*AnnounceMasterV0) Size

func (a *AnnounceMasterV0) Size() int

func (*AnnounceMasterV0) Write

func (a *AnnounceMasterV0) Write(w io.Writer) error

type ChangeInterfaceV0

type ChangeInterfaceV0 struct {
	Header *TLV
	Ifaces *[ALFRED_IFNAMSIZ * 16]byte
}

A client sends this to a server to request the server to (re-)bind its interfaces to send/receive on.

func NewChangeInterfaceV0

func NewChangeInterfaceV0(interfaces []byte) *ChangeInterfaceV0

func ReadChangeInterfaceV0

func ReadChangeInterfaceV0(r io.Reader, header *TLV) (*ChangeInterfaceV0, error, int)

Read a ChangeInterfaceV0 packet Returns the packet, an error if anything went wrong, and the number of bytes read from the io.Reader

func (*ChangeInterfaceV0) Size

func (c *ChangeInterfaceV0) Size() int

func (*ChangeInterfaceV0) Write

func (c *ChangeInterfaceV0) Write(w io.Writer) error

type Client

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

An A.L.F.R.E.D. client.

It will create a new connection for each request - since the C implementation will close the connection when it has handled a request.

func NewClient

func NewClient(network string, address string, timeout *time.Duration) *Client

Return a new client instance

func (*Client) ChangeInterface

func (c *Client) ChangeInterface(interfaces []byte) error

change interface(s) the server listens on for UDP connections

func (*Client) Connect

func (c *Client) Connect(handler func(net.Conn, *bufio.Writer) error) error

wrapper for the network connection

func (*Client) ModeSwitch

func (c *Client) ModeSwitch(mode uint8) error

switch server mode

func (*Client) PushData

func (c *Client) PushData(packettype uint8, data []byte) error

push data of a given type

func (*Client) Request

func (c *Client) Request(packettype uint8, handler func(Data) error) error

Request data of a given type

func (*Client) RequestContent

func (c *Client) RequestContent(contentitem Content, handler func() error) error

Request data and put it into structured data conforming to the Content interface

func (*Client) Updater

func (c *Client) Updater(
	contentitem Content,
	updatewait time.Duration, retrywait time.Duration,
	notifyQuit *topic.Topic,
	notifySuccess *topic.Topic,
	handler func() error)

Create a new update client. the time to wait between updates in updatewait and the time to wait after failure before retrying in retrywait. The updatewait duration is also the timeout duration for the actual network connections.

type Content

type Content interface {
	GetPacketType() uint8
	ReadAlfred(Data) error
}

interface for data that is being transported via A.L.F.R.E.D.

type Data

type Data struct {
	// origin of the data
	Source HardwareAddr
	// descriptor for the data
	Header *TLV
	// the actual data
	Data []byte
}

A wrapper for data received from or transmitted to another instance of A.L.F.R.E.D.

func ReadData

func ReadData(r io.Reader) (*Data, error, int)

Read a Data packet Returns the packet, an error if anything went wrong, and the number of bytes read from the io.Reader

func (*Data) Equals

func (d *Data) Equals(other *Data) bool

check for equality

func (*Data) Size

func (d *Data) Size() int

func (*Data) Write

func (d *Data) Write(w io.Writer) error

type HardwareAddr

type HardwareAddr net.HardwareAddr

Wrapper for the MAC addresses found as main identifier.

func (*HardwareAddr) IsUnset

func (i *HardwareAddr) IsUnset() bool

check if it is unset - i.e. nil pointer or 00:00:00:00:00:00

func (HardwareAddr) MarshalJSON

func (i HardwareAddr) MarshalJSON() ([]byte, error)

JSON encoder for MAC addresses

func (*HardwareAddr) Parse

func (i *HardwareAddr) Parse(addr string) error

parse address from a string

func (HardwareAddr) String

func (i HardwareAddr) String() string

wrap printing from net.HardwareAddr

func (*HardwareAddr) UnmarshalJSON

func (i *HardwareAddr) UnmarshalJSON(data []byte) error

very forgiving parser for MAC addresses

type ModeSwitchV0

type ModeSwitchV0 struct {
	Header *TLV
	Mode   uint8
}

A client sends this to a server to request a switch of the operation mode

func NewModeSwitchV0

func NewModeSwitchV0(mode uint8) *ModeSwitchV0

func ReadModeSwitchV0

func ReadModeSwitchV0(r io.Reader, header *TLV) (*ModeSwitchV0, error, int)

Read a ModeSwitchV0 packet Returns the packet, an error if anything went wrong, and the number of bytes read from the io.Reader

func (*ModeSwitchV0) Size

func (m *ModeSwitchV0) Size() int

func (*ModeSwitchV0) Write

func (m *ModeSwitchV0) Write(w io.Writer) error

type Packet

type Packet interface {
	// write the packet to the io.Writer, return an error if anything
	// went wrong.
	Write(io.Writer) error
	// return the size of the packet when put onto wire.
	Size() int
}

func Read

func Read(r io.Reader) (Packet, error, int)

Read a packet and all its contained data from an io.Reader. Returns the packet, an error if anything went wrong, and the number of bytes read from the io.Reader

type PushDataV0

type PushDataV0 struct {
	Header *TLV
	Tx     *TransactionMgmt
	Data   []Data
}

Wrapper for a data "push" transaction, to be sent from the "pushing" instance of A.L.F.R.E.D. to a receiving instance. It refers to a transaction and contains the actually pushed data.

func NewPushDataV0

func NewPushDataV0(tx *TransactionMgmt, data []Data) *PushDataV0

func ReadPushDataV0

func ReadPushDataV0(r io.Reader, header *TLV) (*PushDataV0, error, int)

Read a TransactionMgmt packet Returns the packet, an error if anything went wrong, and the number of bytes read from the io.Reader

func (*PushDataV0) Size

func (p *PushDataV0) Size() int

func (*PushDataV0) SizeData

func (p *PushDataV0) SizeData() int

As a PushDataV0 packet can contain many Data packets in its data part, this convenience method can be used to query their combined size.

func (*PushDataV0) Write

func (p *PushDataV0) Write(w io.Writer) error

type ReqGetAll

type ReqGetAll struct {
	// if PACKETTYPE_ALL, return data of all types, otherwise
	// specify type to return
	TypeFilter uint8
	// if set to true, return only data that has its origin
	// locally, i.e. was not propagated by another master server
	LocalOnly bool
	// data will be sent back via this channel, it will get
	// closed as soon as all data has been sent
	Return chan<- Data
}

send this to fetch data from the store

type ReqPut

type ReqPut struct {
	// flag that tells if the data origin is the local server
	// instance (i.e. not propagated by another master server)
	IsLocal bool
	// actual data item
	Data
}

send this to the store to put data into it (or update data)

type RequestV0

type RequestV0 struct {
	Header        *TLV
	RequestedType uint8
	TxId          uint16
}

Query send either from client to server or from server to server for requesting (all) data of a given type, initializing a transaction.

func NewRequestV0

func NewRequestV0(requestedtype uint8, txid uint16) *RequestV0

func ReadRequestV0

func ReadRequestV0(r io.Reader, header *TLV) (*RequestV0, error, int)

Read a RequestV0 packet Returns the packet, an error if anything went wrong, and the number of bytes read from the io.Reader

func (*RequestV0) Size

func (r *RequestV0) Size() int

func (*RequestV0) Write

func (r *RequestV0) Write(w io.Writer) error

type Server

type Server struct {
	// this will typically be auto-calculated
	MaxPayload int
	// interval between sending master announcements in master modes
	AnnouncementInterval time.Duration
	// keep information about known masters for this duration after having last
	// heard of them
	MasterMaxAge time.Duration
	// interval between checks
	MasterPurgeInterval time.Duration
	// wait for outstanding packets for this time after
	// receiving final packet (which might arrive out of band)
	TransactionWaitComplete time.Duration
	// after this time, a started transaction is removed
	// from the list, regardless in what state it is
	TransactionMaxAge time.Duration
	// interval between checks
	TransactionPurgeInterval time.Duration
	// wait this duration for a reply of a master server
	// after forwarding a (client) request to it
	WaitForMasterReply time.Duration
	// interval between data synchronization pushes to other
	// master servers
	SyncInterval time.Duration
	// should we drop transactions that are not complete?
	// defaults to true, and should better be left that way
	// except your data/network policy mandates differently
	DropIncompleteTransactions bool
	// operation mode
	Mode int

	sync.Mutex
	// contains filtered or unexported fields
}

state information for an A.L.F.R.E.D. server

func NewServer

func NewServer(mode int) *Server

run a new server instance Select an operation mode in "mode"

func (*Server) NewListenerStream

func (s *Server) NewListenerStream(network string, address string) error

start a task for listening on a TCP or Unix socket

func (*Server) NewListenerUDP

func (s *Server) NewListenerUDP(address string, ifname string) error

spawn a task that listens of incoming UDP packets

func (*Server) Shutdown

func (s *Server) Shutdown()

shutdown a server

type StatusV0

type StatusV0 struct {
	Header *TLV
	Tx     *TransactionMgmt
}

This is used as the packet format for various status reports which differ in their numerical packet type.

func NewStatusV0

func NewStatusV0(statustype uint8, tx *TransactionMgmt) *StatusV0

func ReadStatusV0

func ReadStatusV0(r io.Reader, header *TLV) (*StatusV0, error, int)

Read a StatusV0 packet Returns the packet, an error if anything went wrong, and the number of bytes read from the io.Reader

func (*StatusV0) Size

func (s *StatusV0) Size() int

func (*StatusV0) Write

func (s *StatusV0) Write(w io.Writer) error

type Store

type Store struct {
	NotifyUpdates *topic.Topic
	// contains filtered or unexported fields
}

the state of the data storage used by an A.L.F.R.E.D. server

func NewStore

func NewStore(purgeAfter time.Duration, purgeInterval time.Duration) *Store

return a new store instance

func (*Store) Request

func (s *Store) Request(req interface{})

send a request to the store

func (*Store) Shutdown

func (s *Store) Shutdown()

shutdown data store and spawned tasks

type TLV

type TLV struct {
	// type of the data following (or the information expressed by
	// just the TLV itself)
	Type uint8
	// version of the typed data
	Version uint8
	// length of the data following
	Length uint16
}

A type-length-version data element. It is the main descriptor used in the data stream provided or received by an instance of A.L.F.R.E.D.

func ReadTLV

func ReadTLV(r io.Reader) (*TLV, error, int)

Read a TLV packet. Returns the packet, an error if anything went wrong, and the number of bytes read from the io.Reader

func (*TLV) Size

func (t *TLV) Size() int

func (*TLV) Write

func (t *TLV) Write(w io.Writer) error

type TransactionMgmt

type TransactionMgmt struct {
	// Transaction ID
	Id uint16
	// Sequence number
	SeqNo uint16
}

Information referring to a transaction between two instances of A.L.F.R.E.D.

func ReadTransactionMgmt

func ReadTransactionMgmt(r io.Reader) (*TransactionMgmt, error, int)

Read a TransactionMgmt packet Returns the packet, an error if anything went wrong, and the number of bytes read from the io.Reader

func (*TransactionMgmt) Size

func (t *TransactionMgmt) Size() int

func (*TransactionMgmt) Write

func (t *TransactionMgmt) Write(w io.Writer) error

Jump to

Keyboard shortcuts

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