forked from ashish10677/dht-implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (77 loc) · 2.05 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
"github.com/multiformats/go-multiaddr"
"time"
)
func CreateNode(externalIp string, port string) (host.Host, error) {
addressFactory, err := getAddressFactory(externalIp, port)
if err != nil {
return nil, err
}
node, err := libp2p.New(
libp2p.AddrsFactory(addressFactory))
if err != nil {
return nil, err
}
fmt.Printf("Node id: %s\n", node.ID().String())
fmt.Println("Connect on: ")
for _, addr := range node.Addrs() {
fmt.Printf(" %s/p2p/%s", addr, node.ID().String())
}
return node, nil
}
func getAddressFactory(externalIP string, port string) (func([]multiaddr.Multiaddr) []multiaddr.Multiaddr, error) {
var (
externalAddr multiaddr.Multiaddr
err error
)
// binding external IP to libp2p node
if len(externalIP) != 0 {
externalAddr, err = multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%s", externalIP, port))
if err != nil {
return nil, fmt.Errorf("fail to create listen with given external IP: %v", err)
}
fmt.Printf("Binding to external IP: %v", externalAddr.String())
}
addressFactory := func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
if externalAddr != nil {
return []multiaddr.Multiaddr{externalAddr}
}
return addrs
}
return addressFactory, nil
}
func main() {
var discoveryPeers addrList
var externalIp string
var port string
flag.Var(&discoveryPeers, "peer", "Peer multi address for peer discovery")
flag.StringVar(&externalIp, "externalIp", "", "Public IP address of user")
flag.StringVar(&port, "port", "", "Port of user")
flag.Parse()
node, err := CreateNode(externalIp, port)
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(120 * time.Second)
fmt.Println("Cancelling context now!")
cancel()
}()
routingDiscovery, err := Announce(ctx, node, discoveryPeers)
if err != nil {
panic(err)
}
Discover(ctx, node, routingDiscovery)
select {
case <-ctx.Done():
fmt.Println("Context cancelled!")
break
}
}