import "code.google.com/p/tcgl/state"
The state package provides a universal finite state machine.
It uses a type implementing methods with defined signature. The returned string represents the next state and is the name of the method that will be called.
type FSM struct {
// contains filtered or unexported fields
}State machine type.
func New(h Handler, tick time.Duration) *FSM
Create a new finite state machine.
func (fsm *FSM) Handle(cmd string, payload interface{})
Handle lets the FSM handle a command and payload.
func (fsm *FSM) HandleAfter(cmd string, payload interface{}, after time.Duration)
HandeAfter lets the FSM handle a command and payload after a given duration.
func (fsm *FSM) HandleWithResult(cmd string, payload interface{}) chan interface{}
HandleWithResult lets the FSM handle a command and payload and returns a channel for a possible result.
func (fsm *FSM) State() string
State returns the current state.
type Handler interface {
Init() (*HandlerMap, string)
Error(*Transition, error) string
Terminate()
}Handler interface.
type HandlerMap struct {
// contains filtered or unexported fields
}HandlerMap maps states to handler methods.
func NewHandlerMap(h Handler) *HandlerMap
NewHandlerMap creates a new handler map with initial state to method assignments.
func (hm *HandlerMap) Assign(state, method string) error
Assign adds an assignment of a state to a handler method.
type Transition struct {
Timestamp time.Time
Command string
State string
Payload interface{}
ResultChan chan interface{}
}Transition type.