-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.go
66 lines (58 loc) · 1.28 KB
/
state.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
package smpp
import (
"sync"
)
// Control Loop for managing a ESME state (not a Finite State Machine!).
// The implementation is concurrent safe as SMPP protocol require to know
// which state we're in to take some decisions.
type State struct {
state string
setState chan string
reportState chan string
done chan bool
mu sync.Mutex
}
func NewESMEState(state string) *State {
obj := State{
state: state,
reportState: make(chan string),
setState: make(chan string),
done: make(chan bool),
}
go obj.stateDispatcher()
return &obj
}
func (state *State) stateDispatcher() {
stateDispatcherLoop:
for {
select {
case msg2 := <-state.setState:
state.state = msg2
case state.reportState <- state.state:
continue
case <-state.done:
close(state.reportState)
break stateDispatcherLoop
}
}
}
func (state *State) GetState() string {
if state.controlLoopStillAlive() {
return <-state.reportState
}
return CLOSED
}
func (state *State) SetState(desired_state string) {
state.setState <- desired_state
}
func (state *State) controlLoopStillAlive() bool {
_, ok := <-state.reportState
return ok
}
func (state *State) Close() {
state.mu.Lock()
defer state.mu.Unlock()
if state.controlLoopStillAlive() {
state.done <- true
}
}