shared

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2015 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BufferedReaderFlagDelimiter enables reading for a delimiter. This flag is
	// ignored if an MLE flag is set.
	BufferedReaderFlagDelimiter = BufferedReaderFlags(0)

	// BufferedReaderFlagMLE enables reading if length encoded messages.
	// Runlength is read as ASCII (to uint64) until the first byte (ASCII char)
	// of the delimiter string.
	// Only one MLE flag is supported at a time.
	BufferedReaderFlagMLE = BufferedReaderFlags(1)

	// BufferedReaderFlagMLE8 enables reading if length encoded messages.
	// Runlength is read as binary (to uint8).
	// Only one MLE flag is supported at a time.
	BufferedReaderFlagMLE8 = BufferedReaderFlags(2)

	// BufferedReaderFlagMLE16 enables reading if length encoded messages.
	// Runlength is read as binary (to uint16).
	// Only one MLE flag is supported at a time.
	BufferedReaderFlagMLE16 = BufferedReaderFlags(3)

	// BufferedReaderFlagMLE32 enables reading if length encoded messages.
	// Runlength is read as binary (to uint32).
	// Only one MLE flag is supported at a time.
	BufferedReaderFlagMLE32 = BufferedReaderFlags(4)

	// BufferedReaderFlagMLE64 enables reading if length encoded messages.
	// Runlength is read as binary (to uint64).
	// Only one MLE flag is supported at a time.
	BufferedReaderFlagMLE64 = BufferedReaderFlags(5)

	// BufferedReaderFlagMLEFixed enables reading messages with a fixed length.
	// Only one MLE flag is supported at a time.
	BufferedReaderFlagMLEFixed = BufferedReaderFlags(6)

	// BufferedReaderFlagMaskMLE is a bitmask to mask out everything but MLE flags
	BufferedReaderFlagMaskMLE = BufferedReaderFlags(7)

	// BufferedReaderFlagBigEndian sets binary reading to big endian encoding.
	BufferedReaderFlagBigEndian = BufferedReaderFlags(8)

	// BufferedReaderFlagEverything will keep MLE and/or delimiters when
	// building a message.
	BufferedReaderFlagEverything = BufferedReaderFlags(16)
)
View Source
const (
	// ParserFlagContinue continues parsing at the position of the match.
	// By default the matched token will be skipped. This flag prevents the
	// default behavior. In addition to that the parser will add the parsed
	// token to the value of the next match.
	ParserFlagContinue = ParserFlag(1 << iota)

	// ParserFlagAppend causes the parser to keep the current value for the next
	// match. By default a value will be restarted after each match. This flag
	// prevents the default behavior.
	ParserFlagAppend = ParserFlag(1 << iota)

	// ParserFlagInclude includes the matched token in the read value.
	// By default the value does not contain the matched token.
	ParserFlagInclude = ParserFlag(1 << iota)

	// ParserFlagPush pushes the current state on the stack before making the
	// transition.
	ParserFlagPush = ParserFlag(1 << iota)

	// ParserFlagPop pops the stack and uses the returned state as the next
	// state. If no state is on the stack the regular next state is used.
	ParserFlagPop = ParserFlag(1 << iota)

	// ParserStateStop defines a state that causes the parsing to stop when
	// transitioned into.
	ParserStateStop = ParserStateID(0xFFFFFFFF)
)
View Source
const (
	// MetricProcessStart is the metric name storing the time when this process
	// has been started.
	MetricProcessStart = "ProcessStart"
)

Variables

View Source
var BufferDataInvalid = bufferError("Invalid data")

BufferDataInvalid is returned when a parsing encounters an error

View Source
var LowResolutionTimeNow = time.Now()

LowResolutionTimeNow is a cached time.Now() that gets updated every 10 to 100ms. This timer can be used in performance ciritcal situations where a nanosecond based timer is not required.

View Source
var Metric = metrics{new(sync.Mutex), make(map[string]*int64)}

