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
- Variables
- func GetMessageContent(r io.Reader) ([]byte, error)
- func ParseHeader(headerBytes []byte) (version byte, length int32, term int64, crc uint32)
- func ReadHeaderGetLength(r io.Reader) (int, error)
- func ReadSingleMessage(r io.Reader) (length int32, term int64, crc uint32, payload []byte, err error)
- type MessageProvider
- type MessageProviders
- type Messages
- func (msgs *Messages) GetCount() int
- func (msgs *Messages) GetMessageTerm(index int) (int64, error)
- func (msgs *Messages) Join(extraMessages Messages) (Messages, error)
- func (msgs *Messages) Offsets() ([]int, error)
- func (msgs *Messages) Payloads() ([][]byte, error)
- func (msgs *Messages) RawData() []byte
- func (msgs *Messages) SetMessageTerm(newTerm int64) error
- func (msgs *Messages) Slice(fromMessageIndex int, toMessageIndex int) (Messages, error)
- func (msgs *Messages) Write(w io.Writer) (int, error)
Constants ¶
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.
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.
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.
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.
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 ¶
var EMPTY_MESSAGES = Messages{}
EMPTY_MESSAGES represts an empty set of messages.
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.
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 ¶
GetMessageContent parses the bytes of a Message and returns just the Payload
func ParseHeader ¶
ParseHeader takes the given slice of bytes and returns the individual fields that form the header.
func ReadHeaderGetLength ¶
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.
Types ¶
type MessageProvider ¶
type MessageProvider interface {
Messages() [][]byte
}
type MessageProviders ¶
type MessageProviders []MessageProvider
type Messages ¶
Messages consists of the count of messages and a slice of bytes.
func MessagesFromClientData ¶
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 ¶
GetCount returns the number of whole messages represented by the data contained in this set of Messages.
func (*Messages) GetMessageTerm ¶
GetMessageTerm returns the term of the message at the given local index (starting at zero) within this set of Messages.
func (*Messages) Join ¶
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) Payloads ¶
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) SetMessageTerm ¶
SetMessageTerm sets the term of all messages in this set of Messages to the given value.