github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/peer/nbr_peers.go (about) 1 package peer 2 3 import ( 4 "github.com/sixexorg/magnetic-ring/log" 5 "sync" 6 7 comm "github.com/sixexorg/magnetic-ring/common" 8 "github.com/sixexorg/magnetic-ring/p2pserver/common" 9 "strings" 10 ) 11 12 //NbrPeers: The neigbor list 13 type NbrPeers struct { 14 sync.RWMutex 15 List map[uint64]*Peer 16 } 17 18 //Broadcast tranfer msg buffer to all establish peer 19 func (this *NbrPeers) Broadcast(msg common.Message, isConsensus bool) { 20 this.RLock() 21 defer this.RUnlock() 22 for _, node := range this.List { 23 if node.syncState == common.ESTABLISH && node.GetRelay() == true { 24 err := node.Send(msg, isConsensus) 25 if err != nil { 26 log.Error("an error occured when node.Send(msg, isConsensus)","error",err) 27 } 28 } 29 } 30 } 31 32 //DelNbrOrg ownnode del the org so nbnodes all del the org 33 func (this *NbrPeers) DelNbrOrg(orgid comm.Address) []uint64 { 34 this.RLock() 35 defer this.RUnlock() 36 delpeerid := make([]uint64,0) 37 for _, node := range this.List { 38 if node.DelRemoteOrg(orgid) { 39 delpeerid = append(delpeerid,node.GetID()) 40 } 41 } 42 return delpeerid 43 } 44 45 //NodeExisted return when peer in nbr list 46 func (this *NbrPeers) NodeExisted(uid uint64) bool { 47 _, ok := this.List[uid] 48 return ok 49 } 50 51 //GetPeer return peer according to id 52 func (this *NbrPeers) GetPeer(id uint64) *Peer { 53 this.Lock() 54 defer this.Unlock() 55 n, ok := this.List[id] 56 if ok == false { 57 return nil 58 } 59 return n 60 } 61 62 //GetPeer return peer according to discoverID 63 func (this *NbrPeers) GetPeerFromDiscoverNodeId(discoverNodeID string) *Peer { 64 this.Lock() 65 defer this.Unlock() 66 for intID , peer := range this.List { 67 peerNodeIDString := peer.node.ID.String() 68 if strings.EqualFold(discoverNodeID,peerNodeIDString){ 69 return this.List[intID] 70 } 71 72 } 73 return nil 74 } 75 76 //AddNbrNode add peer to nbr list 77 func (this *NbrPeers) AddNbrNode(p *Peer) { 78 this.Lock() 79 defer this.Unlock() 80 81 if this.NodeExisted(p.GetID()) { 82 //fmt.Printf("[p2p]insert an existed node\n") 83 } else { 84 this.List[p.GetID()] = p 85 } 86 } 87 88 //DelNbrNode delete peer from nbr list 89 func (this *NbrPeers) DelNbrNode(id uint64) (*Peer, bool) { 90 this.Lock() 91 defer this.Unlock() 92 93 n, ok := this.List[id] 94 if ok == false { 95 return nil, false 96 } 97 delete(this.List, id) 98 return n, true 99 } 100 101 //initialize nbr list 102 func (this *NbrPeers) Init() { 103 this.List = make(map[uint64]*Peer) 104 } 105 106 //NodeEstablished whether peer established according to id 107 func (this *NbrPeers) NodeEstablished(id uint64) bool { 108 this.RLock() 109 defer this.RUnlock() 110 111 n, ok := this.List[id] 112 if ok == false { 113 return false 114 } 115 116 if n.syncState != common.ESTABLISH { 117 return false 118 } 119 120 return true 121 } 122 123 //GetNeighborAddrs return all establish peer address 124 func (this *NbrPeers) GetNeighborAddrs() []common.PeerAddr { 125 this.RLock() 126 defer this.RUnlock() 127 128 var addrs []common.PeerAddr 129 for _, p := range this.List { 130 if p.GetSyncState() != common.ESTABLISH { 131 continue 132 } 133 var addr common.PeerAddr 134 addr.IpAddr, _ = p.GetAddr16() 135 addr.Time = uint64(p.GetTimeStamp()) 136 addr.Services = p.GetServices() 137 addr.Port = p.GetSyncPort() 138 addr.ID = p.GetID() 139 addr.Node = *(p.GetNode()) 140 addrs = append(addrs, addr) 141 } 142 143 return addrs 144 } 145 146 //GetNeighborHeights return the id-height map of nbr peers 147 func (this *NbrPeers) GetNeighborHeights() map[uint64]uint64 { 148 this.RLock() 149 defer this.RUnlock() 150 151 hm := make(map[uint64]uint64) 152 for _, n := range this.List { 153 if n.GetSyncState() == common.ESTABLISH { 154 hm[n.GetID()] = n.GetHeight() 155 } 156 } 157 return hm 158 } 159 160 //GetNeighbors return all establish peers in nbr list 161 func (this *NbrPeers) GetNeighbors() []*Peer { 162 this.RLock() 163 defer this.RUnlock() 164 peers := []*Peer{} 165 for _, n := range this.List { 166 if n.GetSyncState() == common.ESTABLISH { 167 node := n 168 peers = append(peers, node) 169 } 170 } 171 return peers 172 } 173 174 //GetNbrNodeCnt return count of establish peers in nbrlist 175 func (this *NbrPeers) GetNbrNodeCnt() uint32 { 176 this.RLock() 177 defer this.RUnlock() 178 var count uint32 179 for _, n := range this.List { 180 if n.GetSyncState() == common.ESTABLISH { 181 count++ 182 } 183 } 184 return count 185 }