forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initiator.go
190 lines (161 loc) · 4.14 KB
/
initiator.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
package quickfix
import (
"bufio"
"crypto/tls"
"net"
"sync"
"time"
)
//Initiator initiates connections and processes messages for all sessions.
type Initiator struct {
app Application
settings *Settings
sessionSettings map[SessionID]*SessionSettings
storeFactory MessageStoreFactory
logFactory LogFactory
globalLog Log
stopChan chan interface{}
wg sync.WaitGroup
sessions map[SessionID]*session
sessionFactory
}
//Start Initiator.
func (i *Initiator) Start() (err error) {
i.stopChan = make(chan interface{})
for sessionID, settings := range i.sessionSettings {
//TODO: move into session factory
var tlsConfig *tls.Config
if tlsConfig, err = loadTLSConfig(settings); err != nil {
return
}
i.wg.Add(1)
go func(sessID SessionID) {
i.handleConnection(i.sessions[sessID], tlsConfig)
i.wg.Done()
}(sessionID)
}
return
}
//Stop Initiator.
func (i *Initiator) Stop() {
close(i.stopChan)
i.wg.Wait()
}
//NewInitiator creates and initializes a new Initiator.
func NewInitiator(app Application, storeFactory MessageStoreFactory, appSettings *Settings, logFactory LogFactory) (*Initiator, error) {
i := &Initiator{
app: app,
storeFactory: storeFactory,
settings: appSettings,
sessionSettings: appSettings.SessionSettings(),
logFactory: logFactory,
sessions: make(map[SessionID]*session),
sessionFactory: sessionFactory{true},
}
var err error
i.globalLog, err = logFactory.Create()
if err != nil {
return i, err
}
for sessionID, s := range i.sessionSettings {
session, err := i.createSession(sessionID, storeFactory, s, logFactory, app)
if err != nil {
return nil, err
}
i.sessions[sessionID] = session
}
return i, nil
}
//waitForInSessionTime returns true if the session is in session, false if the handler should stop
func (i *Initiator) waitForInSessionTime(session *session) bool {
inSessionTime := make(chan interface{})
go func() {
session.waitForInSessionTime()
close(inSessionTime)
}()
select {
case <-inSessionTime:
case <-i.stopChan:
return false
}
return true
}
//watiForReconnectInterval returns true if a reconnect should be re-attempted, false if handler should stop
func (i *Initiator) waitForReconnectInterval(reconnectInterval time.Duration) bool {
select {
case <-time.After(reconnectInterval):
case <-i.stopChan:
return false
}
return true
}
func (i *Initiator) handleConnection(session *session, tlsConfig *tls.Config) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
session.run()
wg.Done()
}()
defer func() {
session.stop()
wg.Wait()
}()
connectionAttempt := 0
for {
if !i.waitForInSessionTime(session) {
return
}
var disconnected chan interface{}
var msgIn chan fixIn
var msgOut chan []byte
address := session.SocketConnectAddress[connectionAttempt%len(session.SocketConnectAddress)]
session.log.OnEventf("Connecting to: %v", address)
var netConn net.Conn
if tlsConfig != nil {
tlsConn, err := tls.Dial("tcp", address, tlsConfig)
if err != nil {
session.log.OnEventf("Failed to connect: %v", err)
goto reconnect
}
err = tlsConn.Handshake()
if err != nil {
session.log.OnEventf("Failed handshake:%v", err)
goto reconnect
}
netConn = tlsConn
} else {
var err error
netConn, err = net.Dial("tcp", address)
if err != nil {
session.log.OnEventf("Failed to connect: %v", err)
goto reconnect
}
}
msgIn = make(chan fixIn)
msgOut = make(chan []byte)
if err := session.connect(msgIn, msgOut); err != nil {
session.log.OnEventf("Failed to initiate: %v", err)
goto reconnect
}
go readLoop(newParser(bufio.NewReader(netConn)), msgIn)
disconnected = make(chan interface{})
go func() {
writeLoop(netConn, msgOut, session.log)
if err := netConn.Close(); err != nil {
session.log.OnEvent(err.Error())
}
close(disconnected)
}()
select {
case <-disconnected:
case <-i.stopChan:
return
}
reconnect:
connectionAttempt++
session.log.OnEventf("Reconnecting in %v", session.ReconnectInterval)
if !i.waitForReconnectInterval(session.ReconnectInterval) {
return
}
}
}