/
discovery.go
107 lines (82 loc) · 2.73 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
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
package discovery
import (
"context"
"log"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
)
// DiscoveryInterval is how often we re-publish our mDNS records.
const DiscoveryInterval = time.Hour
// DiscoveryServiceTag is used in our mDNS advertisements to discover other chat peers.
const DiscoveryServiceTag = "p2pdb-example"
type Discovery interface {
// create a new libp2p Host that listens on a random TCP port
// Connect() (Host string)
Create(host string) (host.Host, error)
SetupDiscovery(host host.Host) error
}
type DiscoveryFactory struct {
h host.Host
}
const LISTEN_ADDRESS_STRINGS = "/ip4/0.0.0.0/tcp/0"
// discoveryNotifee gets notified when we find a new peer via mDNS discovery
type discoveryNotifee struct {
h host.Host
}
func (d *DiscoveryFactory) GetLocalPeerId() peer.ID {
return d.h.ID()
}
func (d *DiscoveryFactory) SetLocalPeerId(id peer.ID) {
}
func NewDiscoveryFactory() *DiscoveryFactory {
return &DiscoveryFactory{}
}
func CreatePeerKey(key string) (string, error) {
return key, nil
}
func GetPublicKey(key string) (string, error) {
return key, nil
}
func GetPrivateKey(key string) (string, error) {
return key, nil
}
// func (d *DiscoveryFactory) Start(publicKey string) (host.Host, error) {
// h, err := libp2p.New(libp2p.ListenAddrStrings(LISTEN_ADDRESS_STRINGS))
// return h, err
// }
// func (d *DiscoveryFactory) Connect() (Host string) (Discovery, error){
// return d,nil
// }
func (d *DiscoveryFactory) Create(host string) (host.Host, error) {
if host == "" {
host = LISTEN_ADDRESS_STRINGS
}
var options = libp2p.ListenAddrStrings(host)
h, err := libp2p.New(options)
d.h = h
return h, err
}
// setupDiscovery creates an mDNS discovery service and attaches it to the libp2p Host.
// This lets us automatically discover peers on the same LAN and connect to them.
func (d *DiscoveryFactory) SetupDiscovery(h host.Host) error {
// setup mDNS discovery to find local peers
s := mdns.NewMdnsService(h, DiscoveryServiceTag, &discoveryNotifee{h: h})
log.Printf("local peer id is %s\n", h.ID())
return s.Start()
}
// HandlePeerFound connects to peers discovered via mDNS. Once they're connected,
// the PubSub system will automatically start interacting with them if they also
// support PubSub.
func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
var localPeerId = n.h.ID()
if pi.ID.Pretty() != localPeerId.String() {
log.Printf("discovered new remote peer id %s\n", pi.ID.Pretty())
}
err := n.h.Connect(context.Background(), pi)
if pi.ID.Pretty() != localPeerId.String() && err != nil {
log.Printf("error connecting to remote peer id %s: %s\n", pi.ID.Pretty(), err)
}
}