-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
99 lines (79 loc) · 1.97 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/multiformats/go-multiaddr"
)
type Config struct {
Port int
ProtocolID string
Rendezvous string
Seed int64
DiscoveryPeers addrList
}
func main() {
config := Config{}
flag.StringVar(&config.Rendezvous, "rendezvous", "ldej/echo", "")
flag.Int64Var(&config.Seed, "seed", 0, "Seed value for generating a PeerID, 0 is random")
flag.Var(&config.DiscoveryPeers, "peer", "Peer multiaddress for peer discovery")
flag.StringVar(&config.ProtocolID, "protocolid", "/p2p/rpc/ldej", "")
flag.IntVar(&config.Port, "port", 0, "")
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
h, err := NewHost(ctx, config.Seed, config.Port)
if err != nil {
log.Fatal(err)
}
log.Printf("Host ID: %s", h.ID().Pretty())
log.Printf("Connect to me on:")
for _, addr := range h.Addrs() {
log.Printf(" %s/p2p/%s", addr, h.ID().Pretty())
}
dht, err := NewDHT(ctx, h, config.DiscoveryPeers)
if err != nil {
log.Fatal(err)
}
service := NewService(h, protocol.ID(config.ProtocolID))
err = service.SetupRPC()
if err != nil {
log.Fatal(err)
}
go Discover(ctx, h, dht, config.Rendezvous)
go service.StartMessaging(ctx)
run(h, cancel)
}
func run(h host.Host, cancel func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
<-c
fmt.Printf("\rExiting...\n")
cancel()
if err := h.Close(); err != nil {
panic(err)
}
os.Exit(0)
}
type addrList []multiaddr.Multiaddr
func (al *addrList) String() string {
strs := make([]string, len(*al))
for i, addr := range *al {
strs[i] = addr.String()
}
return strings.Join(strs, ",")
}
func (al *addrList) Set(value string) error {
addr, err := multiaddr.NewMultiaddr(value)
if err != nil {
return err
}
*al = append(*al, addr)
return nil
}