fsm

package
v0.0.0-...-7ce9f83 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2023 License: Apache-2.0 Imports: 7 Imported by: 3

Documentation

Overview

Copyright (c) 2021-2023 - for information on the respective copyright owner see the NOTICE file and/or the repository https://github.com/carbynestack/ephemeral.

SPDX-License-Identifier: Apache-2.0

Index

Constants

View Source
const (
	// CallbackAfterEnter is a callback type which is triggered when a new state just entered.
	CallbackAfterEnter = "AfterEnter"
	// CallbackBeforeEnter is a callback type which is triggered when a new state just entered.
	CallbackBeforeEnter = "BeforeEnter"
	// CallbackWhenStateTimeout is a type of callback which is triggered when state timeout is reached.
	CallbackWhenStateTimeout = "WhenStateTimeout"
)
View Source
const (
	Stopped = "_Stopped"
)

Variables

This section is empty.

Functions

func InitCallbacksAndTransitions

func InitCallbacksAndTransitions(cbs []*Callback, trs []*Transition) (map[string][]*Callback, map[TransitionID]*Transition)

InitCallbacksAndTransitions converts slices to maps.

Types

type Action

type Action func(interface{}) error

Action is a user defined function executed in the callback.

type Callback

type Callback struct {
	Type   string
	Src    string
	Action Action
}

Callback is a function which is executed as a response to event during state transition.

func AfterEnter

func AfterEnter(state string) *Callback

AfterEnter defines state this callback is bound to.

func BeforeEnter

func BeforeEnter(state string) *Callback

BeforeEnter defines callback which is executed before entering the state.

func WhenStateTimeout

func WhenStateTimeout() *Callback

WhenStateTimeout defines a callback which is called when state timeout is reached.

func (*Callback) Do

func (c *Callback) Do(a Action) *Callback

Do defines a function to execute in the callback.

type Event

type Event struct {
	Name   string
	GameID string
	Meta   *Metadata
}

Event is an event consumed by FSM.

type FSM

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

FSM is a finate state machine. Before and after callbacks for the same source state can be defined. If several callbacks are provided for each type, all of them are executed in order.

func NewFSM

func NewFSM(ctx context.Context, initState string, trn map[TransitionID]*Transition, cb map[string][]*Callback, stateTimeout time.Duration, logger *zap.SugaredLogger) (*FSM, error)

NewFSM returns a new finate state machine.

func (*FSM) Current

func (f *FSM) Current() string

Current returns the current state of FSM.

func (*FSM) History

func (f *FSM) History() *History

History returns the state transition history.

func (*FSM) Run

func (f *FSM) Run(errChan chan error)

Run consumes events from the queue until an error occurs or the FSM has been stopped. The error is caused either by an unregistered event or by the callback itself. If the FSM was stopped its state is updated, the timer is stopped and the error channel is closed. The method is blocking and must be started exactly once.

`errChan` is expected to be a buffered channel with minimum capacity of "1".

func (*FSM) Stop

func (f *FSM) Stop()

Stop stops the FSM. No other state transition is possible after the call. This method must be called only once, subsequent calls might be blocked infinitely.

func (*FSM) Write

func (f *FSM) Write(event *Event)

Write sends an event to the FSM FIFO queue and notifies the processor that new event arrived.

type History

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

History contains all received events and passed states including the current one.

func NewHistory

func NewHistory() *History

NewHistory returns an empty fsm history.

func (*History) AddEvent

func (h *History) AddEvent(ev *Event)

AddEvent writes a new event to the history.

func (*History) AddState

func (h *History) AddState(st string)

AddState saves the state to the history.

func (*History) GetEvents

func (h *History) GetEvents() []*Event

GetEvents returns a list of all events.

func (*History) GetStates

func (h *History) GetStates() []string

GetStates returns passed states of FSM including the current one.

type Metadata

type Metadata struct {
	FSM          *FSM
	TargetTopic  string
	SrcTopics    []string
	TransportMsg *proto.Event
}

Metadata contains metada of an FSM event.

type Transition

type Transition struct {
	ID              TransitionID
	Event, Src, Dst string
	Timeout         time.Duration
}

Transition defines a transition between FSM states.

func WhenIn

func WhenIn(state string) *Transition

WhenIn specifies the source state of the transition.

func WhenInAnyState

func WhenInAnyState() *Transition

WhenInAnyState targets transition from all states.

func (*Transition) GoTo

func (i *Transition) GoTo(dst string) *Transition

GoTo specifies the destination State.

func (*Transition) GotEvent

func (i *Transition) GotEvent(event string) *Transition

GotEvent specifies the triggering event for the transition.

func (*Transition) Stay

func (i *Transition) Stay() *Transition

Stay forces the transition to stay in the source state.

func (*Transition) WithTimeout

func (i *Transition) WithTimeout(d time.Duration) *Transition

WithTimeout defines an individual timeout within the transition to the next state is expected.

type TransitionID

type TransitionID struct {
	Event, Source string
}

TransitionID is a tuple containing external Event and source State.

Jump to

Keyboard shortcuts

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