Change to ProtocolP2PPacket, should be easier to implement tcp
parent
c17a7e6cd5
commit
5d05702545
@ -1,5 +1,5 @@
|
|||||||
package exception;
|
package exception;
|
||||||
/** Used on reception side when size as set in datagram is too big, and we cant store this in a int/long as usual. */
|
/** 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 {
|
||||||
private static final long serialVersionUID = 12L;
|
private static final long serialVersionUID = 12L;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,113 @@
|
|||||||
|
package protocolP2P;
|
||||||
|
import exception.InternalError;
|
||||||
|
import exception.ProtocolError;
|
||||||
|
import exception.SizeError;
|
||||||
|
import exception.TransmissionError;
|
||||||
|
import exception.VersionError;
|
||||||
|
import remoteException.EmptyDirectory;
|
||||||
|
import remoteException.InternalRemoteError;
|
||||||
|
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.
|
||||||
|
* @author Louis Royer
|
||||||
|
* @author Flavien Haas
|
||||||
|
* @author JS Auge
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public abstract class ProtocolP2PPacket {
|
||||||
|
private final static byte PROTOCOL_VERSION = 0x12;
|
||||||
|
protected final static int VERSION_POSITION = 0;
|
||||||
|
protected byte version;
|
||||||
|
protected Payload payload;
|
||||||
|
|
||||||
|
/** Constructor with payload parameter (typically used when sending Packet).
|
||||||
|
* @param payload the payload associated with the Packet to send
|
||||||
|
*/
|
||||||
|
public ProtocolP2PPacket(Payload payload) {
|
||||||
|
version = PROTOCOL_VERSION;
|
||||||
|
this.payload = payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Send a request
|
||||||
|
* @param socket Socket used to send packet.
|
||||||
|
* @throws InternalError
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public abstract void sendRequest(Object socket) throws InternalError, IOException;
|
||||||
|
|
||||||
|
/** Send a response
|
||||||
|
* @param response Response to send.
|
||||||
|
* @throws InternalError
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public abstract void sendResponse(ProtocolP2PPacket response) throws InternalError, IOException;
|
||||||
|
|
||||||
|
/** Receive a response
|
||||||
|
* @throws EmptyFile
|
||||||
|
* @throws NotFound
|
||||||
|
* @throws EmptyDirectory
|
||||||
|
* @throws InternalRemoteError
|
||||||
|
* @throws VersionRemoteError
|
||||||
|
* @throws ProtocolRemoteError
|
||||||
|
* @throws TransmissionError
|
||||||
|
* @throws ProtocolError
|
||||||
|
* @throws VersionError
|
||||||
|
* @throws InternalError
|
||||||
|
* @throws SizeError
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public abstract ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException;
|
||||||
|
|
||||||
|
/** Receive a request, subclasses must overwrite this constructor.
|
||||||
|
* @param serverSocket socket used to get the request
|
||||||
|
* @throws TransmissionError
|
||||||
|
* @throws ProtocolError
|
||||||
|
* @throws VersionError
|
||||||
|
* @throws InternalError
|
||||||
|
* @throws SizeError
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
protected ProtocolP2PPacket(Object serverSocket) throws TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException {}
|
||||||
|
|
||||||
|
/** Construct a packet from byte[], subclasses must overwrite this constructor.
|
||||||
|
* @param packet Packet received
|
||||||
|
* @throws TransmissionError
|
||||||
|
* @throws ProtocolError
|
||||||
|
* @throws VersionError
|
||||||
|
* @throws InternalError
|
||||||
|
* @throws SizeError
|
||||||
|
*/
|
||||||
|
protected ProtocolP2PPacket(byte[] packet) throws TransmissionError, ProtocolError, VersionError, InternalError, SizeError {}
|
||||||
|
|
||||||
|
/** Returns Payload associated with the Packet.
|
||||||
|
* @return payload associated with the Packet.
|
||||||
|
*/
|
||||||
|
public Payload getPayload() {
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Used to check protocol version when a Packet is constructed from bytes[].
|
||||||
|
* @throws VersionError
|
||||||
|
*/
|
||||||
|
protected void checkProtocolVersion() throws VersionError {
|
||||||
|
if (PROTOCOL_VERSION != version) {
|
||||||
|
throw new VersionError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue