-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathgrpc_client.go
193 lines (172 loc) · 3.87 KB
/
grpc_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
package goalgo
import (
"bytes"
"log"
"sync"
"time"
"github.com/vmihailenco/msgpack"
"errors"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var (
instance *Client
clientTimeout = time.Second * 5
once sync.Once
)
// Client RPC客户端对象
type Client struct {
conn *grpc.ClientConn
client RobotCtlClient
timeout time.Duration
}
func (c Client) Close() {
c.conn.Close()
}
func (c *Client) GetRobotExchangeInfo(uid string, id string) ([]*RobotExchangeInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
request := &RobotExchangeInfoRequest{
Uid: uid,
RobotId: id,
}
r, err := c.client.GetRobotExchangeInfo(ctx, request)
if err != nil {
log.Printf("Log: %v", err)
return nil, err
}
return r.GetExchanges(), nil
}
func (c *Client) GetRobotOptions(uid string, id string) ([]*RobotOption, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
request := &RobotOptionsRequest{
Uid: uid,
RobotId: id,
}
r, err := c.client.GetRobotOptions(ctx, request)
if err != nil {
log.Printf("Log: %v", err)
return nil, err
}
return r.GetOptions(), nil
}
func (c *Client) GetValue(key string) (Value, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
request := &GetValueRequest{
RobotId: id,
Key: key,
}
r, err := c.client.GetValue(ctx, request)
if err != nil {
log.Printf("Log: %v", err)
return Value{}, err
}
if string(r.Value) == "" {
return Value{}, nil
}
buff := bytes.NewBuffer(r.Value)
dec := msgpack.NewDecoder(buff)
var v Value
err = dec.Decode(&v)
if err != nil {
return Value{}, err
}
return v, nil
}
func (c *Client) SetValue(key string, value Value) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
writer := bytes.NewBuffer(nil)
enc := msgpack.NewEncoder(writer)
var err error
err = enc.Encode(&value)
if err != nil {
return err
}
data := writer.Bytes()
request := &SetValueRequest{
RobotId: id,
Key: key,
Value: data,
}
_, err = c.client.SetValue(ctx, request)
if err != nil {
log.Printf("Log: %v", err)
return err
}
return nil
}
func (c *Client) Log(sid int, id uint64, tm int64, level int32, message string) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
request := &LogRequest{
Sid: int32(sid),
Id: id,
Time: tm,
Level: level,
Message: message,
}
r, err := c.client.Log(ctx, request)
if err != nil {
log.Printf("Log: %v", err)
return err
}
if r.Success {
return nil
}
return errors.New(r.Message)
}
func (c *Client) UpdateStatus(robotID string, status RobotStatus) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
request := &UpdateStatusRequest{
RobotId: robotID,
Status: int32(status),
}
r, err := c.client.UpdateStatus(ctx, request)
if err != nil {
log.Printf("Log: %v", err)
return err
}
if r.Success {
return nil
}
return errors.New(r.Message)
}
func (c *Client) UpdateStat(name string, value []byte) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
request := &StatRequest{
RobotId: id,
Name: name,
Value: value,
}
r, err := c.client.UpdateStat(ctx, request)
if err != nil {
log.Printf("Log: %v", err)
return err
}
if r.Success {
return nil
}
return errors.New(r.Message)
}
// GetClient 获得RPC客户端对象
func GetClient() *Client {
once.Do(func() {
instance = newClient()
})
return instance
}
func newClient() *Client {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
//defer conn.Close()
c := NewRobotCtlClient(conn)
return &Client{client: c, conn: conn, timeout: clientTimeout}
}