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