Merge branch 'master' of mojotrollz.eu:college
This commit is contained in:
commit
b7131cc41e
@ -1,6 +1,11 @@
|
||||
package node;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.nio.ByteBuffer;
|
||||
@ -52,6 +57,11 @@ public class Node {
|
||||
|
||||
private Identifier nodeID = Identifier.getRandomIdentifier(ID_BITS);
|
||||
private IRoutingTable routingTable = new RoutingTableImpl(BUCKET_SIZE, this);
|
||||
|
||||
|
||||
|
||||
private ArrayList<File> files;
|
||||
|
||||
|
||||
public Node() {
|
||||
System.setProperty("java.net.preferIPv4Stack", "true");
|
||||
@ -382,4 +392,57 @@ public class Node {
|
||||
private boolean sendLeave(NodeIdentifier n) {
|
||||
return send(n, MessageType.LEAVE, null, false, null);
|
||||
}
|
||||
|
||||
|
||||
private void sendFile(NodeIdentifier nodeID, File file){
|
||||
|
||||
//calculate chunk size
|
||||
int CHUNK_SIZE = BUFFER_SIZE - 15;
|
||||
|
||||
int FILE_SIZE = (int)file.length();
|
||||
|
||||
int NUMBER_OF_CHUNKS = 0;
|
||||
byte[] temp = null;
|
||||
|
||||
int totalBytesRead = 0;
|
||||
|
||||
Identifier fileID = new Identifier(10, file.getName().getBytes());
|
||||
|
||||
try {
|
||||
InputStream inStream = new BufferedInputStream(new FileInputStream(file));
|
||||
|
||||
while(totalBytesRead < FILE_SIZE){
|
||||
int bytesReamaining = FILE_SIZE-totalBytesRead;
|
||||
if(bytesReamaining < CHUNK_SIZE){
|
||||
CHUNK_SIZE = bytesReamaining;
|
||||
}
|
||||
temp = new byte[CHUNK_SIZE];
|
||||
int bytesRead = inStream.read(temp, 0, CHUNK_SIZE);
|
||||
|
||||
if(bytesRead > 0){
|
||||
totalBytesRead += bytesRead;
|
||||
NUMBER_OF_CHUNKS++;
|
||||
}
|
||||
|
||||
//send chunk
|
||||
String data = fileID.toString()+"|"+NUMBER_OF_CHUNKS+"-"+temp.toString();
|
||||
|
||||
send(nodeID, MessageType.DATA, data.getBytes(), true, null);
|
||||
|
||||
}
|
||||
|
||||
inStream.close();
|
||||
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user