]> Sergey Matveev's repositories - btrtrc.git/blob - request-strategy-impls.go
Use interfaces to lazily expose the bare minimum inputs to GetRequestablePieces
[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) Piece(i int) request_strategy.Piece {
48         return requestStrategyPiece{r.t, i}
49 }
50
51 func (r requestStrategyTorrent) ChunksPerPiece() uint32 {
52         return r.t.chunksPerRegularPiece()
53 }
54
55 func (r requestStrategyTorrent) PieceLength() int64 {
56         return r.t.info.PieceLength
57 }
58
59 var _ request_strategy.Torrent = requestStrategyTorrent{}
60
61 type requestStrategyPiece struct {
62         t *Torrent
63         i pieceIndex
64 }
65
66 func (r requestStrategyPiece) Request() bool {
67         return !r.t.ignorePieceForRequests(r.i)
68 }
69
70 func (r requestStrategyPiece) NumPendingChunks() int {
71         return int(r.t.pieceNumPendingChunks(r.i))
72 }
73
74 var _ request_strategy.Piece = requestStrategyPiece{}