mirror of
https://github.com/binlaab/nanofiles.git
synced 2026-07-01 18:16:29 +02:00
empezada implementación peerdl, cambiar DEFAULT_DIRECTORY_HOSTNAME en NanoFiles
This commit is contained in:
@@ -19,7 +19,8 @@ import es.um.redes.nanoFiles.util.FileInfo;
|
||||
public class NFConnector {
|
||||
private Socket socket;
|
||||
private InetSocketAddress serverAddr;
|
||||
|
||||
|
||||
public static final int CHUNK_SIZE = 64 * 1024; // 64 kB máx
|
||||
|
||||
private DataInputStream dis;
|
||||
private DataOutputStream dos;
|
||||
@@ -84,7 +85,9 @@ public class NFConnector {
|
||||
LinkedList<FileInfo> filelist = new LinkedList<>();
|
||||
PeerMessage msgOut = new PeerMessage(PeerMessageOps.OPCODE_REQUEST_PEER_FILES);
|
||||
msgOut.writeMessageToOutputStream(dos);
|
||||
System.out.println("gfl enviado");
|
||||
PeerMessage msgIn = PeerMessage.readMessageFromInputStream(dis);
|
||||
System.out.println("gfl leido");
|
||||
if (msgIn.getOpcode() == PeerMessageOps.OPCODE_PEER_FILE) {
|
||||
boolean last = false;
|
||||
while (!last) {
|
||||
@@ -93,31 +96,41 @@ public class NFConnector {
|
||||
String name = msgIn.getFilenameVal();
|
||||
FileInfo file = new FileInfo(hash, name, size, null);
|
||||
filelist.add(file);
|
||||
last = msgIn.getLast();
|
||||
last = msgIn.getLast();
|
||||
System.out.println("Tenemos otro archivo, last = " + last);
|
||||
if (!last) msgIn = PeerMessage.readMessageFromInputStream(dis); // tengo que ver si puedo hacerlo diferente
|
||||
}
|
||||
} else { return null; }
|
||||
System.out.println("Nos vamos de gfl");
|
||||
return filelist.toArray(new FileInfo[0]);
|
||||
} catch (IOException e) { e.printStackTrace(); return null; }
|
||||
}
|
||||
|
||||
public byte[] downloadChunk(String hash, int chunkNum) {
|
||||
public boolean downloadChunk(String hash, int chunkNum, RandomAccessFile raf) {
|
||||
boolean success = false;
|
||||
try {
|
||||
PeerMessage msgOut = new PeerMessage(PeerMessageOps.OPCODE_REQUEST_PEER_DL);
|
||||
msgOut.setFileHash(hash);
|
||||
msgOut.setChunkNum(chunkNum);
|
||||
|
||||
msgOut.writeMessageToOutputStream(dos);
|
||||
|
||||
PeerMessage msgIn = PeerMessage.readMessageFromInputStream(dis);
|
||||
|
||||
if (msgIn.getOpcode() == PeerMessageOps.OPCODE_PEER_DL) {
|
||||
return msgIn.getFileData();
|
||||
byte[] datos = msgIn.getFileData();
|
||||
raf.seek((long) chunkNum * CHUNK_SIZE); // buscamos el trozo que estamos leyendo
|
||||
raf.write(datos);
|
||||
success = true;
|
||||
|
||||
} else if (msgIn.getOpcode() == PeerMessageOps.OPCODE_AMBIGUOUS) {
|
||||
System.err.println("More than one file exists with this hash substring");
|
||||
} else if (msgIn.getOpcode() == PeerMessageOps.OPCODE_FILE_NOT_FOUND) {
|
||||
System.err.println("No file matches the hash substring");
|
||||
}
|
||||
|
||||
} catch (IOException e) { e.printStackTrace(); }
|
||||
|
||||
return null;
|
||||
return success;
|
||||
}
|
||||
|
||||
public InetSocketAddress getServerAddr() {
|
||||
|
||||
Reference in New Issue
Block a user