-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
335 lines (309 loc) · 6.52 KB
/
client.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package dmap
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strconv"
"strings"
)
type Client interface {
Dial() error
Close() error
Put(string, []byte) error
Get(string) ([]byte, error)
Delete(string) error
Size() (int, error)
Clear() error
}
type MapClient struct {
conn net.Conn
host string
port int
}
func (mc *MapClient) Close() error {
mc.conn.Write([]byte("CLOSE"))
return mc.conn.Close()
}
func (mc *MapClient) Put(key string, value []byte) error {
command := fmt.Sprintf("PUT %s %s", key, string(value[:]))
_, err := mc.conn.Write([]byte(command))
if err != nil {
return err
}
var buf [1024]byte
l, err := mc.conn.Read(buf[:])
if err != nil {
return nil
}
_, err = mc.parse(buf[:], l)
if err != nil {
return err
}
return nil
}
func (mc *MapClient) Get(key string) ([]byte, error) {
command := fmt.Sprintf("GET %s", key)
_, err := mc.conn.Write([]byte(command))
if err != nil {
return nil, err
}
var buf [1024]byte
l, err := mc.conn.Read(buf[:])
if err != nil {
return nil, nil
}
b, err := mc.parse(buf[:], l)
if err != nil {
return nil, err
}
return []byte(b), nil
}
func (mc *MapClient) Size() (int, error) {
_, err := mc.conn.Write([]byte("SIZE"))
if err != nil {
return 0, err
}
var buf [1024]byte
l, err := mc.conn.Read(buf[:])
if err != nil {
return 0, err
}
b, err := mc.parse(buf[:], l)
if err != nil {
return 0, err
}
i, err := strconv.Atoi(b)
if err != nil {
return -1, err
}
return i, nil
}
func (mc *MapClient) Clear() error {
_, err := mc.conn.Write([]byte("CLEAR"))
if err != nil {
return err
}
var buf [1024]byte
l, err := mc.conn.Read(buf[:])
if err != nil {
return err
}
_, err = mc.parse(buf[:], l)
if err != nil {
return err
}
return nil
}
func (mc *MapClient) Delete(key string) error {
command := fmt.Sprintf("DEL %s", key)
_, err := mc.conn.Write([]byte(command))
if err != nil {
return err
}
var buf [1024]byte
l, err := mc.conn.Read(buf[:])
if err != nil {
return nil
}
_, err = mc.parse(buf[:], l)
if err != nil {
return err
}
return nil
}
func (mc *MapClient) parse(buf []byte, length int) (string, error) {
res := string(buf[:length])
log.Println(res)
parts := strings.Split(res, "=")
if len(parts) != 2 {
return "", errors.New("Unexpected response: " + res)
}
if parts[0] != "OK" {
return "", errors.New(parts[1])
}
return parts[1], nil
}
type UDPMapClient struct {
MapClient
}
func NewUDPMapClient(host string, port int) *UDPMapClient {
uc := &UDPMapClient{
MapClient: MapClient{
host: host,
port: port,
},
}
return uc
}
func (uc *UDPMapClient) Dial() error {
conn, err := net.Dial("udp", fmt.Sprintf("%s:%d", uc.host, uc.port))
if err != nil {
return err
}
uc.conn = conn
return nil
}
type TCPMapClient struct {
MapClient
}
func NewTCPMapClient(host string, port int) *TCPMapClient {
tc := &TCPMapClient{
MapClient: MapClient{
host: host,
port: port,
},
}
return tc
}
func (uc *TCPMapClient) Dial() error {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", uc.host, uc.port))
if err != nil {
return err
}
uc.conn = conn
return nil
}
type HTTPMapClient struct {
MapClient
client *http.Client
}
func NewHTTPMapClient(host string, port int) *HTTPMapClient {
client := &http.Client{}
hc := &HTTPMapClient{
client: client,
MapClient: MapClient{
host: host,
port: port,
},
}
return hc
}
func (hc *HTTPMapClient) Dial() error {
// nothing to do, as per HTTP client semantic
return nil
}
func (hc *HTTPMapClient) Close() error {
// nothing to do, as per HTTP client semantic
return nil
}
func (hc *HTTPMapClient) Put(key string, value []byte) error {
url := fmt.Sprintf("http://%s:%d/api/v1/map", hc.host, hc.port)
body := fmt.Sprintf("{ \"key\": \"%s\", \"value\": \"%s\" }", key, string(value))
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(body)))
if err != nil {
return nil
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := hc.client.Do(req)
if err != nil {
return err
}
json, err := hc.parseBody(resp)
if err != nil {
return err
}
if json["outcome"].(string) == "KO" {
return errors.New(json["error"].(string))
}
return nil
}
func (hc *HTTPMapClient) Get(key string) ([]byte, error) {
url := fmt.Sprintf("http://%s:%d/api/v1/map?key=%s", hc.host, hc.port, key)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := hc.client.Do(req)
if err != nil {
return nil, err
}
json, err := hc.parseBody(resp)
if err != nil {
return nil, err
}
if json["outcome"].(string) == "KO" {
return []byte(""), nil
}
return []byte(json["value"].(string)), nil
}
func (hc *HTTPMapClient) Delete(key string) error {
url := fmt.Sprintf("http://%s:%d/api/v1/map?key=%s", hc.host, hc.port, key)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := hc.client.Do(req)
if err != nil {
return err
}
_, err = hc.parseBody(resp)
if err != nil {
return err
}
return nil
}
func (hc *HTTPMapClient) Clear() error {
url := fmt.Sprintf("http://%s:%d/api/v1/map?key=*", hc.host, hc.port)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := hc.client.Do(req)
if err != nil {
return err
}
_, err = hc.parseBody(resp)
if err != nil {
return err
}
return nil
}
func (hc *HTTPMapClient) Size() (int, error) {
url := fmt.Sprintf("http://%s:%d/api/v1/map?key=*", hc.host, hc.port)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return -1, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := hc.client.Do(req)
if err != nil {
return -1, err
}
json, err := hc.parseBody(resp)
if err != nil {
return -1, err
}
if json["outcome"].(string) == "KO" {
return -1, nil
}
return int(json["size"].(float64)), nil
}
func (hc *HTTPMapClient) parseBody(res *http.Response) (map[string]interface{}, error) {
if res.StatusCode != 200 {
return nil, errors.New(res.Status)
}
c, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var j map[string]interface{}
err = json.Unmarshal(c, &j)
if err != nil {
return nil, err
}
log.Println(j)
return j, nil
}