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:
@@ -7,6 +7,7 @@ import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Map;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import es.um.redes.nanoFiles.tcp.client.NFConnector;
|
||||
|
||||
@@ -80,7 +81,7 @@ public class DirectoryConnector {
|
||||
* datagramas al directorio
|
||||
*/
|
||||
directoryAddress = new InetSocketAddress(InetAddress.getByName(hostname), DIRECTORY_PORT);
|
||||
socket = new DatagramSocket(6767);
|
||||
socket = new DatagramSocket();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -291,18 +292,19 @@ public class DirectoryConnector {
|
||||
*/
|
||||
|
||||
// este trozo hay que cambiarlo porque qué cojones
|
||||
// encima tiene algo mal
|
||||
String ip = null;
|
||||
try {
|
||||
socket.connect(directoryAddress);
|
||||
ip = socket.getLocalAddress().getHostAddress();
|
||||
socket.disconnect();
|
||||
} catch(Exception e) {}
|
||||
socket.connect(directoryAddress);
|
||||
ip = socket.getLocalAddress().getHostAddress();
|
||||
socket.disconnect();
|
||||
} catch (Exception e) {}
|
||||
|
||||
DirMessage serve = new DirMessage(DirMessageOps.OPERATION_SERVE, NanoFiles.peerNickname, ip, serverPort);
|
||||
byte[] serveBytes = serve.toString().getBytes();
|
||||
byte[] response = sendAndReceiveDatagrams(serveBytes);
|
||||
String respStr = new String(response, 0, response.length);
|
||||
DirMessage respServe= DirMessage.fromString(respStr);
|
||||
DirMessage respServe = DirMessage.fromString(respStr);
|
||||
success = respServe.getOperation().equals(DirMessageOps.OPERATION_SERVE_OK);
|
||||
|
||||
if (success) {
|
||||
@@ -345,9 +347,34 @@ public class DirectoryConnector {
|
||||
|
||||
public Map<String, InetSocketAddress[]> searchFilesByHash(String hashSubstring) {
|
||||
Map<String, InetSocketAddress[]> results = new LinkedHashMap<String, InetSocketAddress[]>();
|
||||
|
||||
|
||||
|
||||
|
||||
Map<String, InetSocketAddress> peers = getPeerList();
|
||||
|
||||
for (InetSocketAddress addr : peers.values()) {
|
||||
try {
|
||||
NFConnector nfc = new NFConnector(addr);
|
||||
FileInfo[] peerFiles = nfc.getFileList();
|
||||
|
||||
// la longitud tiene que ser EXACTAMENTE 1, si es 0 no hay, si es > 1 es ambiguo
|
||||
// y si resulta que dos peers tienen un mismo subhash sin tener el mismo hash?
|
||||
// mando que no se ha podido descargar? comparo contra el hash del primero?
|
||||
FileInfo[] peerFilesFound = FileInfo.lookupHashSubstring(peerFiles, hashSubstring);
|
||||
|
||||
for (FileInfo fi : peerFilesFound) {
|
||||
InetSocketAddress[] peersWithHash = results.getOrDefault(fi.fileHash, null);
|
||||
|
||||
if (peersWithHash == null) {
|
||||
peersWithHash = new InetSocketAddress[1];
|
||||
peersWithHash[0] = addr;
|
||||
} else {
|
||||
peersWithHash = Arrays.copyOf(peersWithHash, peersWithHash.length + 1);
|
||||
peersWithHash[peersWithHash.length - 1] = addr;
|
||||
}
|
||||
|
||||
results.put(fi.fileHash, peersWithHash);
|
||||
}
|
||||
} catch (IOException e) { e.printStackTrace(); }
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user