diff --git a/src/clientP2P/ClientDownloadPartTCP.java b/src/clientP2P/ClientDownloadPartTCP.java index 11a277c..a56b049 100644 --- a/src/clientP2P/ClientDownloadPartTCP.java +++ b/src/clientP2P/ClientDownloadPartTCP.java @@ -6,22 +6,22 @@ import protocolP2P.ProtocolP2PPacketTCP; import protocolP2P.Payload; import protocolP2P.LoadRequest; import protocolP2P.FilePart; -import exception.InternalError; +import localException.InternalError; import remoteException.EmptyDirectory; import remoteException.EmptyFile; -import exception.ProtocolError; +import localException.ProtocolError; import remoteException.InternalRemoteError; import remoteException.VersionRemoteError; -import exception.TransmissionError; +import localException.TransmissionError; import remoteException.ProtocolRemoteError; -import exception.VersionError; -import exception.SizeError; +import localException.VersionError; +import localException.SizeError; import remoteException.NotFound; import java.nio.file.Files; import java.io.File; import java.nio.file.Paths; import java.io.IOException; -import exception.SocketClosed; +import localException.SocketClosed; /** Class to download file parts on tcp. * @author Louis Royer diff --git a/src/clientP2P/ClientDownloadPartUDP.java b/src/clientP2P/ClientDownloadPartUDP.java index 30c373c..da25d4b 100644 --- a/src/clientP2P/ClientDownloadPartUDP.java +++ b/src/clientP2P/ClientDownloadPartUDP.java @@ -6,16 +6,16 @@ import protocolP2P.ProtocolP2PPacketUDP; import protocolP2P.Payload; import protocolP2P.LoadRequest; import protocolP2P.FilePart; -import exception.InternalError; +import localException.InternalError; import remoteException.EmptyDirectory; import remoteException.EmptyFile; -import exception.ProtocolError; +import localException.ProtocolError; import remoteException.InternalRemoteError; import remoteException.VersionRemoteError; -import exception.TransmissionError; +import localException.TransmissionError; import remoteException.ProtocolRemoteError; -import exception.VersionError; -import exception.SizeError; +import localException.VersionError; +import localException.SizeError; import remoteException.NotFound; import java.nio.file.Files; import java.io.File; @@ -165,7 +165,7 @@ public class ClientDownloadPartUDP implements Runnable { if (p == null) { stop = true; } - + failed = downloadPart(p); if (failed) { System.err.println("Error: DownloadPart failed."); @@ -176,7 +176,7 @@ public class ClientDownloadPartUDP implements Runnable { } catch (IndexOutOfBoundsException e) { noTask = true; } - + } } diff --git a/src/clientP2P/ClientDownloadTCP.java b/src/clientP2P/ClientDownloadTCP.java index 442d105..bc80c86 100644 --- a/src/clientP2P/ClientDownloadTCP.java +++ b/src/clientP2P/ClientDownloadTCP.java @@ -16,11 +16,11 @@ import protocolP2P.HashResponse; import protocolP2P.HashRequest; import protocolP2P.ProtocolP2PPacketTCP; import protocolP2P.Payload; -import exception.ProtocolError; -import exception.InternalError; -import exception.TransmissionError; -import exception.SizeError; -import exception.VersionError; +import localException.ProtocolError; +import localException.InternalError; +import localException.TransmissionError; +import localException.SizeError; +import localException.VersionError; import protocolP2P.FilePart; import protocolP2P.LoadRequest; import java.io.IOException; @@ -29,7 +29,7 @@ import java.io.File; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.nio.file.StandardCopyOption; -import exception.SocketClosed; +import localException.SocketClosed; /** Class to download file from tcp * @author Louis Royer diff --git a/src/clientP2P/ClientDownloadUDP.java b/src/clientP2P/ClientDownloadUDP.java index 2bd2f29..46a4476 100644 --- a/src/clientP2P/ClientDownloadUDP.java +++ b/src/clientP2P/ClientDownloadUDP.java @@ -16,11 +16,11 @@ import protocolP2P.HashResponse; import protocolP2P.HashRequest; import protocolP2P.ProtocolP2PPacketUDP; import protocolP2P.Payload; -import exception.ProtocolError; -import exception.InternalError; -import exception.TransmissionError; -import exception.SizeError; -import exception.VersionError; +import localException.ProtocolError; +import localException.InternalError; +import localException.TransmissionError; +import localException.SizeError; +import localException.VersionError; import protocolP2P.FilePart; import protocolP2P.LoadRequest; import java.io.IOException; @@ -70,7 +70,7 @@ public class ClientDownloadUDP implements Runnable { public void setStop() { stop = true; } - + /** Runnable implementation */ public void run() { @@ -188,7 +188,7 @@ public class ClientDownloadUDP implements Runnable { assert pHash instanceof HashResponse : "This payload must be instance of HashResponse"; if (!(pHash instanceof HashResponse)) { throw new InternalError(); - } else { + } else { hash = ((HashResponse)pHash).getHash(HashAlgorithm.SHA512); } } catch (EmptyDirectory e) { @@ -260,14 +260,14 @@ public class ClientDownloadUDP implements Runnable { private void init() throws InternalError { // get size setSize(); - + // get hashsum from 1st server in list hash512 = getHashSum512(hostList.get(0)); if (hash512.length == 0) { System.err.println("Error: no hash512sum support."); throw new InternalError(); } - + // Add tasks if (!stop) { for(long i=MAX_PARTIAL_SIZE; i= 0 : "totalSize cannot be negative"; - assert partialContent.length != 0 : "partialContent.length cannot be zero, see RRCode.EMPTY_FILE"; + assert partialContent.length != 0 : "partialContent.length cannot be zero, see RRCode.EMPTY_FILE"; assert totalSize >= partialContent.length : "totalSize must be greater than partialContent.length"; assert offset >= 0 : "offset cannot be negative"; assert filename != null : "filename is required"; diff --git a/src/protocolP2P/HashRequest.java b/src/protocolP2P/HashRequest.java index 92ab812..452fd32 100644 --- a/src/protocolP2P/HashRequest.java +++ b/src/protocolP2P/HashRequest.java @@ -2,10 +2,10 @@ package protocolP2P; import protocolP2P.Payload; import protocolP2P.HashAlgorithm; import java.io.UnsupportedEncodingException; -import exception.TransmissionError; -import exception.SizeError; -import exception.ProtocolError; -import exception.InternalError; +import localException.TransmissionError; +import localException.SizeError; +import localException.ProtocolError; +import localException.InternalError; import tools.BytesArrayTools; @@ -21,12 +21,12 @@ public class HashRequest extends Payload { private HashAlgorithm[] algoList; private static final int FILENAME_SIZE_POSITION = PAYLOAD_START_POSITION; private static final int FILENAME_POSITION = FILENAME_SIZE_POSITION + 4; - + /** Constructor (typically used by the server) with a filename parameter. * @param filename name of the file to download. Must not be empty. * @param algoList List of hash algorithms used * @throws InternalError - * + * */ public HashRequest(String filename, HashAlgorithm[] algoList) throws InternalError { super(RequestResponseCode.HASH_REQUEST); diff --git a/src/protocolP2P/HashResponse.java b/src/protocolP2P/HashResponse.java index 2ff0440..769fbd7 100644 --- a/src/protocolP2P/HashResponse.java +++ b/src/protocolP2P/HashResponse.java @@ -3,10 +3,10 @@ import protocolP2P.Payload; import java.util.HashMap; import java.util.Map; import java.io.UnsupportedEncodingException; -import exception.TransmissionError; -import exception.SizeError; -import exception.ProtocolError; -import exception.InternalError; +import localException.TransmissionError; +import localException.SizeError; +import localException.ProtocolError; +import localException.InternalError; import tools.BytesArrayTools; @@ -22,12 +22,12 @@ public class HashResponse extends Payload { private Map hashes = new HashMap<>(); private static final int FILENAME_SIZE_POSITION = PAYLOAD_START_POSITION; private static final int FILENAME_POSITION = FILENAME_SIZE_POSITION + 4; - + /** Constructor (typically used by the server) with a filename parameter. * @param filename name of the file to download. Must not be empty. * @param hashes HashMap containing hashes for file. * @throws InternalError - * + * */ public HashResponse(String filename, Map hashes) throws InternalError { super(RequestResponseCode.HASH_RESPONSE); @@ -80,7 +80,7 @@ public class HashResponse extends Payload { } start += hashSize; } while (start < size); - + } /** Returns a byte[] containing Packet with padding. @@ -100,7 +100,7 @@ public class HashResponse extends Payload { size += 4 + s.length; } byte[] packet = new byte[size + 1]; // java initialize all to zero - + // set request/response code packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // set Payload size diff --git a/src/protocolP2P/LoadRequest.java b/src/protocolP2P/LoadRequest.java index 4017cb0..251981d 100644 --- a/src/protocolP2P/LoadRequest.java +++ b/src/protocolP2P/LoadRequest.java @@ -1,10 +1,10 @@ package protocolP2P; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; -import exception.TransmissionError; -import exception.ProtocolError; -import exception.InternalError; -import exception.SizeError; +import localException.TransmissionError; +import localException.ProtocolError; +import localException.InternalError; +import localException.SizeError; import tools.BytesArrayTools; import java.io.UnsupportedEncodingException; @@ -61,7 +61,7 @@ public class LoadRequest extends Payload { /* Read maxSizePartialContent */ maxSizePartialContent = BytesArrayTools.readLong(packet, MAX_SIZE_PARTIAL_CONTENT_POSITION); - + /* Read filename */ int size = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION); filename = BytesArrayTools.readString(packet, FILENAME_POSITION, size); diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index cbe9767..f0dcb34 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -5,10 +5,10 @@ import protocolP2P.FileList; import protocolP2P.LoadRequest; import protocolP2P.HashRequest; import protocolP2P.HashResponse; -import exception.ProtocolError; -import exception.InternalError; -import exception.TransmissionError; -import exception.SizeError; +import localException.ProtocolError; +import localException.InternalError; +import localException.TransmissionError; +import localException.SizeError; import tools.BytesArrayTools; /** Representation of payload. If payload has a size, use subclasses instead. * @author Louis Royer @@ -59,7 +59,7 @@ public class Payload { /** Used to check RRCode used is compatible with this class use, or if a more specific subclass is required. * @throws InternalError - */ + */ private void checkRequestResponseCode() throws InternalError { /* Incorrect use cases (use subclasses instead) */ if ((requestResponseCode == RequestResponseCode.LIST_RESPONSE && !(this instanceof FileList)) @@ -68,7 +68,7 @@ public class Payload { throw new InternalError(); } } - + /** Returns a byte[] containing Packet with padding. * This Packet is still incomplete and should not be send directly. * ProtocolP2PPacket will use this method to generate the complete Packet. @@ -90,7 +90,7 @@ public class Payload { * @param size integer representing payload size * @param packet Packet to be completed * @throws InternalError - */ + */ protected static void setPayloadSize(int size, byte[] packet) throws InternalError { /* assert to help debugging */ assert size >= 0: "Payload size cannot be negative"; diff --git a/src/protocolP2P/ProtocolP2PPacket.java b/src/protocolP2P/ProtocolP2PPacket.java index d1298d5..be23aec 100644 --- a/src/protocolP2P/ProtocolP2PPacket.java +++ b/src/protocolP2P/ProtocolP2PPacket.java @@ -1,10 +1,10 @@ package protocolP2P; -import exception.InternalError; -import exception.ProtocolError; -import exception.SizeError; -import exception.TransmissionError; -import exception.VersionError; -import exception.SocketClosed; +import localException.InternalError; +import localException.ProtocolError; +import localException.SizeError; +import localException.TransmissionError; +import localException.VersionError; +import localException.SocketClosed; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; import remoteException.NotFound; @@ -88,7 +88,7 @@ public abstract class ProtocolP2PPacket { * @throws SizeError */ protected ProtocolP2PPacket(byte[] packet) throws TransmissionError, ProtocolError, VersionError, InternalError, SizeError {} - + /** Returns Payload associated with the Packet. * @return payload associated with the Packet. */ diff --git a/src/protocolP2P/ProtocolP2PPacketTCP.java b/src/protocolP2P/ProtocolP2PPacketTCP.java index 40280d3..7c80474 100644 --- a/src/protocolP2P/ProtocolP2PPacketTCP.java +++ b/src/protocolP2P/ProtocolP2PPacketTCP.java @@ -1,10 +1,10 @@ package protocolP2P; -import exception.InternalError; -import exception.ProtocolError; -import exception.SizeError; -import exception.TransmissionError; -import exception.VersionError; -import exception.SocketClosed; +import localException.InternalError; +import localException.ProtocolError; +import localException.SizeError; +import localException.TransmissionError; +import localException.VersionError; +import localException.SocketClosed; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; import remoteException.NotFound; @@ -30,10 +30,10 @@ import java.net.Socket; * @version 1.0 */ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { - + private Socket responseSocket; // socket used to recept request and send response private Socket requestSocket; // socket used to send request and to reception response - + /** Constructor with payload parameter (typically used when sending packet). * @param payload the payload associated with the packet to send */ diff --git a/src/protocolP2P/ProtocolP2PPacketUDP.java b/src/protocolP2P/ProtocolP2PPacketUDP.java index a997754..aea6dcc 100644 --- a/src/protocolP2P/ProtocolP2PPacketUDP.java +++ b/src/protocolP2P/ProtocolP2PPacketUDP.java @@ -1,10 +1,10 @@ package protocolP2P; -import exception.InternalError; -import exception.ProtocolError; -import exception.SizeError; -import exception.TransmissionError; -import exception.VersionError; -import exception.SocketClosed; +import localException.InternalError; +import localException.ProtocolError; +import localException.SizeError; +import localException.TransmissionError; +import localException.VersionError; +import localException.SocketClosed; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; import remoteException.NotFound; @@ -31,12 +31,12 @@ import java.io.IOException; * @version 1.0 */ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { - + private final static int CHECKSUM_POSITION = 2; private SocketAddress responseSocketAddress; // socket address used when receptionning request and to sending response private DatagramSocket responseSocket; // socket used to recept request and send response private DatagramSocket requestSocket; // socket used to send request and to reception response - + /** Constructor with payload parameter (typically used when sending packet). * @param payload the payload associated with the packet to send */ diff --git a/src/protocolP2P/RequestResponseCode.java b/src/protocolP2P/RequestResponseCode.java index b69ebb3..a6e68d3 100644 --- a/src/protocolP2P/RequestResponseCode.java +++ b/src/protocolP2P/RequestResponseCode.java @@ -1,6 +1,6 @@ package protocolP2P; import protocolP2P.CodeType; -import exception.ProtocolError; +import localException.ProtocolError; import java.util.HashMap; import java.util.Map; import java.lang.Byte; @@ -24,7 +24,7 @@ public enum RequestResponseCode { EMPTY_DIRECTORY(CodeType.ERROR, (byte)0xC3), NOT_FOUND(CodeType.ERROR, (byte)0xC4), EMPTY_FILE(CodeType.ERROR, (byte)0xC5); - + public final CodeType codeType; public final byte codeValue; protected final static int RRCODE_POSITION = 1; @@ -62,7 +62,5 @@ public enum RequestResponseCode { return r; } - -} - +} diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index e1bc711..257fe3d 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -17,12 +17,12 @@ import protocolP2P.Payload; import protocolP2P.LoadRequest; import protocolP2P.FileList; import protocolP2P.FilePart; -import exception.InternalError; -import exception.ProtocolError; -import exception.SizeError; -import exception.TransmissionError; -import exception.VersionError; -import exception.SocketClosed; +import localException.InternalError; +import localException.ProtocolError; +import localException.SizeError; +import localException.TransmissionError; +import localException.VersionError; +import localException.SocketClosed; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; import remoteException.NotFound; @@ -106,7 +106,7 @@ public class ServerManagementTCP implements Runnable { /** Implementation of runnable. This method allow to serve one client. */ public void run() { - + boolean end = false; logger.writeTCP(addr + "New connection", LogLevel.Action); do { @@ -176,7 +176,7 @@ public class ServerManagementTCP implements Runnable { private void initSha512() { for(String f: fileList) { try { - MessageDigest md = MessageDigest.getInstance(HashAlgorithm.SHA512.getName()); + MessageDigest md = MessageDigest.getInstance(HashAlgorithm.SHA512.getName()); sha512.put(f, md.digest(Files.readAllBytes(Paths.get(baseDirectory + f)))); md.reset(); } catch (NoSuchAlgorithmException e) { @@ -254,7 +254,7 @@ public class ServerManagementTCP implements Runnable { } } } - + /** Send response to load request * @param pd Request received diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index ced8643..c6b3078 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -16,12 +16,12 @@ import protocolP2P.Payload; import protocolP2P.LoadRequest; import protocolP2P.FileList; import protocolP2P.FilePart; -import exception.InternalError; -import exception.ProtocolError; -import exception.SizeError; -import exception.TransmissionError; -import exception.VersionError; -import exception.SocketClosed; +import localException.InternalError; +import localException.ProtocolError; +import localException.SizeError; +import localException.TransmissionError; +import localException.VersionError; +import localException.SocketClosed; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; import remoteException.NotFound; @@ -116,7 +116,7 @@ public class ServerManagementUDP implements Runnable { for (String f: fileList) { logger.writeUDP("- " + f, LogLevel.Debug); } - + throw new IOException(); // to send a NOT_FOUND in the catch block } } catch (IOException e) { @@ -182,7 +182,7 @@ public class ServerManagementUDP implements Runnable { private void initSha512() { for(String f: fileList) { try { - MessageDigest md = MessageDigest.getInstance(HashAlgorithm.SHA512.getName()); + MessageDigest md = MessageDigest.getInstance(HashAlgorithm.SHA512.getName()); sha512.put(f, md.digest(Files.readAllBytes(Paths.get(baseDirectory + f)))); md.reset(); } catch (NoSuchAlgorithmException e) { @@ -192,7 +192,7 @@ public class ServerManagementUDP implements Runnable { } } } - + /** Send an internal error message. * @param pd ProtocolP2PPacketUDP to respond */ @@ -245,4 +245,3 @@ public class ServerManagementUDP implements Runnable { } } - diff --git a/src/tools/BytesArrayTools.java b/src/tools/BytesArrayTools.java index c11088c..8f80fcb 100644 --- a/src/tools/BytesArrayTools.java +++ b/src/tools/BytesArrayTools.java @@ -1,6 +1,6 @@ package tools; -import exception.SizeError; -import exception.ProtocolError; +import localException.SizeError; +import localException.ProtocolError; import java.io.UnsupportedEncodingException; import java.util.Arrays;