-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
231 lines (193 loc) · 4.4 KB
/
connection.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
// Go ddfplus API
//
// Copyright 2019 Barchart.com, Inc. All rights reserved.
//
// This Source Code Form is subject to the terms of the GNU license
// available at https://github.com/barchart/go-ddfplus-api/blob/master/LICENSE.
package ddf
import (
"bufio"
"fmt"
"io"
"log"
"net"
"strings"
)
const (
JerqVersion = 4
)
type Connection struct {
connected bool
credentials *Credentials
settings UserSettings
marketDepthChannels map[string][]chan Message
marketUpdateChannels map[string][]chan Message
marketUpdateAllChannels []chan Message
timestampChannels []chan MessageTimestamp
}
func (c *Connection) connect() {
}
func (c *Connection) buildRequest() string {
list := make(map[string]string)
for s := range c.marketUpdateChannels {
s2 := list[s]
s2 += "Ss"
list[s] = s2
}
for s := range c.marketDepthChannels {
s2 := list[s]
s2 += "Ss"
list[s] = s2
}
sb := strings.Builder{}
count := 0
for k, v := range list {
if count > 0 {
sb.WriteString(",")
}
sb.WriteString(k + "=" + v)
count++
}
return sb.String()
}
func (c *Connection) Server() {
}
func (c *Connection) RegisterMarketUpdateAll(ch chan Message) {
add := true
for i, _ := range c.marketUpdateAllChannels {
if c.marketUpdateAllChannels[i] == ch {
fmt.Println("DON'T ADD", ch)
add = false
break
}
}
if add {
c.marketUpdateAllChannels = append(c.marketUpdateAllChannels, ch)
}
}
func (c *Connection) RegisterMarketUpdate(symbols []string, ch chan Message) {
newlist := make([]string, 0)
for _, s := range symbols {
channels := c.marketUpdateChannels[s]
if channels == nil {
channels = make([]chan Message, 0)
newlist = append(newlist, s)
}
add := true
for i, _ := range channels {
if channels[i] == ch {
add = false
break
}
}
if add {
channels = append(channels, ch)
c.marketUpdateChannels[s] = channels
}
if c.connected {
// TO DO: Maybe we need to reset the connection
}
}
}
func (c *Connection) RegisterTimestamp(ch chan MessageTimestamp) {
add := true
for i, _ := range c.timestampChannels {
if c.timestampChannels[i] == ch {
add = false
break
}
}
if add {
c.timestampChannels = append(c.timestampChannels, ch)
}
}
func (c *Connection) Start() {
// Dial the tcp
conn, err := net.Dial("tcp", "qs01.ddfplus.com:7500")
if err != nil {
log.Printf("Error connecting. %v", err)
return
}
reader := bufio.NewReader(conn)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
s := string(line)
if s[0] == '+' {
break
}
}
fmt.Fprintf(conn, "LOGIN %s:%s\r\n", c.credentials.Username, c.credentials.Password)
line, _, err := reader.ReadLine()
if err != nil {
log.Printf("Network error. %v", err)
return
}
if line[0] != '+' {
log.Printf("Error logging in. Server said: \"%s\"", string(line))
return
}
fmt.Fprintf(conn, "VERSION %d\r\n", JerqVersion)
line, _, err = reader.ReadLine()
if err != nil {
log.Printf("Network error. %v", err)
return
}
command := c.buildRequest()
fmt.Printf("Sending \"%s\" to server.\n", command)
fmt.Fprintf(conn, "GO %s\r\n", command)
for {
line, _, _ := reader.ReadLine()
m, err := Parse(line)
if err == nil {
if m != nil {
switch m.Type() {
case Timestamp:
var ts MessageTimestamp
ts = m.(MessageTimestamp)
for _, ch := range c.timestampChannels {
ch <- ts
}
case BidAsk, Refresh, Trade:
var symbol string
switch m.Type() {
case BidAsk:
symbol = m.(MessageBidAsk).Symbol
case Refresh:
symbol = m.(MessageRefresh).Symbol
case Trade:
symbol = m.(MessageTrade).Symbol
}
if symbol != "" {
for _, ch := range c.marketUpdateAllChannels {
ch <- m
}
for _, ch := range c.marketUpdateChannels[symbol] {
ch <- m
}
}
}
}
} else {
log.Printf("Error parsing jerq data. %v", err)
}
}
conn.Close()
}
func NewConnection(credentials *Credentials) (*Connection, error) {
conn := &Connection{
credentials: credentials,
}
conn.marketDepthChannels = make(map[string][]chan Message)
conn.marketUpdateChannels = make(map[string][]chan Message)
conn.marketUpdateAllChannels = make([]chan Message, 0)
conn.timestampChannels = make([]chan MessageTimestamp, 0)
settings, err := GetUserSettings(credentials)
if err != nil {
return nil, err
}
conn.settings = settings
return conn, nil
}