forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
session_factory.go
360 lines (291 loc) · 9.16 KB
/
session_factory.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package quickfix
import (
"errors"
"net"
"strconv"
"time"
"github.com/alpacahq/quickfix/config"
"github.com/alpacahq/quickfix/datadictionary"
"github.com/alpacahq/quickfix/internal"
)
var dayLookup = map[string]time.Weekday{
"Sunday": time.Sunday,
"Monday": time.Monday,
"Tuesday": time.Tuesday,
"Wednesday": time.Wednesday,
"Thursday": time.Thursday,
"Friday": time.Friday,
"Saturday": time.Saturday,
"Sun": time.Sunday,
"Mon": time.Monday,
"Tue": time.Tuesday,
"Wed": time.Wednesday,
"Thu": time.Thursday,
"Fri": time.Friday,
"Sat": time.Saturday,
}
var applVerIDLookup = map[string]string{
BeginStringFIX40: "2",
BeginStringFIX41: "3",
BeginStringFIX42: "4",
BeginStringFIX43: "5",
BeginStringFIX44: "6",
"FIX.5.0": "7",
"FIX.5.0SP1": "8",
"FIX.5.0SP2": "9",
}
type sessionFactory struct {
//True if building sessions that initiate logon
BuildInitiators bool
}
// Creates Session, associates with internal session registry
func (f sessionFactory) createSession(
sessionID SessionID, storeFactory MessageStoreFactory, settings *SessionSettings,
logFactory LogFactory, application Application,
) (session *session, err error) {
if session, err = f.newSession(sessionID, storeFactory, settings, logFactory, application); err != nil {
return
}
if err = registerSession(session); err != nil {
return
}
application.OnCreate(session.sessionID)
session.log.OnEvent("Created session")
return
}
func (f sessionFactory) newSession(
sessionID SessionID, storeFactory MessageStoreFactory, settings *SessionSettings, logFactory LogFactory,
application Application) (s *session, err error) {
s = &session{sessionID: sessionID}
var validatorSettings = defaultValidatorSettings
if settings.HasSetting(config.ValidateFieldsOutOfOrder) {
if validatorSettings.CheckFieldsOutOfOrder, err = settings.BoolSetting(config.ValidateFieldsOutOfOrder); err != nil {
return
}
}
if sessionID.IsFIXT() {
if s.DefaultApplVerID, err = settings.Setting(config.DefaultApplVerID); err != nil {
return
}
if applVerID, ok := applVerIDLookup[s.DefaultApplVerID]; ok {
s.DefaultApplVerID = applVerID
}
//If the transport or app data dictionary setting is set, the other also needs to be set.
if settings.HasSetting(config.TransportDataDictionary) || settings.HasSetting(config.AppDataDictionary) {
var transportDataDictionaryPath, appDataDictionaryPath string
if transportDataDictionaryPath, err = settings.Setting(config.TransportDataDictionary); err != nil {
return
}
if appDataDictionaryPath, err = settings.Setting(config.AppDataDictionary); err != nil {
return
}
if s.transportDataDictionary, err = datadictionary.Parse(transportDataDictionaryPath); err != nil {
return
}
if s.appDataDictionary, err = datadictionary.Parse(appDataDictionaryPath); err != nil {
return
}
s.validator = &fixtValidator{s.transportDataDictionary, s.appDataDictionary, validatorSettings}
}
} else if settings.HasSetting(config.DataDictionary) {
var dataDictionaryPath string
if dataDictionaryPath, err = settings.Setting(config.DataDictionary); err != nil {
return
}
if s.appDataDictionary, err = datadictionary.Parse(dataDictionaryPath); err != nil {
return
}
s.validator = &fixValidator{s.appDataDictionary, validatorSettings}
}
if settings.HasSetting(config.ResetOnLogon) {
if s.ResetOnLogon, err = settings.BoolSetting(config.ResetOnLogon); err != nil {
return
}
}
if settings.HasSetting(config.RefreshOnLogon) {
if s.RefreshOnLogon, err = settings.BoolSetting(config.RefreshOnLogon); err != nil {
return
}
}
if settings.HasSetting(config.ResetOnLogout) {
if s.ResetOnLogout, err = settings.BoolSetting(config.ResetOnLogout); err != nil {
return
}
}
if settings.HasSetting(config.ResetOnDisconnect) {
if s.ResetOnDisconnect, err = settings.BoolSetting(config.ResetOnDisconnect); err != nil {
return
}
}
if settings.HasSetting(config.EnableLastMsgSeqNumProcessed) {
if s.EnableLastMsgSeqNumProcessed, err = settings.BoolSetting(config.EnableLastMsgSeqNumProcessed); err != nil {
return
}
}
if settings.HasSetting(config.CheckLatency) {
var doCheckLatency bool
if doCheckLatency, err = settings.BoolSetting(config.CheckLatency); err != nil {
return
}
s.SkipCheckLatency = !doCheckLatency
}
if !settings.HasSetting(config.MaxLatency) {
s.MaxLatency = 120 * time.Second
} else {
var maxLatency int
if maxLatency, err = settings.IntSetting(config.MaxLatency); err != nil {
return
}
if maxLatency <= 0 {
err = errors.New("MaxLatency must be a positive integer")
return
}
s.MaxLatency = time.Duration(maxLatency) * time.Second
}
if settings.HasSetting(config.ResendRequestChunkSize) {
if s.ResendRequestChunkSize, err = settings.IntSetting(config.ResendRequestChunkSize); err != nil {
return
}
}
if settings.HasSetting(config.StartTime) || settings.HasSetting(config.EndTime) {
var startTimeStr, endTimeStr string
if startTimeStr, err = settings.Setting(config.StartTime); err != nil {
return
}
if endTimeStr, err = settings.Setting(config.EndTime); err != nil {
return
}
var start, end internal.TimeOfDay
if start, err = internal.ParseTimeOfDay(startTimeStr); err != nil {
return
}
if end, err = internal.ParseTimeOfDay(endTimeStr); err != nil {
return
}
loc := time.UTC
if settings.HasSetting(config.TimeZone) {
var locStr string
if locStr, err = settings.Setting(config.TimeZone); err != nil {
return
}
loc, err = time.LoadLocation(locStr)
if err != nil {
return
}
}
if !settings.HasSetting(config.StartDay) && !settings.HasSetting(config.EndDay) {
s.SessionTime = internal.NewTimeRangeInLocation(start, end, loc)
} else {
var startDayStr, endDayStr string
if startDayStr, err = settings.Setting(config.StartDay); err != nil {
return
}
if endDayStr, err = settings.Setting(config.EndDay); err != nil {
return
}
parseDay := func(setting, dayStr string) (day time.Weekday, err error) {
day, ok := dayLookup[dayStr]
if !ok {
return day, IncorrectFormatForSetting{Setting: setting, Value: dayStr}
}
return
}
var startDay, endDay time.Weekday
if startDay, err = parseDay(config.StartDay, startDayStr); err != nil {
return
}
if endDay, err = parseDay(config.EndDay, endDayStr); err != nil {
return
}
s.SessionTime = internal.NewWeekRangeInLocation(start, end, startDay, endDay, loc)
}
}
if settings.HasSetting(config.TimeStampPrecision) {
var precisionStr string
if precisionStr, err = settings.Setting(config.TimeStampPrecision); err != nil {
return
}
switch precisionStr {
case "SECONDS":
s.timestampPrecision = Seconds
case "MILLIS":
s.timestampPrecision = Millis
case "MICROS":
s.timestampPrecision = Micros
case "NANOS":
s.timestampPrecision = Nanos
default:
err = IncorrectFormatForSetting{Setting: config.TimeStampPrecision, Value: precisionStr}
return
}
}
if settings.HasSetting(config.PersistMessages) {
var persistMessages bool
if persistMessages, err = settings.BoolSetting(config.PersistMessages); err != nil {
return
}
s.DisableMessagePersist = !persistMessages
}
if f.BuildInitiators {
if err = f.buildInitiatorSettings(s, settings); err != nil {
return
}
}
if s.log, err = logFactory.CreateSessionLog(s.sessionID); err != nil {
return
}
if s.store, err = storeFactory.Create(s.sessionID); err != nil {
return
}
s.sessionEvent = make(chan internal.Event)
s.messageEvent = make(chan bool, 1)
s.admin = make(chan interface{})
s.application = application
return
}
func (f sessionFactory) buildInitiatorSettings(session *session, settings *SessionSettings) error {
session.InitiateLogon = true
heartBtInt, err := settings.IntSetting(config.HeartBtInt)
if err != nil {
return err
}
if heartBtInt <= 0 {
return errors.New("Heartbeat must be greater than zero")
}
session.HeartBtInt = time.Duration(heartBtInt) * time.Second
session.ReconnectInterval = 30 * time.Second
if settings.HasSetting(config.ReconnectInterval) {
interval, err := settings.IntSetting(config.ReconnectInterval)
if err != nil {
return err
}
if interval <= 0 {
return errors.New("ReconnectInterval must be greater than zero")
}
session.ReconnectInterval = time.Duration(interval) * time.Second
}
return f.configureSocketConnectAddress(session, settings)
}
func (f sessionFactory) configureSocketConnectAddress(session *session, settings *SessionSettings) (err error) {
session.SocketConnectAddress = []string{}
var socketConnectHost, socketConnectPort string
for i := 0; ; {
hostConfig := config.SocketConnectHost
portConfig := config.SocketConnectPort
if i > 0 {
hostConfig = hostConfig + strconv.Itoa(i)
portConfig = portConfig + strconv.Itoa(i)
if !(settings.HasSetting(hostConfig) || settings.HasSetting(portConfig)) {
return
}
}
if socketConnectHost, err = settings.Setting(hostConfig); err != nil {
return
}
if socketConnectPort, err = settings.Setting(portConfig); err != nil {
return
}
session.SocketConnectAddress = append(session.SocketConnectAddress, net.JoinHostPort(socketConnectHost, socketConnectPort))
i++
}
}