-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogress.go
More file actions
138 lines (121 loc) · 2.55 KB
/
progress.go
File metadata and controls
138 lines (121 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"errors"
"fmt"
"math"
"math/rand"
)
var BlockLength = 16384
const (
BlockStateNeed = iota
BlockStateHave
BlockStateDownloading
)
const (
PieceStateNeed = iota
PieceStateHave
PieceStateHavePartial
)
type Progress struct {
length int
pieces []*Piece
}
type Piece struct {
length int
blocks []*Block
}
type Block struct {
index int // piece index
begin int
length int
state int
}
func NewProgress(pieceCount int, pieceLength int) *Progress {
var pieces []*Piece
length := pieceCount * pieceLength
for i := 0; i < pieceCount; i++ {
blockCount := int(math.Ceil(float64(pieceLength) / float64(BlockLength)))
var blocks []*Block
for x := 0; x < blockCount; x++ {
var length int
begin := x * BlockLength
if begin+BlockLength > pieceLength {
length = pieceLength - begin
} else {
length = BlockLength
}
block := &Block{
index: i,
begin: begin,
length: length,
state: BlockStateNeed,
}
blocks = append(blocks, block)
}
piece := &Piece{
length: pieceLength,
blocks: blocks,
}
pieces = append(pieces, piece)
}
return &Progress{
length: length,
pieces: pieces,
}
}
func (p *Progress) percentComplete() float64 {
downloadedLength := p.downloadedLength()
return (float64(downloadedLength) / float64(p.length))
}
func (p *Progress) randomNeededBlock() (block *Block, err error) {
neededBlocks := p.blocksWeNeed()
if len(neededBlocks) == 0 {
err = errors.New("No blocks found")
return
}
randomIndex := rand.Intn(len(neededBlocks))
block = neededBlocks[randomIndex]
block.state = BlockStateDownloading
return
}
func (p *Progress) blocksWeHave() (blocks []*Block) {
for _, piece := range p.pieces {
for _, block := range piece.blocks {
if block.state == BlockStateHave {
blocks = append(blocks, block)
}
}
}
return
}
func (p *Progress) blocksWeNeed() (blocks []*Block) {
for _, piece := range p.pieces {
for _, block := range piece.blocks {
if block.state == BlockStateNeed {
blocks = append(blocks, block)
}
}
}
return
}
func (p *Progress) findBlockFor(index int, begin int) (theBlock *Block, err error) {
for _, piece := range p.pieces {
for _, block := range piece.blocks {
if block.index == index &&
block.begin == begin {
theBlock = block
return
}
}
}
err = fmt.Errorf("Couldn't find block {index:%v, length:%v}", index, begin)
return
}
func (p *Progress) downloadedLength() int {
var length int
haveBlocks := p.blocksWeHave()
for _, block := range haveBlocks {
length += block.length
}
return length
}