mirror of
https://github.com/binlaab/nanofiles.git
synced 2026-07-01 21:16:41 +02:00
Merge branch 'master' of https://github.com/binlaab/nanofiles
This commit is contained in:
@@ -12,7 +12,7 @@ public class NanoFiles {
|
|||||||
* que combine los DNIs de ambos miembros del grupo de prácticas.
|
* que combine los DNIs de ambos miembros del grupo de prácticas.
|
||||||
*/
|
*/
|
||||||
public static final String PROTOCOL_ID = "123456789A";
|
public static final String PROTOCOL_ID = "123456789A";
|
||||||
private static final String DEFAULT_DIRECTORY_HOSTNAME = "192.168.1.46";
|
private static final String DEFAULT_DIRECTORY_HOSTNAME = "localhost";
|
||||||
public static String sharedDirname = DEFAULT_SHARED_DIRNAME;
|
public static String sharedDirname = DEFAULT_SHARED_DIRNAME;
|
||||||
public static FileDatabase db;
|
public static FileDatabase db;
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -104,6 +104,13 @@ public class NFConnector {
|
|||||||
System.out.println("Nos vamos de gfl");
|
System.out.println("Nos vamos de gfl");
|
||||||
return filelist.toArray(new FileInfo[0]);
|
return filelist.toArray(new FileInfo[0]);
|
||||||
} catch (IOException e) { e.printStackTrace(); return null; }
|
} 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) {
|
public boolean downloadChunk(String hash, int chunkNum, RandomAccessFile raf) {
|
||||||
@@ -129,6 +136,13 @@ public class NFConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) { e.printStackTrace(); }
|
} catch (IOException e) { e.printStackTrace(); }
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
socket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package es.um.redes.nanoFiles.tcp.server;
|
|||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.EOFException;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
@@ -49,7 +50,12 @@ public class NFServer implements Runnable {
|
|||||||
this.stopServer = true;
|
this.stopServer = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (serverSocket != null && !serverSocket.isBound()) {
|
System.out.println("NFServer - tenemos sockets.size() = " + state.getSockets().size());
|
||||||
|
for (Socket s : state.getSockets()) {
|
||||||
|
System.out.println("Socket cerrado? " + s.isClosed());
|
||||||
|
}
|
||||||
|
if (serverSocket != null && serverSocket.isBound()) {
|
||||||
|
System.out.println("Cerramos socket servidor");
|
||||||
serverSocket.close();
|
serverSocket.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e) { e.printStackTrace(); }
|
} catch (IOException e) { e.printStackTrace(); }
|
||||||
@@ -172,10 +178,6 @@ public class NFServer implements Runnable {
|
|||||||
System.out.println("Total connections = " + connNum);
|
System.out.println("Total connections = " + connNum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* TODO: (Boletín SocketsTCP) Añadir métodos a esta clase para: 1) Arrancar el
|
* TODO: (Boletín SocketsTCP) Añadir métodos a esta clase para: 1) Arrancar el
|
||||||
@@ -286,9 +288,20 @@ public class NFServer implements Runnable {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (EOFException e) {
|
||||||
|
// nada
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
socket.close();
|
||||||
|
System.out.println("nfserver - cerramos socket de cliente");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import java.net.DatagramPacket;
|
|||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Enumeration;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import es.um.redes.nanoFiles.tcp.client.NFConnector;
|
import es.um.redes.nanoFiles.tcp.client.NFConnector;
|
||||||
|
|
||||||
@@ -211,6 +213,26 @@ public class DirectoryConnector {
|
|||||||
return directoryHostname;
|
return directoryHostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getClientAddress() {
|
||||||
|
try {
|
||||||
|
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||||
|
|
||||||
|
while (interfaces.hasMoreElements()) {
|
||||||
|
NetworkInterface ni = interfaces.nextElement();
|
||||||
|
if (ni.isUp() && !ni.isVirtual() && !ni.isLoopback()) {
|
||||||
|
Enumeration<InetAddress> direccionesInterfaz = ni.getInetAddresses();
|
||||||
|
|
||||||
|
while (direccionesInterfaz.hasMoreElements()) {
|
||||||
|
InetAddress addr = direccionesInterfaz.nextElement();
|
||||||
|
if (addr instanceof java.net.Inet4Address) return addr.getHostAddress();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) { e.printStackTrace(); }
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Método para "hacer ping" al directorio, comprobar que está operativo y que
|
* Método para "hacer ping" al directorio, comprobar que está operativo y que
|
||||||
* usa un protocolo compatible. Este método no usa mensajes bien formados.
|
* usa un protocolo compatible. Este método no usa mensajes bien formados.
|
||||||
@@ -305,14 +327,9 @@ public class DirectoryConnector {
|
|||||||
|
|
||||||
// este trozo hay que cambiarlo porque qué cojones
|
// este trozo hay que cambiarlo porque qué cojones
|
||||||
// encima tiene algo mal
|
// encima tiene algo mal
|
||||||
String ip = null;
|
|
||||||
try {
|
|
||||||
socket.connect(directoryAddress);
|
|
||||||
ip = socket.getLocalAddress().getHostAddress();
|
|
||||||
socket.disconnect();
|
|
||||||
} catch (Exception e) {}
|
|
||||||
|
|
||||||
DirMessage serve = new DirMessage(DirMessageOps.OPERATION_SERVE, NanoFiles.peerNickname, ip, serverPort);
|
|
||||||
|
DirMessage serve = new DirMessage(DirMessageOps.OPERATION_SERVE, NanoFiles.peerNickname, this.getClientAddress(), serverPort);
|
||||||
byte[] serveBytes = serve.toString().getBytes();
|
byte[] serveBytes = serve.toString().getBytes();
|
||||||
byte[] response = sendAndReceiveDatagrams(serveBytes);
|
byte[] response = sendAndReceiveDatagrams(serveBytes);
|
||||||
String respStr = new String(response, 0, response.length);
|
String respStr = new String(response, 0, response.length);
|
||||||
|
|||||||
Reference in New Issue
Block a user