mirror of
https://github.com/binlaab/nanofiles.git
synced 2026-07-01 20:06:28 +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() {
|
||||
|
||||
@@ -89,6 +89,7 @@ public class PeerMessage {
|
||||
}
|
||||
|
||||
public void setFilenameVal(String filenameVal) {
|
||||
this.filenameLong = (byte) filenameVal.length();
|
||||
this.filenameVal = filenameVal;
|
||||
}
|
||||
|
||||
@@ -130,6 +131,8 @@ public class PeerMessage {
|
||||
PeerMessage message = new PeerMessage(opcode);
|
||||
switch (opcode) {
|
||||
case PeerMessageOps.OPCODE_REQUEST_PEER_FILES:
|
||||
case PeerMessageOps.OPCODE_FILE_NOT_FOUND:
|
||||
case PeerMessageOps.OPCODE_AMBIGUOUS:
|
||||
case PeerMessageOps.OPCODE_PEER_FILES_ERROR: break;
|
||||
case PeerMessageOps.OPCODE_PEER_FILE: {
|
||||
|
||||
@@ -137,6 +140,7 @@ public class PeerMessage {
|
||||
byte lastVal = dis.readByte();
|
||||
|
||||
if (lastVal == 1) {
|
||||
System.out.println("rmfis - last = " + last);
|
||||
last = true;
|
||||
}
|
||||
long fileSize = dis.readLong();
|
||||
@@ -158,19 +162,22 @@ public class PeerMessage {
|
||||
}
|
||||
|
||||
case PeerMessageOps.OPCODE_REQUEST_PEER_DL: {
|
||||
// buscar archivo supongo
|
||||
}
|
||||
case PeerMessageOps.OPCODE_PEER_DL: {
|
||||
int longitudSubHash = (int) dis.readByte();
|
||||
byte[] subHash = new byte[longitudSubHash];
|
||||
dis.readFully(subHash);
|
||||
|
||||
|
||||
|
||||
message.setFileHash(new String(subHash));
|
||||
message.setChunkNum(dis.readInt());
|
||||
break;
|
||||
}
|
||||
|
||||
case PeerMessageOps.OPCODE_PEER_DL: {
|
||||
int length = dis.readInt();
|
||||
byte[] datos = new byte[length];
|
||||
dis.readFully(datos);
|
||||
message.setFileData(datos);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
default:
|
||||
System.err.println("PeerMessage.readMessageFromInputStream doesn't know how to parse this message opcode: "
|
||||
@@ -193,6 +200,8 @@ public class PeerMessage {
|
||||
System.out.println(opcode);
|
||||
switch (opcode) {
|
||||
case PeerMessageOps.OPCODE_REQUEST_PEER_FILES:
|
||||
case PeerMessageOps.OPCODE_FILE_NOT_FOUND:
|
||||
case PeerMessageOps.OPCODE_AMBIGUOUS:
|
||||
case PeerMessageOps.OPCODE_PEER_FILES_ERROR: break;
|
||||
case PeerMessageOps.OPCODE_PEER_FILE: {
|
||||
byte lastVal = 0;
|
||||
@@ -207,9 +216,20 @@ public class PeerMessage {
|
||||
dos.write(filenameVal.getBytes());
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
case PeerMessageOps.OPCODE_REQUEST_PEER_DL: {
|
||||
byte[] hash = fileHash.getBytes();
|
||||
dos.writeByte((byte) hash.length);
|
||||
dos.write(hash);
|
||||
dos.writeInt(chunkNum);
|
||||
break;
|
||||
}
|
||||
|
||||
case PeerMessageOps.OPCODE_PEER_DL: {
|
||||
dos.writeInt(fileData.length);
|
||||
dos.write(fileData);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
System.err.println("PeerMessage.writeMessageToOutputStream found unexpected message opcode " + opcode + "("
|
||||
|
||||
@@ -20,6 +20,7 @@ public class PeerMessageOps {
|
||||
public static final byte OPCODE_FILE_NOT_FOUND = 5;
|
||||
public static final byte OPCODE_AMBIGUOUS = 6;
|
||||
public static final byte OPCODE_PEER_DL = 7;
|
||||
public static final byte OPCODE_PEER_DL_ERROR = 8;
|
||||
|
||||
|
||||
|
||||
@@ -36,7 +37,8 @@ public class PeerMessageOps {
|
||||
OPCODE_REQUEST_PEER_DL,
|
||||
OPCODE_FILE_NOT_FOUND,
|
||||
OPCODE_AMBIGUOUS,
|
||||
OPCODE_PEER_DL
|
||||
OPCODE_PEER_DL,
|
||||
OPCODE_PEER_DL_ERROR
|
||||
};
|
||||
private static final String[] _valid_operations_str = { "INVALID_OPCODE",
|
||||
"REQUEST_PEER_FILES",
|
||||
@@ -45,7 +47,8 @@ public class PeerMessageOps {
|
||||
"REQUEST_PEER_DL",
|
||||
"FILE_NOT_FOUND",
|
||||
"FILE_AMBIGUOUS",
|
||||
"PEER_DL"
|
||||
"PEER_DL",
|
||||
"PEER_DL_ERROR"
|
||||
};
|
||||
|
||||
private static Map<String, Byte> _operation_to_opcode;
|
||||
|
||||
@@ -3,11 +3,13 @@ package es.um.redes.nanoFiles.tcp.server;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
import es.um.redes.nanoFiles.application.NanoFiles;
|
||||
import es.um.redes.nanoFiles.tcp.client.NFConnector;
|
||||
import es.um.redes.nanoFiles.tcp.message.PeerMessage;
|
||||
import es.um.redes.nanoFiles.tcp.message.PeerMessageOps;
|
||||
import es.um.redes.nanoFiles.util.FileInfo;
|
||||
@@ -104,7 +106,7 @@ public class NFServer implements Runnable {
|
||||
System.out.println("sent " + PeerMessageOps.opcodeToOperation(msgOut.getOpcode()));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// ???????
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,24 +209,62 @@ public class NFServer implements Runnable {
|
||||
while(true) {
|
||||
|
||||
PeerMessage msgIn = PeerMessage.readMessageFromInputStream(dis);
|
||||
System.out.println("nfserver - " + msgIn.getOpcode());
|
||||
switch (msgIn.getOpcode()) {
|
||||
case PeerMessageOps.OPCODE_REQUEST_PEER_FILES:
|
||||
|
||||
|
||||
System.out.println("hemos llegado a nfserver");
|
||||
FileInfo[] archivos = NanoFiles.db.getFiles();
|
||||
|
||||
for (int i = 0; i < archivos.length; i++) {
|
||||
PeerMessage msgOut = new PeerMessage(PeerMessageOps.OPCODE_PEER_FILE);
|
||||
FileInfo archivo = archivos[i];
|
||||
|
||||
System.out.println("Tenemos una lista de archivos de " + archivos.length);
|
||||
msgOut.setFileHash(archivo.fileHash);
|
||||
msgOut.setFileSize(archivo.fileSize);
|
||||
msgOut.setFilenameVal(archivo.filePath + archivo.fileName);
|
||||
msgOut.setFilenameLong((byte)archivo.fileName.length());
|
||||
msgOut.setFilenameVal(archivo.fileName);
|
||||
msgOut.setLast(false);
|
||||
if (i == archivos.length - 1) msgOut.setLast(true);
|
||||
if (i == archivos.length - 1) { msgOut.setLast(true); System.out.println("Hemos llegado al último archivo"); };
|
||||
msgOut.writeMessageToOutputStream(dos);
|
||||
}
|
||||
break;
|
||||
|
||||
case PeerMessageOps.OPCODE_REQUEST_PEER_DL:
|
||||
String subHash = msgIn.getFileHash();
|
||||
int chunkNum = msgIn.getChunkNum();
|
||||
FileInfo[] files = FileInfo.lookupHashSubstring(NanoFiles.db.getFiles(), subHash);
|
||||
|
||||
if (files.length == 0) {
|
||||
PeerMessage msgOut = new PeerMessage(PeerMessageOps.OPCODE_FILE_NOT_FOUND);
|
||||
msgOut.writeMessageToOutputStream(dos);
|
||||
break;
|
||||
} else if (files.length > 1) {
|
||||
PeerMessage msgOut = new PeerMessage(PeerMessageOps.OPCODE_AMBIGUOUS);
|
||||
msgOut.writeMessageToOutputStream(dos);
|
||||
break;
|
||||
}
|
||||
|
||||
String path = NanoFiles.db.lookupFilePath(files[0].fileHash);
|
||||
try (RandomAccessFile raf = new RandomAccessFile(path, "r")) {
|
||||
long chunk = (long) chunkNum * NFConnector.CHUNK_SIZE;
|
||||
int bytesALeer = (int) Math.min(NFConnector.CHUNK_SIZE, raf.length() - chunk);
|
||||
// si quedan menos de CHUNK_SIZE bytes restantes, readFully se quedaría esperando
|
||||
// a rellenar el buffer, lo cual no va a pasar
|
||||
|
||||
if (bytesALeer > 0) {
|
||||
byte[] datos = new byte[bytesALeer];
|
||||
raf.seek(chunk);
|
||||
raf.readFully(datos);
|
||||
|
||||
PeerMessage msgOut = new PeerMessage(PeerMessageOps.OPCODE_PEER_DL);
|
||||
msgOut.setFileData(datos);
|
||||
msgOut.writeMessageToOutputStream(dos);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
PeerMessage msgOut = new PeerMessage(PeerMessageOps.OPCODE_PEER_DL_ERROR);
|
||||
msgOut.writeMessageToOutputStream(dos);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user