-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbr_peers.go
175 lines (151 loc) · 3.84 KB
/
nbr_peers.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package peer
import (
"fmt"
"sync"
comm "github.com/dnaproject2/DNA/common"
"github.com/dnaproject2/DNA/p2pserver/common"
"github.com/dnaproject2/DNA/p2pserver/message/types"
)
//NbrPeers: The neigbor list
type NbrPeers struct {
sync.RWMutex
List map[uint64]*Peer
}
//Broadcast tranfer msg buffer to all establish peer
func (this *NbrPeers) Broadcast(msg types.Message) {
sink := comm.NewZeroCopySink(nil)
types.WriteMessage(sink, msg)
this.RLock()
defer this.RUnlock()
for _, node := range this.List {
if node.linkState == common.ESTABLISH && node.GetRelay() == true {
node.SendRaw(msg.CmdType(), sink.Bytes())
}
}
}
//NodeExisted return when peer in nbr list
func (this *NbrPeers) NodeExisted(uid uint64) bool {
_, ok := this.List[uid]
return ok
}
//GetPeer return peer according to id
func (this *NbrPeers) GetPeer(id uint64) *Peer {
this.Lock()
defer this.Unlock()
n, ok := this.List[id]
if ok == false {
return nil
}
return n
}
//AddNbrNode add peer to nbr list
func (this *NbrPeers) AddNbrNode(p *Peer) {
this.Lock()
defer this.Unlock()
if this.NodeExisted(p.GetID()) {
fmt.Printf("[p2p]insert an existed node\n")
} else {
this.List[p.GetID()] = p
}
}
//DelNbrNode delete peer from nbr list
func (this *NbrPeers) DelNbrNode(id uint64) (*Peer, bool) {
this.Lock()
defer this.Unlock()
n, ok := this.List[id]
if ok == false {
return nil, false
}
delete(this.List, id)
return n, true
}
//initialize nbr list
func (this *NbrPeers) Init() {
this.List = make(map[uint64]*Peer)
}
//NodeEstablished whether peer established according to id
func (this *NbrPeers) NodeEstablished(id uint64) bool {
this.RLock()
defer this.RUnlock()
n, ok := this.List[id]
if ok == false {
return false
}
if n.linkState != common.ESTABLISH {
return false
}
return true
}
//GetNeighborAddrs return all establish peer address
func (this *NbrPeers) GetNeighborAddrs() []common.PeerAddr {
this.RLock()
defer this.RUnlock()
var addrs []common.PeerAddr
for _, p := range this.List {
if p.GetState() != common.ESTABLISH {
continue
}
var addr common.PeerAddr
addr.IpAddr, _ = p.GetAddr16()
addr.Time = p.GetTimeStamp()
addr.Services = p.GetServices()
addr.Port = p.GetPort()
addr.ID = p.GetID()
addrs = append(addrs, addr)
}
return addrs
}
//GetNeighborHeights return the id-height map of nbr peers
func (this *NbrPeers) GetNeighborHeights() map[uint64]uint64 {
this.RLock()
defer this.RUnlock()
hm := make(map[uint64]uint64)
for _, n := range this.List {
if n.GetState() == common.ESTABLISH {
hm[n.GetID()] = n.GetHeight()
}
}
return hm
}
//GetNeighbors return all establish peers in nbr list
func (this *NbrPeers) GetNeighbors() []*Peer {
this.RLock()
defer this.RUnlock()
peers := []*Peer{}
for _, n := range this.List {
if n.GetState() == common.ESTABLISH {
node := n
peers = append(peers, node)
}
}
return peers
}
//GetNbrNodeCnt return count of establish peers in nbrlist
func (this *NbrPeers) GetNbrNodeCnt() uint32 {
this.RLock()
defer this.RUnlock()
var count uint32
for _, n := range this.List {
if n.GetState() == common.ESTABLISH {
count++
}
}
return count
}