RequestToJoinEnum contains an int instead of a byte.

Added requestToJoin and joinAcceptance encoding functions to RaceVisionByteEncoder.
Implemented JoinAcceptanceDecoder.
Implemented RequestToJoinDecoder.
Added tests for encoding/decoding RequestToJoin and JoinAcceptance messages.

issue #35
#story[1095]
This commit is contained in:
fjc40
2017-08-05 20:42:25 +12:00
parent ca2b8a8899
commit 7ea5b31fa1
6 changed files with 366 additions and 15 deletions
@@ -0,0 +1,70 @@
package network.MessageDecoders;
import network.Messages.Enums.JoinAcceptanceEnum;
import network.Messages.JoinAcceptance;
import network.Utils.ByteConverter;
import java.util.Arrays;
/**
* Decoder for {@link JoinAcceptance} messages.
*/
public class JoinAcceptanceDecoder {
/**
* The encoded message.
*/
private byte[] encodedMessage;
/**
* The decoded message.
*/
private JoinAcceptance message;
/**
* Constructs a decoder to decode a given message.
* @param encodedMessage The message to decode.
*/
public JoinAcceptanceDecoder(byte[] encodedMessage) {
this.encodedMessage = encodedMessage;
decode();
}
/**
* Decodes the contained message.
*/
private void decode() {
//SourceID is first four bytes.
byte[] sourceIdBytes = Arrays.copyOfRange(encodedMessage, 0, 4);
//Next byte is acceptance type.
byte[] acceptanceBytes = Arrays.copyOfRange(encodedMessage, 4, 5);
//SourceID is an int.
int sourceID = ByteConverter.bytesToInt(sourceIdBytes);
//Acceptance enum is a byte.
JoinAcceptanceEnum acceptanceType = JoinAcceptanceEnum.fromByte(acceptanceBytes[0]);
message = new JoinAcceptance(acceptanceType, sourceID);
}
/**
* Returns the decoded message.
* @return The decoded message.
*/
public JoinAcceptance getMessage() {
return message;
}
}
@@ -0,0 +1,62 @@
package network.MessageDecoders;
import network.Messages.Enums.RequestToJoinEnum;
import network.Messages.RequestToJoin;
import network.Utils.ByteConverter;
import java.util.Arrays;
/**
* Decoder for {@link network.Messages.RequestToJoin} messages.
*/
public class RequestToJoinDecoder {
/**
* The encoded message.
*/
private byte[] encodedRequest;
/**
* The decoded message.
*/
private RequestToJoin message;
/**
* Constructs a decoder to decode a given message.
* @param encodedRequest The message to decode.
*/
public RequestToJoinDecoder(byte[] encodedRequest) {
this.encodedRequest = encodedRequest;
decode();
}
/**
* Decodes the contained message.
*/
private void decode() {
//Request type is first four bytes.
byte[] requestTypeBytes = Arrays.copyOfRange(encodedRequest, 0, 4);
//Request type is an integral type.
int requestTypeInt = ByteConverter.bytesToInt(requestTypeBytes);
RequestToJoinEnum requestType = RequestToJoinEnum.fromInt(requestTypeInt);
message = new RequestToJoin(requestType);
}
/**
* Returns the decoded message.
* @return The decoded message.
*/
public RequestToJoin getMessage() {
return message;
}
}
@@ -346,4 +346,42 @@ public class RaceVisionByteEncoder {
return result;
}
/**
* Encodes a {@link RequestToJoin} message.
* @param requestToJoin Message to encode.
* @return Encoded message.
*/
public static byte[] requestToJoin(RequestToJoin requestToJoin) {
ByteBuffer requestToJoinBuffer = ByteBuffer.allocate(4);
requestToJoinBuffer.put(intToBytes(requestToJoin.getRequestType().getValue(), 4));
byte [] result = requestToJoinBuffer.array();
return result;
}
/**
* Encodes a {@link JoinAcceptance} message.
* @param joinAcceptance Message to encode.
* @return Encoded message.
*/
public static byte[] joinAcceptance(JoinAcceptance joinAcceptance) {
//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;
}
}
@@ -35,22 +35,22 @@ public enum RequestToJoinEnum {
/**
* Primitive value of the enum.
*/
private byte value;
private int value;
/**
* Ctor. Creates a RequestToJoinEnum from a given primitive integer value, cast to a byte.
* @param value Integer, which is cast to byte, to construct from.
* Ctor. Creates a RequestToJoinEnum from a given int value.
* @param value Integer to construct from.
*/
private RequestToJoinEnum(int value) {
this.value = (byte) value;
this.value = value;
}
/**
* Returns the primitive value of the enum.
* @return Primitive value of the enum.
*/
public byte getValue() {
public int getValue() {
return value;
}
@@ -58,32 +58,32 @@ public enum RequestToJoinEnum {
/**
* Stores a mapping between Byte values and RequestToJoinEnum values.
* Stores a mapping between Integer values and RequestToJoinEnum values.
*/
private static final Map<Byte, RequestToJoinEnum> byteToRequestMap = new HashMap<>();
private static final Map<Integer, RequestToJoinEnum> intToRequestMap = new HashMap<>();
/*
Static initialization block. Initializes the byteToRequestMap.
Static initialization block. Initializes the intToRequestMap.
*/
static {
for (RequestToJoinEnum type : RequestToJoinEnum.values()) {
RequestToJoinEnum.byteToRequestMap.put(type.value, type);
RequestToJoinEnum.intToRequestMap.put(type.value, type);
}
}
/**
* Returns the enumeration value which corresponds to a given byte value.
* @param requestToJoinEnum Byte value to convert to a RequestToJoinEnum value.
* @return The RequestToJoinEnum value which corresponds to the given byte value.
* Returns the enumeration value which corresponds to a given int value.
* @param requestToJoinEnum int value to convert to a RequestToJoinEnum value.
* @return The RequestToJoinEnum value which corresponds to the given int value.
*/
public static RequestToJoinEnum fromByte(byte requestToJoinEnum) {
public static RequestToJoinEnum fromInt(int requestToJoinEnum) {
//Gets the corresponding MessageType from the map.
RequestToJoinEnum type = RequestToJoinEnum.byteToRequestMap.get(requestToJoinEnum);
RequestToJoinEnum type = RequestToJoinEnum.intToRequestMap.get(requestToJoinEnum);
if (type == null) {
//If the byte value wasn't found, return the NOT_A_REQUEST_TYPE RequestToJoinEnum.
//If the int value wasn't found, return the NOT_A_REQUEST_TYPE RequestToJoinEnum.
return RequestToJoinEnum.NOT_A_REQUEST_TYPE;
} else {
//Otherwise, return the RequestToJoinEnum.