-
Notifications
You must be signed in to change notification settings - Fork 2
/
statemachine.go
283 lines (240 loc) · 7.84 KB
/
statemachine.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Copyright (c) 2013 Ondřej Kupka
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package statemachine
import (
"errors"
)
// PUBLIC TYPES ---------------------------------------------------------------
type (
State int
EventType int
EventData interface{}
)
// Events are the basic units that can be processed by a state machine.
type Event struct {
Type EventType
Data EventData
}
// Various EventHandlers can be registered to process events in particular states.
// By registering event handlers we build up a mapping of state x event -> handler
// and the handler is invoked exactly in the defined state when the defined event
// is emitted.
//
// Once a handler is invoked, its role is to take the StateMachine into the next
// state, doing some useful work on the way.
//
// If an event is emitted in a state where no handler is defined,
// ErrIllegalEvent is returned.
type EventHandler func(s State, e *Event) (next State)
// StateMachine is the only struct this package exports. Once an event is
// emitted on a StateMachine, the relevant handler is fetched and invoked.
// StateMachine takes care of all the synchronization, it is thread-safe.
// It does not use any locking, just channels. While that may be a bit more
// overhead, it is more robust and clear.
//
// It uses an unbuffered channel for passing events to the internal goroutine,
// so all the methods block until their requests read from that channel.
type StateMachine struct {
// Internal StateMachine state
state State
// Registered event handlers
handlers [][]EventHandler
// Communication channels
cmdCh chan *command // Send commands to the background loop
terminatedCh chan struct{} // Signal that the state machine is terminated
}
// CONSTRUCTOR ----------------------------------------------------------------
// Create new StateMachine. Allocate internal memory for particular number of
// states and events.
func New(initState State, stateCount, eventCount uint) *StateMachine {
// Allocate enough space for the handlers.
table := make([][]EventHandler, stateCount)
for i := range table {
table[i] = make([]EventHandler, eventCount)
}
sm := StateMachine{
state: initState,
handlers: table,
cmdCh: make(chan *command),
terminatedCh: make(chan struct{}),
}
// Start background goroutine.
go sm.loop()
return &sm
}
// COMMANDS -------------------------------------------------------------------
const (
cmdOn EventType = iota
cmdOff
cmdIsHandlerAssigned
cmdEmit
cmdSetState
cmdTerminate
)
type command struct {
cmd EventType
args interface{}
}
// On -------------------------------------------------------------------------
type onArgs struct {
s State
t EventType
h EventHandler
}
// Register an event handler. Only one handler can be set per state and event.
func (sm *StateMachine) On(t EventType, ss []State, h EventHandler) error {
for _, s := range ss {
if err := sm.send(&command{
cmdOn,
&onArgs{s, t, h},
}); err != nil {
return err
}
}
return nil
}
// Off ------------------------------------------------------------------------
type offArgs struct {
s State
t EventType
}
// Drop the handler assigned to the requested state and event.
func (sm *StateMachine) Off(t EventType, s State) error {
return sm.send(&command{
cmdOff,
&offArgs{s, t},
})
}
// IsHandlerAssigned ----------------------------------------------------------
type isHandlerAssignedArgs struct {
s State
t EventType
ch chan bool
}
// Check if a handler is defined for this state and event.
func (sm *StateMachine) IsHandlerAssigned(t EventType, s State) (defined bool, err error) {
replyCh := make(chan bool, 1)
err = sm.send(&command{
cmdIsHandlerAssigned,
&isHandlerAssignedArgs{s, t, replyCh},
})
if err != nil {
return
}
defined = <-replyCh
return
}
// Emit -----------------------------------------------------------------------
type emitArgs struct {
e *Event
ch chan<- error
}
// Emit an event.
func (sm *StateMachine) Emit(event *Event) error {
errCh := make(chan error, 1)
err := sm.send(&command{
cmdEmit,
&emitArgs{event, errCh},
})
if err != nil {
return err
}
return <-errCh
}
// SetState -------------------------------------------------------------------
// SetState changes the internal state machine state, nothing more, nothing less.
func (sm *StateMachine) SetState(state State) error {
return sm.send(&command{
cmdSetState,
state,
})
}
// Terminate ------------------------------------------------------------------
// Terminate the internal event loop and close all internal channels.
// Particularly the termination channel is closed to signal all producers that
// they can no longer emit any events and shall exit.
func (sm *StateMachine) Terminate() error {
return sm.send(&command{
cmdTerminate,
nil,
})
}
// TerminateChannel can be used to obtain a channel that is closed once
// the state machine is terminated and is no longer willing to accept any events.
// This is useful if you want to start multiple goroutines to asynchronously
// post events. You can just start them, pass them this termination channel
// and leave them be. The only requirement is that those producer goroutines
// should exit or simply stop posting any events as soon as the channel is closed.
func (sm *StateMachine) TerminatedChannel() (isTerminatedCh chan struct{}) {
return sm.terminatedCh
}
// INTERNALS ------------------------------------------------------------------
// Helper method for sending events to the internal event loop.
func (sm *StateMachine) send(cmd *command) error {
select {
case sm.cmdCh <- cmd:
return nil
case <-sm.terminatedCh:
return ErrTerminated
}
}
// The internal event loop processes events (commands) passed to it in
// a sequential manner.
func (sm *StateMachine) loop() {
for {
cmd := <-sm.cmdCh
switch cmd.cmd {
case cmdEmit:
args := cmd.args.(*emitArgs)
handler := sm.handlers[sm.state][args.e.Type]
if handler == nil {
args.ch <- ErrIllegalEvent
close(args.ch)
continue
}
close(args.ch)
next := handler(sm.state, args.e)
sm.state = next
case cmdSetState:
sm.state = cmd.args.(State)
case cmdOn:
args := cmd.args.(*onArgs)
sm.handlers[args.s][args.t] = args.h
case cmdOff:
args := cmd.args.(*offArgs)
sm.handlers[args.s][args.t] = nil
case cmdIsHandlerAssigned:
args := cmd.args.(*isHandlerAssignedArgs)
args.ch <- (sm.handlers[args.s][args.t] != nil)
close(args.ch)
case cmdTerminate:
close(sm.terminatedCh)
return
default:
panic("Unknown command received")
}
}
}
// ERRORS ---------------------------------------------------------------------
var (
// Returned from Emit if there is no mapping for the current state and the
// event that is being emitted.
ErrIllegalEvent = errors.New("Illegal event received")
// Returned from a method if the state machine is already terminated.
ErrTerminated = errors.New("State machine terminated")
)