Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Mar 4, 2019
1 parent 3736b1c commit 054d038
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 53 deletions.
10 changes: 5 additions & 5 deletions internal/infodownloader/infodownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type InfoDownloader struct {
Bytes []byte

blocks []block
numRequested int // in-flight requests
pending int // in-flight requests
nextBlockIndex uint32
}

Expand Down Expand Up @@ -44,7 +44,7 @@ func (d *InfoDownloader) GotBlock(index uint32, data []byte) error {
if uint32(len(data)) != b.size {
return fmt.Errorf("peer sent invalid size for metadata message: %q", len(data))
}
d.numRequested--
d.pending--
begin := index * blockSize
end := begin + b.size
copy(d.Bytes[begin:end], data)
Expand All @@ -70,13 +70,13 @@ func (d *InfoDownloader) createBlocks() []block {
}

func (d *InfoDownloader) RequestBlocks(queueLength int) {
for ; d.nextBlockIndex < uint32(len(d.blocks)) && d.numRequested < queueLength; d.nextBlockIndex++ {
for ; d.nextBlockIndex < uint32(len(d.blocks)) && d.pending < queueLength; d.nextBlockIndex++ {
d.Peer.RequestMetadataPiece(d.nextBlockIndex)
d.blocks[d.nextBlockIndex].requested = true
d.numRequested++
d.pending++
}
}

func (d *InfoDownloader) Done() bool {
return d.nextBlockIndex == uint32(len(d.blocks)) && d.numRequested == 0
return d.nextBlockIndex == uint32(len(d.blocks)) && d.pending == 0
}
16 changes: 8 additions & 8 deletions internal/infodownloader/infodownloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@ func TestInfoDownloader(t *testing.T) {
assert.False(t, d.Done())

d.RequestBlocks(4)
assert.Equal(t, 4, d.numRequested)
assert.Equal(t, 4, d.pending)
assert.False(t, d.Done())
assert.Equal(t, []uint32{0, 1, 2, 3}, p.requested)

d.RequestBlocks(4)
assert.Equal(t, 4, d.numRequested)
assert.Equal(t, 4, d.pending)
assert.Equal(t, []uint32{0, 1, 2, 3}, p.requested)

assert.Nil(t, d.GotBlock(0, make([]byte, blockSize)))
assert.Equal(t, 3, d.numRequested)
assert.Equal(t, 3, d.pending)
d.RequestBlocks(4)
assert.Equal(t, 4, d.numRequested)
assert.Equal(t, 4, d.pending)
assert.Equal(t, []uint32{0, 1, 2, 3, 4}, p.requested)

d.GotBlock(1, make([]byte, blockSize))
d.GotBlock(2, make([]byte, blockSize))
d.GotBlock(3, make([]byte, blockSize))
d.GotBlock(4, make([]byte, blockSize))
assert.Equal(t, 0, d.numRequested)
assert.Equal(t, 0, d.pending)
d.RequestBlocks(4)
assert.Equal(t, 4, d.numRequested)
assert.Equal(t, 4, d.pending)
assert.Equal(t, []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8}, p.requested)

println("---")
d.GotBlock(5, make([]byte, blockSize))
d.GotBlock(6, make([]byte, blockSize))
d.GotBlock(7, make([]byte, blockSize))
d.GotBlock(8, make([]byte, blockSize))
assert.Equal(t, 0, d.numRequested)
assert.Equal(t, 0, d.pending)
d.RequestBlocks(4)
assert.Equal(t, 2, d.numRequested)
assert.Equal(t, 2, d.pending)
assert.False(t, d.Done())

d.GotBlock(9, make([]byte, blockSize))
Expand Down
40 changes: 20 additions & 20 deletions internal/piecedownloader/piecedownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type PieceDownloader struct {
AllowedFast bool
Buffer bufferpool.Buffer

unrequested []int
requested map[int]struct{} // in-flight requests
done map[int]struct{} // downloaded requests
remaining []int
pending map[int]struct{} // in-flight requests
done map[int]struct{} // downloaded requests
}

type Peer interface {
Expand All @@ -30,48 +30,48 @@ type Peer interface {
}

func New(pi *piece.Piece, pe Peer, allowedFast bool, buf bufferpool.Buffer) *PieceDownloader {
unrequested := make([]int, pi.NumBlocks())
for i := range unrequested {
unrequested[i] = i
remaining := make([]int, pi.NumBlocks())
for i := range remaining {
remaining[i] = i
}
return &PieceDownloader{
Piece: pi,
Peer: pe,
AllowedFast: allowedFast,
Buffer: buf,
unrequested: unrequested,
requested: make(map[int]struct{}),
remaining: remaining,
pending: make(map[int]struct{}),
done: make(map[int]struct{}),
}
}

func (d *PieceDownloader) Choked() {
for i := range d.requested {
d.unrequested = append(d.unrequested, i)
delete(d.requested, i)
for i := range d.pending {
d.remaining = append(d.remaining, i)
delete(d.pending, i)
}
}

func (d *PieceDownloader) GotBlock(block piece.Block, data []byte) error {
var err error
if _, ok := d.done[block.Index]; ok {
return ErrBlockDuplicate
} else if _, ok := d.requested[block.Index]; !ok {
} else if _, ok := d.pending[block.Index]; !ok {
err = ErrBlockNotRequested
}
copy(d.Buffer.Data[block.Begin:block.Begin+block.Length], data)
delete(d.requested, block.Index)
delete(d.pending, block.Index)
d.done[block.Index] = struct{}{}
return err
}

func (d *PieceDownloader) Rejected(block piece.Block) {
d.unrequested = append(d.unrequested, block.Index)
delete(d.requested, block.Index)
d.remaining = append(d.remaining, block.Index)
delete(d.pending, block.Index)
}

func (d *PieceDownloader) CancelPending() {
for i := range d.requested {
for i := range d.pending {
b, ok := d.Piece.GetBlock(i)
if !ok {
panic("cannot get block")
Expand All @@ -81,18 +81,18 @@ func (d *PieceDownloader) CancelPending() {
}

func (d *PieceDownloader) RequestBlocks(queueLength int) {
remaining := d.unrequested
remaining := d.remaining
for _, i := range remaining {
if len(d.requested) >= queueLength {
if len(d.pending) >= queueLength {
break
}
b, ok := d.Piece.GetBlock(i)
if !ok {
panic("cannot get block")
}
d.Peer.RequestPiece(d.Piece.Index, b.Begin, b.Length)
d.unrequested = d.unrequested[1:]
d.requested[b.Index] = struct{}{}
d.remaining = d.remaining[1:]
d.pending[b.Index] = struct{}{}
}
}

Expand Down
40 changes: 20 additions & 20 deletions internal/piecedownloader/piecedownloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func TestPieceDownloader(t *testing.T) {
}
pe := &TestPeer{}
d := New(pi, pe, false, buf)
assert.Equal(t, 11, len(d.unrequested))
assert.Equal(t, 0, len(d.requested))
assert.Equal(t, 11, len(d.remaining))
assert.Equal(t, 0, len(d.pending))
assert.Equal(t, 0, len(d.done))
assert.False(t, d.Done())

d.RequestBlocks(4)
assert.Equal(t, 7, len(d.unrequested))
assert.Equal(t, 4, len(d.requested))
assert.Equal(t, 7, len(d.remaining))
assert.Equal(t, 4, len(d.pending))
assert.Equal(t, 0, len(d.done))
assert.False(t, d.Done())
assert.Equal(t, []Message{
Expand All @@ -62,8 +62,8 @@ func TestPieceDownloader(t *testing.T) {
}, pe.requested)

d.RequestBlocks(4)
assert.Equal(t, 7, len(d.unrequested))
assert.Equal(t, 4, len(d.requested))
assert.Equal(t, 7, len(d.remaining))
assert.Equal(t, 4, len(d.pending))
assert.Equal(t, 0, len(d.done))
assert.False(t, d.Done())
assert.Equal(t, []Message{
Expand All @@ -75,14 +75,14 @@ func TestPieceDownloader(t *testing.T) {

assert.Nil(t, d.GotBlock(piece.Block{Index: 0, Begin: 0, Length: blockSize}, make([]byte, blockSize)))
assert.Equal(t, 4, len(pe.requested))
assert.Equal(t, 7, len(d.unrequested))
assert.Equal(t, 3, len(d.requested))
assert.Equal(t, 7, len(d.remaining))
assert.Equal(t, 3, len(d.pending))
assert.Equal(t, 1, len(d.done))
assert.False(t, d.Done())

d.RequestBlocks(4)
assert.Equal(t, 6, len(d.unrequested))
assert.Equal(t, 4, len(d.requested))
assert.Equal(t, 6, len(d.remaining))
assert.Equal(t, 4, len(d.pending))
assert.Equal(t, 1, len(d.done))
assert.False(t, d.Done())
assert.Equal(t, []Message{
Expand All @@ -97,14 +97,14 @@ func TestPieceDownloader(t *testing.T) {
assert.Nil(t, d.GotBlock(piece.Block{Index: 2, Begin: 2 * blockSize, Length: blockSize}, make([]byte, blockSize)))
assert.Nil(t, d.GotBlock(piece.Block{Index: 3, Begin: 3 * blockSize, Length: blockSize}, make([]byte, blockSize)))
assert.Nil(t, d.GotBlock(piece.Block{Index: 4, Begin: 4 * blockSize, Length: blockSize}, make([]byte, blockSize)))
assert.Equal(t, 6, len(d.unrequested))
assert.Equal(t, 0, len(d.requested))
assert.Equal(t, 6, len(d.remaining))
assert.Equal(t, 0, len(d.pending))
assert.Equal(t, 5, len(d.done))
assert.False(t, d.Done())

d.RequestBlocks(4)
assert.Equal(t, 2, len(d.unrequested))
assert.Equal(t, 4, len(d.requested))
assert.Equal(t, 2, len(d.remaining))
assert.Equal(t, 4, len(d.pending))
assert.Equal(t, 5, len(d.done))
assert.False(t, d.Done())
assert.Equal(t, []Message{
Expand All @@ -120,14 +120,14 @@ func TestPieceDownloader(t *testing.T) {
}, pe.requested)

assert.Nil(t, d.GotBlock(piece.Block{Index: 5, Begin: 5 * blockSize, Length: blockSize}, make([]byte, blockSize)))
assert.Equal(t, 2, len(d.unrequested))
assert.Equal(t, 3, len(d.requested))
assert.Equal(t, 2, len(d.remaining))
assert.Equal(t, 3, len(d.pending))
assert.Equal(t, 6, len(d.done))
assert.False(t, d.Done())

d.Choked()
assert.Equal(t, 5, len(d.unrequested))
assert.Equal(t, 0, len(d.requested))
assert.Equal(t, 5, len(d.remaining))
assert.Equal(t, 0, len(d.pending))
assert.Equal(t, 6, len(d.done))
assert.False(t, d.Done())

Expand All @@ -137,8 +137,8 @@ func TestPieceDownloader(t *testing.T) {
assert.Nil(t, d.GotBlock(piece.Block{Index: 8, Begin: 8 * blockSize, Length: blockSize}, make([]byte, blockSize)))
assert.Nil(t, d.GotBlock(piece.Block{Index: 9, Begin: 9 * blockSize, Length: blockSize}, make([]byte, blockSize)))
assert.Nil(t, d.GotBlock(piece.Block{Index: 10, Begin: 10 * blockSize, Length: 42}, make([]byte, 42)))
assert.Equal(t, 0, len(d.unrequested))
assert.Equal(t, 0, len(d.requested))
assert.Equal(t, 0, len(d.remaining))
assert.Equal(t, 0, len(d.pending))
assert.Equal(t, 11, len(d.done))
assert.True(t, d.Done())
}

0 comments on commit 054d038

Please sign in to comment.