-
Notifications
You must be signed in to change notification settings - Fork 39
/
fsm.go
67 lines (57 loc) · 1.31 KB
/
fsm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package utils
import (
"context"
"sync"
"github.com/pkg/errors"
)
type Event struct {
Id uint64
Name string
}
type State struct {
Id uint64
Name string
}
type Action func(ctx context.Context)
type TransitionItem struct {
NewState uint64
Action Action
}
type Fsm struct {
initStateId uint64
stateId uint64
StateTransTable [][]TransitionItem
stateList []State
eventList []Event
mutex sync.Mutex
}
func (e Event) string() string {
return e.Name
}
func (s State) string() string {
return s.Name
}
func (fsm *Fsm) RunFsm(ctx context.Context, e uint64) {
fsm.mutex.Lock()
oldState := fsm.stateList[fsm.stateId]
item := fsm.StateTransTable[fsm.stateId][e]
fsm.stateId = item.NewState
if item.Action != nil {
item.Action(ctx)
}
DebugLogf("RunFsm: S.%v ---E.%v---> S.%v", oldState.string(), fsm.eventList[e].string(), fsm.stateList[fsm.stateId].string())
fsm.mutex.Unlock()
}
func (fsm *Fsm) GetState() State {
return fsm.stateList[fsm.stateId]
}
func (fsm *Fsm) InitFsm(stateList []State, eventList []Event, f func(f *Fsm), initStateId uint64) error {
if len(stateList) == 0 || len(eventList) == 0 {
return errors.New("invalid number of states or events")
}
fsm.stateList = stateList
fsm.eventList = eventList
f(fsm)
fsm.initStateId = initStateId
return nil
}