Update tests and fix a couple failures

This commit is contained in:
Justin Li
2014-07-17 00:41:59 -04:00
parent da19ed3e21
commit ed126dda29
5 changed files with 52 additions and 57 deletions
+28 -41
View File
@@ -82,10 +82,10 @@ func (t *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
conn.PurgeInactiveTorrent(torrent.Infohash)
}
return w.WriteAnnounce(newAnnounceResponse(ann, user, torrent))
return w.WriteAnnounce(newAnnounceResponse(ann, peer, torrent))
}
func updateTorrent(c Conn, a *models.Announce, p *models.Peer, t *models.Torrent) (created bool, err error) {
func updateTorrent(c Conn, ann *models.Announce, p *models.Peer, t *models.Torrent) (created bool, err error) {
c.TouchTorrent(t.Infohash)
switch {
@@ -104,7 +104,7 @@ func updateTorrent(c Conn, a *models.Announce, p *models.Peer, t *models.Torrent
t.Leechers[p.Key()] = *p
default:
if a.Left == 0 {
if ann.Left == 0 {
err = c.PutSeeder(t.Infohash, p)
if err != nil {
return
@@ -123,9 +123,9 @@ func updateTorrent(c Conn, a *models.Announce, p *models.Peer, t *models.Torrent
return
}
func handleEvent(c Conn, a *models.Announce, p *models.Peer, u *models.User, t *models.Torrent) (snatched bool, err error) {
func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t *models.Torrent) (snatched bool, err error) {
switch {
case a.Event == "stopped" || a.Event == "paused":
case ann.Event == "stopped" || ann.Event == "paused":
if t.InSeederPool(p) {
err = c.DeleteSeeder(t.Infohash, p.Key())
if err != nil {
@@ -140,7 +140,7 @@ func handleEvent(c Conn, a *models.Announce, p *models.Peer, u *models.User, t *
delete(t.Leechers, p.Key())
}
case a.Event == "completed":
case ann.Event == "completed":
err = c.IncrementSnatches(t.Infohash)
if err != nil {
return
@@ -155,7 +155,7 @@ func handleEvent(c Conn, a *models.Announce, p *models.Peer, u *models.User, t *
}
}
case t.InLeecherPool(p) && a.Left == 0:
case t.InLeecherPool(p) && ann.Left == 0:
// A leecher completed but the event was never received.
err = LeecherFinished(c, t.Infohash, p)
if err != nil {
@@ -166,44 +166,39 @@ func handleEvent(c Conn, a *models.Announce, p *models.Peer, u *models.User, t *
return
}
func newAnnounceResponse(a *models.Announce, u *models.User, t *models.Torrent) *AnnounceResponse {
func newAnnounceResponse(ann *models.Announce, announcer *models.Peer, t *models.Torrent) *AnnounceResponse {
seedCount := len(t.Seeders)
leechCount := len(t.Leechers)
var peerCount int
if a.Left == 0 {
peerCount = minInt(a.NumWant, leechCount)
} else {
peerCount = minInt(a.NumWant, leechCount+seedCount-1)
}
res := &AnnounceResponse{
Complete: seedCount,
Incomplete: leechCount,
Interval: a.Config.Announce.Duration,
MinInterval: a.Config.MinAnnounce.Duration,
Compact: a.Compact,
Interval: ann.Config.Announce.Duration,
MinInterval: ann.Config.MinAnnounce.Duration,
Compact: ann.Compact,
}
if a.NumWant > 0 && a.Event != "stopped" && a.Event != "paused" {
res.IPv4Peers, res.IPv6Peers = getPeers(a, u, t, peerCount)
if ann.NumWant > 0 && ann.Event != "stopped" && ann.Event != "paused" {
res.IPv4Peers, res.IPv6Peers = getPeers(ann, announcer, t, ann.NumWant)
}
return res
}
func getPeers(a *models.Announce, u *models.User, t *models.Torrent, wanted int) (ipv4s, ipv6s PeerList) {
if a.Left == 0 {
func getPeers(ann *models.Announce, announcer *models.Peer, t *models.Torrent, wanted int) (ipv4s, ipv6s PeerList) {
ipv4s, ipv6s = PeerList{}, PeerList{}
if ann.Left == 0 {
// If they're seeding, give them only leechers.
return appendPeers(nil, nil, u, t.Leechers, wanted)
} else {
// If they're leeching, prioritize giving them seeders.
ipv4s, ipv6s = appendPeers(nil, nil, u, t.Seeders, wanted)
return appendPeers(ipv4s, ipv6s, u, t.Leechers, wanted-len(ipv4s)-len(ipv6s))
return appendPeers(ipv4s, ipv6s, announcer, t.Leechers, wanted)
}
// If they're leeching, prioritize giving them seeders.
ipv4s, ipv6s = appendPeers(ipv4s, ipv6s, announcer, t.Seeders, wanted)
return appendPeers(ipv4s, ipv6s, announcer, t.Leechers, wanted-len(ipv4s)-len(ipv6s))
}
func appendPeers(ipv4s, ipv6s PeerList, u *models.User, peers map[string]models.Peer, wanted int) (PeerList, PeerList) {
func appendPeers(ipv4s, ipv6s PeerList, announcer *models.Peer, peers map[string]models.Peer, wanted int) (PeerList, PeerList) {
count := 0
for _, peer := range peers {
@@ -211,14 +206,14 @@ func appendPeers(ipv4s, ipv6s PeerList, u *models.User, peers map[string]models.
break
}
if u != nil && peer.UserID == u.ID {
if peer.ID == announcer.ID || peer.UserID != 0 && peer.UserID == announcer.UserID {
continue
}
if ip := peer.IP.To4(); len(ip) == 4 {
ipv4s = append(ipv4s, &peer)
} else if ip := peer.IP.To16(); len(ip) == 16 {
ipv6s = append(ipv6s, &peer)
if ip := peer.IP.To4(); ip != nil {
ipv4s = append(ipv4s, peer)
} else if ip := peer.IP.To16(); ip != nil {
ipv6s = append(ipv6s, peer)
}
count++
@@ -226,11 +221,3 @@ func appendPeers(ipv4s, ipv6s PeerList, u *models.User, peers map[string]models.
return ipv4s, ipv6s
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
+1 -1
View File
@@ -10,7 +10,7 @@ import (
"github.com/chihaya/chihaya/tracker/models"
)
type PeerList []*models.Peer
type PeerList []models.Peer
type AnnounceResponse struct {
Complete, Incomplete int