Move AnnounceResponse and ScrapeResponse to models

This commit is contained in:
Justin Li 2014-07-17 01:10:50 -04:00
parent 3ad3f11b2c
commit 9dde295b7c
6 changed files with 33 additions and 42 deletions

View file

@ -9,7 +9,6 @@ import (
"net/http"
"github.com/chihaya/bencode"
"github.com/chihaya/chihaya/tracker"
"github.com/chihaya/chihaya/tracker/models"
)
@ -25,7 +24,7 @@ func (w *Writer) WriteError(err error) error {
return bencoder.Encode(dict)
}
func (w *Writer) WriteAnnounce(res *tracker.AnnounceResponse) error {
func (w *Writer) WriteAnnounce(res *models.AnnounceResponse) error {
dict := bencode.NewDict()
dict["complete"] = res.Complete
dict["incomplete"] = res.Incomplete
@ -45,7 +44,7 @@ func (w *Writer) WriteAnnounce(res *tracker.AnnounceResponse) error {
return bencoder.Encode(dict)
}
func (w *Writer) WriteScrape(res *tracker.ScrapeResponse) error {
func (w *Writer) WriteScrape(res *models.ScrapeResponse) error {
dict := bencode.NewDict()
dict["files"] = filesDict(res.Files)
@ -53,7 +52,7 @@ func (w *Writer) WriteScrape(res *tracker.ScrapeResponse) error {
return bencoder.Encode(dict)
}
func compactPeers(ipv6 bool, peers tracker.PeerList) []byte {
func compactPeers(ipv6 bool, peers models.PeerList) []byte {
var compactPeers bytes.Buffer
if ipv6 {
@ -75,7 +74,7 @@ func compactPeers(ipv6 bool, peers tracker.PeerList) []byte {
return compactPeers.Bytes()
}
func peersList(ipv4s, ipv6s tracker.PeerList) (peers []bencode.Dict) {
func peersList(ipv4s, ipv6s models.PeerList) (peers []bencode.Dict) {
for _, peer := range ipv4s {
peers = append(peers, peerDict(&peer))
}

View file

@ -166,11 +166,11 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t
return
}
func newAnnounceResponse(ann *models.Announce, announcer *models.Peer, t *models.Torrent) *AnnounceResponse {
func newAnnounceResponse(ann *models.Announce, announcer *models.Peer, t *models.Torrent) *models.AnnounceResponse {
seedCount := len(t.Seeders)
leechCount := len(t.Leechers)
res := &AnnounceResponse{
res := &models.AnnounceResponse{
Complete: seedCount,
Incomplete: leechCount,
Interval: ann.Config.Announce.Duration,
@ -185,8 +185,8 @@ func newAnnounceResponse(ann *models.Announce, announcer *models.Peer, t *models
return res
}
func getPeers(ann *models.Announce, announcer *models.Peer, t *models.Torrent, wanted int) (ipv4s, ipv6s PeerList) {
ipv4s, ipv6s = PeerList{}, PeerList{}
func getPeers(ann *models.Announce, announcer *models.Peer, t *models.Torrent, wanted int) (ipv4s, ipv6s models.PeerList) {
ipv4s, ipv6s = models.PeerList{}, models.PeerList{}
if ann.Left == 0 {
// If they're seeding, give them only leechers.
@ -198,7 +198,7 @@ func getPeers(ann *models.Announce, announcer *models.Peer, t *models.Torrent, w
return appendPeers(ipv4s, ipv6s, announcer, t.Leechers, wanted-len(ipv4s)-len(ipv6s))
}
func appendPeers(ipv4s, ipv6s PeerList, announcer *models.Peer, peers map[string]models.Peer, wanted int) (PeerList, PeerList) {
func appendPeers(ipv4s, ipv6s models.PeerList, announcer *models.Peer, peers map[string]models.Peer, wanted int) (models.PeerList, models.PeerList) {
count := 0
for _, peer := range peers {

View file

@ -34,6 +34,8 @@ type Peer struct {
LastAnnounce int64 `json:"last_announce"`
}
type PeerList []Peer
// NewPeer returns the Peer representation of an Announce. When provided nil
// for the announce parameter, it panics. When provided nil for the user or
// torrent parameter, it returns a Peer{UserID: 0} or Peer{TorrentID: 0}
@ -165,6 +167,15 @@ type AnnounceDelta struct {
Downloaded uint64
}
// AnnounceResponse contains the information needed to fulfill an announce.
type AnnounceResponse struct {
Complete, Incomplete int
Interval, MinInterval time.Duration
IPv4Peers, IPv6Peers PeerList
Compact bool
}
// NewAnnounceDelta calculates a Peer's download and upload deltas between
// Announces and generates an AnnounceDelta.
func NewAnnounceDelta(a *Announce, p *Peer, u *User, t *Torrent, created, snatched bool) *AnnounceDelta {
@ -206,3 +217,8 @@ type Scrape struct {
Passkey string
Infohashes []string
}
// ScrapeResponse contains the information needed to fulfill a scrape.
type ScrapeResponse struct {
Files []*Torrent
}

View file

@ -1,31 +0,0 @@
// Copyright 2014 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package tracker
import (
"time"
"github.com/chihaya/chihaya/tracker/models"
)
type PeerList []models.Peer
type AnnounceResponse struct {
Complete, Incomplete int
Interval, MinInterval time.Duration
IPv4Peers, IPv6Peers PeerList
Compact bool
}
type ScrapeResponse struct {
Files []*models.Torrent
}
type Writer interface {
WriteError(error) error
WriteAnnounce(*AnnounceResponse) error
WriteScrape(*ScrapeResponse) error
}

View file

@ -36,5 +36,5 @@ func (t *Tracker) HandleScrape(scrape *models.Scrape, w Writer) error {
torrents = append(torrents, torrent)
}
return w.WriteScrape(&ScrapeResponse{torrents})
return w.WriteScrape(&models.ScrapeResponse{torrents})
}

View file

@ -9,6 +9,7 @@ package tracker
import (
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/drivers/backend"
"github.com/chihaya/chihaya/tracker/models"
)
type Tracker struct {
@ -41,3 +42,9 @@ func New(cfg *config.Config) (*Tracker, error) {
backend: bc,
}, nil
}
type Writer interface {
WriteError(error) error
WriteAnnounce(*models.AnnounceResponse) error
WriteScrape(*models.ScrapeResponse) error
}