forked from amy/Bittorrent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpiecemanager.go
439 lines (365 loc) · 11.5 KB
/
piecemanager.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
/*
* matains state of which pieces we have and what pieces we need
* accessed by multiple peer connections for piece requests
*/
import (
"errors"
"fmt"
"math"
"sync"
"time"
//"os"
"io"
)
/*
* manages pieces for a single peer connection
*/
type ConnectionPieceManager struct {
requestQueue []int //holds the next pieces to request
peerField []byte //pieces the peer has
haveBroadcastQueue chan int32 //used to receive a have broadcast
}
/*
PieceManager manages the pieces client needs to request
*/
type PieceManager struct {
//global fields
bitField []byte //pieces client has
//missingField []byte //missing
transitField []byte //determine if piece is intransit
maxQueueSize int //capacity of the requestQueue slice(chosen by user)
manager []*ConnectionPieceManager //manages piece queues for a given peer
numConnections int
fileWriter *FileWriter
infoDict *InfoDict
mutex *sync.Mutex
managerMutex *sync.Mutex
downloadStatus chan<- byte
}
/*
NewPieceManager constructor
@tInfo: contains information of about the torrent [pieceLength,length] see torrent.go
@requestQueueSize: capacity for requestQueue slice [remains constant]
returns: returns new PieceManager
*/
func NewPieceManager(tInfo *InfoDict, requestQueueSize int, fileName string) PieceManager {
//create new piecemanager
var p PieceManager
//number of pieces in total
numPieces := math.Ceil(float64(tInfo.Length) / float64(tInfo.PieceLength))
//store the request queue capacity
p.maxQueueSize = requestQueueSize
//number of bytes in bitField for client
numBytes := math.Ceil(numPieces / 8)
//create file writer
fW := NewFileWriter(tInfo, fileName, int64(numBytes))
p.fileWriter = &fW
//get bitfield from file
p.bitField = p.LoadBitFieldFromFile(int(numBytes))
/*fmt.Println(p.bitField)
for i := 0; i < 64; i++ {
p.bitField[i] = 255
}*/
p.bitField[63] |= 2
//pieces which peers have claimed responsbility
p.transitField = make([]byte, int(numBytes), int(numBytes))
p.numConnections = 0
p.infoDict = tInfo
p.mutex = &sync.Mutex{}
p.managerMutex = &sync.Mutex{}
fmt.Printf("%v\n", p.bitField)
p.downloadStatus = make(chan<- byte, int32(numPieces))
//p.downloadStatus <- byte(1)
for _, entry := range p.bitField {
for _, offset := range []uint{0, 1, 2, 3, 4, 5, 6, 7} {
if entry&(1<<(7-offset)) != 0 {
p.downloadStatus <- byte(1)
}
}
}
//fmt.Printf("Len: %v\n", len(p.downloadStatus))
return p
}
//returns a channel to get notified of download completion
func (t *PieceManager) WaitForDownload() <-chan bool {
done := make(chan bool)
go func(done chan<- bool, status chan<- byte) {
for len(status) != cap(status) {
time.Sleep(1 * time.Second)
fmt.Printf("Downloaded %2.0f%%", (float32(len(status))/float32(cap(status)))*100)
}
done <- true
return
}(done, t.downloadStatus)
return done
}
// Returns the bitfield from the metadata file
// Creates an empty bitfield if there was an error
func (t *PieceManager) LoadBitFieldFromFile(size int) []byte {
data, err := t.fileWriter.GetMetaData(size)
if err != nil && err != io.EOF {
return make([]byte, size, size)
}
// fmt.Printf("%v\n", data)
return data
}
/*
* create a piece manager for a new connection
* returns: connection descriptor
*/
func (t *PieceManager) RegisterConnection(peerField []byte) int {
conNum := t.numConnections
t.numConnections++
var con ConnectionPieceManager
t.manager = append(t.manager, &con)
con.peerField = make([]byte, cap(t.bitField), cap(t.bitField))
copy(con.peerField, peerField)
t.manager[conNum].requestQueue = make([]int, 0, t.maxQueueSize)
//used to receive have broadcasts
t.manager[conNum].haveBroadcastQueue = make(chan int32, cap(t.bitField)*8)
return conNum
}
func (t *PieceManager) UnregisterConnection(connection int, lastPieceRequest int) {
t.mutex.Lock()
for _, index := range t.manager[connection].requestQueue {
byteIndex := index / 8
offset := uint32(index % 8)
t.transitField[byteIndex] &= ^(1 << (7 - offset))
}
if lastPieceRequest != -1 {
t.transitField[lastPieceRequest/8] &= ^(1 << (7 - uint32(lastPieceRequest%8)))
}
t.mutex.Unlock()
}
/**
* used if a peer sends a have message updating us a of new piece they have
* check if we either have it or another peer is offering it
* @connection: the connection descriptor for this peer
* @pieceIndex: the actual index of this piece
* returns: whether we are interested in it
**/
func (t *PieceManager) UpdatePeerField(connection int, pieceIndex int32) {
//a peer has told us it now has a piece
//are we now interested in it?
//if we are interested add it to missingfield
//compute the location of this piece in the bitfields
index := pieceIndex / 8
offset := uint32(pieceIndex % 8)
bit := byte(1 << (7 - offset))
//add to the peer's list of pieces they have
t.manager[connection].peerField[index] |= bit
}
/*
*determines which pieces we should request from 'peer' using 'connecton'
@connection: connection descriptor for the peer
returns: whether client is interested
*/
func (t *PieceManager) ComputeRequestQueue(connection int) bool {
// fmt.Println(t.manager[connection].requestQueue)
if len(t.manager[connection].requestQueue) != 0 {
return true
}
//construct the new request queue for the peer
t.manager[connection].requestQueue = make([]int, 0, t.maxQueueSize)
//we are not interested by default
interested := false
t.mutex.Lock()
//for all bytes in the peer field
for index, element := range t.manager[connection].peerField {
//compute what this peer has that no other peers has and we don't have
mask := (^(t.bitField[index]) & element & ^(t.transitField[index]))
//if there is anything found
if mask != 0 {
//we are interested
interested = true
nums := [8]uint32{0, 1, 2, 3, 4, 5, 6, 7}
//go through the mask and get the index of those pieces
for _, num := range nums {
bit := byte(1 << (7 - num))
if mask&bit != 0 {
//if can fit in queue, add them to the queue
if len(t.manager[connection].requestQueue) < t.maxQueueSize {
//add piece to request queue
t.manager[connection].requestQueue = append(t.manager[connection].requestQueue, index*8+int(num))
//a peer has claimed responsibility for this piece
t.transitField[index] |= 1 << (7 - num)
}
}
//if we can no longer fit pieces in the queue
if len(t.manager[connection].requestQueue) == t.maxQueueSize {
t.mutex.Unlock()
return interested
}
}
}
}
// fmt.Printf("CONNECTION %d, QUEUE %v\n", connection, t.manager[connection].requestQueue)
t.mutex.Unlock()
return interested
}
/*
GetBitField gets our bitField
returns: slice of bitField
*/
func (t *PieceManager) GetBitField() []byte {
return t.bitField
}
/*
* checks to see if we have the piece requested from us
* @pieceIndex: index of piece to look for
* returns: whether we have it
*/
func (t *PieceManager) GetPiece(pieceIndex int32, pieceLength int32, pieceBegin int32) (error, []byte) {
//implement
index := pieceIndex / 8
offset := uint32(pieceIndex % 8)
bit := byte(1 << (7 - offset))
if t.bitField[index]&bit == 0 {
return errors.New("Piece does not exist"), nil
}
//we have it, so fetch the piece
err, arr := t.fileWriter.Read(pieceIndex)
if err != nil {
return err, nil
}
if pieceBegin+pieceLength > int32(len(arr)) {
return errors.New("PieceLength is bigger than requested piece or part of piece"), nil
}
return err, arr[pieceBegin : pieceBegin+pieceLength]
}
/*
ReceivedPiece writes a received piece and marks it off and writes it
* @pieceIndex: piece we got
* @piece: the actual piece bytes
* returns: status
*/
func (t *PieceManager) ReceivePiece(connection int, pieceIndex int32, piece []byte) error {
index := pieceIndex / 8
offset := uint32(pieceIndex % 8)
bit := byte(1 << (7 - offset))
if t.bitField[index]&bit == 1 {
return errors.New("ReceivePiece: received piece we already have")
} else {
err := t.fileWriter.Write(piece, int(pieceIndex))
if err != nil {
t.mutex.Lock()
t.bitField[index] &= ^bit
t.mutex.Unlock()
// fmt.Printf("%v\n", err)
} else {
t.mutex.Lock()
//we now have the piece
t.bitField[index] |= bit
t.mutex.Unlock()
t.downloadStatus <- byte(1)
}
return err
}
return nil
}
/*
* gets the next piece request for the given connection
* @connection: descriptor for the given connection
* returns index
*/
func (t *PieceManager) GetNextRequest(connection int) int {
// fmt.Println(t.manager[connection].requestQueue)
//if queue is empty
if len(t.manager[connection].requestQueue) == 0 {
//compute a new one if there is more to request
if val := t.ComputeRequestQueue(connection); val == false {
return -1
}
}
//fmt.Println(t.manager[connection].requestQueue)
//pop off queue
next := t.manager[connection].requestQueue[0]
t.manager[connection].requestQueue = t.manager[connection].requestQueue[1:]
return next
}
/*
* attempt to retreive the next have broadcast, if there is one
* @connection: connection descriptor for the calling peer
* returns: the index of the piece broadcasted
*/
func (t *PieceManager) GetNextHaveBroadcast(connection int) chan int32 {
/*//lock
lastSeen := t.manager[connection].mostRecentHave
if len(t.haveQueue) != 0 {
for index := 0; index < len(t.haveQueue); index++ {
if lastSeen < t.haveQueue[index].index {
t.manager[connection].mostRecentHave = t.haveQueue[index].index
t.haveQueue[index].notSeenBy--
have := t.haveQueue[index].pieceIndex
if t.haveQueue[index].notSeenBy == 0 {
t.haveQueue = t.haveQueue[1:]
}
return have
}
}
}*/
curLen := len(t.manager[connection].haveBroadcastQueue)
subChan := make(chan int32, curLen)
for i := 0; i < curLen; i++ {
select {
case have := <-t.manager[connection].haveBroadcastQueue:
//fmt.Println("Got have:", connection, "piece:", have)
subChan <- have
default:
}
}
return subChan
//unlock
}
/*
* creates a have broadcast to all connected peers when we get a new piece
* @pieceIndex: index to broadcast a notification of
*/
func (t *PieceManager) CreateHaveBroadcast(connection int, pieceIndex int32) {
t.managerMutex.Lock()
for index, element := range t.manager {
if index == connection {
continue
}
// fmt.Println("IN", connection)
//fmt.Println("connection", index, "LEN", len(element.haveBroadcastQueue))
element.haveBroadcastQueue <- pieceIndex
// fmt.Println("OUT", connection)
}
t.managerMutex.Unlock()
/*
//lock
var h HaveBroadcast
h.pieceIndex = pieceIndex //set the index of piece to broadcast
t.lastHaveIndex += 1 //increase the piecemanager's have index
h.Index = (t.lastHaveIndex) //set this have broadcast's index to the next monotically increasing index
h.notSeenBy = t.numConnections //keep a reference count of how many peers have seen this
t.haveQueue = append(t.haveQueue, h) //append it to the queue
//unlock*/
}
/**
* returns the current progress of the uploading/downloading
**/
func (t *PieceManager) GetProgress() (uploaded int, downloaded int, left int) {
bitField := t.GetBitField()
uploaded = 0 // for now no uploading
numDownloaded := 0
for _, b := range bitField {
if b != 0 {
numDownloaded += int(((b >> 7) & 1) + ((b >> 6) & 1) + ((b >> 5) & 1) + ((b >> 4) & 1) + ((b >> 3) & 1) + ((b >> 2) & 1) + ((b >> 1) & 1) + b&1)
}
}
downloaded = numDownloaded * t.infoDict.PieceLength
left = t.infoDict.Length - downloaded
return
}
/**
* saves bitfield to file
**/
func (t *PieceManager) SaveProgress() error {
bitField := t.GetBitField()
fmt.Println("Saving progress...")
err := t.fileWriter.WriteMetaData(bitField)
return err
}