store: added peer parameters to AnnouncePeers

This commit is contained in:
Leo Balduf 2016-04-12 12:06:32 -04:00
parent d453bf7a75
commit 38856e82be
4 changed files with 12 additions and 4 deletions

View file

@ -224,7 +224,7 @@ func (s *peerStore) CollectGarbage(cutoff time.Time) error {
return nil
}
func (s *peerStore) AnnouncePeers(infoHash chihaya.InfoHash, seeder bool, numWant int) (peers, peers6 []chihaya.Peer, err error) {
func (s *peerStore) AnnouncePeers(infoHash chihaya.InfoHash, seeder bool, numWant int, peer4, peer6 chihaya.Peer) (peers, peers6 []chihaya.Peer, err error) {
lkey := leechersKey(infoHash)
skey := seedersKey(infoHash)
shard := s.shards[s.shardIndex(infoHash)]
@ -271,8 +271,14 @@ func (s *peerStore) AnnouncePeers(infoHash chihaya.InfoHash, seeder bool, numWan
}
if p.IP.To4() == nil {
if p.Equal(&peer6) {
continue
}
peers6 = append(peers6, p.Peer)
} else {
if p.Equal(&peer4) {
continue
}
peers = append(peers, p.Peer)
}
numWant--

View file

@ -147,7 +147,7 @@ func TestPeerStoreAPI(t *testing.T) {
assert.Equal(t, 7, s.NumSeeders(hash))
assert.Equal(t, 3, s.NumLeechers(hash))
peers1, peers61, err := s.AnnouncePeers(hash, true, 5)
peers1, peers61, err := s.AnnouncePeers(hash, true, 5,peer,chihaya.Peer{})
assert.Nil(t, err)
assert.NotNil(t, peers1)
assert.NotNil(t, peers61)

View file

@ -27,12 +27,14 @@ func (f FailedToRetrievePeers) Error() string { return string(f) }
func responseAnnounceClient(next tracker.AnnounceHandler) tracker.AnnounceHandler {
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
storage := store.MustGetStore()
peer4 := chihaya.Peer{ID: req.PeerID, IP: req.IPv4, Port: req.Port}
peer6 := chihaya.Peer{ID: req.PeerID, IP: req.IPv6, Port: req.Port}
resp.MinInterval = cfg.MinAnnounceInterval
resp.Compact = req.Compact
resp.Complete = int32(storage.NumSeeders(req.InfoHash))
resp.Incomplete = int32(storage.NumLeechers(req.InfoHash))
resp.IPv4Peers, resp.IPv6Peers, err = storage.AnnouncePeers(req.InfoHash, req.Left == 0, int(req.NumWant))
resp.IPv4Peers, resp.IPv6Peers, err = storage.AnnouncePeers(req.InfoHash, req.Left == 0, int(req.NumWant), peer4, peer6)
if err != nil {
return err.(FailedToRetrievePeers)
}

View file

@ -37,7 +37,7 @@ type PeerStore interface {
// If it is false then seeders will be returned up until numWant or the
// available seeders, whichever is smaller. If the available seeders is less
// than numWant then peers are returned until numWant or they run out.
AnnouncePeers(infoHash chihaya.InfoHash, seeder bool, numWant int) (peers, peers6 []chihaya.Peer, err error)
AnnouncePeers(infoHash chihaya.InfoHash, seeder bool, numWant int, peer4, peer6 chihaya.Peer) (peers, peers6 []chihaya.Peer, err error)
// CollectGarbage deletes peers from the peerStore which are older than the
// cutoff time.
CollectGarbage(cutoff time.Time) error