-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise.go
117 lines (90 loc) · 2.61 KB
/
noise.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
108
109
110
111
112
113
114
115
116
117
package noise
import (
"context"
"fmt"
"strings"
"time"
"github.com/perlin-network/noise"
"github.com/perlin-network/noise/kademlia"
"go.uber.org/zap"
)
/*
*/
const printedLength = 8
func New(port uint16) {
logger, err := zap.NewDevelopment(zap.AddStacktrace(zap.PanicLevel))
defer logger.Sync()
log := logger.Sugar()
alice, err := noise.NewNode(noise.WithNodeLogger(logger),
noise.WithNodeBindPort(port))
if err != nil {
panic(err)
}
//node.RegisterMessage(chatMessage{}, unmarshalChatMessage)
events := kademlia.Events{
OnPeerAdmitted: func(id noise.ID) {
log.Info("Learned about a new peer %s(%s).\n", id.Address, id.ID.String()[:printedLength])
},
OnPeerEvicted: func(id noise.ID) {
log.Info("Forgotten a peer %s(%s).\n", id.Address, id.ID.String()[:printedLength])
},
}
overlay := kademlia.New(kademlia.WithProtocolEvents(events))
alice.Bind(overlay.Protocol())
alice.Handle(func(ctx noise.HandlerContext) error {
if !ctx.IsRequest() {
return nil
}
//obj, err := ctx.DecodeMessage()
//if err != nil {
// return nil
//}
//
//msg, ok := obj.(chatMessage)
//if !ok {
// return nil
//}
log.Debug("Got a message from Alice: '%s'\n", string(ctx.Data()))
return ctx.Send([]byte("Hi Alice!"))
})
err = alice.Listen()
if err != nil {
panic(err)
}
//res, err := alice.Request(context.TODO(), bob.Addr(), []byte("Hi Bob!"))
//check(err)
}
// bootstrap pings and dials an array of network addresses which we may interact with and discover peers from.
func bootstrap(node *noise.Node, addresses ...string) {
for _, addr := range addresses {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
_, err := node.Ping(ctx, addr)
cancel()
if err != nil {
fmt.Printf("Failed to ping bootstrap node (%s). Skipping... [error: %s]\n", addr, err)
continue
}
}
}
// discover uses Kademlia to discover new peers from nodes we already are aware of.
func discover(overlay *kademlia.Protocol) {
ids := overlay.Discover()
var str []string
for _, id := range ids {
str = append(str, fmt.Sprintf("%s(%s)", id.Address, id.ID.String()[:printedLength]))
}
if len(ids) > 0 {
fmt.Printf("Discovered %d peer(s): [%v]\n", len(ids), strings.Join(str, ", "))
} else {
fmt.Printf("Did not discover any peers.\n")
}
}
// peers prints out all peers we are already aware of.
func peers(overlay *kademlia.Protocol) {
ids := overlay.Table().Peers()
var str []string
for _, id := range ids {
str = append(str, fmt.Sprintf("%s(%s)", id.Address, id.ID.String()[:printedLength]))
}
fmt.Printf("You know %d peer(s): [%v]\n", len(ids), strings.Join(str, ", "))
}