diff --git a/src/clientP2P/ClientDownloadPartTCP.java b/src/clientP2P/ClientDownloadPartTCP.java index e6635cd..e4db082 100644 --- a/src/clientP2P/ClientDownloadPartTCP.java +++ b/src/clientP2P/ClientDownloadPartTCP.java @@ -5,7 +5,6 @@ import java.net.Socket; import java.io.File; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Paths; import protocolP2P.ProtocolP2PPacketTCP; import protocolP2P.Payload; import protocolP2P.LoadRequest; @@ -22,6 +21,7 @@ import remoteException.InternalRemoteError; import remoteException.VersionRemoteError; import remoteException.ProtocolRemoteError; import remoteException.NotFound; +import remoteException.NotATracker; import tools.Logger; import tools.LogLevel; @@ -351,6 +351,10 @@ public class ClientDownloadPartTCP implements Runnable { System.err.println("Error: downloadPart SocketClosed"); logger.writeTCP("downloadPart SocketClosed", LogLevel.Error); return true; + } catch (NotATracker e) { + System.err.println("Error: downloadPart notATracker"); + logger.writeTCP("downloadPart notATracker", LogLevel.Error); + return true; } return false; } diff --git a/src/clientP2P/ClientDownloadPartUDP.java b/src/clientP2P/ClientDownloadPartUDP.java index b818553..84ef0f8 100644 --- a/src/clientP2P/ClientDownloadPartUDP.java +++ b/src/clientP2P/ClientDownloadPartUDP.java @@ -17,9 +17,9 @@ import remoteException.ProtocolRemoteError; import localException.VersionError; import localException.SizeError; import remoteException.NotFound; +import remoteException.NotATracker; import java.nio.file.Files; import java.io.File; -import java.nio.file.Paths; import java.io.IOException; import tools.Logger; import tools.LogLevel; @@ -336,6 +336,10 @@ public class ClientDownloadPartUDP implements Runnable { System.err.println("Error: downloadPart internalError"); logger.writeUDP("downloadPart internalError", LogLevel.Error); return true; + } catch (NotATracker e) { + System.err.println("Error: downloadPart notATracker"); + logger.writeUDP("downloadPart notATracker", LogLevel.Error); + return true; } return false; } diff --git a/src/clientP2P/ClientDownloadTCP.java b/src/clientP2P/ClientDownloadTCP.java index 92bbc43..bb8e932 100644 --- a/src/clientP2P/ClientDownloadTCP.java +++ b/src/clientP2P/ClientDownloadTCP.java @@ -7,7 +7,6 @@ import java.util.Random; import java.io.IOException; import java.io.File; import java.nio.file.Files; -import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.nio.file.StandardCopyOption; import remoteException.EmptyDirectory; @@ -16,6 +15,7 @@ import remoteException.VersionRemoteError; import remoteException.ProtocolRemoteError; import remoteException.NotFound; import remoteException.InternalRemoteError; +import remoteException.NotATracker; import localException.ProtocolError; import localException.InternalError; import localException.TransmissionError; @@ -239,6 +239,9 @@ public class ClientDownloadTCP implements Runnable { } catch (SizeError e) { logger.writeTCP(e, LogLevel.Error); throw new InternalError(); + } catch (NotATracker e) { + logger.writeTCP(e, LogLevel.Error); + throw new InternalError(); } return hash; } catch (IOException e) { @@ -382,6 +385,9 @@ public class ClientDownloadTCP implements Runnable { } catch (NotFound e) { logger.writeTCP(e, LogLevel.Error); throw new InternalError(); + } catch (NotATracker e) { + logger.writeTCP(e, LogLevel.Error); + throw new InternalError(); } } catch (IOException e) { logger.writeTCP(e, LogLevel.Error); diff --git a/src/clientP2P/ClientDownloadUDP.java b/src/clientP2P/ClientDownloadUDP.java index 87d6bab..932019b 100644 --- a/src/clientP2P/ClientDownloadUDP.java +++ b/src/clientP2P/ClientDownloadUDP.java @@ -7,7 +7,6 @@ import java.util.Random; import java.io.IOException; import java.io.File; import java.nio.file.Files; -import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.nio.file.StandardCopyOption; import localException.ProtocolError; @@ -21,6 +20,7 @@ import remoteException.VersionRemoteError; import remoteException.ProtocolRemoteError; import remoteException.NotFound; import remoteException.InternalRemoteError; +import remoteException.NotATracker; import protocolP2P.HashAlgorithm; import protocolP2P.HashResponse; import protocolP2P.HashRequest; @@ -239,6 +239,9 @@ public class ClientDownloadUDP implements Runnable { } catch (SizeError e) { logger.writeUDP(e, LogLevel.Error); throw new InternalError(); + } catch (NotATracker e) { + logger.writeUDP(e, LogLevel.Error); + throw new InternalError(); } return hash; } catch (IOException e) { @@ -378,6 +381,9 @@ public class ClientDownloadUDP implements Runnable { } catch (NotFound e) { logger.writeUDP(e, LogLevel.Error); throw new InternalError(); + } catch (NotATracker e) { + logger.writeUDP(e, LogLevel.Error); + throw new InternalError(); } } catch (IOException e) { logger.writeUDP(e, LogLevel.Error); diff --git a/src/clientP2P/ClientManagementTCP.java b/src/clientP2P/ClientManagementTCP.java index f479df0..28fe131 100644 --- a/src/clientP2P/ClientManagementTCP.java +++ b/src/clientP2P/ClientManagementTCP.java @@ -4,9 +4,7 @@ import java.net.UnknownHostException; import java.util.Scanner; import java.io.IOException; import java.nio.file.Files; -import java.io.File; import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; import java.util.Arrays; import java.util.List; import java.security.MessageDigest; @@ -23,15 +21,12 @@ import remoteException.InternalRemoteError; import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; +import remoteException.NotATracker; import protocolP2P.ProtocolP2PPacketTCP; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import protocolP2P.FileList; -import protocolP2P.FilePart; -import protocolP2P.LoadRequest; import protocolP2P.HashAlgorithm; -import protocolP2P.HashRequest; -import protocolP2P.HashResponse; import clientP2P.ClientDownloadTCP; import tools.HostItem; import tools.Logger; @@ -74,6 +69,7 @@ public class ClientManagementTCP implements Runnable { } System.out.println("Name of the file to download:"); String f = scanner.nextLine(); + scanner.close(); download(f); System.out.println("File " + f + " sucessfully downloaded"); logger.writeTCP("File " + f + " sucessfully downloaded", LogLevel.Info); @@ -203,6 +199,9 @@ public class ClientManagementTCP implements Runnable { System.err.println("listDirectory : SocketClosed"); logger.writeTCP("listDirectory : SocketClosed", LogLevel.Error); throw new ProtocolError(); + } catch (NotATracker e) { + logger.writeTCP(e, LogLevel.Error); + throw new ProtocolError(); } } diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 914dc44..aa1da60 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -4,12 +4,9 @@ import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.io.IOException; -import java.io.File; -import java.net.DatagramSocket; import java.net.UnknownHostException; import java.nio.file.Files; import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import localException.InternalError; @@ -23,15 +20,12 @@ import remoteException.InternalRemoteError; import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; +import remoteException.NotATracker; import protocolP2P.ProtocolP2PPacketUDP; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import protocolP2P.FileList; -import protocolP2P.FilePart; -import protocolP2P.LoadRequest; import protocolP2P.HashAlgorithm; -import protocolP2P.HashRequest; -import protocolP2P.HashResponse; import tools.HostItem; import tools.Logger; import tools.LogLevel; @@ -74,6 +68,7 @@ public class ClientManagementUDP implements Runnable { } System.out.println("Name of the file to download:"); String f = scanner.nextLine(); + scanner.close(); download(f); System.out.println("File " + f + " sucessfully downloaded"); logger.writeUDP("File " + f + " sucessfully downloaded", LogLevel.Info); @@ -199,6 +194,9 @@ public class ClientManagementUDP implements Runnable { } catch (EmptyFile e) { logger.writeUDP(e, LogLevel.Error); throw new ProtocolError(); + } catch (NotATracker e) { + logger.writeUDP(e, LogLevel.Error); + throw new ProtocolError(); } } diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index e08c1fb..139701f 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -6,7 +6,6 @@ import clientP2P.ClientManagementUDP; import clientP2P.ClientManagementTCP; import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementTCP; -import tools.Directories; import tools.Logger; import tools.LogLevel; import tools.Directories; @@ -21,8 +20,8 @@ import tools.HostList; */ public class ClientP2P { - static private final String subdir = "seeded/"; - static private String parts = ".parts"; + private String subdir = "seeded/"; + private String parts = ".parts"; private Logger loggerServer; private Logger loggerClient; private String host; @@ -86,8 +85,8 @@ public class ClientP2P { } // Server threads - ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + subdir, c.port, c.loggerServer, c.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + subdir, c.port, c.loggerServer, c.tracker); + ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + c.subdir, c.port, c.loggerServer, c.tracker); + ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + c.subdir, c.port, c.loggerServer, c.tracker); Thread tudp = new Thread(smudp); tudp.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")"); tudp.start(); @@ -108,6 +107,7 @@ public class ClientP2P { System.out.println("Client : Which transport protocol do you want to use? [TCP/udp]"); Scanner sc = new Scanner(System.in); String transportchoosen = sc.nextLine(); + sc.close(); Thread t; switch(transportchoosen){ case "UDP": @@ -127,7 +127,6 @@ public class ClientP2P { t = new Thread(cmtcp); break; } - t.setName("client P2P-JAVA-PROJECT"); t.start(); } diff --git a/src/localException/InternalError.java b/src/localException/InternalError.java index 08326f9..42794a5 100644 --- a/src/localException/InternalError.java +++ b/src/localException/InternalError.java @@ -2,6 +2,6 @@ package localException; import exception.LocalException; -public class InternalError extends exception.LocalException { +public class InternalError extends LocalException { private static final long serialVersionUID = 12L; } diff --git a/src/localException/ProtocolError.java b/src/localException/ProtocolError.java index 8a8be52..14aeca8 100644 --- a/src/localException/ProtocolError.java +++ b/src/localException/ProtocolError.java @@ -2,6 +2,6 @@ package localException; import exception.LocalException; -public class ProtocolError extends exception.LocalException { +public class ProtocolError extends LocalException { private static final long serialVersionUID = 12L; } diff --git a/src/localException/SizeError.java b/src/localException/SizeError.java index cb927bc..bb13830 100644 --- a/src/localException/SizeError.java +++ b/src/localException/SizeError.java @@ -4,6 +4,6 @@ import exception.LocalException; /** Used on reception side when size as set in Packet is too big, and we cant store this in a int/long as usual. */ -public class SizeError extends exception.LocalException { +public class SizeError extends LocalException { private static final long serialVersionUID = 12L; } diff --git a/src/localException/SocketClosed.java b/src/localException/SocketClosed.java index ceaac40..d3e68c5 100644 --- a/src/localException/SocketClosed.java +++ b/src/localException/SocketClosed.java @@ -2,6 +2,6 @@ package localException; import exception.LocalException; -public class SocketClosed extends exception.LocalException { +public class SocketClosed extends LocalException { private static final long serialVersionUID = 12L; } diff --git a/src/localException/TransmissionError.java b/src/localException/TransmissionError.java index 38fca4f..d5a539b 100644 --- a/src/localException/TransmissionError.java +++ b/src/localException/TransmissionError.java @@ -2,6 +2,6 @@ package localException; import exception.LocalException; -public class TransmissionError extends exception.LocalException { +public class TransmissionError extends LocalException { private static final long serialVersionUID = 12L; } diff --git a/src/localException/VersionError.java b/src/localException/VersionError.java index c0cb7d4..a793e27 100644 --- a/src/localException/VersionError.java +++ b/src/localException/VersionError.java @@ -2,6 +2,6 @@ package localException; import exception.LocalException; -public class VersionError extends exception.LocalException { +public class VersionError extends LocalException { private static final long serialVersionUID = 12L; } diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index da31f08..b013bea 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -1,12 +1,10 @@ package protocolP2P; -import java.util.Arrays; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import localException.TransmissionError; import localException.ProtocolError; import localException.InternalError; import localException.SizeError; -import java.io.UnsupportedEncodingException; import tools.BytesArrayTools; /** Representation of payload for list response. diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index c394b0f..d168a05 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -6,8 +6,6 @@ import localException.InternalError; import localException.SizeError; import localException.TransmissionError; import tools.BytesArrayTools; -import java.util.Arrays; -import java.io.UnsupportedEncodingException; /** Representation of payload for load response. * @author Louis Royer diff --git a/src/protocolP2P/HashRequest.java b/src/protocolP2P/HashRequest.java index 452fd32..1eec671 100644 --- a/src/protocolP2P/HashRequest.java +++ b/src/protocolP2P/HashRequest.java @@ -1,7 +1,6 @@ package protocolP2P; import protocolP2P.Payload; import protocolP2P.HashAlgorithm; -import java.io.UnsupportedEncodingException; import localException.TransmissionError; import localException.SizeError; import localException.ProtocolError; diff --git a/src/protocolP2P/HashResponse.java b/src/protocolP2P/HashResponse.java index 769fbd7..52df91e 100644 --- a/src/protocolP2P/HashResponse.java +++ b/src/protocolP2P/HashResponse.java @@ -2,7 +2,6 @@ package protocolP2P; import protocolP2P.Payload; import java.util.HashMap; import java.util.Map; -import java.io.UnsupportedEncodingException; import localException.TransmissionError; import localException.SizeError; import localException.ProtocolError; diff --git a/src/protocolP2P/LoadRequest.java b/src/protocolP2P/LoadRequest.java index 251981d..1fa0aeb 100644 --- a/src/protocolP2P/LoadRequest.java +++ b/src/protocolP2P/LoadRequest.java @@ -6,7 +6,6 @@ import localException.ProtocolError; import localException.InternalError; import localException.SizeError; import tools.BytesArrayTools; -import java.io.UnsupportedEncodingException; /** Representation of payload for load request. * @author Louis Royer diff --git a/src/protocolP2P/ProtocolP2PPacket.java b/src/protocolP2P/ProtocolP2PPacket.java index 037da9b..74a1dcd 100644 --- a/src/protocolP2P/ProtocolP2PPacket.java +++ b/src/protocolP2P/ProtocolP2PPacket.java @@ -11,7 +11,7 @@ import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; import remoteException.EmptyFile; -import java.net.InetAddress; +import remoteException.NotATracker; import java.io.IOException; import tools.HostItem; @@ -60,6 +60,7 @@ public abstract class ProtocolP2PPacket { /** Receive a response * @throws EmptyFile * @throws NotFound + * @throws NotATracker * @throws EmptyDirectory * @throws InternalRemoteError * @throws VersionRemoteError @@ -72,7 +73,7 @@ public abstract class ProtocolP2PPacket { * @throws IOException * @throws SocketClosed */ - public abstract ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException, SocketClosed; + public abstract ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, NotATracker, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException, SocketClosed; /** Receive a request, subclasses must overwrite this constructor. * @param socket socket used to get the request @@ -108,6 +109,7 @@ public abstract class ProtocolP2PPacket { */ protected void checkProtocolVersion() throws VersionError { if (PROTOCOL_VERSION != version) { + System.err.println("Error: wrong version in packet:" + version); throw new VersionError(); } } diff --git a/src/protocolP2P/ProtocolP2PPacketTCP.java b/src/protocolP2P/ProtocolP2PPacketTCP.java index f2254f8..9272ea6 100644 --- a/src/protocolP2P/ProtocolP2PPacketTCP.java +++ b/src/protocolP2P/ProtocolP2PPacketTCP.java @@ -5,21 +5,19 @@ import localException.SizeError; import localException.TransmissionError; import localException.VersionError; import localException.SocketClosed; +import remoteException.NotATracker; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; import remoteException.EmptyFile; -import tools.BytesArrayTools; import tools.HostItem; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import protocolP2P.LoadRequest; import protocolP2P.FileList; import protocolP2P.FilePart; -import java.util.ArrayList; -import java.lang.Byte; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; @@ -68,9 +66,9 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { socket.close(); } catch (IOException e2) { System.err.println("Cannot close socket"); - } finally { throw new SocketClosed(); } + } } @@ -117,7 +115,6 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { ss.close(); } catch (IOException e2) { System.err.println("Cannot close socket"); - } finally { throw new SocketClosed(); } } @@ -139,6 +136,8 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { case LOAD_RESPONSE: case LIST_RESPONSE: case HASH_RESPONSE: + case DISCOVER_RESPONSE: + case NOT_A_TRACKER: // we were expecting a request, but we are receiving a response throw new ProtocolError(); default : @@ -189,6 +188,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { * @return ProtocolP2PPacket received * @throws EmptyFile * @throws NotFound + * @throws NotATracker * @throws EmptyDirectory * @throws InternalRemoteError * @throws VersionRemoteError @@ -201,7 +201,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { * @throws IOException * @throws SocketClosed */ - public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException, SocketClosed { + public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, NotATracker, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException, SocketClosed { assert requestSocket != null : "Cannot receive response because request packet not sent."; if (requestSocket == null) { throw new InternalError(); @@ -226,6 +226,8 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { throw new NotFound(); case EMPTY_FILE: throw new EmptyFile(); + case NOT_A_TRACKER: + throw new NotATracker(); default : return (ProtocolP2PPacket)p; } diff --git a/src/protocolP2P/ProtocolP2PPacketUDP.java b/src/protocolP2P/ProtocolP2PPacketUDP.java index 4bc98bb..03ffd64 100644 --- a/src/protocolP2P/ProtocolP2PPacketUDP.java +++ b/src/protocolP2P/ProtocolP2PPacketUDP.java @@ -5,6 +5,7 @@ import localException.SizeError; import localException.TransmissionError; import localException.VersionError; import localException.SocketClosed; +import remoteException.NotATracker; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; import remoteException.NotFound; @@ -18,8 +19,6 @@ import protocolP2P.RequestResponseCode; import protocolP2P.LoadRequest; import protocolP2P.FileList; import protocolP2P.FilePart; -import java.util.ArrayList; -import java.lang.Byte; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketAddress; @@ -133,6 +132,8 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { case LOAD_RESPONSE: case LIST_RESPONSE: case HASH_RESPONSE: + case DISCOVER_RESPONSE: + case NOT_A_TRACKER: // we were expecting a request, but we are receiving a response throw new ProtocolError(); default : @@ -186,6 +187,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { * @return ProtocolP2PPacket received * @throws EmptyFile * @throws NotFound + * @throws NotATracker * @throws EmptyDirectory * @throws InternalRemoteError * @throws VersionRemoteError @@ -197,7 +199,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { * @throws SizeError * @throws IOException */ - public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException { + public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, NotATracker, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException { assert requestSocket != null : "Cannot receive response because request packet not sent."; if (requestSocket == null) { throw new InternalError(); @@ -206,6 +208,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { byte[] packet = new byte[8192]; DatagramPacket reception = new DatagramPacket(packet, packet.length); requestSocket.receive(reception); + // contruction try { ProtocolP2PPacketUDP p = new ProtocolP2PPacketUDP(packet); @@ -223,6 +226,8 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { throw new NotFound(); case EMPTY_FILE: throw new EmptyFile(); + case NOT_A_TRACKER: + throw new NotATracker(); default : return (ProtocolP2PPacket)p; } diff --git a/src/remoteException/EmptyDirectory.java b/src/remoteException/EmptyDirectory.java index 12fce76..45e477a 100644 --- a/src/remoteException/EmptyDirectory.java +++ b/src/remoteException/EmptyDirectory.java @@ -2,6 +2,6 @@ package remoteException; import exception.RemoteException; -public class EmptyDirectory extends exception.RemoteException { +public class EmptyDirectory extends RemoteException { private static final long serialVersionUID = 12L; } diff --git a/src/remoteException/EmptyFile.java b/src/remoteException/EmptyFile.java index cbc580f..e5911d9 100644 --- a/src/remoteException/EmptyFile.java +++ b/src/remoteException/EmptyFile.java @@ -2,6 +2,6 @@ package remoteException; import exception.RemoteException; -public class EmptyFile extends exception.RemoteException { +public class EmptyFile extends RemoteException { private static final long serialVersionUID = 12L; } diff --git a/src/remoteException/InternalRemoteError.java b/src/remoteException/InternalRemoteError.java index 802ee05..1d2e92f 100644 --- a/src/remoteException/InternalRemoteError.java +++ b/src/remoteException/InternalRemoteError.java @@ -2,6 +2,6 @@ package remoteException; import exception.RemoteException; -public class InternalRemoteError extends exception.RemoteException { +public class InternalRemoteError extends RemoteException { private static final long serialVersionUID = 12L; } diff --git a/src/remoteException/NotATracker.java b/src/remoteException/NotATracker.java index dbccea2..1f22ce0 100644 --- a/src/remoteException/NotATracker.java +++ b/src/remoteException/NotATracker.java @@ -2,6 +2,6 @@ package remoteException; import exception.RemoteException; -public class NotATracker extends exception.RemoteException { +public class NotATracker extends RemoteException { private static final long serialVersionUID = 12L; } diff --git a/src/remoteException/NotFound.java b/src/remoteException/NotFound.java index f8002bc..c0e61d9 100644 --- a/src/remoteException/NotFound.java +++ b/src/remoteException/NotFound.java @@ -2,6 +2,6 @@ package remoteException; import exception.RemoteException; -public class NotFound extends exception.RemoteException { +public class NotFound extends RemoteException { private static final long serialVersionUID = 12L; } diff --git a/src/remoteException/ProtocolRemoteError.java b/src/remoteException/ProtocolRemoteError.java index c0c6161..1560083 100644 --- a/src/remoteException/ProtocolRemoteError.java +++ b/src/remoteException/ProtocolRemoteError.java @@ -2,6 +2,6 @@ package remoteException; import exception.RemoteException; -public class ProtocolRemoteError extends exception.RemoteException { +public class ProtocolRemoteError extends RemoteException { private static final long serialVersionUID = 12L; } diff --git a/src/remoteException/VersionRemoteError.java b/src/remoteException/VersionRemoteError.java index 775d9cb..4e7a1d5 100644 --- a/src/remoteException/VersionRemoteError.java +++ b/src/remoteException/VersionRemoteError.java @@ -2,6 +2,6 @@ package remoteException; import exception.RemoteException; -public class VersionRemoteError extends exception.RemoteException { +public class VersionRemoteError extends RemoteException { private static final long serialVersionUID = 12L; } diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index a136574..98ce3cd 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -23,12 +23,6 @@ import localException.SizeError; import localException.TransmissionError; import localException.VersionError; import localException.SocketClosed; -import remoteException.EmptyDirectory; -import remoteException.InternalRemoteError; -import remoteException.NotFound; -import remoteException.ProtocolRemoteError; -import remoteException.VersionRemoteError; -import remoteException.EmptyFile; import java.util.Arrays; import tools.Logger; import tools.LogLevel; @@ -360,11 +354,12 @@ public class ServerManagementTCP implements Runnable { logger.writeTCP("Unregistering from tracker", LogLevel.Info); ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Unregister(host)); p.sendRequest((Object)tracker.getTCPSocket()); + //tracker.closeTCPSocket(); logger.writeTCP("Registering into tracker", LogLevel.Info); p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(host)); p.sendRequest((Object)tracker.getTCPSocket()); logger.writeTCP("Registering completed", LogLevel.Debug); - tracker.closeTCPSocket(); + //tracker.closeTCPSocket(); } } diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index c082825..7573bda 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -22,12 +22,6 @@ import localException.SizeError; import localException.TransmissionError; import localException.VersionError; import localException.SocketClosed; -import remoteException.EmptyDirectory; -import remoteException.InternalRemoteError; -import remoteException.NotFound; -import remoteException.ProtocolRemoteError; -import remoteException.VersionRemoteError; -import remoteException.EmptyFile; import java.util.Arrays; import tools.Logger; import tools.LogLevel; @@ -86,10 +80,10 @@ public class ServerManagementUDP implements Runnable { Payload p = pd.getPayload(); switch (p.getRequestResponseCode()) { case LOAD_REQUEST: - LoadRequestManagement(logger, p, pd); + loadRequestManagement(p, pd); break; case LIST_REQUEST: - ListRequestManagement(logger, pd); + listRequestManagement(pd); break; case HASH_REQUEST: logger.writeUDP("Received HASH_REQUEST", LogLevel.Action); @@ -160,7 +154,7 @@ public class ServerManagementUDP implements Runnable { } } - public void LoadRequestManagement(Logger logger, Payload p, ProtocolP2PPacketUDP pd){ + public void loadRequestManagement(Payload p, ProtocolP2PPacketUDP pd){ logger.writeUDP("Received LOAD_REQUEST", LogLevel.Action); assert p instanceof LoadRequest : "payload must be an instance of LoadRequest"; if (!(p instanceof LoadRequest)) { @@ -210,14 +204,14 @@ public class ServerManagementUDP implements Runnable { } } - public void ListRequestManagement(Logger logger, ProtocolP2PPacketUDP pd){ + public void listRequestManagement(ProtocolP2PPacketUDP pd) { logger.writeUDP("Received LIST_REQUEST", LogLevel.Action); try { if (fileList.length == 0) { - logger.writeUDP("Sending EMPTY_DIRECTORY", LogLevel.Action); + logger.writeUDP("Sending EMPTY_DIRECTORY to host " + pd.getHostItem(), LogLevel.Action); pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY))); } else { - logger.writeUDP("Sending LIST_RESPONSE", LogLevel.Action); + logger.writeUDP("Sending LIST_RESPONSE to host " + pd.getHostItem(), LogLevel.Action); pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FileList(fileList)))); } } catch (Exception e2) { diff --git a/src/tools/Directories.java b/src/tools/Directories.java index 97ec091..8c9a86e 100644 --- a/src/tools/Directories.java +++ b/src/tools/Directories.java @@ -99,6 +99,7 @@ public class Directories { System.out.println("Do you want to open this directory? (y/N)"); Scanner scanner = new Scanner(System.in); String resp = scanner.nextLine(); + scanner.close(); if (resp.equals("y") || resp.equals("Y")) { System.out.println("Openning"); openDataHomeDirectory(subdir); diff --git a/src/tools/HostList.java b/src/tools/HostList.java index 510e3ba..eae5e4e 100644 --- a/src/tools/HostList.java +++ b/src/tools/HostList.java @@ -50,6 +50,7 @@ public class HostList { } } while (!servName.equals("stop")); + scanner.close(); return serverList; } } diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index 33f8ad2..32f5394 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -189,7 +189,7 @@ public class TrackerManagementTCP implements Runnable { // add host to known host list HostItem host = ((Register)p).getHostItem(); if (!hostList.contains(host)) { - hostList.add(pd.getHostItem()); + hostList.add(host); } // send a list request try { @@ -197,7 +197,7 @@ public class TrackerManagementTCP implements Runnable { pLReq.sendRequest((Object)host.getTCPSocket()); logger.writeTCP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action); handleListResponse((ProtocolP2PPacketTCP)pLReq.receiveResponse(), host); - host.closeTCPSocket(); + //host.closeTCPSocket(); } catch (Exception e) { // remove from list because list request could not be send hostList.remove(host); @@ -253,7 +253,7 @@ public class TrackerManagementTCP implements Runnable { * @throws InternalError */ private void handleListResponse(ProtocolP2PPacketTCP pd, HostItem host) throws InternalError { - logger.writeTCP("Received LIST RESPONSE from host " + host + ": registered host.", LogLevel.Action); + logger.writeTCP("Received LIST RESPONSE from host " + host, LogLevel.Action); Payload p = pd.getPayload(); assert p instanceof FileList: "payload must be an instance of FileList"; if (!(p instanceof FileList)) { diff --git a/src/tracker/TrackerManagementUDP.java b/src/tracker/TrackerManagementUDP.java index 3a93fe6..ee604c4 100644 --- a/src/tracker/TrackerManagementUDP.java +++ b/src/tracker/TrackerManagementUDP.java @@ -21,6 +21,7 @@ import protocolP2P.DiscoverResponse; import protocolP2P.FileList; import protocolP2P.HashRequest; import localException.InternalError; +import remoteException.EmptyDirectory; /** Tracker management implementation with udp * @author Louis Royer @@ -142,15 +143,21 @@ public class TrackerManagementUDP implements Runnable { // add host to known host list HostItem host = ((Register)p).getHostItem(); if (!hostList.contains(host)) { - hostList.add(pd.getHostItem()); + hostList.add(host); } // send a list request try { - ProtocolP2PPacket pLReq =(ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST)); + ProtocolP2PPacket pLReq = (ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST)); pLReq.sendRequest((Object)host.getUDPSocket()); logger.writeUDP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action); - handleListResponse((ProtocolP2PPacketUDP)pLReq.receiveResponse(), host); + ProtocolP2PPacket resp = pLReq.receiveResponse(); + handleListResponse((ProtocolP2PPacketUDP)resp, host); + logger.writeUDP("Received LIST RESPONSE from host " + pd.getHostItem(), LogLevel.Action); host.closeUDPSocket(); + } catch (EmptyDirectory e) { + logger.writeUDP("Empty Directory", LogLevel.Debug); + hostList.remove(host); + logger.writeUDP("Received EMPTY DIRECTORY from host " + pd.getHostItem() + ". Aborting.", LogLevel.Action); } catch (Exception e) { // remove from list because list request could not be send hostList.remove(host);