model

package
v0.0.0-...-e848abc Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2015 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

The model package provides the interface for interacting with a stream of messages internally within Forest Bus. The messages are retained as a slice of bytes to enable cheap marshalling as part of the rpc calls and easy writing to underlying files.

Index

Constants

View Source
const MESSAGE_CRC_OFFSET = MESSAGE_TERM_OFFSET + 8

MESSAGE_CRC_OFFSET is the location within the header of the 4 bytes used to record the CRC of the message payload.

View Source
const MESSAGE_LENGTH_OFFSET = MESSAGE_VERSION_OFFEST + 1

MESSAGE_LENGTH_OFFSET is the location within the header of the 4 bytes used to record the length of the message.

View Source
const MESSAGE_OVERHEAD = 1 + 4 + 8 + 4

MESSAGE_OVERHEAD is the total number of bytes used by the header of a message in version 0 of the message format.

View Source
const MESSAGE_TERM_OFFSET = MESSAGE_LENGTH_OFFSET + 4

MESSAGE_TERM_OFFSET is the location within the header of the 8 bytes used to record the election term of the message.

View Source
const MESSAGE_VERSION_OFFEST = 0

MESSAGE_VERSION_OFFSET is the location within the header of the byte used to indicate the version of the message format.

Variables

View Source
var EMPTY_MESSAGES = Messages{}

EMPTY_MESSAGES represts an empty set of messages.

View Source
var ERR_CRC_MISMATCH = errors.New("CRC check on message payload failed.")

ERR_CRC_MISMATCH is thrown if the message payload's CRC doesn't match that recorded in the header.

View Source
var ERR_UNSUPPORTED_MESSAGE_FORMAT = errors.New("Unsupported message format")

ERR_UNSUPPORTED_MESSAGE_FORMAT is thrown if a message format is encountered that this code is not familiar with.

Functions

func GetMessageContent

func GetMessageContent(r io.Reader) ([]byte, error)

GetMessageContent parses the bytes of a Message and returns just the Payload

func ParseHeader

func ParseHeader(headerBytes []byte) (version byte, length int32, term int64, crc uint32)

ParseHeader takes the given slice of bytes and returns the individual fields that form the header.

func ReadHeaderGetLength

func ReadHeaderGetLength(r io.Reader) (int, error)

ReadHeaderGetLength returns the length of the message from the header starting at the current position of the io.Reader. The full MESSAGE_OVERHEAD bytes will be read from the io.Reader.

func ReadSingleMessage

func ReadSingleMessage(r io.Reader) (length int32, term int64, crc uint32, payload []byte, err error)

ReadSingleMessage is a utility function that reads one message from the given io.Reader.

Types

type MessageProvider

type MessageProvider interface {
	Messages() [][]byte
}

type MessageProviders

type MessageProviders []MessageProvider

type Messages

type Messages struct {
	Count int
	Data  []byte
	// contains filtered or unexported fields
}

Messages consists of the count of messages and a slice of bytes.

func MessagesFromClientData

func MessagesFromClientData(data [][]byte) Messages

MessagesFromClientData takes the provided slice of payload data and creates the Forest Bus Messages representation of this data.

func MessagesFromMessageProvider

func MessagesFromMessageProvider(dataSources MessageProviders) Messages

MessagesFromMessageProvider takes a slices of MessageProviders and returns the Forest Bus Messages representation of this data.

func MessagesFromReader

func MessagesFromReader(r io.Reader, targetBytes int) (result Messages, readByteCount int, err error)

MessagesFromReader reads messages from the io.Reader and returns the resulting Message objects, along with the number of bytes actually read and any error.

The targetBytes parameter is the initial number of bytes that the function should attempt to retrieve from the Reader while looking for messages. If the given number of bytes doesn't include a full message, additional bytes will be read in an attempt to retrieve at least one full message.

Bytes that have been read that are for part of message will be discarded, but still reflected in the returned readByteCount which always contains the number of bytes actually read from the stream. The Disklog.Segment logic uses this to keep track of where in a file the current file pointer is up to.

func (*Messages) GetCount

func (msgs *Messages) GetCount() int

GetCount returns the number of whole messages represented by the data contained in this set of Messages.

func (*Messages) GetMessageTerm

func (msgs *Messages) GetMessageTerm(index int) (int64, error)

GetMessageTerm returns the term of the message at the given local index (starting at zero) within this set of Messages.

func (*Messages) Join

func (msgs *Messages) Join(extraMessages Messages) (Messages, error)

Join creates a new instance of Messages that contains both the original Messages content and the given extraMessages as well. This operation requires copying of both the original and the extraMessages underlying byte data.

func (*Messages) Offsets

func (msgs *Messages) Offsets() ([]int, error)

Offsets returns the offset data for these messages.

func (*Messages) Payloads

func (msgs *Messages) Payloads() ([][]byte, error)

Payloads returns a slice of byte slices that only contains the payloads of the messages.

The byte data itself is shared between the Messages instance and the resulting Payloads slices.

func (*Messages) RawData

func (msgs *Messages) RawData() []byte

RawData provides the underlying raw data of the Messages.

func (*Messages) SetMessageTerm

func (msgs *Messages) SetMessageTerm(newTerm int64) error

SetMessageTerm sets the term of all messages in this set of Messages to the given value.

func (*Messages) Slice

func (msgs *Messages) Slice(fromMessageIndex int, toMessageIndex int) (Messages, error)

Slice creates a new Messages object containing a subset of the messages in this Messages. The underlying byte slice is shared between the two instances.

func (*Messages) Write

func (msgs *Messages) Write(w io.Writer) (int, error)

Write outputs all valid messages to the given io.Writer. The number of bytes written is returned along with any errors.

Jump to

Keyboard shortcuts

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