I am trying to implement a BitTorrent client and have implemented many features including parsing the file and connecting to peers and read/send info from the socket but now I am confused on how I put all this together.
public void send_handshake() throws IOException {
byte[] l = {(byte) 19}, name = "BitTorrent protocol".getBytes(), reserved_bytes = new byte[8],
info_hash = torrent.info_hash_as_binary, peer_id = "12345678900987654321".getBytes();
byte[] c = combine(l,name,reserved_bytes,info_hash,peer_id);
out.write(c);
System.out.println("Sent Handshake");
}
public void send_request() throws Exception {
//variables initialized
for (int i = 0; i < numofblocks ; i++) {
bytes_offset = ByteBuffer.allocate(4).putInt(i*(int)Math.pow(2,14)).array();
block_length = ByteBuffer.allocate(4).putInt((int)Math.pow(2,14)).array();
if(lastblocklen!=0 && i==numofblocks-1){
block_length = ByteBuffer.allocate(4).putInt((int)lastblocklen).array();
System.out.println(Main.ANSI_BLUE + "Last block length is : " + new BigInteger(block_length).intValueExact() + " its size : "+ block_length.length + Main.ANSI_RESET);
}
out.write(SendRequest); // Sendrequest = all the byte array fields combined
System.out.println("Thread : "+ thread_num +" : " + "////// Sent request Message for block " + i + " of piece " + next_piece +" //////");
}
}
public int read_message() throws Exception {
boolean handshake = false;
byte[] len = new byte[4];
int offset=0;
while(offset<len.length) {
int t = in.read(len, offset, len.length - offset);
if(t>0)offset += t;
if(t==-1) return -1; //EOS error
}
if((int)len[0]==19 && (byte)len[1]=='B') handshake = true;
int length_of_message = new BigInteger(len).intValue();
byte[] messageid = new byte[1];
int rep = in.read(messageid,0,1);
int message_id = new BigInteger(messageid).intValueExact();
if(handshake){
// READ HANDSHAKE MESSAGE
}
else if (message_id == 5) {
try {
read_Bitfield(length_of_message);
return 0;
}
catch (Exception e){
throw new Exception();
}
}
else if (message_id == 4) {
read_Have(length_of_message);
}
else if (message_id == 1) {
//UNCHOKE MESSAGE
return 1; //unchoke
}
else if(message_id == 0){
//CHOKE MESSAGE
return 3;
}
else if(message_id == 7){
read_piece(length_of_message);
}
else if(message_id == 8){
//READ CANCEL MESSAGE
}
else {
System.out.println("message id not matched - message id : " + message_id);
throw new Exception();
}
return 10;
}
public int read_piece(int length_of_message) throws Exception {
//PIECE MESSAGE
//variables initialized
while(off<index_of_piece.length) {
int t1 = in.read(index_of_piece, off, index_of_piece.length - off);
if (t1 > 0) off += t1;
}
off = 0;
while(off<bytes_sent.length) {
int t1 = in.read(bytes_sent, off, bytes_sent.length - off);
if (t1 > 0) off += t1;
}
off = 0;
while(off<data.length) {
int t1 = in.read(data, off, data.length - off);
if (t1 > 0) off += t1;
}
int index = new BigInteger(index_of_piece).intValueExact();
File f = new File(path + index); //path = location to my program directory
try (FileOutputStream fos = new FileOutputStream(path+index, true)) {
fos.write(data);
}
int bytes_sent_integer = new BigInteger(bytes_sent).intValueExact();
double blocknum = (bytes_sent_integer/Math.pow(2,14));
System.out.println("Thread : " + thread_num + " Written (Appended) block " + blocknum + " to piece file " + index);
if(blocknum==numofblocks-1){
System.out.println("Thread : " + thread_num + " ////// We now have piece "+ index + " //////");
if(torrent.piece_hash_values_as_hex.contains(
Utils.bytesToHex(createSha1(new File(path+index))).toUpperCase())
) {
System.out.println("Thread : "+ thread_num +" : " + "CORRECTLY DOWNLOADED PIECE " + index);
synchronized (pieces){
pieces[index] = 1;
}
}
else{
System.out.println("Hash Mismatch...");
synchronized (pieces){
System.err.println("Piece value of " + index + " set to "+0);
pieces[index] = 0;
}
return -7;
}
}
return 0;
}
I tried making two threads, one that sends request packets to a connected peer and one that continuously reads the InputStream from the socket for responses but it fails due to improper synchronization between the threads (at the end of the download the reader thread reading wrong values continuously). Please suggest possible ways I can implement this.