behaviors

package
v0.0.0-...-fe39cb5 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2015 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package behaviors provides several generic and always useful standard behaviors for the Tideland Go Library Cells. They are simply created by calling NewXyzBehavior(). Their configuration is done by constructor arguments. Additionally some of them take functions or implementations of interfaces to control their processing. These behaviors are:

Broadcaster

The broadcaster behavior simply emits all received events to all of its subscribers. It is intended to be used as a top level behavior to directly rigger multiple handlers instead of emitting an event manually to those handlers.

Callback

The callback behavior allows you to provide a number of functions which will be called when an event is received. Those functions have the topic and the payload of the event as argument.

Collector

The collector behavior collects all received events. They can be retrieved and resetted. It also emits all received events to its subscribers.

Configurator

After receiving a ReadConfigurationTopic with a filename as payload the configuration behavior reads this configuration and emits it. If it is started with a validator the configuration is validated after the reading.

Counter

The counter behavior is created with a counter function as argument. This function is called for each event and returns the IDs of counters which are incremented then. The counters are emitted each time and also can be resetted.

Filter

The filter behavior is created with a filtering function which is called for each event. If this function call returns true the event emitted, otherwise it is dropped.

Finite State Machine

The FSM behavior implements a finite state machine. State functions process the events and return the following state function.

Logger

The logger behavior logs every event. The used level is INFO.

Mapper

The mapper behavior is created with a mapping. It is called with each received event and returns a new mapped one.

Round Robin

The round robin behavior distributes each received event round robin to its subscribers. It can be used for load balancing.

Scene

The scene behavior stores a received payload using the event topic as key in the event scene. So it can be used later by other behaviors or by the external environments, which can wait until the setting.

Simple Processor

The simple behavior is created with a simple event processing function. Useful if no state and no complex recovery is needed.

Ticker

The ticker behavior emits a tick event in a defined interval to its subscribers. So they can process chronological tasks beside other events.

Index

Constants

View Source
const (
	// Topics.
	ReadConfigurationTopic = "read-configuration!"
	ConfigurationTopic     = "configuration"
	TickerTopic            = "tick!"

	// Payload keys.
	ConfigurationFilenamePayload = "configuration:filename"
	ConfigurationPayload         = "configuration"
	TickerIDPayload              = "ticker:id"
	TickerTimePayload            = "ticker:time"
)
View Source
const (
	ErrCannotReadConfiguration = iota + 1
	ErrCannotValidateConfiguration
)

Error codes.

Variables

This section is empty.

Functions

func Configuration

func Configuration(event cells.Event) configuration.Configuration

Configuration returns the configuration payload of the passed event or an empty configuration.

func NewBroadcasterBehavior

func NewBroadcasterBehavior() cells.Behavior

NewBroadcasterBehavior creates a broadcasting behavior that just emits every received event. It's intended to work as an entry point for events, which shall be immediately processed by several subscribers.

func NewCallbackBehavior

func NewCallbackBehavior(cbfs ...CallbackFunc) cells.Behavior

NewCallbackBehavior creates a behavior with a number of callback functions. Each time an event is received those functions are called in the same order they have been passed.

func NewCollectorBehavior

func NewCollectorBehavior(max int) cells.Behavior

NewCollectorBehaviorFactory creates a collector behavior. It collects a configured maximum number events emitted directly or by subscription. The event is passed through. The collected events can be requested with the topic "collected?" and will be stored in the scene store named in the events payload. Additionally the collection can be resetted with "reset!".

func NewConfiguratorBehavior

func NewConfiguratorBehavior(validator ConfigurationValidator) cells.Behavior

NewConfiguratorBehavior creates the configurator behavior. It loads a configuration file and emits the it to its subscribers. If a validator is passed the read configuration will be validated using it. Errors will be logged.

func NewCounterBehavior

func NewCounterBehavior(cf CounterFunc) cells.Behavior

NewCounterBehavior creates a counter behavior based on the passed function. It increments and emits those counters named by the result of the counter function. The counters can be retrieved with the request "counters?" and reset with "reset!".

