fixup! Change to ProtocolP2PPacket, should be easier to implement tcp

pull/12/head
Louis Royer 5 years ago
parent 5d05702545
commit 2de911b617

@ -85,11 +85,11 @@ public class FilePart extends Payload {
// set Payload size // set Payload size
setPayloadSize(size - OFFSET_POSITION, packet); setPayloadSize(size - OFFSET_POSITION, packet);
// write offset to Packet // write offset to Packet
BytesArrayTools.write(packet, OFFSET_POSITION, (long)offset); BytesArrayTools.write(packet, OFFSET_POSITION, offset);
// write totalSize to Packet // write totalSize to Packet
BytesArrayTools.write(packet, TOTAL_FILESIZE_POSITION, (long)totalSize); BytesArrayTools.write(packet, TOTAL_FILESIZE_POSITION, totalSize);
// write filenames size to Packet // write filenames size to Packet
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, (int)filename.length()); BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filename.length());
try { try {
// write filename to Packet // write filename to Packet
int i = FILENAME_POSITION; int i = FILENAME_POSITION;
@ -135,7 +135,7 @@ public class FilePart extends Payload {
if (size == 0) { if (size == 0) {
throw new ProtocolError(); throw new ProtocolError();
} }
// cannot excess datagram size // cannot excess packet size
if ((FILENAME_POSITION + size) > (getPayloadSize(packet) + OFFSET_POSITION)) { if ((FILENAME_POSITION + size) > (getPayloadSize(packet) + OFFSET_POSITION)) {
throw new ProtocolError(); throw new ProtocolError();
} }

@ -32,24 +32,24 @@ public class Payload {
checkRequestResponseCode(); // this can throw InternalError 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. * 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 ProtocolError
* @throws InternalError * @throws InternalError
* @throws TransmissionError * @throws TransmissionError
* @throws SizeError * @throws SizeError
*/ */
protected Payload(byte[] datagram) throws SizeError, ProtocolError, InternalError, TransmissionError { protected Payload(byte[] packet) throws SizeError, ProtocolError, InternalError, TransmissionError {
/* asserts to help debugging */ /* asserts to help debugging */
assert getPayloadSize(datagram) + 8 <= datagram.length : "Payload is truncated"; assert getPayloadSize(packet) + 8 <= packet.length : "Payload is truncated";
if (datagram.length < getPayloadSize(datagram) + 8) { if (packet.length < getPayloadSize(packet) + 8) {
throw new TransmissionError(); 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(packet[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(packet[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"; assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_REQUEST || (this instanceof LoadRequest) : "LOAD_REQUEST must use LoadRequest class";
requestResponseCode = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); requestResponseCode = RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]);
checkRequestResponseCode(); // this can throw InternalError checkRequestResponseCode(); // this can throw InternalError
} }

@ -10,21 +10,10 @@ import remoteException.NotFound;
import remoteException.ProtocolRemoteError; import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError; import remoteException.VersionRemoteError;
import remoteException.EmptyFile; 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.net.InetAddress;
import java.io.IOException; import java.io.IOException;
import java.net.UnknownHostException;
/** Representation of datagram. /** Representation of packet.
* @author Louis Royer * @author Louis Royer
* @author Flavien Haas * @author Flavien Haas
* @author JS Auge * @author JS Auge

@ -23,7 +23,7 @@ import java.net.DatagramSocket;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.io.IOException; import java.io.IOException;
/** Representation of datagram. /** Representation of packet.
* @author Louis Royer * @author Louis Royer
* @author Flavien Haas * @author Flavien Haas
* @author JS Auge * @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 responseSocket; // socket used to recept request and send response
private DatagramSocket requestSocket; // socket used to send request and to reception response private DatagramSocket requestSocket; // socket used to send request and to reception response
/** Constructor with payload parameter (typically used when sending datagram). /** Constructor with payload parameter (typically used when sending packet).
* @param payload the payload associated with the datagram to send * @param payload the payload associated with the packet to send
*/ */
public ProtocolP2PPacketUDP(Payload payload) { public ProtocolP2PPacketUDP(Payload payload) {
super(payload); super(payload);

@ -48,7 +48,7 @@ public enum RequestResponseCode {
} }
/** Gives enum from Packet code. /** Gives enum from Packet code.
* @param code value of the element in datagram * @param code value of the element in packet
* @return enum element * @return enum element
*/ */
protected static RequestResponseCode fromCode(byte code) throws ProtocolError { protected static RequestResponseCode fromCode(byte code) throws ProtocolError {

Loading…
Cancel
Save