]> Sergey Matveev's repositories - btrtrc.git/blob - request-strategy-impls.go
Drop support for go 1.20
[btrtrc.git] / request-strategy-impls.go
1 package torrent
2
3 import (
4         "github.com/anacrolix/torrent/metainfo"
5         request_strategy "github.com/anacrolix/torrent/request-strategy"
6         "github.com/anacrolix/torrent/storage"
7 )
8
9 type requestStrategyInput struct {
10         cl      *Client
11         capFunc storage.TorrentCapacity
12 }
13
14 func (r requestStrategyInput) Torrent(ih metainfo.Hash) request_strategy.Torrent {
15         return requestStrategyTorrent{r.cl.torrents[ih]}
16 }
17
18 func (r requestStrategyInput) Capacity() (int64, bool) {
19         if r.capFunc == nil {
20                 return 0, false
21         }
22         return (*r.capFunc)()
23 }
24
25 func (r requestStrategyInput) MaxUnverifiedBytes() int64 {
26         return r.cl.config.MaxUnverifiedBytes
27 }
28
29 var _ request_strategy.Input = requestStrategyInput{}
30
31 // Returns what is necessary to run request_strategy.GetRequestablePieces for primaryTorrent.
32 func (cl *Client) getRequestStrategyInput(primaryTorrent *Torrent) (input request_strategy.Input) {
33         return requestStrategyInput{
34                 cl:      cl,
35                 capFunc: primaryTorrent.storage.Capacity,
36         }
37 }
38
39 func (t *Torrent) getRequestStrategyInput() request_strategy.Input {
40         return t.cl.getRequestStrategyInput(t)
41 }
42
43 type requestStrategyTorrent struct {
44         t *Torrent
45 }
46
47 func (r requestStrategyTorrent) IgnorePiece(i int) bool {
48         if r.t.ignorePieceForRequests(i) {
49                 return true
50         }
51         if r.t.pieceNumPendingChunks(i) == 0 {
52                 return true
53         }
54
55         return false
56 }
57
58 func (r requestStrategyTorrent) PieceLength() int64 {
59         return r.t.info.PieceLength
60 }
61
62 var _ request_strategy.Torrent = requestStrategyTorrent{}
63
64 type requestStrategyPiece struct {
65         t *Torrent
66         i pieceIndex
67 }
68
69 func (r requestStrategyPiece) Request() bool {
70         return !r.t.ignorePieceForRequests(r.i)
71 }
72
73 func (r requestStrategyPiece) NumPendingChunks() int {
74         return int(r.t.pieceNumPendingChunks(r.i))
75 }
76
77 var _ request_strategy.Piece = requestStrategyPiece{}