tcgl: code.google.com/p/tcgl/state Index | Files

package state

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.

Index

type FSM

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

State machine type.

func New

func New(h Handler, tick time.Duration) *FSM

Create a new finite state machine.

func (*FSM) Handle

func (fsm *FSM) Handle(cmd string, payload interface{})

Handle lets the FSM handle a command and payload.

func (*FSM) HandleAfter

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) HandleWithResult

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) State

func (fsm *FSM) State() string

State returns the current state.

type Handler

type Handler interface {
    Init() (*HandlerMap, string)
    Error(*Transition, error) string
    Terminate()
}

Handler interface.

type HandlerMap

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

HandlerMap maps states to handler methods.

func NewHandlerMap

func NewHandlerMap(h Handler) *HandlerMap

NewHandlerMap creates a new handler map with initial state to method assignments.

func (*HandlerMap) Assign

func (hm *HandlerMap) Assign(state, method string) error

Assign adds an assignment of a state to a handler method.

type Transition

type Transition struct {
    Timestamp  time.Time
    Command    string
    State      string
    Payload    interface{}
    ResultChan chan interface{}
}

Transition type.

Files

doc.go state.go

Package state imports 4 packages (graph). Updated 2013-04-04. Refresh.