forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptor.go
116 lines (98 loc) · 2.83 KB
/
acceptor.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
package quickfix
import (
"fmt"
"github.com/quickfixgo/quickfix/config"
"net"
"strconv"
)
//Acceptor accepts connections from FIX clients and manages the associated sessions.
type Acceptor struct {
app Application
settings *Settings
logFactory LogFactory
storeFactory MessageStoreFactory
globalLog Log
qualifiedSessionIDs map[SessionID]SessionID
quitChans map[int]chan bool
doneChannel chan int
}
//Start accepting connections.
func (a *Acceptor) Start() (e error) {
port, err := a.settings.GlobalSettings().IntSetting(config.SocketAcceptPort)
if err != nil {
return fmt.Errorf("error fetching required SocketAcceptPort: %v", err)
}
server, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if server == nil {
return err
}
connections := a.listenForConnections(server)
go func() {
var i = 0
for {
select {
case cxn := <-connections:
a.quitChans[i] = make(chan bool)
go handleAcceptorConnection(cxn, a.qualifiedSessionIDs, a.globalLog, a.quitChans[i], a.doneChannel, i)
i++
case j := <-a.doneChannel:
delete(a.quitChans, j)
}
}
}()
return
}
//Stop logs out existing sessions, close their connections, and stop accepting new connections.
func (a *Acceptor) Stop() {
defer func() {
_ = recover() // suppress sending on closed channel error
}()
for _, channel := range a.quitChans {
close(channel)
}
}
//NewAcceptor creates and initializes a new Acceptor.
func NewAcceptor(app Application, storeFactory MessageStoreFactory, settings *Settings, logFactory LogFactory) (*Acceptor, error) {
a := new(Acceptor)
a.app = app
a.storeFactory = storeFactory
a.settings = settings
a.logFactory = logFactory
a.qualifiedSessionIDs = make(map[SessionID]SessionID)
a.quitChans = make(map[int]chan bool)
a.doneChannel = make(chan int, 10)
var err error
a.globalLog, err = logFactory.Create()
if err != nil {
return a, err
}
for sessionID, sessionSettings := range settings.SessionSettings() {
//unqualified sessionIDs must be unique
unqualifiedSessionID := SessionID{
BeginString: sessionID.BeginString,
TargetCompID: sessionID.TargetCompID,
SenderCompID: sessionID.SenderCompID}
if _, dup := a.qualifiedSessionIDs[unqualifiedSessionID]; dup {
return nil, fmt.Errorf("duplicate SessionID %v", unqualifiedSessionID)
}
a.qualifiedSessionIDs[unqualifiedSessionID] = sessionID
if err = createSession(sessionID, storeFactory, sessionSettings, logFactory, app); err != nil {
return nil, err
}
}
return a, nil
}
func (a *Acceptor) listenForConnections(listener net.Listener) (ch chan net.Conn) {
ch = make(chan net.Conn)
go func() {
for {
netConn, err := listener.Accept()
if netConn == nil {
a.globalLog.OnEventf("Couldn't Accept: %v", err.Error())
continue
}
ch <- netConn
}
}()
return ch
}