@@ -10,45 +10,101 @@ import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
/**
|
||||
* Abstract class representing a packet in the Open Autonomous Connection (OAC) protocol.
|
||||
* This class extends the base Packet class and includes additional functionality specific to OAC.
|
||||
*/
|
||||
public abstract class OACPacket extends Packet {
|
||||
|
||||
/**
|
||||
* The protocol version associated with this packet.
|
||||
*/
|
||||
@Getter
|
||||
private final ProtocolVersion protocolVersion;
|
||||
|
||||
/**
|
||||
* The response code for the packet, defaulting to RESPONSE_NOT_REQUIRED.
|
||||
*/
|
||||
private DNSResponseCode responseCode = DNSResponseCode.RESPONSE_NOT_REQUIRED;
|
||||
|
||||
/**
|
||||
* Constructor for OACPacket.
|
||||
* @param id The unique identifier for the packet.
|
||||
* @param protocolVersion The protocol version associated with this packet.
|
||||
*/
|
||||
public OACPacket(int id, ProtocolVersion protocolVersion) {
|
||||
super(id);
|
||||
this.protocolVersion = protocolVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the response code for the packet.
|
||||
* @return The DNSResponseCode associated with the packet.
|
||||
*/
|
||||
protected final DNSResponseCode getResponseCode() {
|
||||
return responseCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the response code for the packet.
|
||||
* @param responseCode The DNSResponseCode to set for the packet.
|
||||
*/
|
||||
protected final void setResponseCode(DNSResponseCode responseCode) {
|
||||
this.responseCode = responseCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the packet data to the output stream.
|
||||
* @param packetHandler The packet handler managing the packet.
|
||||
* @param objectOutputStream The output stream to write the packet data to.
|
||||
* @throws IOException If an I/O error occurs.
|
||||
* @throws ClassNotFoundException If a class cannot be found during serialization.
|
||||
*/
|
||||
@Override
|
||||
public final void write(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException {
|
||||
// Write the specific packet data
|
||||
onWrite(packetHandler, objectOutputStream);
|
||||
|
||||
// Write the response code if the protocol version is not classic
|
||||
if (protocolVersion != ProtocolVersion.PV_1_0_0_CLASSIC) objectOutputStream.writeObject(responseCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void read(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||||
// Read the specific packet data
|
||||
onRead(packetHandler, objectInputStream);
|
||||
if (protocolVersion != ProtocolVersion.PV_1_0_0_CLASSIC) {
|
||||
responseCode = (DNSResponseCode) objectInputStream.readObject();
|
||||
onResponseCodeRead(packetHandler, objectInputStream);
|
||||
}
|
||||
|
||||
// Read the response code if the protocol version is not classic
|
||||
if (protocolVersion != ProtocolVersion.PV_1_0_0_CLASSIC) responseCode = (DNSResponseCode) objectInputStream.readObject();
|
||||
else responseCode = DNSResponseCode.RESPONSE_NOT_REQUIRED;
|
||||
|
||||
// Call the response code read handler
|
||||
onResponseCodeRead(packetHandler, objectInputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method to be implemented by subclasses for writing specific packet data.
|
||||
* @param packetHandler The packet handler managing the packet.
|
||||
* @param objectOutputStream The output stream to write the packet data to.
|
||||
* @throws IOException If an I/O error occurs.
|
||||
* @throws ClassNotFoundException If a class cannot be found during serialization.
|
||||
*/
|
||||
public abstract void onWrite(PacketHandler packetHandler, ObjectOutputStream objectOutputStream) throws IOException, ClassNotFoundException;
|
||||
|
||||
/**
|
||||
* Abstract method to be implemented by subclasses for reading specific packet data.
|
||||
* @param packetHandler The packet handler managing the packet.
|
||||
* @param objectInputStream The input stream to read the packet data from.
|
||||
* @throws IOException If an I/O error occurs.
|
||||
* @throws ClassNotFoundException If a class cannot be found during deserialization.
|
||||
*/
|
||||
public abstract void onRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException;
|
||||
|
||||
protected void onResponseCodeRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) {
|
||||
}
|
||||
/**
|
||||
* Method called after the response code has been read from the input stream.
|
||||
* Subclasses can override this method to handle any additional logic based on the response code.
|
||||
* @param packetHandler The packet handler managing the packet.
|
||||
* @param objectInputStream The input stream from which the response code was read.
|
||||
*/
|
||||
protected void onResponseCodeRead(PacketHandler packetHandler, ObjectInputStream objectInputStream) {}
|
||||
}
|
||||
|
Reference in New Issue
Block a user