Rename Peer to PeerInfo, and unexport PeerInfos
This commit is contained in:
parent
c7ea314de0
commit
cb37a914c1
|
@ -1075,13 +1075,13 @@ func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) (
|
||||||
infoHash: ih,
|
infoHash: ih,
|
||||||
peers: prioritizedPeers{
|
peers: prioritizedPeers{
|
||||||
om: btree.New(32),
|
om: btree.New(32),
|
||||||
getPrio: func(p Peer) peerPriority {
|
getPrio: func(p PeerInfo) peerPriority {
|
||||||
return bep40PriorityIgnoreError(cl.publicAddr(addrIpOrNil(p.Addr)), p.addr())
|
return bep40PriorityIgnoreError(cl.publicAddr(addrIpOrNil(p.Addr)), p.addr())
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
conns: make(map[*PeerConn]struct{}, 2*cl.config.EstablishedConnsPerTorrent),
|
conns: make(map[*PeerConn]struct{}, 2*cl.config.EstablishedConnsPerTorrent),
|
||||||
|
|
||||||
halfOpen: make(map[string]Peer),
|
halfOpen: make(map[string]PeerInfo),
|
||||||
pieceStateChanges: pubsub.NewPubSub(),
|
pieceStateChanges: pubsub.NewPubSub(),
|
||||||
|
|
||||||
storageOpener: storageClient,
|
storageOpener: storageClient,
|
||||||
|
@ -1307,7 +1307,7 @@ func (cl *Client) onDHTAnnouncePeer(ih metainfo.Hash, ip net.IP, port int, portO
|
||||||
if t == nil {
|
if t == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.addPeers([]Peer{{
|
t.addPeers([]PeerInfo{{
|
||||||
Addr: ipPortAddr{ip, port},
|
Addr: ipPortAddr{ip, port},
|
||||||
Source: PeerSourceDhtAnnouncePeer,
|
Source: PeerSourceDhtAnnouncePeer,
|
||||||
}})
|
}})
|
||||||
|
|
|
@ -24,14 +24,14 @@ import (
|
||||||
|
|
||||||
// fmt.Fprintf(os.Stderr, "Usage: %s \n", os.Args[0])
|
// fmt.Fprintf(os.Stderr, "Usage: %s \n", os.Args[0])
|
||||||
|
|
||||||
func resolvedPeerAddrs(ss []string) (ret []torrent.Peer, err error) {
|
func resolvedPeerAddrs(ss []string) (ret []torrent.PeerInfo, err error) {
|
||||||
for _, s := range ss {
|
for _, s := range ss {
|
||||||
var addr *net.TCPAddr
|
var addr *net.TCPAddr
|
||||||
addr, err = net.ResolveTCPAddr("tcp", s)
|
addr, err = net.ResolveTCPAddr("tcp", s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ret = append(ret, torrent.Peer{
|
ret = append(ret, torrent.PeerInfo{
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,9 +113,9 @@ func addTorrents(client *torrent.Client) error {
|
||||||
if flags.Progress {
|
if flags.Progress {
|
||||||
torrentBar(t, flags.PieceStates)
|
torrentBar(t, flags.PieceStates)
|
||||||
}
|
}
|
||||||
t.AddPeers(func() (ret []torrent.Peer) {
|
t.AddPeers(func() (ret []torrent.PeerInfo) {
|
||||||
for _, ta := range flags.TestPeer {
|
for _, ta := range flags.TestPeer {
|
||||||
ret = append(ret, torrent.Peer{
|
ret = append(ret, torrent.PeerInfo{
|
||||||
Addr: ta,
|
Addr: ta,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ func exitSignalHandlers(fs *torrentfs.TorrentFS) {
|
||||||
|
|
||||||
func addTestPeer(client *torrent.Client) {
|
func addTestPeer(client *torrent.Client) {
|
||||||
for _, t := range client.Torrents() {
|
for _, t := range client.Torrents() {
|
||||||
t.AddPeers([]torrent.Peer{{
|
t.AddPeers([]torrent.PeerInfo{{
|
||||||
Addr: args.TestPeer,
|
Addr: args.TestPeer,
|
||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Peer connection info, handed about publicly.
|
// Peer connection info, handed about publicly.
|
||||||
type Peer struct {
|
type PeerInfo struct {
|
||||||
Id [20]byte
|
Id [20]byte
|
||||||
Addr net.Addr
|
Addr net.Addr
|
||||||
Source PeerSource
|
Source PeerSource
|
||||||
|
@ -20,7 +20,7 @@ type Peer struct {
|
||||||
Trusted bool
|
Trusted bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Peer) Equal(other Peer) bool {
|
func (me PeerInfo) Equal(other PeerInfo) bool {
|
||||||
return me.Id == other.Id &&
|
return me.Id == other.Id &&
|
||||||
me.Addr.String() == other.Addr.String() &&
|
me.Addr.String() == other.Addr.String() &&
|
||||||
me.Source == other.Source &&
|
me.Source == other.Source &&
|
||||||
|
@ -29,8 +29,8 @@ func (me Peer) Equal(other Peer) bool {
|
||||||
me.Trusted == other.Trusted
|
me.Trusted == other.Trusted
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromPex generate Peer from peer exchange
|
// Generate PeerInfo from peer exchange
|
||||||
func (me *Peer) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
|
func (me *PeerInfo) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
|
||||||
me.Addr = ipPortAddr{append([]byte(nil), na.IP...), na.Port}
|
me.Addr = ipPortAddr{append([]byte(nil), na.IP...), na.Port}
|
||||||
me.Source = PeerSourcePex
|
me.Source = PeerSourcePex
|
||||||
// If they prefer encryption, they must support it.
|
// If they prefer encryption, they must support it.
|
||||||
|
@ -40,6 +40,6 @@ func (me *Peer) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
|
||||||
me.PexPeerFlags = fs
|
me.PexPeerFlags = fs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Peer) addr() IpPort {
|
func (me PeerInfo) addr() IpPort {
|
||||||
return IpPort{IP: addrIpOrNil(me.Addr), Port: uint16(addrPortOrZero(me.Addr))}
|
return IpPort{IP: addrIpOrNil(me.Addr), Port: uint16(addrPortOrZero(me.Addr))}
|
||||||
}
|
}
|
|
@ -7,11 +7,12 @@ import (
|
||||||
"github.com/anacrolix/torrent/tracker"
|
"github.com/anacrolix/torrent/tracker"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Peers []Peer
|
// Helper-type used to bulk-manage PeerInfos.
|
||||||
|
type peerInfos []PeerInfo
|
||||||
|
|
||||||
func (me *Peers) AppendFromPex(nas []krpc.NodeAddr, fs []peer_protocol.PexPeerFlags) {
|
func (me *peerInfos) AppendFromPex(nas []krpc.NodeAddr, fs []peer_protocol.PexPeerFlags) {
|
||||||
for i, na := range nas {
|
for i, na := range nas {
|
||||||
var p Peer
|
var p PeerInfo
|
||||||
var f peer_protocol.PexPeerFlags
|
var f peer_protocol.PexPeerFlags
|
||||||
if i < len(fs) {
|
if i < len(fs) {
|
||||||
f = fs[i]
|
f = fs[i]
|
||||||
|
@ -21,9 +22,9 @@ func (me *Peers) AppendFromPex(nas []krpc.NodeAddr, fs []peer_protocol.PexPeerFl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ret Peers) AppendFromTracker(ps []tracker.Peer) Peers {
|
func (ret peerInfos) AppendFromTracker(ps []tracker.Peer) peerInfos {
|
||||||
for _, p := range ps {
|
for _, p := range ps {
|
||||||
_p := Peer{
|
_p := PeerInfo{
|
||||||
Addr: ipPortAddr{p.IP, p.Port},
|
Addr: ipPortAddr{p.IP, p.Port},
|
||||||
Source: PeerSourceTracker,
|
Source: PeerSourceTracker,
|
||||||
}
|
}
|
|
@ -3,7 +3,7 @@ package torrent
|
||||||
// Peer client ID.
|
// Peer client ID.
|
||||||
type PeerID [20]byte
|
type PeerID [20]byte
|
||||||
|
|
||||||
// // Pretty prints the ID as hex, except parts that adher to the Peer ID
|
// // Pretty prints the ID as hex, except parts that adher to the PeerInfo ID
|
||||||
// // Conventions of BEP 20.
|
// // Conventions of BEP 20.
|
||||||
// func (me PeerID) String() string {
|
// func (me PeerID) String() string {
|
||||||
// // if me[0] == '-' && me[7] == '-' {
|
// // if me[0] == '-' && me[7] == '-' {
|
||||||
|
|
|
@ -96,7 +96,7 @@ func (s *pexConnState) Recv(payload []byte) error {
|
||||||
torrent.Add("pex added peers received", int64(len(rx.Added)))
|
torrent.Add("pex added peers received", int64(len(rx.Added)))
|
||||||
torrent.Add("pex added6 peers received", int64(len(rx.Added6)))
|
torrent.Add("pex added6 peers received", int64(len(rx.Added6)))
|
||||||
|
|
||||||
var peers Peers
|
var peers peerInfos
|
||||||
peers.AppendFromPex(rx.Added6, rx.Added6Flags)
|
peers.AppendFromPex(rx.Added6, rx.Added6Flags)
|
||||||
peers.AppendFromPex(rx.Added, rx.AddedFlags)
|
peers.AppendFromPex(rx.Added, rx.AddedFlags)
|
||||||
s.dbg.Printf("adding %d peers from PEX", len(peers))
|
s.dbg.Printf("adding %d peers from PEX", len(peers))
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
// change if our apparent IP changes, we don't currently handle that.
|
// change if our apparent IP changes, we don't currently handle that.
|
||||||
type prioritizedPeersItem struct {
|
type prioritizedPeersItem struct {
|
||||||
prio peerPriority
|
prio peerPriority
|
||||||
p Peer
|
p PeerInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
var hashSeed = maphash.MakeSeed()
|
var hashSeed = maphash.MakeSeed()
|
||||||
|
@ -34,10 +34,10 @@ func (me prioritizedPeersItem) Less(than btree.Item) bool {
|
||||||
|
|
||||||
type prioritizedPeers struct {
|
type prioritizedPeers struct {
|
||||||
om *btree.BTree
|
om *btree.BTree
|
||||||
getPrio func(Peer) peerPriority
|
getPrio func(PeerInfo) peerPriority
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *prioritizedPeers) Each(f func(Peer)) {
|
func (me *prioritizedPeers) Each(f func(PeerInfo)) {
|
||||||
me.om.Ascend(func(i btree.Item) bool {
|
me.om.Ascend(func(i btree.Item) bool {
|
||||||
f(i.(prioritizedPeersItem).p)
|
f(i.(prioritizedPeersItem).p)
|
||||||
return true
|
return true
|
||||||
|
@ -49,12 +49,12 @@ func (me *prioritizedPeers) Len() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if a peer is replaced.
|
// Returns true if a peer is replaced.
|
||||||
func (me *prioritizedPeers) Add(p Peer) bool {
|
func (me *prioritizedPeers) Add(p PeerInfo) bool {
|
||||||
return me.om.ReplaceOrInsert(prioritizedPeersItem{me.getPrio(p), p}) != nil
|
return me.om.ReplaceOrInsert(prioritizedPeersItem{me.getPrio(p), p}) != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if a peer is replaced.
|
// Returns true if a peer is replaced.
|
||||||
func (me *prioritizedPeers) AddReturningReplacedPeer(p Peer) (ret Peer, ok bool) {
|
func (me *prioritizedPeers) AddReturningReplacedPeer(p PeerInfo) (ret PeerInfo, ok bool) {
|
||||||
item := me.om.ReplaceOrInsert(prioritizedPeersItem{me.getPrio(p), p})
|
item := me.om.ReplaceOrInsert(prioritizedPeersItem{me.getPrio(p), p})
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return
|
return
|
||||||
|
@ -74,6 +74,6 @@ func (me *prioritizedPeers) DeleteMin() (ret prioritizedPeersItem, ok bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *prioritizedPeers) PopMax() Peer {
|
func (me *prioritizedPeers) PopMax() PeerInfo {
|
||||||
return me.om.DeleteMax().(prioritizedPeersItem).p
|
return me.om.DeleteMax().(prioritizedPeersItem).p
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,14 +11,14 @@ import (
|
||||||
func TestPrioritizedPeers(t *testing.T) {
|
func TestPrioritizedPeers(t *testing.T) {
|
||||||
pp := prioritizedPeers{
|
pp := prioritizedPeers{
|
||||||
om: btree.New(3),
|
om: btree.New(3),
|
||||||
getPrio: func(p Peer) peerPriority {
|
getPrio: func(p PeerInfo) peerPriority {
|
||||||
return bep40PriorityIgnoreError(p.addr(), IpPort{IP: net.ParseIP("0.0.0.0")})
|
return bep40PriorityIgnoreError(p.addr(), IpPort{IP: net.ParseIP("0.0.0.0")})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, ok := pp.DeleteMin()
|
_, ok := pp.DeleteMin()
|
||||||
assert.Panics(t, func() { pp.PopMax() })
|
assert.Panics(t, func() { pp.PopMax() })
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
ps := []Peer{
|
ps := []PeerInfo{
|
||||||
{Addr: ipPortAddr{IP: net.ParseIP("1.2.3.4")}},
|
{Addr: ipPortAddr{IP: net.ParseIP("1.2.3.4")}},
|
||||||
{Addr: ipPortAddr{IP: net.ParseIP("1::2")}},
|
{Addr: ipPortAddr{IP: net.ParseIP("1::2")}},
|
||||||
{Addr: ipPortAddr{IP: net.ParseIP("")}},
|
{Addr: ipPortAddr{IP: net.ParseIP("")}},
|
||||||
|
@ -30,14 +30,14 @@ func TestPrioritizedPeers(t *testing.T) {
|
||||||
assert.True(t, pp.Add(p))
|
assert.True(t, pp.Add(p))
|
||||||
assert.Equal(t, i+1, pp.Len())
|
assert.Equal(t, i+1, pp.Len())
|
||||||
}
|
}
|
||||||
pop := func(expected *Peer) {
|
pop := func(expected *PeerInfo) {
|
||||||
if expected == nil {
|
if expected == nil {
|
||||||
assert.Panics(t, func() { pp.PopMax() })
|
assert.Panics(t, func() { pp.PopMax() })
|
||||||
} else {
|
} else {
|
||||||
assert.Equal(t, *expected, pp.PopMax())
|
assert.Equal(t, *expected, pp.PopMax())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
min := func(expected *Peer) {
|
min := func(expected *PeerInfo) {
|
||||||
i, ok := pp.DeleteMin()
|
i, ok := pp.DeleteMin()
|
||||||
if expected == nil {
|
if expected == nil {
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
|
|
2
t.go
2
t.go
|
@ -221,7 +221,7 @@ func (t *Torrent) Files() []*File {
|
||||||
return *t.files
|
return *t.files
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) AddPeers(pp []Peer) int {
|
func (t *Torrent) AddPeers(pp []PeerInfo) int {
|
||||||
cl := t.cl
|
cl := t.cl
|
||||||
cl.lock()
|
cl.lock()
|
||||||
defer cl.unlock()
|
defer cl.unlock()
|
||||||
|
|
22
torrent.go
22
torrent.go
|
@ -83,7 +83,7 @@ type Torrent struct {
|
||||||
maxEstablishedConns int
|
maxEstablishedConns int
|
||||||
// Set of addrs to which we're attempting to connect. Connections are
|
// Set of addrs to which we're attempting to connect. Connections are
|
||||||
// half-open until all handshakes are completed.
|
// half-open until all handshakes are completed.
|
||||||
halfOpen map[string]Peer
|
halfOpen map[string]PeerInfo
|
||||||
fastestConn *PeerConn
|
fastestConn *PeerConn
|
||||||
|
|
||||||
// Reserve of peers to connect to. A peer can be both here and in the
|
// Reserve of peers to connect to. A peer can be both here and in the
|
||||||
|
@ -181,9 +181,9 @@ func (t *Torrent) Closed() <-chan struct{} {
|
||||||
|
|
||||||
// KnownSwarm returns the known subset of the peers in the Torrent's swarm, including active,
|
// KnownSwarm returns the known subset of the peers in the Torrent's swarm, including active,
|
||||||
// pending, and half-open peers.
|
// pending, and half-open peers.
|
||||||
func (t *Torrent) KnownSwarm() (ks []Peer) {
|
func (t *Torrent) KnownSwarm() (ks []PeerInfo) {
|
||||||
// Add pending peers to the list
|
// Add pending peers to the list
|
||||||
t.peers.Each(func(peer Peer) {
|
t.peers.Each(func(peer PeerInfo) {
|
||||||
ks = append(ks, peer)
|
ks = append(ks, peer)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -195,7 +195,7 @@ func (t *Torrent) KnownSwarm() (ks []Peer) {
|
||||||
// Add active peers to the list
|
// Add active peers to the list
|
||||||
for conn := range t.conns {
|
for conn := range t.conns {
|
||||||
|
|
||||||
ks = append(ks, Peer{
|
ks = append(ks, PeerInfo{
|
||||||
Id: conn.PeerID,
|
Id: conn.PeerID,
|
||||||
Addr: conn.remoteAddr,
|
Addr: conn.remoteAddr,
|
||||||
Source: conn.Discovery,
|
Source: conn.Discovery,
|
||||||
|
@ -254,7 +254,7 @@ func (t *Torrent) unclosedConnsAsSlice() (ret []*PeerConn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) addPeer(p Peer) (added bool) {
|
func (t *Torrent) addPeer(p PeerInfo) (added bool) {
|
||||||
cl := t.cl
|
cl := t.cl
|
||||||
torrent.Add(fmt.Sprintf("peers added by source %q", p.Source), 1)
|
torrent.Add(fmt.Sprintf("peers added by source %q", p.Source), 1)
|
||||||
if t.closed.IsSet() {
|
if t.closed.IsSet() {
|
||||||
|
@ -1451,7 +1451,7 @@ func (t *Torrent) consumeDhtAnnouncePeers(pvs <-chan dht.PeersValues) {
|
||||||
// Can't do anything with this.
|
// Can't do anything with this.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
t.addPeer(Peer{
|
t.addPeer(PeerInfo{
|
||||||
Addr: ipPortAddr{cp.IP, cp.Port},
|
Addr: ipPortAddr{cp.IP, cp.Port},
|
||||||
Source: PeerSourceDhtGetPeers,
|
Source: PeerSourceDhtGetPeers,
|
||||||
})
|
})
|
||||||
|
@ -1506,7 +1506,7 @@ func (t *Torrent) dhtAnnouncer(s DhtServer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) addPeers(peers []Peer) (added int) {
|
func (t *Torrent) addPeers(peers []PeerInfo) (added int) {
|
||||||
for _, p := range peers {
|
for _, p := range peers {
|
||||||
if t.addPeer(p) {
|
if t.addPeer(p) {
|
||||||
added++
|
added++
|
||||||
|
@ -1552,7 +1552,7 @@ func (t *Torrent) numTotalPeers() int {
|
||||||
for addr := range t.halfOpen {
|
for addr := range t.halfOpen {
|
||||||
peers[addr] = struct{}{}
|
peers[addr] = struct{}{}
|
||||||
}
|
}
|
||||||
t.peers.Each(func(peer Peer) {
|
t.peers.Each(func(peer PeerInfo) {
|
||||||
peers[peer.Addr.String()] = struct{}{}
|
peers[peer.Addr.String()] = struct{}{}
|
||||||
})
|
})
|
||||||
return len(peers)
|
return len(peers)
|
||||||
|
@ -1859,7 +1859,7 @@ func (t *Torrent) VerifyData() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the process of connecting to the given peer for the given torrent if appropriate.
|
// Start the process of connecting to the given peer for the given torrent if appropriate.
|
||||||
func (t *Torrent) initiateConn(peer Peer) {
|
func (t *Torrent) initiateConn(peer PeerInfo) {
|
||||||
if peer.Id == t.cl.peerID {
|
if peer.Id == t.cl.peerID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1878,9 +1878,9 @@ func (t *Torrent) initiateConn(peer Peer) {
|
||||||
// Adds a trusted, pending peer for each of the given Client's addresses. Typically used in tests to
|
// Adds a trusted, pending peer for each of the given Client's addresses. Typically used in tests to
|
||||||
// quickly make one Client visible to the Torrent of another Client.
|
// quickly make one Client visible to the Torrent of another Client.
|
||||||
func (t *Torrent) AddClientPeer(cl *Client) int {
|
func (t *Torrent) AddClientPeer(cl *Client) int {
|
||||||
return t.AddPeers(func() (ps []Peer) {
|
return t.AddPeers(func() (ps []PeerInfo) {
|
||||||
for _, la := range cl.ListenAddrs() {
|
for _, la := range cl.ListenAddrs() {
|
||||||
ps = append(ps, Peer{
|
ps = append(ps, PeerInfo{
|
||||||
Addr: la,
|
Addr: la,
|
||||||
Trusted: true,
|
Trusted: true,
|
||||||
})
|
})
|
||||||
|
|
|
@ -132,7 +132,7 @@ func (me *trackerScraper) announce(event tracker.AnnounceEvent) (ret trackerAnno
|
||||||
ret.Err = fmt.Errorf("error announcing: %s", err)
|
ret.Err = fmt.Errorf("error announcing: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
me.t.AddPeers(Peers(nil).AppendFromTracker(res.Peers))
|
me.t.AddPeers(peerInfos(nil).AppendFromTracker(res.Peers))
|
||||||
ret.NumPeers = len(res.Peers)
|
ret.NumPeers = len(res.Peers)
|
||||||
ret.Interval = time.Duration(res.Interval) * time.Second
|
ret.Interval = time.Duration(res.Interval) * time.Second
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue