-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathv2_client.go
73 lines (60 loc) · 1.46 KB
/
v2_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
package votifier
import (
"bytes"
"encoding/json"
"errors"
"net"
"time"
)
// V2Client represents a Votifier v2 client.
type V2Client struct {
address string
token string
}
type v2Response struct {
Status string `json:'status'`
Cause string `json:'cause'`
Error string `json:'error'`
}
// NewV2Client creates a new Votifier v2 client.
func NewV2Client(address string, token string) *V2Client {
return &V2Client{address, token}
}
// SendVote sends a vote through the client.
func (client *V2Client) SendVote(vote Vote) error {
conn, err := net.DialTimeout("tcp", client.address, 3*time.Second)
if err != nil {
return err
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(3 * time.Second))
greeting := make([]byte, 64)
read, err := conn.Read(greeting)
if err != nil {
return err
}
parts := bytes.Split(greeting[:read-1], []byte(" "))
if len(parts) != 3 {
return errors.New("not a v2 server")
}
challenge := string(parts[2])
serialized, err := vote.serializev2(client.token, challenge)
if err != nil {
return err
}
_, err = conn.Write(*serialized)
// read response
responseBuf := make([]byte, 256)
read, err = conn.Read(responseBuf)
if err != nil {
return err
}
var response v2Response
if err := json.NewDecoder(bytes.NewBuffer(responseBuf[:read])).Decode(&response); err != nil {
return nil
}
if response.Status == "ok" {
return nil
}
return errors.New("remote server error: " + response.Cause + ": " + response.Error)
}