torrent/download_strategies.go

78 lines
2.0 KiB
Go
Raw Normal View History

package torrent
import (
2014-08-22 07:33:17 +00:00
"io"
2014-08-21 11:08:56 +00:00
pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
)
type DownloadStrategy interface {
// Tops up the outgoing pending requests.
FillRequests(*torrent, *connection)
2014-08-25 12:14:10 +00:00
TorrentStarted(*torrent)
TorrentStopped(*torrent)
DeleteRequest(*torrent, request)
TorrentPrioritize(t *torrent, off, _len int64)
2014-08-25 12:14:10 +00:00
TorrentGotChunk(*torrent, request)
TorrentGotPiece(t *torrent, piece int)
2014-08-22 07:33:17 +00: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 07:33:17 +00: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) {
if len(c.Requests) >= 32 {
return false
}
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-14 17:25:53 +00: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) {}