FedP2P/download_strategies.go

75 lines
1.9 KiB
Go
Raw Normal View History

package torrent
import (
2014-08-22 15:33:17 +08:00
"io"
2014-08-21 19:08:56 +08:00
pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
)
type DownloadStrategy interface {
// Tops up the outgoing pending requests.
FillRequests(*torrent, *connection)
2014-08-25 20:14:10 +08:00
TorrentStarted(*torrent)
TorrentStopped(*torrent)
DeleteRequest(*torrent, request)
TorrentPrioritize(t *torrent, off, _len int64)
2014-08-25 20:14:10 +08:00
TorrentGotChunk(*torrent, request)
TorrentGotPiece(t *torrent, piece int)
2014-08-22 15:33:17 +08:00
WriteStatus(w io.Writer)
AssertNotRequested(*torrent, request)
PendingData(*torrent) bool
}
type DefaultDownloadStrategy struct{}
func (me *DefaultDownloadStrategy) PendingData(t *torrent) bool {
return !t.haveAllPieces()
}
func (me *DefaultDownloadStrategy) AssertNotRequested(t *torrent, r request) {}
2014-08-22 15:33:17 +08:00
func (me *DefaultDownloadStrategy) WriteStatus(w io.Writer) {}
func (s *DefaultDownloadStrategy) FillRequests(t *torrent, c *connection) {
if c.Interested {
if c.PeerChoked {
return
}
if len(c.Requests) > c.requestsLowWater {
return
}
}
addRequest := func(req request) (again bool) {
return c.Request(req)
}
for e := c.pieceRequestOrder.First(); e != nil; e = e.Next() {
pieceIndex := e.Piece()
if !c.PeerHasPiece(pp.Integer(pieceIndex)) {
panic("piece in request order but peer doesn't have it")
}
if !t.wantPiece(pieceIndex) {
panic("unwanted piece in connection request order")
}
piece := t.Pieces[pieceIndex]
for _, cs := range piece.shuffledPendingChunkSpecs() {
r := request{pp.Integer(pieceIndex), cs}
if !addRequest(r) {
return
}
}
}
2014-09-15 01:25:53 +08:00
return
}
func (s *DefaultDownloadStrategy) TorrentStarted(t *torrent) {}
func (s *DefaultDownloadStrategy) TorrentStopped(t *torrent) {
}
func (s *DefaultDownloadStrategy) DeleteRequest(t *torrent, r request) {
}
func (me *DefaultDownloadStrategy) TorrentGotChunk(t *torrent, c request) {}
func (me *DefaultDownloadStrategy) TorrentGotPiece(t *torrent, piece int) {}
func (*DefaultDownloadStrategy) TorrentPrioritize(t *torrent, off, _len int64) {}