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
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -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…
Reference in new issue