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]
main
fjc40 8 years ago
parent ca2b8a8899
commit 7ea5b31fa1

@ -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.

@ -0,0 +1,99 @@
package network.MessageDecoders;
import network.MessageEncoders.RaceVisionByteEncoder;
import network.Messages.Enums.JoinAcceptanceEnum;
import network.Messages.JoinAcceptance;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test for the {@link network.Messages.JoinAcceptance} encoder and decoder
*/
public class JoinAcceptanceDecoderTest {
/**
* Encodes and decodes a given message.
* @param message Message to encode/decode.
* @return The decoded message.
*/
private JoinAcceptance encodeDecodeMessage(JoinAcceptance message) {
//Encode.
byte [] testEncodedMessage = RaceVisionByteEncoder.joinAcceptance(message);
//Decode.
JoinAcceptanceDecoder testDecoder = new JoinAcceptanceDecoder(testEncodedMessage);
JoinAcceptance decodedMessage = testDecoder.getMessage();
return decodedMessage;
}
/**
* Tests if a specific acceptance type message can be encoded and decoded correctly.
* @param type The type of acceptance response.
* @param sourceID The source ID to assign.
*/
private void responseTypeTest(JoinAcceptanceEnum type, int sourceID) throws Exception {
//Prepare message.
JoinAcceptance beforeMessage = new JoinAcceptance(type, sourceID);
//Encode/decode it.
JoinAcceptance afterMessage = encodeDecodeMessage(beforeMessage);
//Compare.
assertEquals(beforeMessage.getAcceptanceType().getValue(), afterMessage.getAcceptanceType().getValue());
assertEquals(beforeMessage.getSourceID(), afterMessage.getSourceID());
}
/**
* Tests if a join success message, with a source ID, can be encoded and decoded correctly.
*/
@Test
public void joinSuccessSourceIDTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL, 12345);
}
/**
* Tests if a join success message, with no source ID, can be encoded and decoded correctly.
*/
@Test
public void joinSuccessNoSourceIDTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.JOIN_SUCCESSFUL, 0);
}
/**
* Tests if a participants full message can be encoded and decoded correctly.
*/
@Test
public void participantFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.RACE_PARTICIPANTS_FULL, 0);
}
/**
* Tests if a ghosts full message can be encoded and decoded correctly.
*/
@Test
public void ghostFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.GHOST_PARTICIPANTS_FULL, 0);
}
/**
* Tests if a server full message can be encoded and decoded correctly.
*/
@Test
public void serverFullTest() throws Exception {
responseTypeTest(JoinAcceptanceEnum.SERVER_FULL, 0);
}
}

@ -0,0 +1,82 @@
package network.MessageDecoders;
import network.MessageEncoders.RaceVisionByteEncoder;
import network.Messages.Enums.RequestToJoinEnum;
import network.Messages.RequestToJoin;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test for the RequestToJoin encoder and decoder
*/
public class RequestToJoinDecoderTest {
/**
* Encodes and decodes a given message.
* @param message Message to encode/decode.
* @return The decoded message.
*/
private RequestToJoin encodeDecodeMessage(RequestToJoin message) {
//Encode.
byte [] testEncodedMessage = RaceVisionByteEncoder.requestToJoin(message);
//Decode.
RequestToJoinDecoder testDecoder = new RequestToJoinDecoder(testEncodedMessage);
RequestToJoin decodedMessage = testDecoder.getMessage();
return decodedMessage;
}
/**
* Tests if a specific request type message can be encoded and decoded correctly.
* @param type The type of join request.
*/
private void requestTypeTest(RequestToJoinEnum type) throws Exception {
//Prepare message.
RequestToJoin beforeMessage = new RequestToJoin(type);
//Encode/decode it.
RequestToJoin afterMessage = encodeDecodeMessage(beforeMessage);
//Compare.
assertEquals(beforeMessage.getRequestType().getValue(), afterMessage.getRequestType().getValue());
}
/**
* Tests if a spectator request message can be encoded and decoded correctly.
*/
@Test
public void spectatorTest() throws Exception {
requestTypeTest(RequestToJoinEnum.SPECTATOR);
}
/**
* Tests if a participant request message can be encoded and decoded correctly.
*/
@Test
public void participantTest() throws Exception {
requestTypeTest(RequestToJoinEnum.PARTICIPANT);
}
/**
* Tests if a ghost request message can be encoded and decoded correctly.
*/
@Test
public void ghostTest() throws Exception {
requestTypeTest(RequestToJoinEnum.GHOST);
}
}
Loading…
Cancel
Save