parent
5b6b0a9978
commit
cb8e81a7b2
@ -0,0 +1,63 @@
|
|||||||
|
package network.MessageDecoders;
|
||||||
|
|
||||||
|
import network.Exceptions.InvalidMessageException;
|
||||||
|
import network.Messages.AC35Data;
|
||||||
|
import network.Messages.CourseWinds;
|
||||||
|
import network.Messages.Enums.RaceStatusEnum;
|
||||||
|
import network.Messages.HostGame;
|
||||||
|
import network.Messages.RaceStatus;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static network.Utils.ByteConverter.bytesToInt;
|
||||||
|
|
||||||
|
public class HostGameMessageDecoder implements MessageDecoder {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The encoded message.
|
||||||
|
*/
|
||||||
|
private byte[] encodedMessage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The decoded message.
|
||||||
|
*/
|
||||||
|
private HostGame message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public HostGameMessageDecoder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AC35Data decode(byte[] encodedMessage) throws InvalidMessageException {
|
||||||
|
this.encodedMessage = encodedMessage;
|
||||||
|
|
||||||
|
try{
|
||||||
|
byte ipPart1 = encodedMessage[0];
|
||||||
|
byte ipPart2 = encodedMessage[1];
|
||||||
|
byte ipPart3 = encodedMessage[2];
|
||||||
|
byte ipPart4 = encodedMessage[3];
|
||||||
|
String ipString = ipPart1 + "." + ipPart2 + "." + ipPart3 + "." + ipPart4;
|
||||||
|
// System.out.println(ipString);
|
||||||
|
int port = bytesToInt(Arrays.copyOfRange(encodedMessage, 4, 8));
|
||||||
|
byte map = encodedMessage[8];
|
||||||
|
byte speed = encodedMessage[9];
|
||||||
|
byte status = encodedMessage[10];
|
||||||
|
byte requiredNumPlayers = encodedMessage[11];
|
||||||
|
byte currentNumPlayers = encodedMessage[12];
|
||||||
|
|
||||||
|
|
||||||
|
message = new HostGame(ipString, port, map,
|
||||||
|
speed, RaceStatusEnum.fromByte(status),
|
||||||
|
requiredNumPlayers, currentNumPlayers);
|
||||||
|
|
||||||
|
return message;
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new InvalidMessageException("Could not decode Host game message.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,54 @@
|
|||||||
|
package network.MessageEncoders;
|
||||||
|
|
||||||
|
import network.Exceptions.InvalidMessageException;
|
||||||
|
import network.Messages.AC35Data;
|
||||||
|
import network.Messages.HostGame;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static network.Utils.ByteConverter.intToBytes;
|
||||||
|
|
||||||
|
|
||||||
|
public class HostGameMessageEncoder implements MessageEncoder{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public HostGameMessageEncoder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] encode(AC35Data message) throws InvalidMessageException {
|
||||||
|
try{
|
||||||
|
//Downcast
|
||||||
|
HostGame hostGame = (HostGame) message;
|
||||||
|
|
||||||
|
ByteBuffer hostGameMessage = ByteBuffer.allocate(14);
|
||||||
|
|
||||||
|
ByteBuffer ipBytes = ByteBuffer.allocate(4);
|
||||||
|
String ip = hostGame.getIp();
|
||||||
|
String[] ipValues = ip.split("\\.");
|
||||||
|
for(String value:ipValues){
|
||||||
|
ipBytes.put(intToBytes(Integer.parseInt(value), 1)[0]);
|
||||||
|
}
|
||||||
|
byte raceStatus = hostGame.getStatus().getValue();
|
||||||
|
|
||||||
|
hostGameMessage.put(ipBytes.array());
|
||||||
|
hostGameMessage.put(intToBytes(hostGame.getPort()));
|
||||||
|
hostGameMessage.put(hostGame.getMap());
|
||||||
|
hostGameMessage.put(hostGame.getSpeed());
|
||||||
|
hostGameMessage.put(raceStatus);
|
||||||
|
hostGameMessage.put(hostGame.getRequiredNumPlayers());
|
||||||
|
hostGameMessage.put(hostGame.getCurrentNumPlayers());
|
||||||
|
|
||||||
|
|
||||||
|
// System.out.println(hostGameMessage.array()[4]);
|
||||||
|
return hostGameMessage.array();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new InvalidMessageException("Could not encode Host game message.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package network.Messages;
|
||||||
|
|
||||||
|
|
||||||
|
import network.Messages.Enums.MessageType;
|
||||||
|
import network.Messages.Enums.RaceStatusEnum;
|
||||||
|
|
||||||
|
public class HostGame extends AC35Data {
|
||||||
|
|
||||||
|
private String ip;
|
||||||
|
private int port;
|
||||||
|
private byte map;
|
||||||
|
private byte speed;
|
||||||
|
private RaceStatusEnum status;
|
||||||
|
private byte requiredNumPlayers;
|
||||||
|
private byte currentNumPlayers;
|
||||||
|
|
||||||
|
public HostGame(String ip, int port, byte map, byte speed,
|
||||||
|
RaceStatusEnum status, byte requiredNumPlayers,
|
||||||
|
byte currentNumPlayers) {
|
||||||
|
super(MessageType.HOST_GAME);
|
||||||
|
this.ip = ip;
|
||||||
|
this.port = port;
|
||||||
|
this.map = map;
|
||||||
|
this.speed = speed;
|
||||||
|
this.status = status;
|
||||||
|
this.requiredNumPlayers = requiredNumPlayers;
|
||||||
|
this.currentNumPlayers = currentNumPlayers;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the ip of host
|
||||||
|
*/
|
||||||
|
public String getIp() {
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the port of host
|
||||||
|
*/
|
||||||
|
public int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the map index
|
||||||
|
*/
|
||||||
|
public byte getMap() {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the speed value of game
|
||||||
|
*/
|
||||||
|
public byte getSpeed() {
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the status of race
|
||||||
|
*/
|
||||||
|
public RaceStatusEnum getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return required number of players
|
||||||
|
*/
|
||||||
|
public byte getRequiredNumPlayers() {
|
||||||
|
return requiredNumPlayers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return current number of players
|
||||||
|
*/
|
||||||
|
public byte getCurrentNumPlayers() {
|
||||||
|
return currentNumPlayers;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
package network.MessageDecoders;
|
||||||
|
|
||||||
|
import network.MessageEncoders.HostGameMessageEncoder;
|
||||||
|
import network.Messages.AC35Data;
|
||||||
|
import network.Messages.Enums.RaceStatusEnum;
|
||||||
|
import network.Messages.HostGame;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class HostGameMessageDecoderTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hostGameMessageDecoderTest() throws Exception {
|
||||||
|
HostGame testHost = new HostGame("127.0.0.1", 3779, (byte) 1, (byte) 2, RaceStatusEnum.PRESTART, (byte) 6, (byte) 2);
|
||||||
|
|
||||||
|
|
||||||
|
HostGameMessageEncoder encoder = new HostGameMessageEncoder();
|
||||||
|
|
||||||
|
byte[] encodedMessage = encoder.encode(testHost);
|
||||||
|
|
||||||
|
HostGameMessageDecoder decoder = new HostGameMessageDecoder();
|
||||||
|
|
||||||
|
HostGame decodedTest = (HostGame) decoder.decode(encodedMessage);
|
||||||
|
|
||||||
|
compareHostGameMessage(testHost, decodedTest);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void compareHostGameMessage(HostGame original, HostGame decoded) {
|
||||||
|
Assert.assertEquals(original.getIp(), decoded.getIp());
|
||||||
|
Assert.assertEquals(original.getPort(), decoded.getPort());
|
||||||
|
Assert.assertEquals(original.getSpeed(), decoded.getSpeed());
|
||||||
|
Assert.assertEquals(original.getStatus(), decoded.getStatus());
|
||||||
|
Assert.assertEquals(original.getRequiredNumPlayers(), decoded.getRequiredNumPlayers());
|
||||||
|
Assert.assertEquals(original.getCurrentNumPlayers(), decoded.getCurrentNumPlayers());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue