-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpeer.go
More file actions
78 lines (65 loc) · 1.61 KB
/
peer.go
File metadata and controls
78 lines (65 loc) · 1.61 KB
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
package main
import (
"fmt"
"log"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
)
// Creating new p2p server
func newServer(name string, port int) (*p2p.Server, error) {
pkey, err := crypto.GenerateKey()
if err != nil {
log.Printf("Generate private key failed with err: %v", err)
return nil, err
}
cfg := p2p.Config{
PrivateKey: pkey,
Name: name,
MaxPeers: 1,
Protocols: []p2p.Protocol{proto},
EnableMsgEvents: true,
}
if port > 0 {
cfg.ListenAddr = fmt.Sprintf(":%d", port)
}
srv := &p2p.Server{
Config: cfg,
}
err = srv.Start()
if err != nil {
log.Printf("Start server failed with err: %v", err)
return nil, err
}
return srv, nil
}
func connectToPeer(srv *p2p.Server, enode string) error {
// Parsing the enode url
node, err := discover.ParseNode(enode)
if err != nil {
log.Printf("Failed to parse enode url with err: %v", err)
return err
}
// Connecting to the peer
srv.AddPeer(node)
return nil
}
func subscribeToEvents(srv *p2p.Server, communicated chan<- bool) {
// Subscribing to the peer events
peerEvent := make(chan *p2p.PeerEvent)
eventSub := srv.SubscribeEvents(peerEvent)
for {
select {
case event := <-peerEvent:
if event.Type == p2p.PeerEventTypeMsgRecv {
log.Println("Received message received notification")
communicated <- true
}
case <-eventSub.Err():
log.Println("subscription closed")
// Closing the channel so that server gets stopped since
// there won't be any more events coming in
close(communicated)
}
}
}