Merge branch 'exceptionsheritage' of flavien/Projet_JAVA_P2P_STRI2A into etape4
All checks were successful
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/pr-master This commit looks good
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/head This commit looks good

This commit is contained in:
Louis Royer 2020-03-19 14:55:31 +01:00 committed by Gitea
commit 0ac0eab231
36 changed files with 189 additions and 145 deletions

View File

@ -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

View File

@ -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;
}
}
}

View File

@ -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

View File

@ -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<size;i+=MAX_PARTIAL_SIZE) {

View File

@ -1,9 +1,9 @@
package clientP2P;
import exception.InternalError;
import exception.ProtocolError;
import exception.SizeError;
import exception.TransmissionError;
import exception.VersionError;
import localException.InternalError;
import localException.ProtocolError;
import localException.SizeError;
import localException.TransmissionError;
import localException.VersionError;
import remoteException.EmptyFile;
import remoteException.EmptyDirectory;
import remoteException.InternalRemoteError;
@ -32,7 +32,7 @@ import protocolP2P.HashResponse;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import clientP2P.ClientDownloadTCP;
import exception.SocketClosed;
import localException.SocketClosed;
/** Implementation of P2P-JAVA-PROJECT CLIENT
* @author Louis Royer

View File

@ -1,9 +1,9 @@
package clientP2P;
import exception.InternalError;
import exception.ProtocolError;
import exception.SizeError;
import exception.TransmissionError;
import exception.VersionError;
import localException.InternalError;
import localException.ProtocolError;
import localException.SizeError;
import localException.TransmissionError;
import localException.VersionError;
import remoteException.EmptyFile;
import remoteException.EmptyDirectory;
import remoteException.InternalRemoteError;

View File

@ -1,4 +1,5 @@
package exception;
public class InternalError extends Exception {
public class LocalException extends Exception {
private static final long serialVersionUID = 12L;
}

View File

@ -1,4 +1,5 @@
package exception;
public class ProtocolError extends Exception {
public class RemoteException extends Exception {
private static final long serialVersionUID = 12L;
}

View File

@ -1,4 +0,0 @@
package exception;
public class SocketClosed extends Exception {
private static final long serialVersionUID = 12L;
}

View File

@ -1,4 +0,0 @@
package exception;
public class TransmissionError extends Exception {
private static final long serialVersionUID = 12L;
}

View File

@ -1,4 +0,0 @@
package exception;
public class VersionError extends Exception {
private static final long serialVersionUID = 12L;
}

View File

@ -0,0 +1,7 @@
package localException;
import exception.LocalException;
public class InternalError extends exception.LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -0,0 +1,7 @@
package localException;
import exception.LocalException;
public class ProtocolError extends exception.LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -1,5 +1,9 @@
package exception;
package localException;
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 {
public class SizeError extends exception.LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -0,0 +1,7 @@
package localException;
import exception.LocalException;
public class SocketClosed extends exception.LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -0,0 +1,7 @@
package localException;
import exception.LocalException;
public class TransmissionError extends exception.LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -0,0 +1,7 @@
package localException;
import exception.LocalException;
public class VersionError extends exception.LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,10 +2,10 @@ package protocolP2P;
import java.util.Arrays;
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 java.io.UnsupportedEncodingException;
import tools.BytesArrayTools;

View File

@ -1,10 +1,10 @@
package protocolP2P;
import protocolP2P.Payload;
import protocolP2P.RequestResponseCode;
import exception.ProtocolError;
import exception.InternalError;
import exception.SizeError;
import exception.TransmissionError;
import localException.ProtocolError;
import localException.InternalError;
import localException.SizeError;
import localException.TransmissionError;
import tools.BytesArrayTools;
import java.util.Arrays;
import java.io.UnsupportedEncodingException;
@ -36,7 +36,7 @@ public class FilePart extends Payload {
super(RequestResponseCode.LOAD_RESPONSE);
/* asserts to help debugging */
assert totalSize >= 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";

View File

@ -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);

View File

@ -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<HashAlgorithm, byte[]> 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<HashAlgorithm, byte[]> 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

View File

@ -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);

View File

@ -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";

View File

@ -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.
*/

View File

@ -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
*/

View File

@ -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
*/

View File

@ -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;
}
}

View File

@ -1,4 +1,7 @@
package remoteException;
public class EmptyDirectory extends Exception {
import exception.RemoteException;
public class EmptyDirectory extends exception.RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -1,4 +1,7 @@
package remoteException;
public class EmptyFile extends Exception {
import exception.RemoteException;
public class EmptyFile extends exception.RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -1,4 +1,7 @@
package remoteException;
public class InternalRemoteError extends Exception {
import exception.RemoteException;
public class InternalRemoteError extends exception.RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -1,4 +1,7 @@
package remoteException;
public class NotFound extends Exception {
import exception.RemoteException;
public class NotFound extends exception.RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -1,4 +1,7 @@
package remoteException;
public class ProtocolRemoteError extends Exception {
import exception.RemoteException;
public class ProtocolRemoteError extends exception.RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -1,4 +1,7 @@
package remoteException;
public class VersionRemoteError extends Exception {
import exception.RemoteException;
public class VersionRemoteError extends exception.RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -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

View File

@ -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 {
}
}

View File

@ -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;