-
Notifications
You must be signed in to change notification settings - Fork 211
/
example_rpc_test.go
66 lines (48 loc) · 1.46 KB
/
example_rpc_test.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
package noise_test
import (
"context"
"fmt"
"github.com/perlin-network/noise"
)
// This example demonstrates how to send/handle RPC requests across peers, how to listen for incoming peers, how
// to check if a message received is a request or not, how to reply to a RPC request, and how to cleanup node
// instances after you are done using them.
func Example_rPC() {
// Let there be nodes Alice and Bob.
alice, err := noise.NewNode()
if err != nil {
panic(err)
}
bob, err := noise.NewNode()
if err != nil {
panic(err)
}
// Gracefully release resources for Alice and Bob at the end of the example.
defer alice.Close()
defer bob.Close()
// When Bob gets a message from Alice, print it out and respond to Alice with 'Hi Alice!'
bob.Handle(func(ctx noise.HandlerContext) error {
if !ctx.IsRequest() {
return nil
}
fmt.Printf("Got a message from Alice: '%s'\n", string(ctx.Data()))
return ctx.Send([]byte("Hi Alice!"))
})
// Have Alice and Bob start listening for new peers.
if err := alice.Listen(); err != nil {
panic(err)
}
if err := bob.Listen(); err != nil {
panic(err)
}
// Have Alice send Bob a request with the message 'Hi Bob!'
res, err := alice.Request(context.TODO(), bob.Addr(), []byte("Hi Bob!"))
if err != nil {
panic(err)
}
// Print out the response Bob got from Alice.
fmt.Printf("Got a message from Bob: '%s'\n", string(res))
// Output:
// Got a message from Alice: 'Hi Bob!'
// Got a message from Bob: 'Hi Alice!'
}