-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
78 lines (60 loc) · 1.36 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
package zeroless
type Address struct {
Ip string
Port int
}
type Client struct {
sock
addresses []Address
}
func NewClient() *Client {
client := Client{sock{}, make([]Address, 0)}
client.setInit(&client)
return &client
}
func (this Client) init(s socket) error {
var err error
for _, address := range this.Addresses() {
err = connectZmqSock(s, address.Ip, address.Port)
if err != nil {
return err
}
}
return nil
}
func (this Client) Addresses() []Address {
return this.addresses
}
func (this *Client) Connect(ip string, port int) error {
if !this.Ready() {
this.addresses = append(this.addresses, Address{ip, port})
return nil
}
return connectZmqSock(this.getZmqSock(), ip, port)
}
func (this *Client) ConnectLocal(port int) error {
return this.Connect("127.0.0.1", port)
}
func (this *Client) Disconnect(ip string, port int) error {
index := -1
var err error
for i, address := range this.addresses {
if address.Ip == ip && address.Port == port {
index = i
err = disconnectZmqSock(this.getZmqSock(), address.Ip, address.Port)
if err != nil {
return err
}
break
}
}
if index != -1 {
begin := this.addresses[:index]
end := this.addresses[(index + 1):]
this.addresses = append(begin, end...)
}
return nil
}
func (this *Client) DisconnectLocal(port int) error {
return this.Disconnect("127.0.0.1", port)
}