-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageHandler.java
721 lines (667 loc) · 30.5 KB
/
MessageHandler.java
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
import java.io.*;
import java.net.*;
import java.util.Arrays;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* MessageHandler.java
* Used to send to and receive messages from peers.
*
* @author Shuhan Liu (sl1041) 154007082
* @author Nicole Heimbach (nsh43) 153002353
* @version RUBTClient Phase 2, 11/04/2015
*/
public class MessageHandler
{
static enum P2PMessage{
KEEP_ALIVE (new byte[] {0,0,0,0}),
CHOKE (new byte[] {0,0,0,1,0}),
UNCHOKE (new byte[] {0,0,0,1,1}),
INTERESTED (new byte[] {0,0,0,1,2}),
UNINTERESTED (new byte[] {0,0,0,1,3}),
HAVE (new byte[] {0,0,0,5,4}),
BITFIELD (new byte[]{0,0,0,1,5}),
REQUEST (new byte[]{0,0,0,13,6}),
PIECE (new byte[]{0,0,0,9,7}), //doesn't work?
CANCEL(new byte[]{0,0,0,13,8}),
PORT(new byte[]{0,0,0,3,9});
private byte[] message;
private P2PMessage(byte[] message){
this.message = message;
}
private P2PMessage(byte[] message, int length){
this.message=message;
//message[3] is the length of the message
this.message[3]= (byte)(message[3]+length);
}
public byte[] bytes(){
return message;
}
public byte[] msgWithLength(int length){
byte[] messageWithLength = bytes();
int lengthIndex = messageWithLength.length-2;
messageWithLength[lengthIndex]+=length;
return messageWithLength;
}
}
static enum MessageID{
CHOKE(0), UNCHOKE(1), INTERESTED(2), UNINTERESTED(3), HAVE(4), BITFIELD(5), REQUEST(6), PIECE(7), CANCEL(8), PORT(9), INVALID(-1);
private int messageID;
private MessageID(int messageID){
this.messageID = messageID;
}
}
/*public static int sendKeepAlive(DataOutputStream output){
try {
output.writeInt(0);
return 0;
} catch (IOException ex) {
Logger.getLogger(MessageHandler.class.getName()).log(Level.SEVERE, null, ex);
return -1;
}
}
public static int sendMessage(MessageID messageID, DataOutputStream output){
try{
switch(messageID){
case CHOKE:
output.writeInt(1); //write length
output.write(0); //write ID
case UNCHOKE:
output.writeInt(1); //write length
output.write(1); //write ID
case INTERESTED:
output.writeInt(1); //write length
output.write(2); //write ID
case UNINTERESTED:
output.writeInt(1); //write length
output.write(3); //write ID
case HAVE:
output.writeInt(5); //write length
output.write(4); //write ID
case REQUEST:
output.writeInt(13); //write length
output.write(6); //write ID
case CANCEL:
output.writeInt(13); //write length
output.write(8); //write ID
case PORT:
output.writeInt(3); //write length
output.write(9); //write ID
}
return 0;
}
catch(IOException e){
System.err.println(e);
e.printStackTrace();
return -1;
}
}
public static int sendMessage(MessageID messageID, int pieceIndex, DataOutputStream output){
try{
switch(messageID){
case HAVE:
output.writeInt(5); //write length
output.write(4); //write ID
output.writeInt(pieceIndex);
case CANCEL:
output.writeInt(13); //write length
output.write(8); //write ID
case PORT:
output.writeInt(3); //write length
output.write(9); //write ID
}
return 0;
}
catch(IOException e){
System.err.println(e);
e.printStackTrace();
return -1;
}
}*/
/**
* Returns the MessageID that corresponds with messageID
* @param messageID a byte that represents a MessageID
* @returns the MessageID associated with messageID
*/
public static MessageID getMessageID(byte messageID){
switch(messageID){
case 0:
return MessageID.CHOKE;
case 1:
return MessageID.UNCHOKE;
case 2:
return MessageID.INTERESTED;
case 3:
return MessageID.UNINTERESTED;
case 4:
return MessageID.HAVE;
case 5:
return MessageID.BITFIELD;
case 6:
return MessageID.REQUEST;
case 7:
return MessageID.PIECE;
case 8:
return MessageID.CANCEL;
case 9:
return MessageID.PORT;
default:
System.err.println("ERROR: Invalid message type");
return null;
}
}
/**
* Gets the sha1 hash of an array of bytes.
* @param bytesToHash bytes that will be used to generate an sha1 hash
* @return the sha1 hash as an array of bytes
*/
public static byte[] sha1Bytes(byte[] bytesToHash){
try{
MessageDigest msgDigest = MessageDigest.getInstance("SHA-1");
msgDigest.update(bytesToHash);
byte[] sha1Hash = msgDigest.digest();
return sha1Hash;
}
catch(NoSuchAlgorithmException e){
System.err.println(e);
e.printStackTrace();
return null;
}
}
//WORKS
/**
* Receive a message of Message ID 1 (choke).
* @param commInfo information used for communicating with the peer
* @param peer the corresponding peer
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @return -1 if EOFException or IOException
* @return 0 if success
*/
public static int peerChoked(CommunicationInfo commInfo, Peer peer, DataInputStream input, DataOutputStream output){
try{
peer.choked = true;
output.write(MessageHandler.P2PMessage.KEEP_ALIVE.bytes());
output.write(MessageHandler.P2PMessage.INTERESTED.bytes());
output.flush();
}
catch (EOFException e){
RUBTClient.debugPrint("Reached end of file...");
e.printStackTrace();
return -1;
}
catch(IOException e){
System.err.println(e);
e.printStackTrace();
return -1;
}
return 0;
}
//WORKS
/**
* Receive a message of Message ID 2 (unchoke).
* If the client has not finished downloading, follows up by sending a request for a piece.
* @param torrentInfo information retrieved from the parsed torrent file
* @param commInfo information used for communicating with the peer
* @param processes defines a current set of processes and properties that provide information about execution
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @return -1 if EOFException or IOException
* @return 0 if success
*/
public static int peerUnchoked(TorrentInfo torrentInfo, CommunicationInfo commInfo, ProcessHandler processes, Peer peer, DataInputStream input, DataOutputStream output){
RUBTClient.debugPrint("-----Unchoked :) ----------");
RUBTClient.debugPrint("-----------Starting request file :D ------------");
peer.choked = false;
int len = 0; //the length of the file we are going to download this time.
if ((torrentInfo.file_length - processes.downloaded) < torrentInfo.piece_length)
len = torrentInfo.file_length - processes.downloaded;
else{
len = torrentInfo.piece_length;
}
try{
if(!commInfo.client.downloadComplete){
RUBTClient.debugPrint("download is complete in peer unchoked");
output.write(MessageHandler.P2PMessage.REQUEST.bytes());
//total number of indexes - index left to download.
output.writeInt(commInfo.requestedIndex);
output.writeInt(commInfo.downloadOffset);
output.writeInt(len);
output.flush();
RUBTClient.debugPrint("Request for "+len+" bytes from ["+commInfo.requestedIndex+"]["+commInfo.downloadOffset+"] sent!!!");
}
}
catch (EOFException e){
RUBTClient.debugPrint("Reached end of file...");
e.printStackTrace();
return -1;
}
catch(IOException e){
System.err.println(e);
e.printStackTrace();
return -1;
}
return 0;
}
//open a connection
/**
* Receive a message of ID 2(interested message).
* @param commInfo information used for communicating with the peer
* @param peer the corresponding peer
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @return -1 if IOException
* @return 0 if success
*/
public static int peerInterested(CommunicationInfo commInfo, Peer peer, DataInputStream input, DataOutputStream output){
peer.interested = true;
try{
output.write(MessageHandler.P2PMessage.UNCHOKE.bytes());
output.flush();
}
catch(IOException e){
System.out.println(e);
return -1;
}
return 0;
}
//close a connection
/**
* Receive message of ID 3 (Uninterested message).
* @param commInfo information used for communicating with the peer
* @param peer the corresponding peer
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @return 0 on success
*/
public static int peerUninterested(CommunicationInfo commInfo, Peer peer, DataInputStream input, DataOutputStream output){
peer.interested = false;
return 0;
}
//NOT IMPORTANT FOR PHASE2
/**
* Receives a message of ID 4 (have message) and updates the peer's bitfield accordingly.
* @param commInfo information used for communicating with the peer
* @param peer the corresponding peer
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @return -1 if EOFException or IOException
* @return 0 if success
*/
public static int peerHave(CommunicationInfo commInfo, Peer peer, DataInputStream input, DataOutputStream output){
try{
int bytes = commInfo.msgLength;
byte[] buffer = new byte[bytes];
input.readFully(buffer);
int pieceIndex = input.readInt();
if (!peer.getBitfield()[pieceIndex]){
commInfo.client.getPiece(pieceIndex).incrementRarity();
commInfo.client.getRarityQueue().updatePiecePriority(commInfo.client.getPiece(pieceIndex));
}
peer.setBitfieldValue(pieceIndex, true);
commInfo.client.getPiece(pieceIndex).incrementRarity();
commInfo.client.getRarityQueue().updatePiecePriority(commInfo.client.getPiece(pieceIndex));
RUBTClient.debugPrint("Peer: " + peer.getIP()+ " -- " + peer.getBitfield());
output.write(MessageHandler.P2PMessage.INTERESTED.bytes());
output.flush();
}
catch (EOFException e){
RUBTClient.debugPrint("Reached end of file...");
e.printStackTrace();
return -1;
}
catch(IOException e){
System.err.println(e);
e.printStackTrace();
return -1;
}
return 0;
}
//WORKS
/**
* Sends a single Have message (message ID 4) to all of the client's local peers to indicate a new piece the client has obtained
* Executed after the successful download of a piece.
* @param torrentInfo information retrieved from the parsed torrent file
* @param commInfo information used for communicating with the peer
* @param processes defines a current set of processes and properties that provide information about execution
* @param pieceIndex index of the newly obtained piece
* @return -1 if EOFException or IOException
* @return 0 if success
*/
public static int sendHave(CommunicationInfo commInfo, ProcessHandler processes, int pieceIndex){
try{
List<PeerSwingWorker> peerWorkers = processes.getPeerWorkers();
DataOutputStream output;
for (int i = 0; i<peerWorkers.size();i++){
Socket peerSocket = peerWorkers.get(i).peer.getSocket();
if (peerSocket!=null && !peerSocket.isClosed() && peerWorkers.get(i).peer.validHandshake
&& peerWorkers.get(i).peer.receivedFirstHave && !peerWorkers.get(i).peer.getSentHave()[pieceIndex]){
output = new DataOutputStream(peerWorkers.get(i).peer.getSocket().getOutputStream());
output.write(MessageHandler.P2PMessage.HAVE.bytes());
output.writeInt(pieceIndex);
output.flush();
peerWorkers.get(i).peer.getSentHave()[i] = true;
RUBTClient.debugPrint("Sent have message for index " + pieceIndex + " to peer: " + peerWorkers.get(i).peer);
}
}
}
catch (EOFException e){
RUBTClient.debugPrint("Reached end of file...");
e.printStackTrace();
return -1;
}
catch(IOException e){
System.err.println(e);
e.printStackTrace();
return -1;
}
return 0;
}
/**
* Sends a series of Have messages (message ID 4) to a single peer to indicate the pieces the client currently has.
* Only executed right after the initial handshake.
* @param torrentInfo information retrieved from the parsed torrent file
* @param commInfo information used for communicating with the peer
* @param processes defines a current set of processes and properties that provide information about execution
* @param peer the corresponding peer
* @param peerSocket socket of the corresponding peer
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @param peerSocket socket of the corresponding peer
* @return -1 if EOFException or IOException
* @return 0 if success
*/
public static int sendFirstHave(CommunicationInfo commInfo, ProcessHandler processes, Peer peer, Socket peerSocket, DataOutputStream output){
try{
for (int i = 0; i<processes.getClient().getBitfield().length; i++){
if (processes.getClient().getBitfield()[i]){
if (peerSocket!=null && !peerSocket.isClosed() && !peer.getSentHave()[i]){
output.write(MessageHandler.P2PMessage.HAVE.bytes());
output.writeInt(i);
output.flush();
peer.getSentHave()[i]=true;
RUBTClient.debugPrint("Sent have message for index " + i + " to peer: " + peer);
}
}
}
peer.receivedFirstHave = true;
RUBTClient.debugPrint(peer.toString());
RUBTClient.debugPrint("Sent first have.");
}
catch (EOFException e){
RUBTClient.debugPrint("Reached end of file...");
e.printStackTrace();
return -1;
}
catch(IOException e){
System.err.println(peer);
e.printStackTrace();
return -1;
}
return 0;
}
//WORKS
/**
* Sends a message of message ID 5 (bitfield message).
* Sends the client's bitfield to the peer. Only executed immediately after the handshake message.
* @param commInfo information used for communicating with the peer
* @param peer the corresponding peer
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @return -1 if IOException
* @return 0 if success
*/
public static int sendBitfield(CommunicationInfo commInfo, Peer peer, DataInputStream input, DataOutputStream output){
try {
byte[] clientBitfieldBytes = Bitfield.toByteArray(commInfo.client.getBitfield());
RUBTClient.debugPrint("Sending bitfield to Peer: " + peer + "\n\t" + Arrays.toString(clientBitfieldBytes));
output.write(P2PMessage.BITFIELD.msgWithLength(clientBitfieldBytes.length));
output.write(clientBitfieldBytes);
RUBTClient.debugPrint("bitfield sent");
for (int i = 0; i<commInfo.client.getBitfield().length; i++){
if (commInfo.client.getBitfield()[i]){
peer.getSentHave()[i]=true;
}
}
return 0;
} catch (IOException ex) {
Logger.getLogger(MessageHandler.class.getName()).log(Level.SEVERE, null, ex);
return -1;
}
}
//WORKS
/**
* Receives message of message ID 5 (bitfield).
* Reads in the bitfield and saves it as the peer's bitfield.
* @param commInfo information used for communicating with the peer
* @param peer the corresponding peer
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @return -1 if EOFException or IOException
* @return 0 if downloading was a success, but download is not yet complete
*/
public static int receivedBitfield(CommunicationInfo commInfo, Peer peer, DataInputStream input, DataOutputStream output){
try{
int bytes = commInfo.msgLength-1;
RUBTClient.debugPrint("Bitfield length: " + (int)bytes);
byte[] buffer = new byte[bytes];
input.readFully(buffer);
int numPieces = commInfo.torrentInfo.piece_hashes.length;
Bitfield peerBitfield = new Bitfield(numPieces, buffer);
peer.setBitfield(peerBitfield.toBooleanArray());
for (int i = 0; i < peer.getBitfield().length; i++){
commInfo.client.getPiece(i).incrementRarity();
commInfo.client.getRarityQueue().updatePiecePriority(commInfo.client.getPiece(i));
}
//RUBTClient.debugPrint("Peer bitfield: " + peerBitfield);
//RUBTClient.debugPrint("Peer bitfield boolean: " + Arrays.toString(peerBitfield.toBooleanArray()));
output.write(MessageHandler.P2PMessage.INTERESTED.bytes());
output.flush();
}
catch (EOFException e){
RUBTClient.debugPrint("Reached end of file...");
e.printStackTrace();
return -1;
}
catch(IOException e){
System.err.println(e);
e.printStackTrace();
return -1;
}
return 0;
}
//WORKS
/**
* Response to Message ID 6 (request).
* Reads in the information from the request message and uploads the appropriate block to the peer.
* @param torrentInfo information retrieved from the parsed torrent file
* @param commInfo information used for communicating with the peer
* @param processes defines a current set of processes and properties that provide information about execution
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @param peerSocket socket of the corresponding peer
* @return -1 if IOException
* @return 0 if success
*/
public static int peerRequest(TorrentInfo torrentInfo, CommunicationInfo commInfo, ProcessHandler processes,
Peer peer, Socket peerSocket, DataInputStream input, DataOutputStream output){
try {
int pieceIndex = input.readInt();
RUBTClient.debugPrint("Peer Requested index: " + pieceIndex);
//byte[] clientBitfieldBytes = Bitfield.toByteArray(commInfo.client.getBitfield());
if(!commInfo.client.getBitfield()[pieceIndex]){
RUBTClient.debugPrint("We don't have piece at index: " + pieceIndex);
output.write(MessageHandler.P2PMessage.CHOKE.bytes());
}
else{
int begin = input.readInt();
int pieceLength = input.readInt();
RUBTClient.debugPrint("Begin: " + begin + " -- Length: " + pieceLength);
//send requested piece
byte[] bytesToSend = new byte[pieceLength];
for(int i = 0; i<pieceLength; i++){
bytesToSend[i] = commInfo.client.getPieces()[pieceIndex].getDataByte(i+begin);
}
output.writeInt(9+pieceLength);
output.write((byte)7);
output.writeInt(pieceIndex);
output.writeInt(begin);
output.write(bytesToSend);
peer.downloaded+=bytesToSend.length;
processes.uploaded+=pieceLength;
RUBTClient.debugPrint("UPLOADED INDEX: " + pieceIndex);
RUBTClient.debugPrint("UPLOADED: " + processes.uploaded);
}
return 0;
} catch (IOException e) {
System.err.println(e);
e.printStackTrace();
return -1;
}
}
//WORKS
/**
* Sends message of Message ID 6 (request).
* Requests that the peer send a particular block to the client.
* @param torrentInfo information retrieved from the parsed torrent file
* @param commInfo information used for communicating with the peer
* @param processes defines a current set of processes and properties that provide information about execution
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @return -1 if IOException
* @return 0 if success
*/
public static int sendRequest(TorrentInfo torrentInfo, CommunicationInfo commInfo, ProcessHandler processes,
DataInputStream input, DataOutputStream output){
try {
int len;
output.write(MessageHandler.P2PMessage.KEEP_ALIVE.bytes());
output.write(MessageHandler.P2PMessage.REQUEST.bytes());
output.writeInt(commInfo.requestedIndex);
RUBTClient.debugPrint("Index Request: "+ commInfo.requestedIndex);
output.writeInt(commInfo.downloadOffset);
//if requested the last index
if (commInfo.requestedIndex==torrentInfo.piece_hashes.length-1){
len = torrentInfo.file_length - (torrentInfo.piece_hashes.length-1)*torrentInfo.piece_length;
}
else {
len = torrentInfo.piece_length;
}
RUBTClient.debugPrint("Requested length "+len);
output.writeInt(len);
output.flush();
} catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}
return 0;
}
/**
* Response to Message ID 7 (piece).
* Downloads a block sent by the peer.
* @param torrentInfo information retrieved from the parsed torrent file
* @param commInfo information used for communicating with the peer
* @param processes defines a current set of processes and properties that provide information about execution
* @param input DataInputStream used to manage incoming information from the peer
* @param output DataOutputStream used to manage outgoing information to the peer
* @param raFile RandomAccessFile used for writing downloaded blocks to the file
* @return -1 if EOFException or IOException
* @return 0 if downloading was a success, but download is not yet complete
* @return 1 if download is complete
*/
public static int clientDownloadPiece(TorrentInfo torrentInfo, CommunicationInfo commInfo, ProcessHandler processes,
Peer peer, DataInputStream input, DataOutputStream output, RandomAccessFile raFile){
RUBTClient.debugPrint("------------We can download now :D---------------");
try{
/*--------------------DOWNLOAD--------------------*/
byte[] block;
int blockLength = commInfo.msgLength-9;
int pieceIndex = input.readInt();
int offset = input.readInt();
block = new byte[blockLength];
RUBTClient.debugPrint("READ FIRST BLOCK +"+ offset+" of "+blockLength+" bytes!!!!!!!!!!!!");
RUBTClient.debugPrint("piece length: " + torrentInfo.piece_length);
int bytesRead = 0;
while (bytesRead < blockLength) {
bytesRead += input.read(block, bytesRead, blockLength-bytesRead);
}
RUBTClient.debugPrint(" Total Read: " + bytesRead + " bytes");
/*--------------------END DOWNLOAD--------------------*/
/*--------------------VERIFY PIECE HASH--------------------*/
byte[] dataHash = sha1Bytes(block);
boolean isValid = Arrays.equals(dataHash,torrentInfo.piece_hashes[pieceIndex].array());
if (!isValid){
RUBTClient.debugPrint("DATA HASH: " + Arrays.toString(dataHash));
RUBTClient.debugPrint("PIECE HASH: " + Arrays.toString(torrentInfo.piece_hashes[pieceIndex].array()));
RUBTClient.debugPrint("INVALID PIECE HASH. DOWNLOAD WAS UNSUCCESSFUL.");
System.err.println("ERROR: invalid piece index: " + pieceIndex);
return -1;
}
/*--------------------END VERIFY PIECE HASH--------------------*/
/*--------------------WRITE BLOCK--------------------*/
commInfo.client.addBlock(pieceIndex, block, offset);
commInfo.client.getPiece(pieceIndex).setVerified(isValid); //assumes we downloaded an entire piece... should edit this later
raFile.seek(commInfo.requestedIndex*torrentInfo.piece_length);
raFile.write(block);
RUBTClient.debugPrint("-----------download this part successfully------------");
/*--------------------END WRITE BLOCK--------------------*/
/*--------------------UPDATE BITFIELD--------------------*/
sendHave(commInfo, processes, pieceIndex); //edit this later to only send a have message if we've downloaded the entire piece and not just a block of it
commInfo.client.setBitfieldValue(pieceIndex, true);
//RUBTClient.debugPrint("BITFIELD: " + Arrays.toString(commInfo.client.getBitfield()));
/*--------------------END UPDATE BITFIELD--------------------*/
/*--------------------UPDATE COMM INFO--------------------*/
processes.downloaded+=block.length;
peer.uploaded+=block.length;
/*if(block.length < torrentInfo.piece_length){
processes.downloaded+= block.length;
}
else{
processes.downloaded += torrentInfo.piece_length;
}*/
processes.left = torrentInfo.file_length - processes.downloaded;
RUBTClient.debugPrint("Total length: "+ torrentInfo.file_length);
RUBTClient.debugPrint("Downloaded length: "+ processes.downloaded);
RUBTClient.debugPrint("Percent: "+ (int)(100*processes.downloaded/torrentInfo.file_length) + "%");
RUBTClient.debugPrint("total index "+ (torrentInfo.piece_hashes.length));
/*--------------------END UPDATE COMM INFO--------------------*/
if (processes.downloaded >= torrentInfo.file_length) {
processes.downloaded = torrentInfo.file_length;
processes.left = 0;
commInfo.client.downloadComplete=true;
commInfo.client.getRarityQueue().clear();
RUBTClient.debugPrint("Download complete!!!");
return 1;
}
/*--------------------UPDATE REQUESTED INDEX, RARITY QUEUE--------------------*/
RUBTClient.debugPrint(commInfo.client.getRarityQueue().toString());
Piece temp = commInfo.client.getPieces()[commInfo.requestedIndex];
commInfo.client.getRarityQueue().remove(temp);
commInfo.requestedIndex = commInfo.client.nextRequest();
}
catch (EOFException e){
RUBTClient.debugPrint("Reached end of file...");
e.printStackTrace();
return -1;
}
catch(IOException e) {
//System.err.println(e);
//e.printStackTrace();
return -1;
}
return 0;
}
//NOT IMPLEMENTED YET
public static int clientCancel(){
return 0;
}
//NOT IMPLEMENTED YET
public static int getPeerPort(){
return 0;
}
}