Metric allows any part of gollum to store and/or modify metric values by name.

View Source
var ProcessStartTime time.Time

ProcessStartTime stores the time this process has started. This value is also stored in the metric MetricProcessStart

View Source
var RuntimeType = TypeRegistry{make(map[string]reflect.Type)}

Plugin is the global typeRegistry singleton. Use this singleton to register plugins.

Functions

func Btoi

func Btoi(buffer []byte) (uint64, int)

Btoi is a fast byte buffer to unsigned int parser that reads until the first non-number character is found. It returns the number value as well as the length of the number string encountered. If a number could not be parsed the returned length will be 0

func GetMissingMethods

func GetMissingMethods(objType reflect.Type, ifaceType reflect.Type) (float32, []interface{})

GetMissingMethods checks if a given object implements all methods of a given interface. It returns the interface coverage [0..1] as well as an array of error messages. If the interface is correctly implemented the coverage is 1 and the error message array is empty.

func ItoLen

func ItoLen(number uint64) int

ItoLen returns the length of an unsingned integer when converted to a string

func Itob

func Itob(number uint64, buffer []byte) error

Itob writes an unsigned integer to the start of a given byte buffer.

func Itobe

func Itobe(number uint64, buffer []byte) error

Itobe writes an unsigned integer to the end of a given byte buffer.

func ParseAddress

func ParseAddress(addr string) (address string, protocol string)

ParseAddress takes an address and tries to extract the protocol from it. Protocols may be prepended by the "protocol://" notation. If no protocol is given, "tcp" is assumed. The first parameter returned is the address, the second denotes the protocol. The protocol is allways returned as lowercase string.

func RecoverShutdown

func RecoverShutdown()

RecoverShutdown will trigger a shutdown via interrupt if a panic was issued. Typically used as "defer RecoverShutdown()".

func Unescape

func Unescape(text string) string

Unescape replaces occurences of \\n, \\r and \\t with real escape codes.

Types

type BufferReadCallback

type BufferReadCallback func(msg []byte, sequence uint64)

BufferReadCallback defines the function signature for callbacks passed to ReadAll.

type BufferedReader

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

BufferedReader is a helper struct to read from any io.Reader into a byte slice. The data can arrive "in pieces" and will be assembled. A data "piece" is considered complete if a delimiter or a certain runlength has been reached. The latter has to be enabled by flag and will disable the default behavior, which is looking for a delimiter string. In addition to that every data "piece" will recieve an incrementing sequence number.

func NewBufferedReader

func NewBufferedReader(bufferSize int, flags BufferedReaderFlags, offsetOrLength int, delimiter string) *BufferedReader

NewBufferedReader creates a new buffered reader that reads messages from a continuous stream of bytes. Messages can be separated from the stream by using common methods such as fixed size, encoded message length or delimiter string. The internal buffer is grown statically (by its original size) if necessary. bufferSize defines the initial size / grow size of the buffer flags configures the parsing method offsetOrLength sets either the runlength offset or fixed message size delimiter defines the delimiter used for textual message parsing

func (*BufferedReader) ReadAll

func (buffer *BufferedReader) ReadAll(reader io.Reader, callback BufferReadCallback) error

ReadAll calls ReadOne as long as there are messages in the stream. Messages will be send to the given write callback. If callback is nil, data will be read and discarded.

func (*BufferedReader) ReadOne

func (buffer *BufferedReader) ReadOne(reader io.Reader) (data []byte, seq uint64, more bool, err error)

ReadOne reads the next message from the given stream (if possible) and generates a sequence number for this message. The more return parameter is set to true if there are still messages or parts of messages in the stream. Data and seq is only set if a complete message could be parsed. Errors are returned if reading from the stream failed or the parser encountered an error.

func (*BufferedReader) Reset

func (buffer *BufferedReader) Reset(sequence uint64)

Reset clears the buffer by resetting its internal state

