add helper methods to get IPv4 and IPv6 peer

This commit is contained in:
Leo Balduf 2016-04-18 15:49:36 -04:00
parent 468eefee57
commit 9f229c4ab6
2 changed files with 24 additions and 4 deletions

View file

@ -34,6 +34,28 @@ type AnnounceRequest struct {
Params Params Params Params
} }
// Peer4 returns a Peer using the IPv4 endpoint of the Announce.
// Note that, if the Announce does not contain an IPv4 address, the IP field of
// the returned Peer can be nil.
func (r *AnnounceRequest) Peer4() Peer {
return Peer{
IP: r.IPv4,
Port: r.Port,
ID: r.PeerID,
}
}
// Peer6 returns a Peer using the IPv6 endpoint of the Announce.
// Note that, if the Announce does not contain an IPv6 address, the IP field of
// the returned Peer can be nil.
func (r *AnnounceRequest) Peer6() Peer {
return Peer{
IP: r.IPv6,
Port: r.Port,
ID: r.PeerID,
}
}
// AnnounceResponse represents the parameters used to create an announce // AnnounceResponse represents the parameters used to create an announce
// response. // response.
type AnnounceResponse struct { type AnnounceResponse struct {

View file

@ -27,16 +27,14 @@ func (f FailedToRetrievePeers) Error() string { return string(f) }
func responseAnnounceClient(next tracker.AnnounceHandler) tracker.AnnounceHandler { func responseAnnounceClient(next tracker.AnnounceHandler) tracker.AnnounceHandler {
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) { return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
storage := store.MustGetStore() 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.MinInterval = cfg.MinAnnounceInterval
resp.Compact = req.Compact resp.Compact = req.Compact
resp.Complete = int32(storage.NumSeeders(req.InfoHash)) resp.Complete = int32(storage.NumSeeders(req.InfoHash))
resp.Incomplete = int32(storage.NumLeechers(req.InfoHash)) resp.Incomplete = int32(storage.NumLeechers(req.InfoHash))
resp.IPv4Peers, resp.IPv6Peers, err = storage.AnnouncePeers(req.InfoHash, req.Left == 0, int(req.NumWant), peer4, peer6) resp.IPv4Peers, resp.IPv6Peers, err = storage.AnnouncePeers(req.InfoHash, req.Left == 0, int(req.NumWant), req.Peer4(), req.Peer6())
if err != nil { if err != nil {
return err.(FailedToRetrievePeers) return FailedToRetrievePeers(err.Error())
} }
return next(cfg, req, resp) return next(cfg, req, resp)