2018-04-04 15:59:28 +08:00
|
|
|
package torrent
|
|
|
|
|
2019-12-23 11:04:07 +08:00
|
|
|
import (
|
2020-04-16 09:59:31 +08:00
|
|
|
"hash/maphash"
|
|
|
|
|
2019-12-23 11:04:07 +08:00
|
|
|
"github.com/anacrolix/multiless"
|
|
|
|
"github.com/google/btree"
|
|
|
|
)
|
2018-04-04 15:59:28 +08:00
|
|
|
|
|
|
|
// Peers are stored with their priority at insertion. Their priority may
|
|
|
|
// change if our apparent IP changes, we don't currently handle that.
|
|
|
|
type prioritizedPeersItem struct {
|
|
|
|
prio peerPriority
|
2020-05-29 17:44:48 +08:00
|
|
|
p PeerInfo
|
2018-04-04 15:59:28 +08:00
|
|
|
}
|
|
|
|
|
2020-04-16 09:59:31 +08:00
|
|
|
var hashSeed = maphash.MakeSeed()
|
|
|
|
|
|
|
|
func (me prioritizedPeersItem) addrHash() int64 {
|
|
|
|
var h maphash.Hash
|
|
|
|
h.SetSeed(hashSeed)
|
|
|
|
h.WriteString(me.p.Addr.String())
|
|
|
|
return int64(h.Sum64())
|
|
|
|
}
|
|
|
|
|
2018-04-04 15:59:28 +08:00
|
|
|
func (me prioritizedPeersItem) Less(than btree.Item) bool {
|
2019-12-23 11:04:07 +08:00
|
|
|
other := than.(prioritizedPeersItem)
|
|
|
|
return multiless.New().Bool(
|
|
|
|
me.p.Trusted, other.p.Trusted).Uint32(
|
2020-04-16 09:59:31 +08:00
|
|
|
me.prio, other.prio).Int64(
|
|
|
|
me.addrHash(), other.addrHash(),
|
2019-12-23 11:04:07 +08:00
|
|
|
).Less()
|
2018-04-04 15:59:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type prioritizedPeers struct {
|
|
|
|
om *btree.BTree
|
2020-05-29 17:44:48 +08:00
|
|
|
getPrio func(PeerInfo) peerPriority
|
2018-04-04 15:59:28 +08:00
|
|
|
}
|
|
|
|
|
2020-05-29 17:44:48 +08:00
|
|
|
func (me *prioritizedPeers) Each(f func(PeerInfo)) {
|
2018-04-04 15:59:28 +08:00
|
|
|
me.om.Ascend(func(i btree.Item) bool {
|
|
|
|
f(i.(prioritizedPeersItem).p)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *prioritizedPeers) Len() int {
|
2023-02-19 10:24:25 +08:00
|
|
|
if me == nil || me.om == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2018-04-04 15:59:28 +08:00
|
|
|
return me.om.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if a peer is replaced.
|
2020-05-29 17:44:48 +08:00
|
|
|
func (me *prioritizedPeers) Add(p PeerInfo) bool {
|
2018-04-04 15:59:28 +08:00
|
|
|
return me.om.ReplaceOrInsert(prioritizedPeersItem{me.getPrio(p), p}) != nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 09:59:31 +08:00
|
|
|
// Returns true if a peer is replaced.
|
2020-05-29 17:44:48 +08:00
|
|
|
func (me *prioritizedPeers) AddReturningReplacedPeer(p PeerInfo) (ret PeerInfo, ok bool) {
|
2020-04-16 09:59:31 +08:00
|
|
|
item := me.om.ReplaceOrInsert(prioritizedPeersItem{me.getPrio(p), p})
|
|
|
|
if item == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret = item.(prioritizedPeersItem).p
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-14 19:44:03 +08:00
|
|
|
func (me *prioritizedPeers) DeleteMin() (ret prioritizedPeersItem, ok bool) {
|
|
|
|
i := me.om.DeleteMin()
|
|
|
|
if i == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret = i.(prioritizedPeersItem)
|
|
|
|
ok = true
|
|
|
|
return
|
2018-04-04 15:59:28 +08:00
|
|
|
}
|
|
|
|
|
2020-05-29 17:44:48 +08:00
|
|
|
func (me *prioritizedPeers) PopMax() PeerInfo {
|
2018-04-04 15:59:28 +08:00
|
|
|
return me.om.DeleteMax().(prioritizedPeersItem).p
|
|
|
|
}
|