forked from libp2p/go-libp2p-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdns.go
35 lines (28 loc) · 891 Bytes
/
mdns.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
package main
import (
"context"
"time"
host "github.com/libp2p/go-libp2p-host"
pstore "github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p/p2p/discovery"
)
type discoveryNotifee struct {
PeerChan chan pstore.PeerInfo
}
//interface to be called when new peer is found
func (n *discoveryNotifee) HandlePeerFound(pi pstore.PeerInfo) {
n.PeerChan <- pi
}
//Initialize the MDNS service
func initMDNS(ctx context.Context, peerhost host.Host, rendezvous string) chan pstore.PeerInfo {
// An hour might be a long long period in practical applications. But this is fine for us
ser, err := discovery.NewMdnsService(ctx, peerhost, time.Hour, rendezvous)
if err != nil {
panic(err)
}
//register with service so that we get notified about peer discovery
n := &discoveryNotifee{}
n.PeerChan = make(chan pstore.PeerInfo)
ser.RegisterNotifee(n)
return n.PeerChan
}