type BufferedReaderFlags

type BufferedReaderFlags byte

BufferedReaderFlags is an enum to configure a buffered reader

type ByteStream

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

ByteStream is a more lightweight variant of bytes.Buffer. The managed byte array is increased to the exact required size and never shrinks. Writing moves an internal offset (for appends) but reading always starts at offset 0.

func NewByteStream

func NewByteStream(capacity int) ByteStream

NewByteStream creates a new byte stream of the desired capacity

func NewByteStreamFrom

func NewByteStreamFrom(data []byte) ByteStream

NewByteStreamFrom creates a new byte stream that starts with the given byte array.

func (ByteStream) Bytes

func (stream ByteStream) Bytes() []byte

Bytes returns a slice of the underlying byte array containing all written data up to this point.

func (ByteStream) Cap

func (stream ByteStream) Cap() int

Cap returns the capacity of the underlying array. This is equal to cap(stream.Bytes()).

func (ByteStream) Len

func (stream ByteStream) Len() int

Len returns the length of the underlying array. This is equal to len(stream.Bytes()).

func (*ByteStream) Read

func (stream *ByteStream) Read(target []byte) (int, error)

Read implements the io.Reader interface. The underlying byte array is always copied as a whole.

func (*ByteStream) Reset

func (stream *ByteStream) Reset()

Reset sets the internal write offset to 0

func (*ByteStream) SetCapacity

func (stream *ByteStream) SetCapacity(capacity int)

SetCapacity assures that capacity bytes are available in the buffer, growing the managed byte array if needed. The buffer will grow by at least 64 bytes if growing is required.

func (ByteStream) String

func (stream ByteStream) String() string

String returns a string of the underlying byte array containing all written data up to this point.

func (*ByteStream) Write

func (stream *ByteStream) Write(source []byte) (int, error)

Write implements the io.Writer interface. This function assures that the capacity of the underlying byte array is enough to store the incoming amount of data. Subsequent writes will allways append to the end of the stream until Reset() is called.

func (*ByteStream) WriteByte

func (stream *ByteStream) WriteByte(source byte) error

WriteByte writes a single byte to the stream. Capacity will be ensured.

func (*ByteStream) WriteString

func (stream *ByteStream) WriteString(source string) (int, error)

WriteString is a convenience wrapper for Write([]byte(source))

type Expect

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

Expect is a helper construct for unittesting

func NewExpect

func NewExpect(t *testing.T) Expect

NewExpect creates an Expect helper struct with scope set to the name of the calling function.

func (Expect) Equal

func (e Expect) Equal(val1, val2 interface{}) bool

Equal does a deep equality check on both values and returns true if that test yielded true (val1 == val2)

func (Expect) False

func (e Expect) False(val bool) bool

False tests if the given value is false

func (Expect) Geq

func (e Expect) Geq(val1, val2 interface{}) bool

Geq does a numeric greater or equal check on both values and returns true if that test yielded true (val1 >= val2)

func (Expect) Greater

func (e Expect) Greater(val1, val2 interface{}) bool

Greater does a numeric greater than check on both values and returns true if that test yielded true (val1 > val2)

func (Expect) Leq

func (e Expect) Leq(val1, val2 interface{}) bool

Leq does a numeric less or euqal check on both values and returns true if that test yielded true (val1 <= val2)

func (Expect) Less

func (e Expect) Less(val1, val2 interface{}) bool

Less does a numeric less than check on both values and returns true if that test yielded true (val1 < val2)

func (Expect) MapEqual

func (e Expect) MapEqual(data interface{}, key interface{}, value interface{}) bool

MapEqual returns true if MapSet equals true for the given key and Equal returns true for the returned and given value.

func (Expect) MapGeq

func (e Expect) MapGeq(data interface{}, key interface{}, value interface{}) bool

MapGeq returns true if MapSet equals true for the given key and Geq returns true for the returned and given value.

func (Expect) MapGreater

