forked from vuvuzela/vuvuzela
/
client.go
156 lines (131 loc) · 3.07 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
package main
import (
"fmt"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/websocket"
. "github.com/davidlazar/vuvuzela"
)
type Client struct {
sync.Mutex
EntryServer string
MyPublicKey *BoxKey
ws *websocket.Conn
roundHandlers map[uint32]ConvoHandler
convoHandler ConvoHandler
dialHandler DialHandler
}
type ConvoHandler interface {
NextConvoRequest(round uint32) *ConvoRequest
HandleConvoResponse(response *ConvoResponse)
}
type DialHandler interface {
NextDialRequest(round uint32, buckets uint32) *DialRequest
HandleDialBucket(db *DialBucket)
}
func NewClient(entryServer string, publicKey *BoxKey) *Client {
c := &Client{
EntryServer: entryServer,
MyPublicKey: publicKey,
roundHandlers: make(map[uint32]ConvoHandler),
}
return c
}
func (c *Client) SetConvoHandler(convo ConvoHandler) {
c.Lock()
c.convoHandler = convo
c.Unlock()
}
func (c *Client) SetDialHandler(dialer DialHandler) {
c.Lock()
c.dialHandler = dialer
c.Unlock()
}
func (c *Client) Connect() error {
// TODO check if already connected
if c.convoHandler == nil {
return fmt.Errorf("no convo handler")
}
if c.dialHandler == nil {
return fmt.Errorf("no dial handler")
}
wsaddr := fmt.Sprintf("%s/ws?publickey=%s", c.EntryServer, c.MyPublicKey.String())
dialer := &websocket.Dialer{
HandshakeTimeout: 5 * time.Second,
}
ws, _, err := dialer.Dial(wsaddr, nil)
if err != nil {
return err
}
c.ws = ws
go c.readLoop()
return nil
}
func (c *Client) Close() {
c.ws.Close()
}
func (c *Client) Send(v interface{}) {
const writeWait = 10 * time.Second
e, err := Envelop(v)
if err != nil {
log.WithFields(log.Fields{"bug": true, "call": "Envelop"}).Error(err)
return
}
c.Lock()
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
if err := c.ws.WriteJSON(e); err != nil {
log.WithFields(log.Fields{"call": "WriteJSON"}).Debug(err)
c.Unlock()
c.Close()
return
}
c.Unlock()
}
func (c *Client) readLoop() {
for {
var e Envelope
if err := c.ws.ReadJSON(&e); err != nil {
log.WithFields(log.Fields{"call": "ReadJSON"}).Debug(err)
c.Close()
break
}
v, err := e.Open()
if err != nil {
log.WithFields(log.Fields{"call": "Envelope.Open"}).Error(err)
continue
}
go c.handleResponse(v)
}
}
func (c *Client) handleResponse(v interface{}) {
switch v := v.(type) {
case *BadRequestError:
log.Printf("bad request error: %s", v.Error())
case *AnnounceConvoRound:
c.Send(c.nextConvoRequest(v.Round))
case *AnnounceDialRound:
c.Send(c.dialHandler.NextDialRequest(v.Round, v.Buckets))
case *ConvoResponse:
c.deliverConvoResponse(v)
case *DialBucket:
c.dialHandler.HandleDialBucket(v)
}
}
func (c *Client) nextConvoRequest(round uint32) *ConvoRequest {
c.Lock()
c.roundHandlers[round] = c.convoHandler
c.Unlock()
return c.convoHandler.NextConvoRequest(round)
}
func (c *Client) deliverConvoResponse(r *ConvoResponse) {
c.Lock()
convo, ok := c.roundHandlers[r.Round]
delete(c.roundHandlers, r.Round)
c.Unlock()
if !ok {
log.WithFields(log.Fields{"round": r.Round}).Error("round not found")
return
}
convo.HandleConvoResponse(r)
}