]> Sergey Matveev's repositories - btrtrc.git/blobdiff - request-strategy/peer.go
Add a working request strategy test
[btrtrc.git] / request-strategy / peer.go
index 4a3d0689a4371bce9045b3ecdffd78aa97872f28..21ef0d2eca92e0a54ea0271b3aa2776bf1555efd 100644 (file)
@@ -2,7 +2,6 @@ package request_strategy
 
 import (
        "time"
-       "unsafe"
 )
 
 type PeerNextRequestState struct {
@@ -10,20 +9,30 @@ type PeerNextRequestState struct {
        Requests   map[Request]struct{}
 }
 
-type PeerPointer = unsafe.Pointer
+type PeerId interface {
+       Uintptr() uintptr
+}
 
 type Peer struct {
-       HasPiece           func(pieceIndex) bool
-       MaxRequests        func() int
-       HasExistingRequest func(Request) bool
+       HasPiece           func(pieceIndex) bool
+       MaxRequests        int
+       HasExistingRequest func(Request) bool
        Choking            bool
        PieceAllowedFast   func(pieceIndex) bool
        DownloadRate       float64
        Age                time.Duration
-       Id                 PeerPointer
+       // This is passed back out at the end, so must support equality. Could be a type-param later.
+       Id PeerId
+}
+
+func (p *Peer) pieceAllowedFastOrDefault(i pieceIndex) bool {
+       if f := p.PieceAllowedFast; f != nil {
+               return f(i)
+       }
+       return false
 }
 
 // TODO: This might be used in more places I think.
 func (p *Peer) canRequestPiece(i pieceIndex) bool {
-       return p.HasPiece(i) && (!p.Choking || p.PieceAllowedFast(i))
+       return p.HasPiece(i) && (!p.Choking || (p.PieceAllowedFast != nil && p.PieceAllowedFast(i)))
 }