From d6fbfa66835e055bde7b97e30e028961c1e83511 Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Fri, 27 Mar 2020 17:10:09 +0100 Subject: [PATCH] Update Payload and protocolP2P packet to allow ratio --- src/protocolP2P/Payload.java | 12 ++++++++++++ src/protocolP2P/ProtocolP2PPacketTCP.java | 13 +++++++++++++ src/protocolP2P/ProtocolP2PPacketUDP.java | 13 +++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index 990df6b..5f2a3a2 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -9,6 +9,9 @@ import protocolP2P.DiscoverRequest; import protocolP2P.DiscoverResponse; import protocolP2P.Register; import protocolP2P.Unregister; +import protocolP2P.RatioRequest; +import protocolP2P.RatioResponse; +import protocolP2P.UpdateRatio; import localException.ProtocolError; import localException.InternalError; import localException.TransmissionError; @@ -40,6 +43,9 @@ public class Payload { assert requestResponseCode != RequestResponseCode.DISCOVER_RESPONSE || (this instanceof DiscoverResponse) : "DISCOVER_RESPONSE must use DiscoverResponse class"; assert requestResponseCode != RequestResponseCode.REGISTER || (this instanceof Register) : "REGISTER must use Register class"; assert requestResponseCode != RequestResponseCode.UNREGISTER || (this instanceof Unregister) : "UNREGISTER must use Unregister class"; + assert requestResponseCode != RequestResponseCode.RATIO_REQUEST || (this instanceof RatioRequest) : "RATIO_REQUEST must use RatioRequest class"; + assert requestResponseCode != RequestResponseCode.RATIO_RESPONSE || (this instanceof RatioResponse) : "RATIO_RESPONSE must use RatioResponse class"; + assert requestResponseCode != RequestResponseCode.UPDATE_RATIO || (this instanceof UpdateRatio) : "UPDATE_RATIO must use UpdateRatio class"; this.requestResponseCode = requestResponseCode; checkRequestResponseCode(); // this can throw InternalError } @@ -67,6 +73,9 @@ public class Payload { assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.DISCOVER_RESPONSE || (this instanceof DiscoverResponse) : "DISCOVER_RESPONSE must use DiscoverResponse class"; assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.REGISTER || (this instanceof Register) : "REGISTER must use Register class"; assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.UNREGISTER || (this instanceof Unregister) : "UNREGISTER must use Unregister class"; + assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.RATIO_REQUEST || (this instanceof RatioRequest) : "RATIO_REQUEST must use RatioRequest class"; + assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.RATIO_RESPONSE || (this instanceof RatioResponse) : "RATIO_RESPONSE must use RatioResponse class"; + assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.UPDATE_RATIO || (this instanceof UpdateRatio) : "UPDATE_RATIO must use UpdateRatio class"; requestResponseCode = RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]); checkRequestResponseCode(); // this can throw InternalError } @@ -85,6 +94,9 @@ public class Payload { || (requestResponseCode == RequestResponseCode.DISCOVER_RESPONSE && !(this instanceof DiscoverResponse)) || (requestResponseCode == RequestResponseCode.REGISTER && !(this instanceof Register)) || (requestResponseCode == RequestResponseCode.UNREGISTER && !(this instanceof Unregister)) + || (requestResponseCode == RequestResponseCode.RATIO_REQUEST && !(this instanceof RatioRequest)) + || (requestResponseCode == RequestResponseCode.RATIO_RESPONSE && !(this instanceof RatioResponse)) + || (requestResponseCode == RequestResponseCode.UPDATE_RATIO && !(this instanceof UpdateRatio)) ) { throw new InternalError(); } diff --git a/src/protocolP2P/ProtocolP2PPacketTCP.java b/src/protocolP2P/ProtocolP2PPacketTCP.java index 708ee38..4eb2bfd 100644 --- a/src/protocolP2P/ProtocolP2PPacketTCP.java +++ b/src/protocolP2P/ProtocolP2PPacketTCP.java @@ -18,6 +18,9 @@ import protocolP2P.RequestResponseCode; import protocolP2P.LoadRequest; import protocolP2P.FileList; import protocolP2P.FilePart; +import protocolP2P.RatioRequest; +import protocolP2P.RatioResponse; +import protocolP2P.UpdateRatio; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; @@ -141,6 +144,7 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke case HASH_RESPONSE: case DISCOVER_RESPONSE: case NOT_A_TRACKER: + case RATIO_RESPONSE: // we were expecting a request, but we are receiving a response throw new ProtocolError(); default : @@ -319,6 +323,15 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke case DISCOVER_RESPONSE: payload = (Payload) new DiscoverResponse(packet); break; + case RATIO_REQUEST: + payload = (Payload) new RatioRequest(packet); + break; + case RATIO_RESPONSE: + payload = (Payload) new RatioResponse(packet); + break; + case UPDATE_RATIO: + payload = (Payload) new UpdateRatio(packet); + break; default: payload = new Payload(packet); // this can throw TransmissionError break; diff --git a/src/protocolP2P/ProtocolP2PPacketUDP.java b/src/protocolP2P/ProtocolP2PPacketUDP.java index a54733b..e7fbbc5 100644 --- a/src/protocolP2P/ProtocolP2PPacketUDP.java +++ b/src/protocolP2P/ProtocolP2PPacketUDP.java @@ -19,6 +19,9 @@ import protocolP2P.RequestResponseCode; import protocolP2P.LoadRequest; import protocolP2P.FileList; import protocolP2P.FilePart; +import protocolP2P.RatioRequest; +import protocolP2P.RatioResponse; +import protocolP2P.UpdateRatio; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketAddress; @@ -134,6 +137,7 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket case HASH_RESPONSE: case DISCOVER_RESPONSE: case NOT_A_TRACKER: + case RATIO_RESPONSE: // we were expecting a request, but we are receiving a response throw new ProtocolError(); default : @@ -304,6 +308,15 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket case DISCOVER_RESPONSE: payload = (Payload) new DiscoverResponse(packet); break; + case RATIO_REQUEST: + payload = (Payload) new RatioRequest(packet); + break; + case RATIO_RESPONSE: + payload = (Payload) new RatioResponse(packet); + break; + case UPDATE_RATIO: + payload = (Payload) new UpdateRatio(packet); + break; default: payload = new Payload(packet); // this can throw TransmissionError break;