encoders and decoders for the host game message with datatype and test
#story[1188]
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,8 @@ public enum MessageType {
|
||||
*/
|
||||
ASSIGN_PLAYER_BOAT(121),
|
||||
|
||||
HOST_GAME(108),
|
||||
|
||||
NOTAMESSAGE(0);
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user