Added an InvalidMessageTypeException - thrown whenever we encounter a MessageType that isn't recognised or isn't supported.
Added EncoderFactory. This creates specific MessageEncoders. Supports JoinAcceptance and RequestToJoin. Added MessageEncoder interface. Added JoinAcceptanceEncoder. Added encode(AC35Data) function to RaceVisionByteEncoder. Added RequestToJonEncoder. Updated RequestToJoin and JoinAcceptance decode/encode tests to use the above. issue #35 #36 #story[1095]main
parent
7ea5b31fa1
commit
16686678a7
@ -0,0 +1,17 @@
|
||||
package network.Exceptions;
|
||||
|
||||
|
||||
/**
|
||||
* An exception thrown when we encounter a message type that isn't recognised.
|
||||
*/
|
||||
public class InvalidMessageTypeException extends Exception {
|
||||
|
||||
|
||||
public InvalidMessageTypeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public InvalidMessageTypeException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
package network.MessageEncoders;
|
||||
|
||||
|
||||
import network.Exceptions.InvalidMessageTypeException;
|
||||
import network.Messages.Enums.MessageType;
|
||||
|
||||
/**
|
||||
* Factory to create the appropriate encoder for a given message.
|
||||
*/
|
||||
public class EncoderFactory {
|
||||
|
||||
|
||||
/**
|
||||
* Private constructor. Currently doesn't need to be constructed.
|
||||
*/
|
||||
private EncoderFactory(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates the correct type of encoder for a given message type.
|
||||
* @param type Type of message you want an encoder for.
|
||||
* @return The encoder.
|
||||
* @throws InvalidMessageTypeException If you pass in a {@link MessageType} that isn't recognised.
|
||||
*/
|
||||
public static MessageEncoder create(MessageType type) throws InvalidMessageTypeException {
|
||||
|
||||
|
||||
switch (type) {
|
||||
|
||||
case REQUEST_TO_JOIN: return new RequestToJoinEncoder();
|
||||
|
||||
case JOIN_ACCEPTANCE: return new JoinAcceptanceEncoder();
|
||||
|
||||
|
||||
default: throw new InvalidMessageTypeException("Unrecognised message type: " + type);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
package network.MessageEncoders;
|
||||
|
||||
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.JoinAcceptance;
|
||||
import network.Utils.ByteConverter;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static network.Utils.ByteConverter.intToBytes;
|
||||
|
||||
/**
|
||||
* This encoder can encode a {@link JoinAcceptance} message.
|
||||
*/
|
||||
public class JoinAcceptanceEncoder implements MessageEncoder {
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public JoinAcceptanceEncoder() {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] encode(AC35Data message) {
|
||||
|
||||
//Downcast.
|
||||
JoinAcceptance joinAcceptance = (JoinAcceptance) message;
|
||||
|
||||
//Message is 5 bytes.
|
||||
ByteBuffer joinAcceptanceBuffer = ByteBuffer.allocate(5);
|
||||
|
||||
//Source ID is first four bytes.
|
||||
joinAcceptanceBuffer.put(intToBytes(joinAcceptance.getSourceID(), 4));
|
||||
//Acceptance type is next byte.
|
||||
joinAcceptanceBuffer.put(intToBytes(joinAcceptance.getAcceptanceType().getValue(), 1));
|
||||
|
||||
byte [] result = joinAcceptanceBuffer.array();
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package network.MessageEncoders;
|
||||
|
||||
|
||||
import network.Messages.AC35Data;
|
||||
|
||||
|
||||
/**
|
||||
* This is the interface that all message encoders must implement.
|
||||
* It allows for {@link #encode(AC35Data)}ing messages.
|
||||
*/
|
||||
public interface MessageEncoder {
|
||||
|
||||
|
||||
/**
|
||||
* Encodes a given message.
|
||||
* @param message The message to encode.
|
||||
* @return Message in byte encoded form.
|
||||
*/
|
||||
public byte[] encode(AC35Data message);
|
||||
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package network.MessageEncoders;
|
||||
|
||||
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.RequestToJoin;
|
||||
import network.Utils.ByteConverter;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* This encoder can encode a {@link network.Messages.RequestToJoin} message.
|
||||
*/
|
||||
public class RequestToJoinEncoder implements MessageEncoder {
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public RequestToJoinEncoder() {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] encode(AC35Data message) {
|
||||
|
||||
//Downcast.
|
||||
RequestToJoin requestToJoin = (RequestToJoin) message;
|
||||
|
||||
|
||||
ByteBuffer requestToJoinBuffer = ByteBuffer.allocate(4);
|
||||
|
||||
requestToJoinBuffer.put(ByteConverter.intToBytes(requestToJoin.getRequestType().getValue(), 4));
|
||||
|
||||
byte [] result = requestToJoinBuffer.array();
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue