-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
310 lines (266 loc) · 6.96 KB
/
main.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
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
type sysMsgType int
const (
currentProtocol sysMsgType = iota
registerClientAppCompleted
removeClientAppCompleted
currentLoginID
currentPassword
stats
)
type systemStat struct {
serverIP string
serverPort int
maxSymbols int
numberOfSymbols int
clientsConnected int
secondsSinceLastUpdate int
reconnections int
attemptedReconnections int
startTime string
marketTime string
status string
iqFeedVersion string
loginID string
totalKBsRecv float64
kbsPerSecRecv float64
avgKBsPerSecRecv float64
totalKBsSent float64
kbsPerSecSent float64
avgKBsPerSecSent float64
}
type sysMsg struct {
msgType sysMsgType
auxTxt string
}
func parseSystemStat(xs []string) (*systemStat, error) {
serverIP := xs[2]
serverPort, err := strconv.Atoi(xs[3])
if err != nil {
serverPort = 0
}
maxSymbols, err := strconv.Atoi(xs[4])
if err != nil {
return nil, err
}
numberOfSymbols, err := strconv.Atoi(xs[5])
if err != nil {
return nil, err
}
clientsConnected, err := strconv.Atoi(xs[6])
if err != nil {
return nil, err
}
secondsSinceLastUpdate, err := strconv.Atoi(xs[7])
if err != nil {
return nil, err
}
reconnections, err := strconv.Atoi(xs[8])
if err != nil {
return nil, err
}
attemptedReconnections, err := strconv.Atoi(xs[9])
if err != nil {
return nil, err
}
startTime := xs[10]
marketTime := xs[11]
status := xs[12]
iqFeedVersion := xs[13]
loginID := xs[14]
totalKBsRecv, err := strconv.ParseFloat(xs[15], 64)
if err != nil {
return nil, err
}
kbsPerSecRecv, err := strconv.ParseFloat(xs[16], 64)
if err != nil {
return nil, err
}
avgKBsPerSecRecv, err := strconv.ParseFloat(xs[17], 64)
if err != nil {
avgKBsPerSecRecv = 0
}
totalKBsSent, err := strconv.ParseFloat(xs[18], 64)
if err != nil {
return nil, err
}
kbsPerSecSent, err := strconv.ParseFloat(xs[19], 64)
if err != nil {
return nil, err
}
avgKBsPerSecSent, err := strconv.ParseFloat(xs[20], 64)
if err != nil {
avgKBsPerSecSent = 0
}
s := &systemStat{
serverIP, serverPort,
maxSymbols, numberOfSymbols, clientsConnected, secondsSinceLastUpdate,
reconnections, attemptedReconnections,
startTime, marketTime, status, iqFeedVersion, loginID,
totalKBsRecv, kbsPerSecRecv, avgKBsPerSecRecv,
totalKBsSent, kbsPerSecSent, avgKBsPerSecSent,
}
return s, nil
}
func admRecv(admConn net.Conn, sysMsgCh chan *sysMsg, statMsgCh chan *systemStat) {
reader := bufio.NewReader(admConn)
for {
str, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
xs := strings.Split(str, ",")
switch xs[1] {
case "CURRENT PROTOCOL":
sysMsgCh <- &sysMsg{currentProtocol, xs[2]}
case "REGISTER CLIENT APP COMPLETED":
sysMsgCh <- &sysMsg{registerClientAppCompleted, ""}
case "REMOVE CLIENT APP COMPLETED":
sysMsgCh <- &sysMsg{removeClientAppCompleted, ""}
case "CURRENT LOGINID":
sysMsgCh <- &sysMsg{currentLoginID, xs[2]}
case "CURRENT PASSWORD":
sysMsgCh <- &sysMsg{currentPassword, xs[2]}
case "STATS":
s, err := parseSystemStat(xs)
if err == nil {
statMsgCh <- s
} else {
log.Println(str)
}
default:
log.Println(str)
}
}
}
//type adminConn net.Conn
func setProtocolCmd(version string) []byte {
str := fmt.Sprintf("S,SET PROTOCOL,%s\r\n", version)
return []byte(str)
}
func setClientNameCmd(name string) []byte {
str := fmt.Sprintf("S,SET CLIENT NAME,%s\r\n", name)
return []byte(str)
}
func registerClientAppCmd(productID string, productVersion string) []byte {
str := fmt.Sprintf("S,REGISTER CLIENT APP,%s,%s\r\n", productID, productVersion)
return []byte(str)
}
func removeClientAppCmd(productID string, productVersion string) []byte {
str := fmt.Sprintf("S,REMOVE CLIENT APP,%s,%s\r\n", productID, productVersion)
return []byte(str)
}
func setLoginIDCmd(id string) []byte {
str := fmt.Sprintf("S,SET LOGINID,%s\r\n", id)
return []byte(str)
}
func setPasswordCmd(pw string) []byte {
str := fmt.Sprintf("S,SET PASSWORD,%s\r\n", pw)
return []byte(str)
}
func connectCmd() []byte {
str := "S,CONNECT\r\n"
return []byte(str)
}
func disconnectCmd() []byte {
str := "S,DISCONNECT\r\n"
return []byte(str)
}
func main() {
protocolVersion := "6.1"
ptclVer := os.Getenv("IQFEED_PTCLVER")
if ptclVer != "" {
protocolVersion = ptclVer
}
loginID := os.Getenv("IQFEED_LOGINID")
password := os.Getenv("IQFEED_PASSWD")
productID := os.Getenv("IQFEED_PRODID")
productVersion := os.Getenv("IQFEED_PRODVER")
shouldProxy := os.Getenv("IQFEED_PROXY") == "YES"
cmd := exec.Command("xvfb-run", "-s", "-noreset", "-a", "wine", "iqconnect.exe")
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
sysMsgCh := make(chan *sysMsg, 5)
systemStatCh := make(chan *systemStat, 10)
var admConn net.Conn
for i := 0; i < 50; i++ {
admConn, err = net.Dial("tcp", fmt.Sprintf("localhost:%d", 9300))
if err == nil {
break
} else {
time.Sleep(200 * time.Millisecond)
}
}
if err != nil {
log.Fatal(err)
}
go admRecv(admConn, sysMsgCh, systemStatCh)
srcHostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
dstHostname := "localhost"
if shouldProxy {
go proxy(srcHostname, 5009, dstHostname, 5009)
go proxy(srcHostname, 9100, dstHostname, 9100)
go proxy(srcHostname, 9200, dstHostname, 9200)
go proxy(srcHostname, 9300, dstHostname, 9300)
go proxy(srcHostname, 9400, dstHostname, 9400)
}
var sysMsg *sysMsg
admConn.Write(setProtocolCmd(protocolVersion))
sysMsg = <-sysMsgCh
if sysMsg.msgType != currentProtocol || sysMsg.auxTxt != protocolVersion {
log.Fatal("Unexpected response")
}
admConn.Write(setClientNameCmd("ADMIN_CONN"))
admConn.Write(registerClientAppCmd(productID, productVersion))
sysMsg = <-sysMsgCh
if sysMsg.msgType != registerClientAppCompleted {
log.Fatal("Unexpected response")
}
admConn.Write(setLoginIDCmd(loginID))
sysMsg = <-sysMsgCh
if sysMsg.msgType != currentLoginID || sysMsg.auxTxt != loginID {
log.Fatal("Unexpected response")
}
admConn.Write(setPasswordCmd(password))
sysMsg = <-sysMsgCh
if sysMsg.msgType != currentPassword {
log.Fatal("Unexpected response")
}
admConn.Write(connectCmd())
lastMarketTime := ""
for {
select {
case msg := <-sysMsgCh:
log.Println(msg)
case stat := <-systemStatCh:
if stat == nil {
log.Fatal("Unexpected response")
}
ox := "O"
if stat.status != "Connected" {
ox = "X"
}
if lastMarketTime != stat.marketTime {
log.Printf("[Status] Mkt time: %s Login ID: %s Client start time: %s\n", stat.marketTime, stat.loginID, stat.startTime)
lastMarketTime = stat.marketTime
}
log.Printf("[%s] Send: %.2f kB/s Recv: %.2f kB/s Sym: %d/%d #Conn: %d Reconn: %d/%d\n", ox, stat.kbsPerSecRecv, stat.kbsPerSecSent, stat.numberOfSymbols, stat.maxSymbols, stat.clientsConnected, stat.reconnections, stat.attemptedReconnections)
}
}
}