From d1d79938646a86f1f408bf02f8495212fc3a7f3e Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 22 Mar 2020 13:44:08 +0100 Subject: [PATCH] Use generics --- src/clientP2P/ClientDownloadPartTCP.java | 8 +++--- src/clientP2P/ClientDownloadPartUDP.java | 8 +++--- src/clientP2P/ClientDownloadTCP.java | 4 +-- src/clientP2P/ClientDownloadUDP.java | 4 +-- src/clientP2P/ClientManagementTCP.java | 4 +-- src/clientP2P/ClientManagementUDP.java | 4 +-- src/protocolP2P/HashResponse.java | 4 +-- src/protocolP2P/LoadRequest.java | 6 ++--- src/protocolP2P/ProtocolP2PPacket.java | 10 +++---- src/protocolP2P/ProtocolP2PPacketTCP.java | 32 +++++++++++------------ src/protocolP2P/ProtocolP2PPacketUDP.java | 32 +++++++++++------------ src/serverP2P/FileWatcherTCP.java | 2 +- src/serverP2P/FileWatcherUDP.java | 2 +- src/serverP2P/ServerManagement.java | 15 +++++------ src/serverP2P/ServerManagementTCP.java | 6 ++--- src/serverP2P/ServerManagementUDP.java | 6 ++--- src/tracker/TrackerManagementTCP.java | 26 +++++++++--------- src/tracker/TrackerManagementUDP.java | 28 ++++++++++---------- 18 files changed, 100 insertions(+), 101 deletions(-) diff --git a/src/clientP2P/ClientDownloadPartTCP.java b/src/clientP2P/ClientDownloadPartTCP.java index e4db082..b2872e9 100644 --- a/src/clientP2P/ClientDownloadPartTCP.java +++ b/src/clientP2P/ClientDownloadPartTCP.java @@ -172,7 +172,7 @@ public class ClientDownloadPartTCP implements Runnable { if (!stop) { try { Long offset = toDoTasks.get(0); - ProtocolP2PPacketTCP p = reqPart(offset); + ProtocolP2PPacketTCP p = reqPart(offset); if (p == null) { stop = true; } @@ -197,7 +197,7 @@ public class ClientDownloadPartTCP implements Runnable { * @param offset Offset of the file part to download * @return ProtocolP2PPacketTCP used to send request */ - private ProtocolP2PPacketTCP reqPart(Long offset) { + private ProtocolP2PPacketTCP reqPart(Long offset) { System.err.println("New request: " + offset); logger.writeTCP("New request: " + offset, LogLevel.Info); // maintain tracking of tasks @@ -225,7 +225,7 @@ public class ClientDownloadPartTCP implements Runnable { } // send request try { - ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, offset.longValue(), MAX_PARTIAL_SIZE)); + ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP<>(new LoadRequest(filename, offset.longValue(), MAX_PARTIAL_SIZE)); d.sendRequest((Object)socket); return d; } catch (InternalError e) { @@ -248,7 +248,7 @@ public class ClientDownloadPartTCP implements Runnable { * @param d request packet * @return true on failure, else false */ - public boolean downloadPart(ProtocolP2PPacketTCP d) { + public boolean downloadPart(ProtocolP2PPacketTCP d) { if (d == null) { System.err.println("Error: downloadPart -> d is null."); logger.writeTCP("downloadPart -> d is null.", LogLevel.Error); diff --git a/src/clientP2P/ClientDownloadPartUDP.java b/src/clientP2P/ClientDownloadPartUDP.java index 84ef0f8..2a2e4a7 100644 --- a/src/clientP2P/ClientDownloadPartUDP.java +++ b/src/clientP2P/ClientDownloadPartUDP.java @@ -166,7 +166,7 @@ public class ClientDownloadPartUDP implements Runnable { if (!stop) { try { Long offset = toDoTasks.get(0); - ProtocolP2PPacketUDP p = reqPart(offset); + ProtocolP2PPacketUDP p = reqPart(offset); if (p == null) { stop = true; } @@ -190,7 +190,7 @@ public class ClientDownloadPartUDP implements Runnable { * @param offset Offset of the file part to download * @return ProtocolP2PPacketTCP used to send request */ - private ProtocolP2PPacketUDP reqPart(Long offset) { + private ProtocolP2PPacketUDP reqPart(Long offset) { System.err.println("New request: "+ offset); logger.writeUDP("New request: "+ offset, LogLevel.Info); // maintain tracking of tasks @@ -218,7 +218,7 @@ public class ClientDownloadPartUDP implements Runnable { } // send request try { - ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, offset.longValue(), MAX_PARTIAL_SIZE)); + ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP<>(new LoadRequest(filename, offset.longValue(), MAX_PARTIAL_SIZE)); d.sendRequest((Object)socket); return d; } catch (InternalError e) { @@ -237,7 +237,7 @@ public class ClientDownloadPartUDP implements Runnable { * @param d request packet * @return true on failure, else false */ - public boolean downloadPart(ProtocolP2PPacketUDP d) { + public boolean downloadPart(ProtocolP2PPacketUDP d) { if (d == null) { System.err.println("Error: downloadPart -> d is null."); logger.writeUDP("downloadPart -> d is null.", LogLevel.Error); diff --git a/src/clientP2P/ClientDownloadTCP.java b/src/clientP2P/ClientDownloadTCP.java index bb8e932..3bc8981 100644 --- a/src/clientP2P/ClientDownloadTCP.java +++ b/src/clientP2P/ClientDownloadTCP.java @@ -197,7 +197,7 @@ public class ClientDownloadTCP implements Runnable { byte[] hash; HashAlgorithm[] hashesAlgo = new HashAlgorithm[1]; hashesAlgo[0] = HashAlgorithm.SHA512; - ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new HashRequest(filename, hashesAlgo)); + ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP<>(new HashRequest(filename, hashesAlgo)); try { d.sendRequest((Object)hostItem.getTCPSocket()); try { @@ -319,7 +319,7 @@ public class ClientDownloadTCP implements Runnable { * @throws InternalError */ private void setSize() throws InternalError { - ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); + ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP<>(new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); try { d.sendRequest((Object)hostList.get(0).getTCPSocket()); try { diff --git a/src/clientP2P/ClientDownloadUDP.java b/src/clientP2P/ClientDownloadUDP.java index 932019b..9fa14d2 100644 --- a/src/clientP2P/ClientDownloadUDP.java +++ b/src/clientP2P/ClientDownloadUDP.java @@ -197,7 +197,7 @@ public class ClientDownloadUDP implements Runnable { byte[] hash; HashAlgorithm[] hashesAlgo = new HashAlgorithm[1]; hashesAlgo[0] = HashAlgorithm.SHA512; - ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new HashRequest(filename, hashesAlgo)); + ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP<>(new HashRequest(filename, hashesAlgo)); try { d.sendRequest((Object)hostItem.getUDPSocket()); try { @@ -315,7 +315,7 @@ public class ClientDownloadUDP implements Runnable { * @throws InternalError */ private void setSize() throws InternalError { - ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); + ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP<>(new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); try { d.sendRequest((Object)hostList.get(0).getUDPSocket()); try { diff --git a/src/clientP2P/ClientManagementTCP.java b/src/clientP2P/ClientManagementTCP.java index b52899c..b5263ec 100644 --- a/src/clientP2P/ClientManagementTCP.java +++ b/src/clientP2P/ClientManagementTCP.java @@ -174,7 +174,7 @@ public class ClientManagementTCP implements Runnable { * @throws VersionRemoteError */ private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { - ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST)); + ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP<>(new Payload(RequestResponseCode.LIST_REQUEST)); try { d.sendRequest((Object)hostList.get(0).getTCPSocket()); Payload p = d.receiveResponse().getPayload(); @@ -220,7 +220,7 @@ public class ClientManagementTCP implements Runnable { * @throws InternalError */ private void initHostList() throws ProtocolError, InternalError { - ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new DiscoverRequest(null)); + ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP<>(new DiscoverRequest(null)); try { d.sendRequest((Object)tracker.getTCPSocket()); Payload p = d.receiveResponse().getPayload(); diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index b298fc7..1f20484 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -189,7 +189,7 @@ public class ClientManagementUDP implements Runnable { * @throws VersionRemoteError */ private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { - ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST)); + ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP<>(new Payload(RequestResponseCode.LIST_REQUEST)); d.sendRequest((Object)hostList.get(0).getUDPSocket()); try { Payload p = d.receiveResponse().getPayload(); @@ -234,7 +234,7 @@ public class ClientManagementUDP implements Runnable { * @throws InternalError */ private void initHostList() throws ProtocolError, InternalError { - ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new DiscoverRequest(null)); + ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP<>(new DiscoverRequest(null)); try { d.sendRequest((Object)tracker.getUDPSocket()); Payload p = d.receiveResponse().getPayload(); diff --git a/src/protocolP2P/HashResponse.java b/src/protocolP2P/HashResponse.java index 52df91e..743b13e 100644 --- a/src/protocolP2P/HashResponse.java +++ b/src/protocolP2P/HashResponse.java @@ -111,14 +111,14 @@ public class HashResponse extends Payload { int bCount = FILENAME_POSITION + filename.length(); for(HashAlgorithm h : hashes.keySet()) { String s = h.getName(); - BytesArrayTools.write(packet, bCount, (int)s.length()); + BytesArrayTools.write(packet, bCount, s.length()); bCount += 4; // write algoname BytesArrayTools.write(packet, s, bCount); bCount += s.length(); // write hash size byte[] hashb = hashes.get(HashAlgorithm.fromName(s)); - BytesArrayTools.write(packet, bCount, (int)hashb.length); + BytesArrayTools.write(packet, bCount, hashb.length); bCount += 4; if (hashb.length != 0) { // write hash diff --git a/src/protocolP2P/LoadRequest.java b/src/protocolP2P/LoadRequest.java index 1fa0aeb..86f0b75 100644 --- a/src/protocolP2P/LoadRequest.java +++ b/src/protocolP2P/LoadRequest.java @@ -82,11 +82,11 @@ public class LoadRequest extends Payload { // set Payload size setPayloadSize(size - OFFSET_POSITION, packet); // Write offset - BytesArrayTools.write(packet, OFFSET_POSITION, (long)offset); + BytesArrayTools.write(packet, OFFSET_POSITION, offset); // Write maxSizePartialContent - BytesArrayTools.write(packet, MAX_SIZE_PARTIAL_CONTENT_POSITION, (long)maxSizePartialContent); + BytesArrayTools.write(packet, MAX_SIZE_PARTIAL_CONTENT_POSITION, maxSizePartialContent); // Write filenameSize - BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, (int)filenameSize); + BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize); // Write filename BytesArrayTools.write(packet, filename, FILENAME_POSITION); return packet; diff --git a/src/protocolP2P/ProtocolP2PPacket.java b/src/protocolP2P/ProtocolP2PPacket.java index c0312c4..db9c4b4 100644 --- a/src/protocolP2P/ProtocolP2PPacket.java +++ b/src/protocolP2P/ProtocolP2PPacket.java @@ -21,7 +21,7 @@ import tools.HostItem; * @author JS Auge * @version 1.0 */ -public abstract class ProtocolP2PPacket { +public abstract class ProtocolP2PPacket < T extends Payload>{ private final static byte PROTOCOL_VERSION = 0x12; protected final static int VERSION_POSITION = 0; protected byte version; @@ -30,9 +30,9 @@ public abstract class ProtocolP2PPacket { /** Constructor with payload parameter (typically used when sending Packet). * @param payload the payload associated with the Packet to send */ - public ProtocolP2PPacket(Payload payload) { + public ProtocolP2PPacket (T payload) { version = PROTOCOL_VERSION; - this.payload = payload; + this.payload = (Payload)payload; } /** Send a request @@ -49,7 +49,7 @@ public abstract class ProtocolP2PPacket { * @throws IOException * @throws SocketClosed */ - public abstract void sendResponse(T response) throws InternalError, IOException, SocketClosed; + public abstract > void sendResponse(U response) throws InternalError, IOException, SocketClosed; /** Get hostItem of the sender * @return hostItem of the sender @@ -73,7 +73,7 @@ public abstract class ProtocolP2PPacket { * @throws IOException * @throws SocketClosed */ - public abstract ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, NotATracker, 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 diff --git a/src/protocolP2P/ProtocolP2PPacketTCP.java b/src/protocolP2P/ProtocolP2PPacketTCP.java index b99e83f..708ee38 100644 --- a/src/protocolP2P/ProtocolP2PPacketTCP.java +++ b/src/protocolP2P/ProtocolP2PPacketTCP.java @@ -28,7 +28,7 @@ import java.net.Socket; * @author JS Auge * @version 1.0 */ -public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { +public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacket < T > { private Socket responseSocket; // socket used to recept request and send response private Socket requestSocket; // socket used to send request and to reception response @@ -36,7 +36,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { /** Constructor with payload parameter (typically used when sending packet). * @param payload the payload associated with the packet to send */ - public ProtocolP2PPacketTCP(Payload payload) { + public ProtocolP2PPacketTCP(T payload) { super(payload); } @@ -147,19 +147,19 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { break; } } catch (TransmissionError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); throw e; } catch (ProtocolError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss); throw e; } catch (VersionError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss); throw e; } catch (InternalError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); throw e; } catch (SizeError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); throw e; } if (protocolError) { @@ -173,10 +173,10 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { * @throws IOException * @throws SocketClosed */ - public void sendResponse(ProtocolP2PPacket response) throws InternalError, IOException, SocketClosed { + public > void sendResponse(U response) throws InternalError, IOException, SocketClosed { assert response instanceof ProtocolP2PPacketTCP: "Wrong Packet type"; if (response instanceof ProtocolP2PPacketTCP) { - ProtocolP2PPacketTCP r = (ProtocolP2PPacketTCP) response; + ProtocolP2PPacketTCP r = (ProtocolP2PPacketTCP) response; assert responseSocket != null : "Cannot send response to a packet not received"; if (responseSocket == null) { throw new InternalError(); @@ -204,7 +204,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { * @throws IOException * @throws SocketClosed */ - public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, NotATracker, 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(); @@ -227,7 +227,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { } // contruction try { - ProtocolP2PPacketTCP p = new ProtocolP2PPacketTCP(packet); + ProtocolP2PPacketTCP p = new ProtocolP2PPacketTCP<>(packet); Payload payload = p.getPayload(); switch (payload.getRequestResponseCode()) { case PROTOCOL_ERROR : @@ -248,19 +248,19 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { return (ProtocolP2PPacket)p; } } catch (TransmissionError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); throw e; } catch (ProtocolError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket); throw e; } catch (VersionError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket); throw e; } catch (InternalError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); throw e; } catch (SizeError e) { - (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); + (new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); throw e; } } diff --git a/src/protocolP2P/ProtocolP2PPacketUDP.java b/src/protocolP2P/ProtocolP2PPacketUDP.java index 03ffd64..a54733b 100644 --- a/src/protocolP2P/ProtocolP2PPacketUDP.java +++ b/src/protocolP2P/ProtocolP2PPacketUDP.java @@ -30,7 +30,7 @@ import java.io.IOException; * @author JS Auge * @version 1.0 */ -public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { +public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket < T > { private final static int CHECKSUM_POSITION = 2; private HostItem remoteHost; @@ -41,7 +41,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { /** Constructor with payload parameter (typically used when sending packet). * @param payload the payload associated with the packet to send */ - public ProtocolP2PPacketUDP(Payload payload) { + public ProtocolP2PPacketUDP(T payload) { super(payload); } @@ -140,19 +140,19 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { break; } } catch (TransmissionError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress); throw e; } catch (ProtocolError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss, responseSocketAddress); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss, responseSocketAddress); throw e; } catch (VersionError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss, responseSocketAddress); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss, responseSocketAddress); throw e; } catch (InternalError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress); throw e; } catch (SizeError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress); throw e; } if (protocolError) { @@ -165,10 +165,10 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { * @throws InternalError * @throws IOException */ - public void sendResponse(ProtocolP2PPacket response) throws InternalError, IOException { + public >void sendResponse(U response) throws InternalError, IOException { assert response instanceof ProtocolP2PPacketUDP: "Wrong Packet type"; if (response instanceof ProtocolP2PPacketUDP) { - ProtocolP2PPacketUDP r = (ProtocolP2PPacketUDP) response; + ProtocolP2PPacketUDP r = (ProtocolP2PPacketUDP) response; assert responseSocket != null : "Cannot send response to a packet not received"; if (responseSocket == null) { throw new InternalError(); @@ -199,7 +199,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { * @throws SizeError * @throws IOException */ - public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, NotATracker, 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(); @@ -211,7 +211,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { // contruction try { - ProtocolP2PPacketUDP p = new ProtocolP2PPacketUDP(packet); + ProtocolP2PPacketUDP p = new ProtocolP2PPacketUDP<>(packet); Payload payload = p.getPayload(); switch (payload.getRequestResponseCode()) { case PROTOCOL_ERROR : @@ -232,19 +232,19 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { return (ProtocolP2PPacket)p; } } catch (TransmissionError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); throw e; } catch (ProtocolError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket); throw e; } catch (VersionError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket); throw e; } catch (InternalError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); throw e; } catch (SizeError e) { - (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); + (new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); throw e; } } diff --git a/src/serverP2P/FileWatcherTCP.java b/src/serverP2P/FileWatcherTCP.java index 9f3936c..7aa9df7 100644 --- a/src/serverP2P/FileWatcherTCP.java +++ b/src/serverP2P/FileWatcherTCP.java @@ -51,7 +51,7 @@ public class FileWatcherTCP extends FileWatcher { protected void registerTracker() { try { writeLog("Trying to into tracker", LogLevel.Info); - ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(server)); + ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Register(server)); p.sendRequest((Object)tracker.tryGetTCPSocket()); writeLog("Register request sent.", LogLevel.Debug); tracker.closeTCPSocket(); diff --git a/src/serverP2P/FileWatcherUDP.java b/src/serverP2P/FileWatcherUDP.java index 9cc1021..194039e 100644 --- a/src/serverP2P/FileWatcherUDP.java +++ b/src/serverP2P/FileWatcherUDP.java @@ -51,7 +51,7 @@ public class FileWatcherUDP extends FileWatcher { protected void registerTracker() { try { writeLog("Trying to register into tracker", LogLevel.Info); - ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new Register(server)); + ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Register(server)); p.sendRequest((Object)tracker.getUDPSocket()); writeLog("Register request sent (but cannot ensure reception).", LogLevel.Debug); tracker.closeUDPSocket(); diff --git a/src/serverP2P/ServerManagement.java b/src/serverP2P/ServerManagement.java index 3962b5f..39f79d4 100644 --- a/src/serverP2P/ServerManagement.java +++ b/src/serverP2P/ServerManagement.java @@ -76,12 +76,12 @@ public abstract class ServerManagement implements Runnable { /** Create packets * @param payload Payload */ - protected abstract < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload); + protected abstract < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload); /** Send response to list request * @param pd Request received */ - protected < T extends ProtocolP2PPacket > void sendListResponse(T pd) { + protected < T extends ProtocolP2PPacket > void sendListResponse(T pd) { try { String[] fileList = fileListWatcher.getFileList(); if (fileList.length == 0) { @@ -99,7 +99,7 @@ public abstract class ServerManagement implements Runnable { /** Send a NotATracker error message. * @param pd Request received */ - protected < T extends ProtocolP2PPacket > void sendNotATracker(T pd) { + protected < T extends ProtocolP2PPacket > void sendNotATracker(T pd) { try { pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.NOT_A_TRACKER))); } catch (Exception e) { @@ -110,7 +110,7 @@ public abstract class ServerManagement implements Runnable { /** Send an internal error message. * @param pd Request received */ - protected < T extends ProtocolP2PPacket > void sendInternalError(T pd) { + protected < T extends ProtocolP2PPacket > void sendInternalError(T pd) { writeLog("Internal Error", LogLevel.Warning); try { pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.INTERNAL_ERROR))); @@ -122,7 +122,7 @@ public abstract class ServerManagement implements Runnable { /** Send hash response to hash request * @param pd Request received */ - protected < T extends ProtocolP2PPacket > void sendHashResponse(T pd) { + protected < T extends ProtocolP2PPacket > void sendHashResponse(T pd) { Payload p = pd.getPayload(); assert p instanceof HashRequest : "payload must be an instance of HashRequest"; if (!(p instanceof HashRequest)) { @@ -161,7 +161,7 @@ public abstract class ServerManagement implements Runnable { /** Send response to load request * @param pd Request received */ - protected < T extends ProtocolP2PPacket > void sendLoadResponse(T pd) { + protected < T extends ProtocolP2PPacket > void sendLoadResponse(T pd) { Payload p = pd.getPayload(); assert p instanceof LoadRequest : "payload must be an instance of LoadRequest"; if (!(p instanceof LoadRequest)) { @@ -223,8 +223,7 @@ public abstract class ServerManagement implements Runnable { // unregistering from tracker try { writeLog("Unregistering from tracker", LogLevel.Info); - ProtocolP2PPacket p = createProtocolP2PPacket((Payload)new Unregister(server)); - p.sendRequest(getTrackerSocket()); + createProtocolP2PPacket(new Unregister(server)).sendRequest(getTrackerSocket()); } catch (Exception e) { writeLog("Cannot unregister from tracker", LogLevel.Error); writeLog(e, LogLevel.Error); diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index 8fa5428..e829fd8 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -121,7 +121,7 @@ public class ServerManagementTCP extends ServerManagement { */ private boolean handleRequest() { try { - ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket()); + ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP<>((Object)addr.getTCPSocket()); Payload p = pd.getPayload(); switch (p.getRequestResponseCode()) { case LOAD_REQUEST: @@ -185,8 +185,8 @@ public class ServerManagementTCP extends ServerManagement { /** Create packets * @param payload Payload */ - protected < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload) { - return (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload) payload); + protected < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload) { + return (ProtocolP2PPacket)new ProtocolP2PPacketTCP(payload); } /** Getter for tracker socket diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index 7affa75..a3cd1fc 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -76,7 +76,7 @@ public class ServerManagementUDP extends ServerManagement { (new Thread(fileListWatcher)).start(); while(!stop) { try { - ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket); + ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP<>((Object)socket); Payload p = pd.getPayload(); switch (p.getRequestResponseCode()) { case LOAD_REQUEST: @@ -132,8 +132,8 @@ public class ServerManagementUDP extends ServerManagement { /** Create packets * @param payload Payload */ - protected < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload) { - return (ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload) payload); + protected < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload) { + return (ProtocolP2PPacket)new ProtocolP2PPacketUDP(payload); } /** Getter for tracker socket diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index c26c9c9..b7d9249 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -104,7 +104,7 @@ public class TrackerManagementTCP implements Runnable { */ private boolean handleRequest() { try { - ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket()); + ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP<>((Object)addr.getTCPSocket()); Payload p = pd.getPayload(); switch (p.getRequestResponseCode()) { case LOAD_REQUEST: @@ -150,9 +150,9 @@ public class TrackerManagementTCP implements Runnable { /** Send a not found message. * @param pd ProtocolP2PPacketTCP to respond */ - private void sendNotFound(ProtocolP2PPacketTCP pd) { + private void sendNotFound(ProtocolP2PPacketTCP pd) { try { - pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.NOT_FOUND))); + pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP<>(new Payload(RequestResponseCode.NOT_FOUND))); } catch (Exception e) { logger.writeTCP(e, LogLevel.Error); } @@ -161,9 +161,9 @@ public class TrackerManagementTCP implements Runnable { /** Send an empty directory message. * @param pd ProtocolP2PPacketTCP to respond */ - private void sendEmptyDirectory(ProtocolP2PPacketTCP pd) { + private void sendEmptyDirectory(ProtocolP2PPacketTCP pd) { try { - pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.EMPTY_DIRECTORY))); + pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP<>(new Payload(RequestResponseCode.EMPTY_DIRECTORY))); } catch (Exception e) { logger.writeTCP(e, LogLevel.Error); } @@ -172,10 +172,10 @@ public class TrackerManagementTCP implements Runnable { /** Send an internal error message. * @param pd ProtocolP2PPacketTCP to respond */ - private void sendInternalError(ProtocolP2PPacketTCP pd) { + private void sendInternalError(ProtocolP2PPacketTCP pd) { logger.writeTCP("Internal Error", LogLevel.Warning); try { - pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))); + pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP<>(new Payload(RequestResponseCode.INTERNAL_ERROR))); } catch (Exception e) { logger.writeTCP(e, LogLevel.Error); } @@ -185,7 +185,7 @@ public class TrackerManagementTCP implements Runnable { * @param pd ProtocolP2PPacketTCP to respond * @throws InternalError */ - private void handleRegister(ProtocolP2PPacketTCP pd) throws InternalError { + private void handleRegister(ProtocolP2PPacketTCP pd) throws InternalError { Payload p = pd.getPayload(); assert p instanceof Register : "payload must be an instance of Register"; if (!(p instanceof Register)) { @@ -198,7 +198,7 @@ public class TrackerManagementTCP implements Runnable { } // send a list request try { - ProtocolP2PPacket pLReq = (ProtocolP2PPacket) new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST)); + ProtocolP2PPacket pLReq = (ProtocolP2PPacket) new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST)); 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); @@ -220,7 +220,7 @@ public class TrackerManagementTCP implements Runnable { * @param pd ProtocolP2PPacketTCP to respond * @throws InternalError */ - private void handleUnregister(ProtocolP2PPacketTCP pd) throws InternalError { + private void handleUnregister(ProtocolP2PPacketTCP pd) throws InternalError { Payload p = pd.getPayload(); assert p instanceof Unregister : "payload must be an instance of Unregister"; if (!(p instanceof Unregister)) { @@ -242,7 +242,7 @@ public class TrackerManagementTCP implements Runnable { * @param pd ProtocolP2PPacketTCP to respond * @throws InternalError */ - private void handleDiscover(ProtocolP2PPacketTCP pd) throws InternalError { + private void handleDiscover(ProtocolP2PPacketTCP pd) throws InternalError { logger.writeTCP("Received DISCOVER REQUEST from host " + pd.getHostItem(), LogLevel.Action); Payload p = pd.getPayload(); assert p instanceof DiscoverRequest : "payload must be an instance of DiscoverRequest"; @@ -251,7 +251,7 @@ public class TrackerManagementTCP implements Runnable { } else { String filename = ((DiscoverRequest)p).getFilename(); try { - pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList)))); + pd.sendResponse(new ProtocolP2PPacketTCP(new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList)))); } catch (Exception e) { logger.writeTCP(e, LogLevel.Error); } @@ -262,7 +262,7 @@ public class TrackerManagementTCP implements Runnable { * @param pd ProtocolP2PPacketTCP response * @throws InternalError */ - private void handleListResponse(ProtocolP2PPacketTCP pd, HostItem host) throws InternalError { + private void handleListResponse(ProtocolP2PPacketTCP pd, HostItem host) throws InternalError { 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 42d6445..a99636a 100644 --- a/src/tracker/TrackerManagementUDP.java +++ b/src/tracker/TrackerManagementUDP.java @@ -59,7 +59,7 @@ public class TrackerManagementUDP implements Runnable { logger.writeUDP("Tracker successfully started", LogLevel.Info); while(true) { try { - ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket); + ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP<>((Object)socket); Payload p = pd.getPayload(); switch (p.getRequestResponseCode()) { case LOAD_REQUEST: @@ -101,9 +101,9 @@ public class TrackerManagementUDP implements Runnable { /** Send a not found message. * @param pd ProtocolP2PPacketUDP to respond */ - private void sendNotFound(ProtocolP2PPacketUDP pd) { + private void sendNotFound(ProtocolP2PPacketUDP pd) { try { - pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.NOT_FOUND))); + pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP<>(new Payload(RequestResponseCode.NOT_FOUND))); } catch (Exception e) { logger.writeUDP(e, LogLevel.Error); } @@ -112,9 +112,9 @@ public class TrackerManagementUDP implements Runnable { /** Send an empty directory message. * @param pd ProtocolP2PPacketUDP to respond */ - private void sendEmptyDirectory(ProtocolP2PPacketUDP pd) { + private void sendEmptyDirectory(ProtocolP2PPacketUDP pd) { try { - pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY))); + pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP<>(new Payload(RequestResponseCode.EMPTY_DIRECTORY))); } catch (Exception e) { logger.writeUDP(e, LogLevel.Error); } @@ -123,10 +123,10 @@ public class TrackerManagementUDP implements Runnable { /** Send an internal error message. * @param pd ProtocolP2PPacketUDP to respond */ - private void sendInternalError(ProtocolP2PPacketUDP pd) { + private void sendInternalError(ProtocolP2PPacketUDP pd) { logger.writeUDP("Internal Error", LogLevel.Warning); try { - pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))); + pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP<>(new Payload(RequestResponseCode.INTERNAL_ERROR))); } catch (Exception e) { logger.writeUDP(e, LogLevel.Error); } @@ -136,7 +136,7 @@ public class TrackerManagementUDP implements Runnable { * @param pd ProtocolP2PPacketUDP to respond * @throws InternalError */ - private void handleRegister(ProtocolP2PPacketUDP pd) throws InternalError { + private void handleRegister(ProtocolP2PPacketUDP pd) throws InternalError { Payload p = pd.getPayload(); assert p instanceof Register : "payload must be an instance of Register"; if (!(p instanceof Register)) { @@ -150,10 +150,10 @@ public class TrackerManagementUDP implements Runnable { } // 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); - ProtocolP2PPacket resp = pLReq.receiveResponse(); + ProtocolP2PPacket resp = pLReq.receiveResponse(); handleListResponse((ProtocolP2PPacketUDP)resp, host); logger.writeUDP("Received LIST RESPONSE from host " + pd.getHostItem(), LogLevel.Action); host.closeUDPSocket(); @@ -173,7 +173,7 @@ public class TrackerManagementUDP implements Runnable { * @param pd ProtocolP2PPacketUDP to respond * @throws InternalError */ - private void handleUnregister(ProtocolP2PPacketUDP pd) throws InternalError { + private void handleUnregister(ProtocolP2PPacketUDP pd) throws InternalError { Payload p = pd.getPayload(); assert p instanceof Unregister : "payload must be an instance of Unregister"; if (!(p instanceof Unregister)) { @@ -195,7 +195,7 @@ public class TrackerManagementUDP implements Runnable { * @param pd ProtocolP2PPacketUDP to respond * @throws InternalError */ - private void handleDiscover(ProtocolP2PPacketUDP pd) throws InternalError { + private void handleDiscover(ProtocolP2PPacketUDP pd) throws InternalError { logger.writeUDP("Received DISCOVER REQUEST from host " + pd.getHostItem(), LogLevel.Action); Payload p = pd.getPayload(); assert p instanceof DiscoverRequest : "payload must be an instance of DiscoverRequest"; @@ -204,7 +204,7 @@ public class TrackerManagementUDP implements Runnable { } else { String filename = ((DiscoverRequest)p).getFilename(); try { - pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList)))); + pd.sendResponse(new ProtocolP2PPacketUDP(new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList)))); } catch (Exception e) { logger.writeUDP(e, LogLevel.Error); } @@ -215,7 +215,7 @@ public class TrackerManagementUDP implements Runnable { * @param pd ProtocolP2PPacketUDP response * @throws InternalError */ - private void handleListResponse(ProtocolP2PPacketUDP pd, HostItem host) throws InternalError { + private void handleListResponse(ProtocolP2PPacketUDP pd, HostItem host) throws InternalError { Payload p = pd.getPayload(); assert p instanceof FileList: "payload must be an instance of FileList"; if (!(p instanceof FileList)) {