func (e Expect) MapGreater(data interface{}, key interface{}, value interface{}) bool

MapGreater returns true if MapSet equals true for the given key and Greater returns true for the returned and given value.

func (Expect) MapLeq

func (e Expect) MapLeq(data interface{}, key interface{}, value interface{}) bool

MapLeq returns true if MapSet equals true for the given key and Leq returns true for the returned and given value.

func (Expect) MapLess

func (e Expect) MapLess(data interface{}, key interface{}, value interface{}) bool

MapLess returns true if MapSet equals true for the given key and Less returns true for the returned and given value.

func (Expect) MapNeq

func (e Expect) MapNeq(data interface{}, key interface{}, value interface{}) bool

MapNeq returns true if MapSet equals true for the given key and Neq returns true for the returned and given value.

func (Expect) MapNotSet

func (e Expect) MapNotSet(data interface{}, key interface{}) bool

MapNotSet returns true if the key is not set in the given array

func (Expect) MapSet

func (e Expect) MapSet(data interface{}, key interface{}) bool

MapSet returns true if the key is set in the given array

func (Expect) Neq

func (e Expect) Neq(val1, val2 interface{}) bool

Neq does a deep equality check on both values and returns true if that test yielded false (val1 != val2)

func (Expect) Nil

func (e Expect) Nil(val interface{}) bool

Nil tests if the given value is nil

func (Expect) NoError

func (e Expect) NoError(err error) bool

NoError tests if the given error is nil. If it is not the error will be logged.

func (Expect) NotNil

func (e Expect) NotNil(val interface{}) bool

NotNil tests if the given value is not nil

func (Expect) True

func (e Expect) True(val bool) bool

True tests if the given value is true

type MarshalMap

type MarshalMap map[string]interface{}

MarshalMap is a wrapper type to attach converter methods to maps normally returned by marshalling methods, i.e. key/value parsers. All methods that do a conversion will return an error if the value stored behind key is not of the expected type or if the key is not existing in the map.

func NewMarshalMap

func NewMarshalMap() MarshalMap

NewMarshalMap creates a new marshal map (string -> interface{})

func (MarshalMap) Array

func (mmap MarshalMap) Array(key string) ([]interface{}, error)

Array returns a value at key that is expected to be a []interface{}

func (MarshalMap) Bool

func (mmap MarshalMap) Bool(key string) (bool, error)

Bool returns a value at key that is expected to be a boolean

func (MarshalMap) Float64

func (mmap MarshalMap) Float64(key string) (float64, error)

Float64 returns a value at key that is expected to be a float64

func (MarshalMap) Int

func (mmap MarshalMap) Int(key string) (int, error)

Int returns a value at key that is expected to be an int

func (MarshalMap) Int64

func (mmap MarshalMap) Int64(key string) (int64, error)

Int64 returns a value at key that is expected to be an int64

func (MarshalMap) Map

func (mmap MarshalMap) Map(key string) (map[interface{}]interface{}, error)

Map returns a value at key that is expected to be a map[interface{}]interface{}.

func (MarshalMap) MarshalMap

func (mmap MarshalMap) MarshalMap(key string) (MarshalMap, error)

MarshalMap returns a value at key that is expected to be another MarshalMap This function supports conversion (by copy) from

  • map[interface{}]interface{}

func (MarshalMap) Path

func (mmap MarshalMap) Path(key string) (interface{}, bool)

Path returns a value from a given value path. Fields can be accessed by their name. Nested fields can be accessed by using "/" as a separator. Arrays can be addressed using the standard array notation "[<index>]". Examples: "key" -> mmap["key"] single value "key1/key2" -> mmap["key1"]["key2"] nested map "key1[0]" -> mmap["key1"][0] nested array "key1[0]key2" -> mmap["key1"][0]["key2"] nested array, nested map

func (MarshalMap) String

func (mmap MarshalMap) String(key string) (string, error)

Array returns a value at key that is expected to be a string

