-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpiece.go
144 lines (130 loc) · 3.64 KB
/
piece.go
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
139
140
141
142
143
144
package main
import (
"crypto/sha1"
"sync"
)
// Piece is the general unit that files are divided into.
type Piece struct {
index int
data []byte
size int64
hash [20]byte
chanBlocks chan *Block
verified bool
//hex string // NOTE: no need
pending sync.WaitGroup
success chan bool // For when all blocks are there
}
// Block struct will always have constant size, 16KB.
type Block struct {
index uint32 // NOTE: piece index
offset uint32
data []byte
size int // typicall BLOCKSIZE, except for last
}
// parsePieces constructs the Piece list from
// the torrent file.
func (info *TorrentInfo) parsePieces() {
info.cleanPieces()
numberOfBlocks := info.PieceLength / int64(BLOCKSIZE)
// NOTE: Pieces are global variable of all pieces
d.Pieces = make([]*Piece, 0, len(info.Pieces)/20)
for i := 0; i < len(info.Pieces); i = i + 20 {
j := i + 20 // NOTE: j is hash end
piece := Piece{
size: info.PieceLength,
index: len(d.Pieces),
verified: false,
success: make(chan bool),
}
// Last piece has different amount of blocks
if i+20 >= len(info.Pieces) {
piece.chanBlocks = make(chan *Block, info.lastPieceBlockCount())
piece.size = info.lastPieceSize()
piece.data = make([]byte, piece.size)
//logger.Println(piece.size, cap(piece.chanBlocks), BLOCKSIZE)
} else {
piece.chanBlocks = make(chan *Block, numberOfBlocks)
piece.data = make([]byte, info.PieceLength)
}
// Copy to next 20 into Piece Hash
copy(piece.hash[:], info.Pieces[i:j])
d.Pieces = append(d.Pieces, &piece)
}
bitCap := len(d.Pieces) / 8
if len(d.Pieces)%8 != 0 {
bitCap += 1
}
d.bitfield = make([]byte, bitCap)
}
func (info *TorrentInfo) lastPieceBlockCount() int64 {
if info.Length%info.PieceLength == 0 {
return info.PieceLength / int64(BLOCKSIZE)
}
pieceCount := (info.Length % info.PieceLength) / int64(BLOCKSIZE)
if pieceCount == 0 {
return 1
}
return pieceCount
}
func (info *TorrentInfo) lastBlockSize() int64 {
if info.Length%info.PieceLength == 0 {
return (info.PieceLength / int64(BLOCKSIZE)) * BLOCKSIZE
}
return 0
}
func (info *TorrentInfo) lastPieceSize() int64 {
if info.Length%info.PieceLength != 0 {
return info.Length % info.PieceLength
}
return info.PieceLength
}
// VerifyPiece is called after piece.chanBlocks fills up.
// It checks the hash of all the blocks together (of the piece)
// and if valid will send a message to the piece.success chan.
func (p *Piece) VerifyPiece() {
for {
b := <-p.chanBlocks
copy(p.data[int(b.offset):int(b.offset)+b.size],
b.data)
if len(p.chanBlocks) < 1 {
break
}
}
if p.hash != sha1.Sum(p.data) {
debugger.Printf(
"Error with piece of size %d,\n"+
"the hash is %x, and what I got is %x",
p.size, p.hash, sha1.Sum(p.data))
p.data = nil
p.data = make([]byte, p.size)
logger.Printf("Unable to Validate Blocks to Piece %d", p.index)
return
}
p.verified = true
//logger.Printf("Piece at %d is Collected", p.index)
// TODO: Update personal bitfield
// TODO: Send have to peers
p.success <- true // FIXME: Keep?
}
// UpdateBitfield is not yet written.
// TODO: Convert parse bitfield for update.
func UpdateBitfield() []byte {
// result := make([]bool, len(d.Pieces)/8)
// bitfield := msg[1:]
// // For each byte, look at the bits
// // NOTE: that is 8 * 8
// for i := 0; i < len(bitfield); i++ {
// for j := 0; j < 8; j++ {
// index := i*8 + j
// if index >= len(d.Pieces) {
// break // Hit padding bits
// }
// byte := bitfield[i] // Within bytes
// bit := (byte >> uint32(7-j)) & 1 // some shifting
// result[index] = bit == 1 // if bit is true
// }
// }
// return result
return nil
}