/
client.go
173 lines (146 loc) · 3.32 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
package client
import (
"bufio"
"context"
"errors"
"net"
"strings"
"time"
"github.com/minmax1996/aoimdb/api/msg_protocol"
"github.com/vmihailenco/msgpack"
)
type TcpClient struct {
connection net.Conn
reader *bufio.Reader
writer *bufio.Writer
}
func NewTcpClient(host string) (*TcpClient, error) {
connection, err := net.Dial("tcp", host)
if err != nil {
return nil, err
}
c := TcpClient{
connection: connection,
reader: bufio.NewReader(connection),
writer: bufio.NewWriter(connection),
}
return &c, nil
}
//Send sends command string to establised connection
func (c *TcpClient) Close() error {
return c.connection.Close()
}
//Send sends command string to establised connection
func (c *TcpClient) Send(name string, s ...string) error {
c.writer.WriteString(name + " " + strings.Join(s, " ") + "\n")
return c.writer.Flush()
}
func (c *TcpClient) AuthWithUserPassPair(user, pass string) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := c.Send("auth", user, pass); err != nil {
return err
}
// make error channel
ch := make(chan error, 1)
go func() {
//blocked read response
data, err := c.reader.ReadString('\n')
if err != nil {
ch <- err
return
}
var item msg_protocol.MsgPackRootMessage
err = msgpack.Unmarshal([]byte(data), &item)
if err != nil {
ch <- err
return
}
if item.AuthResponse == nil || item.AuthResponse.Message != "authenticated" {
ch <- errors.New("not authenticated")
return
}
ch <- nil
}()
//listen error channel or context.done
select {
case err := <-ch:
return err
case <-ctx.Done():
return ctx.Err()
}
}
func (c *TcpClient) Get(key string) (*msg_protocol.GetResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := c.Send("get", key); err != nil {
return nil, err
}
// make error channel
var resp *msg_protocol.GetResponse
ch := make(chan error, 1)
go func() {
//blocked read response
data, err := c.reader.ReadString('\n')
if err != nil {
ch <- err
return
}
var item msg_protocol.MsgPackRootMessage
err = msgpack.Unmarshal([]byte(data), &item)
if err != nil {
ch <- err
return
}
if item.GetResponse == nil {
ch <- errors.New("not response")
return
}
resp = item.GetResponse
ch <- nil
}()
//listen error channel or context.done
select {
case err := <-ch:
return resp, err
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (c *TcpClient) Set(key string, value string) (*msg_protocol.SetResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := c.Send("set", key, value); err != nil {
return nil, err
}
// make error channel
var resp *msg_protocol.SetResponse
ch := make(chan error, 1)
go func() {
//blocked read response
data, err := c.reader.ReadString('\n')
if err != nil {
ch <- err
return
}
var item msg_protocol.MsgPackRootMessage
err = msgpack.Unmarshal([]byte(data), &item)
if err != nil {
ch <- err
return
}
if item.SetResponse == nil {
ch <- errors.New("not response")
return
}
resp = item.SetResponse
ch <- nil
}()
//listen error channel or context.done
select {
case err := <-ch:
return resp, err
case <-ctx.Done():
return nil, ctx.Err()
}
}