func (MarshalMap) StringArray

func (mmap MarshalMap) StringArray(key string) ([]string, error)

StringArray returns a value at key that is expected to be a []string This function supports conversion (by copy) from

  • []interface{}

func (MarshalMap) StringArrayMap

func (mmap MarshalMap) StringArrayMap(key string) (map[string][]string, error)

StringArrayMap returns a value at key that is expected to be a map[string][]string. This function supports conversion (by copy) from

  • map[interface{}][]interface{}
  • map[interface{}]interface{}
  • map[string]interface{}

func (MarshalMap) StringMap

func (mmap MarshalMap) StringMap(key string) (map[string]string, error)

StringMap returns a value at key that is expected to be a map[string]string. This function supports conversion (by copy) from

  • map[interface{}]interface{}
  • map[string]interface{}

func (MarshalMap) Uint64

func (mmap MarshalMap) Uint64(key string) (uint64, error)

Uint64 returns a value at key that is expected to be an uint64

type MetricServer

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

MetricServer contains state information about the metric server process

func NewMetricServer

func NewMetricServer() *MetricServer

NewMetricServer creates a new server state for a metric server

func (*MetricServer) Start

func (server *MetricServer) Start(port int)

Start causes a metric server to listen for a specific port. If this port is accessed a JSON containing all metrics will be returned and the connection is closed.

func (*MetricServer) Stop

func (server *MetricServer) Stop()

Stop notifies the metric server to halt.

type ParsedFunc

type ParsedFunc func([]byte, ParserStateID)

ParsedFunc defines a function that a token has been matched

type ParserFlag

type ParserFlag int

ParserFlag is an enum type for flags used in parser transitions.

type ParserStateID

type ParserStateID uint32

ParserStateID is used as an integer-based reference to a specific parser state. You can use any number (e.g. a hash) as a parser state representative.

type StopListener

type StopListener struct {
	*net.TCPListener
	// contains filtered or unexported fields
}

StopListener is a wrapper around net.TCPListener that allows to gracefully shut down a connection loop.

func NewStopListener

func NewStopListener(address string) (*StopListener, error)

NewStopListener creates a new, stoppable TCP server connection. Address needs to be cmpliant to net.Listen.

func (*StopListener) Accept

func (listen *StopListener) Accept() (net.Conn, error)

Accept is analogous to net.TCPListener.Accept but may return a connection closed error if the connection was requested to shut down.

func (*StopListener) Close

func (listen *StopListener) Close() error

Close requests a connection close on this listener

type StopRequestError

type StopRequestError struct{}

StopRequestError is not an error but a note that the connection was requested to be closed.

func (StopRequestError) Error

func (err StopRequestError) Error() string

Error implements the standard error interface

type Transition

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

Transition defines a token based state change

func NewTransition

func NewTransition(nextState ParserStateID, flags ParserFlag, callback ParsedFunc) Transition

NewTransition creates a new transition to a given state.

type TransitionDirective

type TransitionDirective struct {
	State     string
	Token     string
	NextState string
	Flags     ParserFlag
	Callback  ParsedFunc
}

TransitionDirective contains a transition description that can be passed to the AddDirectives functions.

func ParseTransitionDirective

func ParseTransitionDirective(config string, callbacks map[string]ParsedFunc) (TransitionDirective, error)

ParseTransitionDirective creates a transition directive from a string. This string must be of the following format:

State:Token:NextState:Flag,Flag,...:Function

Spaces will be stripped from all fields but Token. If a fields requires a colon it has to be escaped with a backslash. Other escape characters supported are \n, \r and \t. Flag can be a set of the following flags (input will be converted to lowercase):

  • continue -> ParserFlagContinue
  • append -> ParserFlagAppend
  • include -> ParserFlagInclude
  • push -> ParserFlagPush
  • pop -> ParserFlagPop

