forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.go
More file actions
140 lines (129 loc) · 3.63 KB
/
net.go
File metadata and controls
140 lines (129 loc) · 3.63 KB
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
package client
import (
"context"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"github.com/textileio/powergate/iplocation"
n "github.com/textileio/powergate/net"
"github.com/textileio/powergate/net/rpc"
)
// Net provides the Net API.
type Net struct {
client rpc.RPCServiceClient
}
// ListenAddr returns listener address info for the local node.
func (net *Net) ListenAddr(ctx context.Context) (peer.AddrInfo, error) {
resp, err := net.client.ListenAddr(ctx, &rpc.ListenAddrRequest{})
if err != nil {
return peer.AddrInfo{}, err
}
addrs := make([]ma.Multiaddr, len(resp.AddrInfo.Addrs))
for i, addr := range resp.AddrInfo.Addrs {
ma, err := ma.NewMultiaddr(addr)
if err != nil {
return peer.AddrInfo{}, err
}
addrs[i] = ma
}
id, err := peer.Decode(resp.AddrInfo.Id)
if err != nil {
return peer.AddrInfo{}, err
}
return peer.AddrInfo{
ID: id,
Addrs: addrs,
}, nil
}
// Peers returns a list of peers.
func (net *Net) Peers(ctx context.Context) ([]n.PeerInfo, error) {
resp, err := net.client.Peers(ctx, &rpc.PeersRequest{})
if err != nil {
return nil, err
}
peerInfos := make([]n.PeerInfo, len(resp.Peers))
for i, p := range resp.Peers {
peerInfo, err := fromProtoPeerInfo(p)
if err != nil {
return nil, err
}
peerInfos[i] = peerInfo
}
return peerInfos, nil
}
// FindPeer finds a peer by peer id.
func (net *Net) FindPeer(ctx context.Context, peerID peer.ID) (n.PeerInfo, error) {
resp, err := net.client.FindPeer(ctx, &rpc.FindPeerRequest{PeerId: peerID.String()})
if err != nil {
return n.PeerInfo{}, err
}
return fromProtoPeerInfo(resp.PeerInfo)
}
// ConnectPeer connects to a peer.
func (net *Net) ConnectPeer(ctx context.Context, addrInfo peer.AddrInfo) error {
addrs := make([]string, len(addrInfo.Addrs))
for i, addr := range addrInfo.Addrs {
addrs[i] = addr.String()
}
info := &rpc.PeerAddrInfo{
Id: addrInfo.ID.String(),
Addrs: addrs,
}
_, err := net.client.ConnectPeer(ctx, &rpc.ConnectPeerRequest{PeerInfo: info})
return err
}
// DisconnectPeer disconnects from a peer.
func (net *Net) DisconnectPeer(ctx context.Context, peerID peer.ID) error {
_, err := net.client.DisconnectPeer(ctx, &rpc.DisconnectPeerRequest{PeerId: peerID.String()})
return err
}
// Connectedness returns the connection status to a peer.
func (net *Net) Connectedness(ctx context.Context, peerID peer.ID) (n.Connectedness, error) {
resp, err := net.client.Connectedness(ctx, &rpc.ConnectednessRequest{PeerId: peerID.String()})
if err != nil {
return n.Error, err
}
var con n.Connectedness
switch resp.Connectedness {
case rpc.Connectedness_CONNECTEDNESS_CAN_CONNECT:
con = n.CanConnect
case rpc.Connectedness_CONNECTEDNESS_CANNOT_CONNECT:
con = n.CannotConnect
case rpc.Connectedness_CONNECTEDNESS_CONNECTED:
con = n.Connected
case rpc.Connectedness_CONNECTEDNESS_NOT_CONNECTED:
con = n.NotConnected
case rpc.Connectedness_CONNECTEDNESS_ERROR:
con = n.Error
default:
con = n.Unspecified
}
return con, nil
}
func fromProtoPeerInfo(proto *rpc.PeerInfo) (n.PeerInfo, error) {
addrs := make([]ma.Multiaddr, len(proto.AddrInfo.Addrs))
for i, addr := range proto.AddrInfo.Addrs {
ma, err := ma.NewMultiaddr(addr)
if err != nil {
return n.PeerInfo{}, err
}
addrs[i] = ma
}
id, err := peer.Decode(proto.AddrInfo.Id)
if err != nil {
return n.PeerInfo{}, err
}
peerInfo := n.PeerInfo{
AddrInfo: peer.AddrInfo{
ID: id,
Addrs: addrs,
},
}
if proto.Location != nil {
peerInfo.Location = &iplocation.Location{
Country: proto.Location.Country,
Latitude: proto.Location.Latitude,
Longitude: proto.Location.Longitude,
}
}
return peerInfo, nil
}