forked from perlin-network/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (58 loc) · 1.21 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
package main
import (
"bytes"
"context"
"fmt"
"github.com/perlin-network/noise"
"runtime"
"sync"
"sync/atomic"
"time"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
a, err := noise.NewNode()
check(err)
sent, received := uint32(0), uint32(0)
go func() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for range ticker.C {
fmt.Printf("Sent %d request(s), and received %d response(s) in the last second.\n",
atomic.SwapUint32(&sent, 0),
atomic.SwapUint32(&received, 0),
)
}
}()
a.Handle(func(ctx noise.HandlerContext) error {
check(ctx.Send([]byte("hello from alice!")))
atomic.AddUint32(&received, 1)
return nil
})
b, err := noise.NewNode()
check(err)
check(a.Listen())
check(b.Listen())
defer a.Close()
defer b.Close()
for {
var wg sync.WaitGroup
wg.Add(runtime.NumCPU())
for i := 0; i < runtime.NumCPU(); i++ {
go func() {
res, err := b.Request(context.TODO(), a.Addr(), []byte("hello from bob!"))
check(err)
if !bytes.Equal(res, []byte("hello from alice!")) {
check(fmt.Errorf("got unexpected response '%s'", string(res)))
}
atomic.AddUint32(&sent, 1)
wg.Done()
}()
}
wg.Wait()
}
}