github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/p2p/testing/peerpool.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:45</date> 10 //</624342661926490112> 11 12 13 package testing 14 15 import ( 16 "fmt" 17 "sync" 18 19 "github.com/ethereum/go-ethereum/log" 20 "github.com/ethereum/go-ethereum/p2p/discover" 21 ) 22 23 type TestPeer interface { 24 ID() discover.NodeID 25 Drop(error) 26 } 27 28 //testpeerpool是演示对等连接注册的示例对等池 29 type TestPeerPool struct { 30 lock sync.Mutex 31 peers map[discover.NodeID]TestPeer 32 } 33 34 func NewTestPeerPool() *TestPeerPool { 35 return &TestPeerPool{peers: make(map[discover.NodeID]TestPeer)} 36 } 37 38 func (p *TestPeerPool) Add(peer TestPeer) { 39 p.lock.Lock() 40 defer p.lock.Unlock() 41 log.Trace(fmt.Sprintf("pp add peer %v", peer.ID())) 42 p.peers[peer.ID()] = peer 43 44 } 45 46 func (p *TestPeerPool) Remove(peer TestPeer) { 47 p.lock.Lock() 48 defer p.lock.Unlock() 49 delete(p.peers, peer.ID()) 50 } 51 52 func (p *TestPeerPool) Has(id discover.NodeID) bool { 53 p.lock.Lock() 54 defer p.lock.Unlock() 55 _, ok := p.peers[id] 56 return ok 57 } 58 59 func (p *TestPeerPool) Get(id discover.NodeID) TestPeer { 60 p.lock.Lock() 61 defer p.lock.Unlock() 62 return p.peers[id] 63 } 64