tracker: Return announcing peer if there are no others to avoid an empty peers field

This commit is contained in:
Justin Li 2015-02-28 00:56:45 -05:00
parent ac2fcade1e
commit da46ef8c8a
3 changed files with 18 additions and 12 deletions

View file

@ -31,10 +31,10 @@ func TestPublicAnnounce(t *testing.T) {
peer3 := makePeerParams("peer3", false) peer3 := makePeerParams("peer3", false)
peer1["event"] = "started" peer1["event"] = "started"
expected := makeResponse(1, 0) expected := makeResponse(1, 0, peer1)
checkAnnounce(peer1, expected, srv, t) checkAnnounce(peer1, expected, srv, t)
expected = makeResponse(2, 0) expected = makeResponse(2, 0, peer2)
checkAnnounce(peer2, expected, srv, t) checkAnnounce(peer2, expected, srv, t)
expected = makeResponse(2, 1, peer1, peer2) expected = makeResponse(2, 1, peer1, peer2)
@ -147,7 +147,7 @@ func TestPrivateAnnounce(t *testing.T) {
peer2 := makePeerParams("-TR2820-peer2", false) peer2 := makePeerParams("-TR2820-peer2", false)
peer3 := makePeerParams("-TR2820-peer3", true) peer3 := makePeerParams("-TR2820-peer3", true)
expected := makeResponse(0, 1) expected := makeResponse(0, 1, peer1)
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1" srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
checkAnnounce(peer1, expected, srv, t) checkAnnounce(peer1, expected, srv, t)
@ -189,7 +189,7 @@ func TestPreferredSubnet(t *testing.T) {
peerD1 := makePeerParams("peerD1", false, "fc02::1") peerD1 := makePeerParams("peerD1", false, "fc02::1")
peerD2 := makePeerParams("peerD2", false, "fc02::2") peerD2 := makePeerParams("peerD2", false, "fc02::2")
expected := makeResponse(0, 1) expected := makeResponse(0, 1, peerA1)
checkAnnounce(peerA1, expected, srv, t) checkAnnounce(peerA1, expected, srv, t)
expected = makeResponse(0, 2, peerA1) expected = makeResponse(0, 2, peerA1)
@ -255,7 +255,7 @@ func TestCompactAnnounce(t *testing.T) {
peer3["compact"] = "1" peer3["compact"] = "1"
expected := makeResponse(0, 1) expected := makeResponse(0, 1)
expected["peers"] = "" expected["peers"] = compact
checkAnnounce(peer1, expected, srv, t) checkAnnounce(peer1, expected, srv, t)
expected = makeResponse(0, 2) expected = makeResponse(0, 2)

View file

@ -280,6 +280,10 @@ func newAnnounceResponse(ann *models.Announce) *models.AnnounceResponse {
if ann.NumWant > 0 && ann.Event != "stopped" && ann.Event != "paused" { if ann.NumWant > 0 && ann.Event != "stopped" && ann.Event != "paused" {
res.IPv4Peers, res.IPv6Peers = getPeers(ann) res.IPv4Peers, res.IPv6Peers = getPeers(ann)
if len(res.IPv4Peers)+len(res.IPv6Peers) == 0 {
models.AppendPeer(&res.IPv4Peers, &res.IPv6Peers, ann, ann.Peer)
}
} }
return res return res

View file

@ -159,7 +159,7 @@ func (pm *PeerMap) AppendPeers(ipv4s, ipv6s PeerList, ann *Announce, wanted int)
} else if peersEquivalent(&peer, ann.Peer) { } else if peersEquivalent(&peer, ann.Peer) {
continue continue
} else { } else {
appendPeer(&ipv4s, &ipv6s, ann, &peer, &count) count += AppendPeer(&ipv4s, &ipv6s, ann, &peer)
} }
} }
@ -174,7 +174,7 @@ func (pm *PeerMap) AppendPeers(ipv4s, ipv6s PeerList, ann *Announce, wanted int)
} else if peersEquivalent(&peer, ann.Peer) { } else if peersEquivalent(&peer, ann.Peer) {
continue continue
} else { } else {
appendPeer(&ipv4s, &ipv6s, ann, &peer, &count) count += AppendPeer(&ipv4s, &ipv6s, ann, &peer)
} }
} }
} }
@ -183,18 +183,20 @@ func (pm *PeerMap) AppendPeers(ipv4s, ipv6s PeerList, ann *Announce, wanted int)
return ipv4s, ipv6s return ipv4s, ipv6s
} }
// appendPeer adds a peer to its corresponding peerlist. // AppendPeer adds a peer to its corresponding peerlist.
func appendPeer(ipv4s, ipv6s *PeerList, ann *Announce, peer *Peer, count *int) { func AppendPeer(ipv4s, ipv6s *PeerList, ann *Announce, peer *Peer) int {
if ann.HasIPv6() && peer.HasIPv6() { if ann.HasIPv6() && peer.HasIPv6() {
*ipv6s = append(*ipv6s, *peer) *ipv6s = append(*ipv6s, *peer)
*count++ return 1
} else if ann.Config.RespectAF && ann.HasIPv4() && peer.HasIPv4() { } else if ann.Config.RespectAF && ann.HasIPv4() && peer.HasIPv4() {
*ipv4s = append(*ipv4s, *peer) *ipv4s = append(*ipv4s, *peer)
*count++ return 1
} else if !ann.Config.RespectAF && peer.HasIPv4() { } else if !ann.Config.RespectAF && peer.HasIPv4() {
*ipv4s = append(*ipv4s, *peer) *ipv4s = append(*ipv4s, *peer)
*count++ return 1
} }
return 0
} }
// peersEquivalent checks if two peers represent the same entity. // peersEquivalent checks if two peers represent the same entity.