forked from cryptogarageinc/quickfix-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initiator.go
85 lines (70 loc) · 2.13 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
package quickfix
import (
"fmt"
"github.com/quickfixgo/quickfix/config"
)
//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
quitChans map[SessionID]chan bool
}
//Start Initiator.
func (i *Initiator) Start() error {
for sessionID, s := range i.sessionSettings {
socketConnectHost, err := s.Setting(config.SocketConnectHost)
if err != nil {
return fmt.Errorf("error on SocketConnectHost: %v", err)
}
socketConnectPort, err := s.IntSetting(config.SocketConnectPort)
if err != nil {
return fmt.Errorf("error on SocketConnectPort: %v", err)
}
i.quitChans[sessionID] = make(chan bool)
address := fmt.Sprintf("%v:%v", socketConnectHost, socketConnectPort)
go handleInitiatorConnection(address, i.globalLog, sessionID, i.quitChans[sessionID])
}
return nil
}
//Stop Initiator.
func (i *Initiator) Stop() {
defer func() {
_ = recover() // suppress sending on closed channel error
}()
for _, channel := range i.quitChans {
close(channel)
}
}
//NewInitiator creates and initializes a new Initiator.
func NewInitiator(app Application, storeFactory MessageStoreFactory, appSettings *Settings, logFactory LogFactory) (*Initiator, error) {
i := new(Initiator)
i.app = app
i.storeFactory = storeFactory
i.settings = appSettings
i.sessionSettings = appSettings.SessionSettings()
i.logFactory = logFactory
i.quitChans = make(map[SessionID]chan bool)
var err error
i.globalLog, err = logFactory.Create()
if err != nil {
return i, err
}
for sessionID, s := range i.sessionSettings {
//fail fast
if ok := s.HasSetting(config.SocketConnectHost); !ok {
return nil, requiredConfigurationMissing(config.SocketConnectHost)
}
if ok := s.HasSetting(config.SocketConnectPort); !ok {
return nil, requiredConfigurationMissing(config.SocketConnectPort)
}
err = createSession(sessionID, storeFactory, s, logFactory, app)
if err != nil {
return nil, err
}
}
return i, nil
}