forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_state.go
255 lines (199 loc) · 5.79 KB
/
session_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
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
package quickfix
import (
"fmt"
"time"
"github.com/quickfixgo/quickfix/internal"
)
type stateMachine struct {
State sessionState
pendingStop, stopped bool
notifyOnInSessionTime chan interface{}
}
func (sm *stateMachine) Start(s *session) {
sm.pendingStop = false
sm.stopped = false
sm.State = latentState{}
sm.CheckSessionTime(s, time.Now())
}
func (sm *stateMachine) Connect(session *session) {
if !sm.IsSessionTime() {
session.log.OnEvent("Connection outside of session time")
sm.handleDisconnectState(session)
return
}
if session.InitiateLogon {
if session.RefreshOnLogon {
if err := session.store.Refresh(); err != nil {
session.logError(err)
return
}
}
session.log.OnEvent("Sending logon request")
if err := session.sendLogon(false, false); err != nil {
session.logError(err)
return
}
}
sm.setState(session, logonState{})
}
func (sm *stateMachine) Stop(session *session) {
sm.pendingStop = true
sm.setState(session, sm.State.Stop(session))
}
func (sm *stateMachine) Stopped() bool {
return sm.stopped
}
func (sm *stateMachine) Disconnected(session *session) {
if sm.IsConnected() {
sm.setState(session, latentState{})
}
}
func (sm *stateMachine) Incoming(session *session, m fixIn) {
sm.CheckSessionTime(session, time.Now())
if !sm.IsConnected() {
return
}
session.log.OnIncoming(m.bytes.Bytes())
msg := session.messagePool.Get()
if err := ParseMessage(msg, m.bytes); err != nil {
session.log.OnEventf("Msg Parse Error: %v, %q", err.Error(), m.bytes)
} else {
msg.ReceiveTime = m.receiveTime
sm.fixMsgIn(session, msg)
}
if !msg.keepMessage {
session.returnToPool(msg)
}
session.peerTimer.Reset(time.Duration(float64(1.2) * float64(session.HeartBtInt)))
}
func (sm *stateMachine) fixMsgIn(session *session, m *Message) {
sm.setState(session, sm.State.FixMsgIn(session, m))
}
func (sm *stateMachine) SendAppMessages(session *session) {
sm.CheckSessionTime(session, time.Now())
session.sendMutex.Lock()
defer session.sendMutex.Unlock()
if session.IsLoggedOn() {
session.sendQueued()
} else {
session.dropQueued()
}
}
func (sm *stateMachine) Timeout(session *session, e internal.Event) {
sm.CheckSessionTime(session, time.Now())
sm.setState(session, sm.State.Timeout(session, e))
}
func (sm *stateMachine) CheckSessionTime(session *session, now time.Time) {
if !session.SessionTime.IsInRange(now) {
if sm.IsSessionTime() {
session.log.OnEvent("Not in session")
}
sm.State.ShutdownNow(session)
sm.setState(session, notSessionTime{})
if sm.notifyOnInSessionTime == nil {
sm.notifyOnInSessionTime = make(chan interface{})
}
return
}
if !sm.IsSessionTime() {
session.log.OnEvent("In session")
sm.notifyInSessionTime()
sm.setState(session, latentState{})
}
if !session.SessionTime.IsInSameRange(session.store.CreationTime(), now) {
session.log.OnEvent("Session reset")
sm.State.ShutdownNow(session)
if err := session.dropAndReset(); err != nil {
session.logError(err)
}
sm.setState(session, latentState{})
}
}
func (sm *stateMachine) setState(session *session, nextState sessionState) {
if !nextState.IsConnected() {
if sm.IsConnected() {
sm.handleDisconnectState(session)
}
if sm.pendingStop {
sm.stopped = true
sm.notifyInSessionTime()
}
}
sm.State = nextState
}
func (sm *stateMachine) notifyInSessionTime() {
if sm.notifyOnInSessionTime != nil {
close(sm.notifyOnInSessionTime)
}
sm.notifyOnInSessionTime = nil
}
func (sm *stateMachine) handleDisconnectState(s *session) {
doOnLogout := s.IsLoggedOn()
switch s.State.(type) {
case logoutState:
doOnLogout = true
case logonState:
if s.InitiateLogon {
doOnLogout = true
}
}
if doOnLogout {
s.application.OnLogout(s.sessionID)
}
s.onDisconnect()
}
func (sm *stateMachine) IsLoggedOn() bool {
return sm.State.IsLoggedOn()
}
func (sm *stateMachine) IsConnected() bool {
return sm.State.IsConnected()
}
func (sm *stateMachine) IsSessionTime() bool {
return sm.State.IsSessionTime()
}
func handleStateError(s *session, err error) sessionState {
s.logError(err)
return latentState{}
}
//sessionState is the current state of the session state machine. The session state determines how the session responds to
//incoming messages, timeouts, and requests to send application messages.
type sessionState interface {
//FixMsgIn is called by the session on incoming messages from the counter party. The return type is the next session state
//following message processing
FixMsgIn(*session, *Message) (nextState sessionState)
//Timeout is called by the session on a timeout event.
Timeout(*session, internal.Event) (nextState sessionState)
//IsLoggedOn returns true if state is logged on an in session, false otherwise
IsLoggedOn() bool
//IsConnected returns true if the state is connected
IsConnected() bool
//IsSessionTime returns true if the state is in session time
IsSessionTime() bool
//ShutdownNow terminates the session state immediately
ShutdownNow(*session)
//Stop triggers a clean stop
Stop(*session) (nextState sessionState)
//debugging convenience
fmt.Stringer
}
type inSessionTime struct{}
func (inSessionTime) IsSessionTime() bool { return true }
type connected struct{}
func (connected) IsConnected() bool { return true }
func (connected) IsSessionTime() bool { return true }
type connectedNotLoggedOn struct{ connected }
func (connectedNotLoggedOn) IsLoggedOn() bool { return false }
func (connectedNotLoggedOn) ShutdownNow(*session) {}
type loggedOn struct{ connected }
func (loggedOn) IsLoggedOn() bool { return true }
func (loggedOn) ShutdownNow(s *session) {
if err := s.sendLogout(""); err != nil {
s.logError(err)
}
}
func (loggedOn) Stop(s *session) (nextState sessionState) {
if err := s.initiateLogout(""); err != nil {
return handleStateError(s, err)
}
return logoutState{}
}