The name passed to the function token must be in the callbacks map. If it is not the data of the token will not be written. I.e. in empty string equals "do not write" here. An empty NextState will be translated to the "Stop" state as ususal.

type TransitionParser

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

TransitionParser defines the behavior of a parser by storing transitions from one state to another.

func NewTransitionParser

func NewTransitionParser() TransitionParser

NewTransitionParser creates a new transition based parser

func (*TransitionParser) Add

func (parser *TransitionParser) Add(stateName string, token string, nextStateName string, flags ParserFlag, callback ParsedFunc)

Add adds a new transition to a given parser state.

func (*TransitionParser) AddDirectives

func (parser *TransitionParser) AddDirectives(directives []TransitionDirective)

AddDirectives is a convenience function to add multiple transitions in as a batch.

func (*TransitionParser) AddTransition

func (parser *TransitionParser) AddTransition(stateName string, newTrans Transition, token string)

AddTransition adds a transition from a given state to the map

func (*TransitionParser) GetStateID

func (parser *TransitionParser) GetStateID(stateName string) ParserStateID

GetStateID creates a hash from the given state name. Empty state names will be translated to ParserStateStop.

func (*TransitionParser) GetStateName

func (parser *TransitionParser) GetStateName(id ParserStateID) string

GetStateName returns the name for the given state id or an empty string if the id could not be found.

func (*TransitionParser) Parse

func (parser *TransitionParser) Parse(data []byte, state string) ([]byte, ParserStateID)

Parse starts parsing at a given stateID. This function returns the remaining parts of data that did not match a transition as well as the last state the parser has been set to.

func (*TransitionParser) Stop

func (parser *TransitionParser) Stop(stateName string, token string, flags ParserFlag, callback ParsedFunc)

Stop adds a stop transition to a given parser state.

type TrieNode

type TrieNode struct {
	PathLen int
	Payload interface{}
	// contains filtered or unexported fields
}

TrieNode represents a single node inside a trie. Each node can contain a payload which can be retrieved after a successfull match. In addition to that PathLen will contain the length of the match.

func NewTrie

func NewTrie(data []byte, payload interface{}) *TrieNode

NewTrie creates a new root TrieNode

func (*TrieNode) Add

func (node *TrieNode) Add(data []byte, payload interface{}) *TrieNode

Add adds a new data path to the trie. The TrieNode returned is the (new) root node so you should always reassign the root with the return value of Add.

func (*TrieNode) ForEach

func (node *TrieNode) ForEach(callback func(*TrieNode))

ForEach applies a function to each node in the tree including and below the passed node.

func (*TrieNode) Match

func (node *TrieNode) Match(data []byte) *TrieNode

Match compares the trie to the given data stream. Match returns true if data can be completely matched to the trie.

func (*TrieNode) MatchStart

func (node *TrieNode) MatchStart(data []byte) *TrieNode

MatchStart compares the trie to the beginning of the given data stream. MatchStart returns true if the beginning of data can be matched to the trie.

type TypeRegistry

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

TypeRegistry is a name to type registry used to create objects by name.

func (TypeRegistry) GetRegistered

func (registry TypeRegistry) GetRegistered(packageName string) []string

GetRegistered returns the names of all registered types for a given package

func (TypeRegistry) GetTypeOf

func (registry TypeRegistry) GetTypeOf(typeName string) reflect.Type

GetTypeOf returns only the type asscociated with the given name. If the name is not registered, nil is returned. The type returned will be a pointer type.

func (TypeRegistry) New

func (registry TypeRegistry) New(typeName string) (interface{}, error)

New creates an uninitialized object by class name. The class name has to be "package.class" or "package/subpackage.class". The gollum package is omitted from the package path.

func (TypeRegistry) Register

func (registry TypeRegistry) Register(typeInstance interface{})

Register a plugin to the typeRegistry by passing an uninitialized object. Example: var MyConsumerClassID = shared.Plugin.Register(MyConsumer{})

Jump to

Keyboard shortcuts

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