]> Sergey Matveev's repositories - btrtrc.git/blob - request-strategy/peer.go
Optimize chunk calculations in request strategy
[btrtrc.git] / request-strategy / peer.go
1 package request_strategy
2
3 import (
4         "time"
5
6         "github.com/RoaringBitmap/roaring"
7 )
8
9 type PeerNextRequestState struct {
10         Interested bool
11         Requests   roaring.Bitmap
12 }
13
14 type PeerId interface {
15         Uintptr() uintptr
16 }
17
18 type Peer struct {
19         Pieces           roaring.Bitmap
20         MaxRequests      int
21         ExistingRequests roaring.Bitmap
22         Choking          bool
23         PieceAllowedFast roaring.Bitmap
24         DownloadRate     float64
25         Age              time.Duration
26         // This is passed back out at the end, so must support equality. Could be a type-param later.
27         Id PeerId
28 }
29
30 // TODO: This might be used in more places I think.
31 func (p *Peer) canRequestPiece(i pieceIndex) bool {
32         return (!p.Choking || p.PieceAllowedFast.Contains(uint32(i))) && p.HasPiece(i)
33 }
34
35 func (p *Peer) HasPiece(i pieceIndex) bool {
36         return p.Pieces.Contains(uint32(i))
37 }