This commit is contained in:
Mongolila-098
2026-04-29 18:31:15 +02:00
3 changed files with 32 additions and 21 deletions

View File

@@ -177,6 +177,10 @@ public class NFControllerLogicP2P {
} else {
try {
if (dirLogic.fetchPeerList().get(targetPeerNickname) == null) {
System.err.println(targetPeerNickname + " is not a registered peer");
return success;
}
InetSocketAddress[] peerAddr = new InetSocketAddress[] {dirLogic.fetchPeerList().get(targetPeerNickname)};
NFConnector nfc = new NFConnector(peerAddr[0]);
FileInfo[] peerFiles = nfc.getFileList();
@@ -226,14 +230,14 @@ public class NFControllerLogicP2P {
FileInfo[] files = nfc.getFileList();
FileInfo fileToDownload = FileInfo.lookupHashSubstring(files, targetHash)[0]; // espero solo un archivo
filesize = fileToDownload.fileSize;
filename = toDisplayPath(FileNameUtil.chooseAvailableName(fileToDownload.fileName));
filename = toDisplayPath(FileNameUtil.chooseAvailableName(NanoFiles.sharedDirname + "/" + fileToDownload.fileName));
}
peerConns[i] = nfc;
} catch (IOException e) { e.printStackTrace(); }
}
try (RandomAccessFile raf = new RandomAccessFile(NanoFiles.sharedDirname + "/" + filename, "rw")) {
try (RandomAccessFile raf = new RandomAccessFile(filename, "rw")) {
raf.setLength(filesize);
int chunks = (int) Math.ceil((double) filesize / NFConnector.CHUNK_SIZE);
@@ -246,8 +250,15 @@ public class NFControllerLogicP2P {
downloaded = true;
} catch (IOException e) { e.printStackTrace(); }
finally {
for (NFConnector nfc : peerConns) {
if (nfc != null) {
nfc.close();
}
}
}
downloaded = targetHash.equals(FileDigest.computeFileChecksumString(NanoFiles.sharedDirname + "/" + filename));
downloaded = targetHash.equals(FileDigest.computeFileChecksumString(filename));
return downloaded;
}

View File

@@ -43,11 +43,16 @@ public class NFConnector {
} catch (Exception e) { e.printStackTrace(); }
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
}
public void close() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void test() {
/*
* done: (Boletín SocketsTCP) Enviar entero cualquiera a través del socket y
@@ -104,13 +109,6 @@ public class NFConnector {
System.out.println("Nos vamos de gfl");
return filelist.toArray(new FileInfo[0]);
} catch (IOException e) { e.printStackTrace(); return null; }
finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public boolean downloadChunk(String hash, int chunkNum, RandomAccessFile raf) {
@@ -136,13 +134,6 @@ public class NFConnector {
}
} catch (IOException e) { e.printStackTrace(); }
finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return success;
}

View File

@@ -214,7 +214,7 @@ public class DirectoryConnector {
}
public String getClientAddress() {
try {
/* try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
@@ -230,7 +230,16 @@ public class DirectoryConnector {
}
} catch (Exception e) { e.printStackTrace(); }
return null;
*/
// esta forma es una ruina
String ip = null;
try {
socket.connect(directoryAddress);
ip = socket.getLocalAddress().getHostAddress();
socket.disconnect();
} catch (Exception e) {}
return ip;
}
/**