From 2de911b617d3b2df008b48c731a223a8d735e31a Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 29 Feb 2020 00:46:39 +0100 Subject: [PATCH] fixup! Change to ProtocolP2PPacket, should be easier to implement tcp --- src/protocolP2P/FilePart.java | 8 ++++---- src/protocolP2P/Payload.java | 18 +++++++++--------- src/protocolP2P/ProtocolP2PPacket.java | 13 +------------ src/protocolP2P/ProtocolP2PPacketUDP.java | 6 +++--- src/protocolP2P/RequestResponseCode.java | 2 +- 5 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index c8368c9..2e2a211 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -85,11 +85,11 @@ public class FilePart extends Payload { // set Payload size setPayloadSize(size - OFFSET_POSITION, packet); // write offset to Packet - BytesArrayTools.write(packet, OFFSET_POSITION, (long)offset); + BytesArrayTools.write(packet, OFFSET_POSITION, offset); // write totalSize to Packet - BytesArrayTools.write(packet, TOTAL_FILESIZE_POSITION, (long)totalSize); + BytesArrayTools.write(packet, TOTAL_FILESIZE_POSITION, totalSize); // write filename’s size to Packet - BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, (int)filename.length()); + BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filename.length()); try { // write filename to Packet int i = FILENAME_POSITION; @@ -135,7 +135,7 @@ public class FilePart extends Payload { if (size == 0) { throw new ProtocolError(); } - // cannot excess datagram size + // cannot excess packet size if ((FILENAME_POSITION + size) > (getPayloadSize(packet) + OFFSET_POSITION)) { throw new ProtocolError(); } diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index 5ecc5d9..6868b56 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -32,24 +32,24 @@ public class Payload { checkRequestResponseCode(); // this can throw InternalError } - /** Constructor used to create a Payload (when no more specific subclasses exists) using datagram as parameter. + /** Constructor used to create a Payload (when no more specific subclasses exists) using packet as parameter. * If payload size is not empty, using subclass is required. - * @param datagram the full datagram received + * @param packet the full packet received * @throws ProtocolError * @throws InternalError * @throws TransmissionError * @throws SizeError */ - protected Payload(byte[] datagram) throws SizeError, ProtocolError, InternalError, TransmissionError { + protected Payload(byte[] packet) throws SizeError, ProtocolError, InternalError, TransmissionError { /* asserts to help debugging */ - assert getPayloadSize(datagram) + 8 <= datagram.length : "Payload is truncated"; - if (datagram.length < getPayloadSize(datagram) + 8) { + assert getPayloadSize(packet) + 8 <= packet.length : "Payload is truncated"; + if (packet.length < getPayloadSize(packet) + 8) { throw new TransmissionError(); } - assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE || (this instanceof FileList) : "LIST_RESPONSE must use FilePart class"; - assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE || (this instanceof FilePart) : "LOAD_RESPONSE must use FileList class"; - assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_REQUEST || (this instanceof LoadRequest) : "LOAD_REQUEST must use LoadRequest class"; - requestResponseCode = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); + assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE || (this instanceof FileList) : "LIST_RESPONSE must use FilePart class"; + assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE || (this instanceof FilePart) : "LOAD_RESPONSE must use FileList class"; + assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_REQUEST || (this instanceof LoadRequest) : "LOAD_REQUEST must use LoadRequest class"; + requestResponseCode = RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]); checkRequestResponseCode(); // this can throw InternalError } diff --git a/src/protocolP2P/ProtocolP2PPacket.java b/src/protocolP2P/ProtocolP2PPacket.java index f5115fe..094c3f9 100644 --- a/src/protocolP2P/ProtocolP2PPacket.java +++ b/src/protocolP2P/ProtocolP2PPacket.java @@ -10,21 +10,10 @@ import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; import remoteException.EmptyFile; -import tools.BytesArrayTools; -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.net.DatagramPacket; -import java.net.DatagramSocket; import java.net.InetAddress; import java.io.IOException; -import java.net.UnknownHostException; -/** Representation of datagram. +/** Representation of packet. * @author Louis Royer * @author Flavien Haas * @author JS Auge diff --git a/src/protocolP2P/ProtocolP2PPacketUDP.java b/src/protocolP2P/ProtocolP2PPacketUDP.java index 03d6cba..61fe07f 100644 --- a/src/protocolP2P/ProtocolP2PPacketUDP.java +++ b/src/protocolP2P/ProtocolP2PPacketUDP.java @@ -23,7 +23,7 @@ import java.net.DatagramSocket; import java.net.SocketAddress; import java.io.IOException; -/** Representation of datagram. +/** Representation of packet. * @author Louis Royer * @author Flavien Haas * @author JS Auge @@ -36,8 +36,8 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { 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 datagram). - * @param payload the payload associated with the datagram to send + /** Constructor with payload parameter (typically used when sending packet). + * @param payload the payload associated with the packet to send */ public ProtocolP2PPacketUDP(Payload payload) { super(payload); diff --git a/src/protocolP2P/RequestResponseCode.java b/src/protocolP2P/RequestResponseCode.java index b741501..2549c95 100644 --- a/src/protocolP2P/RequestResponseCode.java +++ b/src/protocolP2P/RequestResponseCode.java @@ -48,7 +48,7 @@ public enum RequestResponseCode { } /** Gives enum from Packet code. - * @param code value of the element in datagram + * @param code value of the element in packet * @return enum element */ protected static RequestResponseCode fromCode(byte code) throws ProtocolError {