forked from stith/gorelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
200 lines (161 loc) · 4.19 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
package relp
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"log"
"net"
"time"
)
var okResponse = []byte("200 OK")
type Client struct {
host string
port int
timeout time.Duration
offerData []byte
buffer *bytes.Buffer
connection net.Conn
reader *bufio.Reader
nextTxn int
WillWaitAck bool
secure bool
}
func NewClientConnection(host string, port int, timeout time.Duration, secure bool) (net.Conn, error) {
address := fmt.Sprintf("%s:%d", host, port)
var err error
var connection net.Conn
dialer := &net.Dialer{Timeout: timeout}
if secure {
connection, err = tls.DialWithDialer(dialer, "tcp", address, &tls.Config{})
} else {
connection, err = dialer.Dial("tcp", address)
}
if err != nil {
connection = FailedConn{}
}
return connection, err
}
// NewClientTimeout - Starts a new RELP client with Dial timeout set
func NewClientTimeout(host string, port int, timeout time.Duration, secure bool, offerData map[string]string) (client Client, err error) {
client.host = host
client.port = port
client.timeout = timeout
client.nextTxn = 2
client.WillWaitAck = true
client.offerData = offerDataBytes(offerData)
client.buffer = new(bytes.Buffer)
client.secure = secure
client.connection, err = NewClientConnection(host, port, timeout, secure)
client.reader = bufio.NewReader(client.connection)
if err != nil {
return client, err
}
err = client.Open()
// TODO: Parse the server's info/commands into the Client object
return client, err
}
// NewClient - Starts a new RELP client with Dial timeout set
func NewClient(host string, port int, secure bool, offerData map[string]string) (client Client, err error) {
return NewClientTimeout(host, port, 0, secure, offerData)
}
// SendMessage - Sends a message using the client's connection
func (c *Client) SendMessage(msg Message) error {
c.nextTxn = c.nextTxn + 1
if _, err := msg.send(c.buffer, c.connection); err != nil {
return err
}
if c.WillWaitAck {
return c.WaitAck(&msg)
} else {
return nil
}
}
func (c *Client) WaitAck(msg *Message) (err error) {
ack, err := readMessage(c.reader)
if err != nil {
return err
}
if ack.Command != "rsp" {
return fmt.Errorf("Response to txn %d was %s: %s", msg.Txn, ack.Command, ack.Data)
}
if ack.Txn != msg.Txn {
return fmt.Errorf("Response txn to %d was %d", msg.Txn, ack.Txn)
}
return nil
}
// SetDeadline - make the next operation timeout if not completed before the given time
func (c *Client) SetDeadline(t time.Time) error {
return c.connection.SetDeadline(t)
}
func (c *Client) SetReadDeadline(t time.Time) error {
return c.connection.SetReadDeadline(t)
}
func (c *Client) SetWriteDeadline(t time.Time) error {
return c.connection.SetWriteDeadline(t)
}
// Recreate - recreates connection
func (c *Client) Recreate() error {
var err error
c.connection.Close()
c.connection, err = NewClientConnection(c.host, c.port, c.timeout, c.secure)
c.reader = bufio.NewReader(c.connection)
c.Open()
if err != nil {
log.Println("Error recreating client", err)
}
return err
}
func (c *Client) Open() error {
offer := Message{
Txn: 1,
Command: "open",
Data: c.offerData,
}
if _, err := offer.send(c.buffer, c.connection); err != nil {
return err
}
offerResponse, err := readMessage(c.reader)
if err != nil {
return err
}
if !bytes.HasPrefix(offerResponse.Data, okResponse) {
return fmt.Errorf("Server responded to offer with: %s", offerResponse.Data)
} else {
return nil
}
}
// Close - Closes the connection gracefully
func (c Client) Close() (err error) {
closeMessage := Message{
Txn: c.nextTxn,
Command: "close",
}
_, err = closeMessage.send(c.buffer, c.connection)
return
}
func offerDataBytes(offerData map[string]string) []byte {
var buffer bytes.Buffer
buffer.WriteString(defaultOffer)
for k, v := range offerData {
buffer.WriteRune('\n')
buffer.WriteString(k)
buffer.WriteRune('=')
buffer.WriteString(v)
}
return buffer.Bytes()
}
func (c *Client) PackageString(msg string) Message {
return Message{
Txn: c.nextTxn,
Command: "syslog",
Data: []byte(msg),
}
}
func (c *Client) PackageBytes(data []byte) Message {
return Message{
Txn: c.nextTxn,
Command: "syslog",
Data: data,
}
}