From cb37a914c12e78bad6c620013988c0f082a796b7 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 29 May 2020 19:44:48 +1000 Subject: [PATCH] Rename Peer to PeerInfo, and unexport PeerInfos --- client.go | 6 +++--- cmd/torrent-pick/main.go | 4 ++-- cmd/torrent/main.go | 4 ++-- cmd/torrentfs/main.go | 2 +- Peer.go => peer_info.go | 10 +++++----- Peers.go => peer_infos.go | 11 ++++++----- peerid.go | 2 +- pexconn.go | 2 +- prioritized_peers.go | 12 ++++++------ prioritized_peers_test.go | 8 ++++---- t.go | 2 +- torrent.go | 22 +++++++++++----------- tracker_scraper.go | 2 +- 13 files changed, 44 insertions(+), 43 deletions(-) rename Peer.go => peer_info.go (81%) rename Peers.go => peer_infos.go (63%) diff --git a/client.go b/client.go index ebbf21b3..37a25ae2 100644 --- a/client.go +++ b/client.go @@ -1075,13 +1075,13 @@ func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) ( infoHash: ih, peers: prioritizedPeers{ om: btree.New(32), - getPrio: func(p Peer) peerPriority { + getPrio: func(p PeerInfo) peerPriority { return bep40PriorityIgnoreError(cl.publicAddr(addrIpOrNil(p.Addr)), p.addr()) }, }, conns: make(map[*PeerConn]struct{}, 2*cl.config.EstablishedConnsPerTorrent), - halfOpen: make(map[string]Peer), + halfOpen: make(map[string]PeerInfo), pieceStateChanges: pubsub.NewPubSub(), storageOpener: storageClient, @@ -1307,7 +1307,7 @@ func (cl *Client) onDHTAnnouncePeer(ih metainfo.Hash, ip net.IP, port int, portO if t == nil { return } - t.addPeers([]Peer{{ + t.addPeers([]PeerInfo{{ Addr: ipPortAddr{ip, port}, Source: PeerSourceDhtAnnouncePeer, }}) diff --git a/cmd/torrent-pick/main.go b/cmd/torrent-pick/main.go index 70f82f7c..e68121af 100644 --- a/cmd/torrent-pick/main.go +++ b/cmd/torrent-pick/main.go @@ -24,14 +24,14 @@ import ( // 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 { var addr *net.TCPAddr addr, err = net.ResolveTCPAddr("tcp", s) if err != nil { return } - ret = append(ret, torrent.Peer{ + ret = append(ret, torrent.PeerInfo{ Addr: addr, }) } diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index adc4f869..4ccbf3ea 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -113,9 +113,9 @@ func addTorrents(client *torrent.Client) error { if flags.Progress { torrentBar(t, flags.PieceStates) } - t.AddPeers(func() (ret []torrent.Peer) { + t.AddPeers(func() (ret []torrent.PeerInfo) { for _, ta := range flags.TestPeer { - ret = append(ret, torrent.Peer{ + ret = append(ret, torrent.PeerInfo{ Addr: ta, }) } diff --git a/cmd/torrentfs/main.go b/cmd/torrentfs/main.go index 172f6102..117457c1 100644 --- a/cmd/torrentfs/main.go +++ b/cmd/torrentfs/main.go @@ -61,7 +61,7 @@ func exitSignalHandlers(fs *torrentfs.TorrentFS) { func addTestPeer(client *torrent.Client) { for _, t := range client.Torrents() { - t.AddPeers([]torrent.Peer{{ + t.AddPeers([]torrent.PeerInfo{{ Addr: args.TestPeer, }}) } diff --git a/Peer.go b/peer_info.go similarity index 81% rename from Peer.go rename to peer_info.go index d62b028f..049ae152 100644 --- a/Peer.go +++ b/peer_info.go @@ -9,7 +9,7 @@ import ( ) // Peer connection info, handed about publicly. -type Peer struct { +type PeerInfo struct { Id [20]byte Addr net.Addr Source PeerSource @@ -20,7 +20,7 @@ type Peer struct { Trusted bool } -func (me Peer) Equal(other Peer) bool { +func (me PeerInfo) Equal(other PeerInfo) bool { return me.Id == other.Id && me.Addr.String() == other.Addr.String() && me.Source == other.Source && @@ -29,8 +29,8 @@ func (me Peer) Equal(other Peer) bool { me.Trusted == other.Trusted } -// FromPex generate Peer from peer exchange -func (me *Peer) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) { +// Generate PeerInfo from peer exchange +func (me *PeerInfo) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) { me.Addr = ipPortAddr{append([]byte(nil), na.IP...), na.Port} me.Source = PeerSourcePex // 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 } -func (me Peer) addr() IpPort { +func (me PeerInfo) addr() IpPort { return IpPort{IP: addrIpOrNil(me.Addr), Port: uint16(addrPortOrZero(me.Addr))} } diff --git a/Peers.go b/peer_infos.go similarity index 63% rename from Peers.go rename to peer_infos.go index a49247e2..f3da64e8 100644 --- a/Peers.go +++ b/peer_infos.go @@ -7,11 +7,12 @@ import ( "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 { - var p Peer + var p PeerInfo var f peer_protocol.PexPeerFlags if i < len(fs) { 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 { - _p := Peer{ + _p := PeerInfo{ Addr: ipPortAddr{p.IP, p.Port}, Source: PeerSourceTracker, } diff --git a/peerid.go b/peerid.go index 9bc94754..b689d304 100644 --- a/peerid.go +++ b/peerid.go @@ -3,7 +3,7 @@ package torrent // Peer client ID. 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. // func (me PeerID) String() string { // // if me[0] == '-' && me[7] == '-' { diff --git a/pexconn.go b/pexconn.go index e70ed105..dcb3136c 100644 --- a/pexconn.go +++ b/pexconn.go @@ -96,7 +96,7 @@ func (s *pexConnState) Recv(payload []byte) error { torrent.Add("pex added peers received", int64(len(rx.Added))) 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.Added, rx.AddedFlags) s.dbg.Printf("adding %d peers from PEX", len(peers)) diff --git a/prioritized_peers.go b/prioritized_peers.go index 0eddf161..d0ef43ed 100644 --- a/prioritized_peers.go +++ b/prioritized_peers.go @@ -11,7 +11,7 @@ import ( // change if our apparent IP changes, we don't currently handle that. type prioritizedPeersItem struct { prio peerPriority - p Peer + p PeerInfo } var hashSeed = maphash.MakeSeed() @@ -34,10 +34,10 @@ func (me prioritizedPeersItem) Less(than btree.Item) bool { type prioritizedPeers struct { 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 { f(i.(prioritizedPeersItem).p) return true @@ -49,12 +49,12 @@ func (me *prioritizedPeers) Len() int { } // 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 } // 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}) if item == nil { return @@ -74,6 +74,6 @@ func (me *prioritizedPeers) DeleteMin() (ret prioritizedPeersItem, ok bool) { return } -func (me *prioritizedPeers) PopMax() Peer { +func (me *prioritizedPeers) PopMax() PeerInfo { return me.om.DeleteMax().(prioritizedPeersItem).p } diff --git a/prioritized_peers_test.go b/prioritized_peers_test.go index c08c5a33..5e61c25f 100644 --- a/prioritized_peers_test.go +++ b/prioritized_peers_test.go @@ -11,14 +11,14 @@ import ( func TestPrioritizedPeers(t *testing.T) { pp := prioritizedPeers{ 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")}) }, } _, ok := pp.DeleteMin() assert.Panics(t, func() { pp.PopMax() }) assert.False(t, ok) - ps := []Peer{ + ps := []PeerInfo{ {Addr: ipPortAddr{IP: net.ParseIP("1.2.3.4")}}, {Addr: ipPortAddr{IP: net.ParseIP("1::2")}}, {Addr: ipPortAddr{IP: net.ParseIP("")}}, @@ -30,14 +30,14 @@ func TestPrioritizedPeers(t *testing.T) { assert.True(t, pp.Add(p)) assert.Equal(t, i+1, pp.Len()) } - pop := func(expected *Peer) { + pop := func(expected *PeerInfo) { if expected == nil { assert.Panics(t, func() { pp.PopMax() }) } else { assert.Equal(t, *expected, pp.PopMax()) } } - min := func(expected *Peer) { + min := func(expected *PeerInfo) { i, ok := pp.DeleteMin() if expected == nil { assert.False(t, ok) diff --git a/t.go b/t.go index a808cbf4..9d7ce285 100644 --- a/t.go +++ b/t.go @@ -221,7 +221,7 @@ func (t *Torrent) Files() []*File { return *t.files } -func (t *Torrent) AddPeers(pp []Peer) int { +func (t *Torrent) AddPeers(pp []PeerInfo) int { cl := t.cl cl.lock() defer cl.unlock() diff --git a/torrent.go b/torrent.go index 52c6124d..b106a286 100644 --- a/torrent.go +++ b/torrent.go @@ -83,7 +83,7 @@ type Torrent struct { maxEstablishedConns int // Set of addrs to which we're attempting to connect. Connections are // half-open until all handshakes are completed. - halfOpen map[string]Peer + halfOpen map[string]PeerInfo fastestConn *PeerConn // 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, // pending, and half-open peers. -func (t *Torrent) KnownSwarm() (ks []Peer) { +func (t *Torrent) KnownSwarm() (ks []PeerInfo) { // Add pending peers to the list - t.peers.Each(func(peer Peer) { + t.peers.Each(func(peer PeerInfo) { ks = append(ks, peer) }) @@ -195,7 +195,7 @@ func (t *Torrent) KnownSwarm() (ks []Peer) { // Add active peers to the list for conn := range t.conns { - ks = append(ks, Peer{ + ks = append(ks, PeerInfo{ Id: conn.PeerID, Addr: conn.remoteAddr, Source: conn.Discovery, @@ -254,7 +254,7 @@ func (t *Torrent) unclosedConnsAsSlice() (ret []*PeerConn) { return } -func (t *Torrent) addPeer(p Peer) (added bool) { +func (t *Torrent) addPeer(p PeerInfo) (added bool) { cl := t.cl torrent.Add(fmt.Sprintf("peers added by source %q", p.Source), 1) if t.closed.IsSet() { @@ -1451,7 +1451,7 @@ func (t *Torrent) consumeDhtAnnouncePeers(pvs <-chan dht.PeersValues) { // Can't do anything with this. continue } - t.addPeer(Peer{ + t.addPeer(PeerInfo{ Addr: ipPortAddr{cp.IP, cp.Port}, 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 { if t.addPeer(p) { added++ @@ -1552,7 +1552,7 @@ func (t *Torrent) numTotalPeers() int { for addr := range t.halfOpen { peers[addr] = struct{}{} } - t.peers.Each(func(peer Peer) { + t.peers.Each(func(peer PeerInfo) { peers[peer.Addr.String()] = struct{}{} }) 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. -func (t *Torrent) initiateConn(peer Peer) { +func (t *Torrent) initiateConn(peer PeerInfo) { if peer.Id == t.cl.peerID { 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 // quickly make one Client visible to the Torrent of another Client. func (t *Torrent) AddClientPeer(cl *Client) int { - return t.AddPeers(func() (ps []Peer) { + return t.AddPeers(func() (ps []PeerInfo) { for _, la := range cl.ListenAddrs() { - ps = append(ps, Peer{ + ps = append(ps, PeerInfo{ Addr: la, Trusted: true, }) diff --git a/tracker_scraper.go b/tracker_scraper.go index a7d3041c..13237953 100644 --- a/tracker_scraper.go +++ b/tracker_scraper.go @@ -132,7 +132,7 @@ func (me *trackerScraper) announce(event tracker.AnnounceEvent) (ret trackerAnno ret.Err = fmt.Errorf("error announcing: %s", err) return } - me.t.AddPeers(Peers(nil).AppendFromTracker(res.Peers)) + me.t.AddPeers(peerInfos(nil).AppendFromTracker(res.Peers)) ret.NumPeers = len(res.Peers) ret.Interval = time.Duration(res.Interval) * time.Second return