/
discovery.go
53 lines (42 loc) · 1.31 KB
/
discovery.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
package p2pnet
import (
"context"
"log"
"github.com/libp2p/go-libp2p-host"
p2p "github.com/libp2p/go-libp2p-discovery"
libp2pdht "github.com/libp2p/go-libp2p-kad-dht"
pstore "github.com/libp2p/go-libp2p-peerstore"
)
//Router handles service advertising and discovery.
type Router struct {
dht *libp2pdht.IpfsDHT
dhtDiscovery *p2p.RoutingDiscovery
}
func initRouter(ctx context.Context, host host.Host) (*Router, error) {
d := &Router{}
var err error
//Create dht and discovery handles
d.dht, err = libp2pdht.New(ctx, host)
if err != nil {
log.Println("DHT initialization failed")
return nil, err
}
d.dhtDiscovery = p2p.NewRoutingDiscovery(d.dht)
// Bootstrap the DHT. In the default configuration, this spawns a Background
// thread that will refresh the peer table every five minutes.
err = d.dht.Bootstrap(ctx)
if err != nil {
log.Println("DHT bootstrap failed")
return nil, err
}
return d, nil
}
//Advertise a service to DHT
func (r *Router) Advertise(ctx context.Context, service string) {
p2p.Advertise(ctx, r.dhtDiscovery, service)
}
// FindPeers using DHT. Note that channel is not long standing.
// It will get closed after each peer search
func (r *Router) FindPeers(ctx context.Context, service string) (<-chan pstore.PeerInfo, error) {
return r.dhtDiscovery.FindPeers(ctx, service)
}