forked from dahernan/gopherdiscovery
/
client.go
184 lines (151 loc) · 3.6 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
package gopherdiscovery
import (
"errors"
"log"
"strings"
"github.com/gdamore/mangos"
"github.com/gdamore/mangos/protocol/respondent"
"github.com/gdamore/mangos/protocol/sub"
"github.com/gdamore/mangos/transport/ipc"
"github.com/gdamore/mangos/transport/tcp"
"golang.org/x/net/context"
)
type DiscoveryClient struct {
// url for the survey heartbeat
// for example tcp://127.0.0.1:40007
urlServer string
// url for the Pub/Sub
// in this url you are going to get the changes on the set of nodes
// for example tcp://127.0.0.1:50007
urlPubSub string
// Service that needs to be discovered, for example for a web server could be
// http://192.168.1.1:8080
service string
ctx context.Context
cancel context.CancelFunc
sock mangos.Socket
subscriber *Subscriber
}
type Subscriber struct {
// url for the Pub/Sub
url string
ctx context.Context
sock mangos.Socket
changes chan []string
}
func Client(urlServer string, service string) (*DiscoveryClient, error) {
return ClientWithSub(urlServer, "", service)
}
func ClientWithSub(urlServer string, urlPubSub string, service string) (*DiscoveryClient, error) {
var sock mangos.Socket
var err error
var subscriber *Subscriber
ctx, cancel := context.WithCancel(context.Background())
if urlPubSub != "" {
subCtx, _ := context.WithCancel(ctx)
subscriber, err = NewSubscriber(subCtx, urlPubSub)
if err != nil {
return nil, err
}
}
sock, err = respondent.NewSocket()
if err != nil {
return nil, err
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
err = sock.Dial(urlServer)
if err != nil {
return nil, err
}
client := &DiscoveryClient{
urlServer: urlServer,
urlPubSub: urlPubSub,
service: service,
ctx: ctx,
cancel: cancel,
sock: sock,
subscriber: subscriber,
}
go client.run()
return client, nil
}
func (d *DiscoveryClient) Peers() (chan []string, error) {
if d.subscriber == nil {
return nil, errors.New("No subscribe url is provided to discover the Peers")
}
return d.subscriber.Changes(), nil
}
func (d *DiscoveryClient) Cancel() {
d.cancel()
}
func (d *DiscoveryClient) run() {
var err error
for {
_, err = d.sock.Recv()
if err != nil {
log.Println("DiscoveryClient: Cannot receive the SURVEY", err.Error())
} else {
select {
case <-d.ctx.Done():
return
default:
err = d.sock.Send([]byte(d.service))
if err != nil {
log.Println("DiscoveryClient: Cannot send the SURVEY response", err.Error())
}
}
}
}
}
func NewSubscriber(ctx context.Context, url string) (*Subscriber, error) {
var sock mangos.Socket
var err error
sock, err = sub.NewSocket()
if err != nil {
return nil, err
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
err = sock.Dial(url)
if err != nil {
return nil, err
}
// subscribes to everything
err = sock.SetOption(mangos.OptionSubscribe, []byte(""))
if err != nil {
return nil, err
}
subscriber := &Subscriber{
url: url,
ctx: ctx,
sock: sock,
changes: make(chan []string, 8),
}
go subscriber.run()
return subscriber, nil
}
func (s *Subscriber) Changes() chan []string {
return s.changes
}
func (s *Subscriber) run() {
var msg []byte
var err error
for {
select {
case <-s.ctx.Done():
close(s.changes)
return
default:
msg, err = s.sock.Recv()
if err != nil {
log.Println("DiscoveryClient: Cannot SUBSCRIBE to the changes", err.Error())
}
// non-blocking send to the channel, discards changes if the channel is not ready
select {
case s.changes <- strings.Split(string(msg), "|"):
default:
}
}
}
}