-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterface.go
153 lines (126 loc) · 3.99 KB
/
interface.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
package fix
import (
"github.com/quickfixgo/enum"
"github.com/quickfixgo/field"
"github.com/quickfixgo/quickfix"
"go.uber.org/zap"
)
/* IMPLEMENT quickfix.Application INTERFACE */
// OnCreate implemented as part of Application interface.
func (c *Client) OnCreate(quickfix.SessionID) {}
// OnLogon notification of a session successfully logging on.
func (c *Client) OnLogon(quickfix.SessionID) {
c.isConnected.Store(true)
c.l.Info("Logon successfully!")
}
// OnLogout notification of a session logging off or disconnecting.
func (c *Client) OnLogout(quickfix.SessionID) {
defer func() {
if err := recover(); err != nil {
c.l.Errorw("Recover from panic", "error", err)
}
}()
c.isConnected.Store(false)
c.l.Info("Logged out!")
for _, call := range c.pending {
call.done <- ErrClosed
close(call.done)
}
}
// ToAdmin notification of admin message being sent to target.
func (c *Client) ToAdmin(msg *quickfix.Message, _ quickfix.SessionID) {
msgType, err := msg.MsgType()
if err != nil {
c.l.Errorw("Failed to get msg type", "err", err)
return
}
c.l.Infow("ToAdmin message type", "data", msgType)
if enum.MsgType(msgType) == enum.MsgType_LOGON {
rawData := GetLogonRawData(c.privateKey, c.senderCompID, c.targetCompID, SendingTimeNow())
msg.Body.Set(field.NewRawDataLength(len(rawData)))
msg.Body.Set(field.NewRawData(rawData))
msg.Body.Set(field.NewUsername(c.apiKey))
msg.Body.Set(field.NewResetSeqNumFlag(true))
msg.Body.SetInt(tagMessageHandling, int(c.options.messageHandling))
msg.Body.SetInt(tagResponseMode, int(c.options.responseMode))
}
}
// ToApp notification of app message being sent to target.
func (c *Client) ToApp(msg *quickfix.Message, _ quickfix.SessionID) error {
c.l.Infow("Sending message to server", "msg", msg)
return nil
}
// FromAdmin notification of admin message being received from target.
func (c *Client) FromAdmin(msg *quickfix.Message, _ quickfix.SessionID) quickfix.MessageRejectError {
c.l.Infow("FromAdmin message", "msg", msg)
return nil
}
// FromApp notification of app message being received from target.
func (c *Client) FromApp(msg *quickfix.Message, s quickfix.SessionID) quickfix.MessageRejectError {
// Process message according to message type.
msgType, err := msg.MsgType()
if err != nil {
c.l.Errorw("Failed to get response message type", "error", err)
return err
}
c.handleSubscriptions(msgType, msg)
reqIDTag, err2 := getReqIDTagFromMsgType(enum.MsgType(msgType))
if err2 != nil {
c.l.Warnw("Could not get request ID tag", "msgType", msgType, "error", err2)
return nil
}
id, err := msg.Body.GetString(reqIDTag)
if err != nil {
c.l.Errorw("Failed to get request ID", "tag", reqIDTag, "error", err)
return err
}
c.mu.Lock()
call := c.pending[id]
delete(c.pending, id)
c.mu.Unlock()
if call != nil {
c.l.Infow(
"Matching response message",
"id_tag", reqIDTag,
"id", id,
"request", call.request,
"response", msg,
)
response, err2 := copyMessage(msg)
if err2 != nil {
c.l.Fatalw("Failed to copy response message", "error", err2)
}
call.response = response
call.done <- nil
close(call.done)
}
return nil
}
/* IMPLEMENT quickfix.Log INTERFACE */
type zapLog struct {
logger *zap.SugaredLogger
}
func (l *zapLog) OnIncoming(data []byte) {
l.logger.Infow("OnIncoming message", "data", string(data))
}
func (l *zapLog) OnOutgoing(data []byte) {
l.logger.Infow("OnOutgoing message", "data", string(data))
}
func (l *zapLog) OnEvent(data string) {
l.logger.Infow("OnEvent message", "data", data)
}
func (l *zapLog) OnEventf(data string, params ...interface{}) {
l.logger.Infow("OnEventf message", "data", data, "params", params)
}
type zapLogFactory struct {
logger *zap.SugaredLogger
}
func (f *zapLogFactory) Create() (quickfix.Log, error) {
return &zapLog{f.logger}, nil
}
func (f *zapLogFactory) CreateSessionLog(sessionID quickfix.SessionID) (quickfix.Log, error) {
return &zapLog{f.logger}, nil
}
func NewZapLogFactory(logger *zap.SugaredLogger) *zapLogFactory {
return &zapLogFactory{logger}
}