func NewFSMBehavior

func NewFSMBehavior(state FSMState) cells.Behavior

NewFSMBehavior creates a finite state machine behavior based on the passed initial state function. The function is called with the event has to return the next state, which can be the same one. In case of nil the stae will be transfered into a generic end state, if an error is returned the state is a generic error state.

func NewFilterBehavior

func NewFilterBehavior(ff FilterFunc) cells.Behavior

NewFilterBehavior creates a filter behavior based on the passed function. It emits every received event for which the filter function returns true.

func NewLoggerBehavior

func NewLoggerBehavior() cells.Behavior

NewLoggerBehavior creates a logging behavior. It logs emitted events with info level.

func NewMapperBehavior

func NewMapperBehavior(mf MapFunc) cells.Behavior

NewMapperBehavior creates a map behavior based on the passed function. It emits the mapped events.

func NewRoundRobinBehavior

func NewRoundRobinBehavior() cells.Behavior

NewRoundRobinBehavior creates a behavior emitting the received events to its subscribers in a very simple way. Subscriptions or unsubscriptions during runtime may influence the order.

func NewRouterBehavior

func NewRouterBehavior(rf RouterFunc) cells.Behavior

NewRouterBehavior creates a router behavior using the passed function to determine to which subscriber the received event will be emitted.

func NewSceneBehavior

func NewSceneBehavior() cells.Behavior

NewSceneBehavior creates a scene behavior that stores the payload of an event using the topic of an event as key in its scene. This way external code can wait for this topic as flag and fetch the value. It's not intended to use it as a standard behvior, even if it works. Instead it can be used in testing scenarios.

func NewSimpleProcessorBehavior

func NewSimpleProcessorBehavior(spf SimpleProcessorFunc) cells.Behavior

NewSimpleProcessorBehavior creates a filter behavior based on the passed function. Instead of an own logic and an own state it uses the passed simple processor function for the event processing.

func NewTickerBehavior

func NewTickerBehavior(duration time.Duration) cells.Behavior

NewTickerBehavior creates a ticker behavior.

func PackageVersion

func PackageVersion() version.Version

PackageVersion returns the version of the version package.

Types

type CallbackFunc

type CallbackFunc func(topic string, payload cells.Payload) error

CallbackFunc is a function called by the behavior when it recieves an event.

type ConfigurationValidator

type ConfigurationValidator func(configuration.Configuration) error

ConfigurationValidator defines a function for the validation of a new read configuration.

type CounterFunc

type CounterFunc func(id string, event cells.Event) []string

CounterFunc is the signature of a function which analyzis an event and returns, which counters shall be incremented.

type Counters

type Counters map[string]int64

Counters is a set of named counters and their values.

type EventData

type EventData struct {
	Topic   string
	Payload cells.Payload
}

EventData represents the pure collected event data.

type FSMState

type FSMState func(ctx cells.Context, event cells.Event) (FSMState, error)

FSMState is the signature of a function or method which processes an event and returns the following state or an error.

type FSMStatus

type FSMStatus struct {
	Done  bool
	Error error
}

FSMStatus contains information about the current status of the FSM.

func RequestFSMStatus

func RequestFSMStatus(env cells.Environment, id string) FSMStatus

RequestFSMStatus retrieves the status of a FSM cell.

func (FSMStatus) String

func (s FSMStatus) String() string

String is specified on the Stringer interface.

type FilterFunc

type FilterFunc func(id string, event cells.Event) bool

FilterFunc is a function type checking if an event shall be filtered.

type MapFunc

type MapFunc func(id string, event cells.Event) (cells.Event, error)

MapFunc is a function type mapping an event to another one.

type RouterFunc

type RouterFunc func(emitterID, subscriberID string, event cells.Event) (bool, error)

RouterFunc is a function type determinig which subscribed cells shall receive the event.

type SimpleProcessorFunc

type SimpleProcessorFunc func(ctx cells.Context, event cells.Event) error

SimpleProcessorFunc is a function type doing the event processing.

Jump to

Keyboard shortcuts

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