fsm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2018 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMachineNotInitialized is the error thatthe  state machine is not initialized.
	ErrMachineNotInitialized = errors.New("state machine is not initialized")
	// ErrTransitionNotPermitted is the error that the state transition is not permitted.
	ErrTransitionNotPermitted = errors.New("state transition is not permitted")
	// ErrStateUndefined is the error that the state is undefined
	ErrStateUndefined = errors.New("state is undefined")
	// ErrStateHandlerNotMatched is the error that the current event is not matched to state handler
	ErrStateHandlerNotMatched = errors.New("event is not matched to state handler")
	// ErrNoTransitionApplied is the error that no state transition is applied
	ErrNoTransitionApplied = errors.New("no state transition is applied")
)

Functions

This section is empty.

Types

type Event

type Event struct {
	Err error

	State         State
	StateTimedOut bool

	Block     *blockchain.Block
	BlockHash *cp.Hash32B

	SenderAddr net.Addr

	ExpireAt *time.Time
}

Event is holding request event info across the handler and the rule.

type Handler

type Handler interface {
	Handle(event *Event)

	// TimeoutDuration returns the timeout of the state handler.
	// If it returns a duration, after the duration and the state is still not
	// If it returns nil, then no timeout task is set.
	TimeoutDuration() *time.Duration
}

Handler handles events for the state The difference between state handler and rule is that

  1. rule dose not handle state event, it's just a transition from one state to another.
  2. rule must succeed. even it has side-effects, no matter they fail or not, the transition is deterministic to the destination state.

type Machine

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

Machine is the state machine.

func NewMachine

func NewMachine() Machine

NewMachine returns a state machine instance

func (*Machine) AddState

func (m *Machine) AddState(state State, handler Handler)

AddState add the state and the handler.

func (*Machine) AddTransition

func (m *Machine) AddTransition(src State, dest State, rule Rule) error

AddTransition is a function for adding a valid state transition to the machine.

func (*Machine) CurrentState

func (m *Machine) CurrentState() State

CurrentState returns the machine's current state. It returns "" when not initialized.

func (*Machine) HandleTransition

func (m *Machine) HandleTransition(ctx *Event) error

HandleTransition tries to move to the next state by the request ctx.

func (*Machine) SetInitialState

func (m *Machine) SetInitialState(state State, handler Handler)

SetInitialState sets the initial state which not only handles request for itself but also accepts state types for all direct neighbor states

type NilTimeout

type NilTimeout struct{}

NilTimeout is the base handler struct that has no timeout

func (*NilTimeout) TimeoutDuration

func (h *NilTimeout) TimeoutDuration() *time.Duration

TimeoutDuration returns the duration for timeout

type Rule

type Rule interface {
	Condition(event *Event) bool
}

Rule condition is evaluated when state handler is called.

type State

type State string

State is the machine state name.

type TransitionRuleMap

type TransitionRuleMap map[State]Rule

TransitionRuleMap is a set of map of transition destination states to rules.

func (TransitionRuleMap) Copy

Copy clones the TransitionRuleMap.

Jump to

Keyboard shortcuts

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