Implemented MessageRouter.
Added ConnectionToServerCommandFactory, and JoinSuccessfulCommand, RaceParticipantsFullCOmmand, ServerFullCommand. Added IncomingHeartBeatCommandFactory, and IncomingHeartBeatCommand. Added ConnectionToServerState, which represents the client's connection state to server. Renamed VisualiserInput to VisualiserRaceController. Added ConnectionToServer, which tracks the client's connection to server. Added ConnectionToServerController, which accepts JoinAcceptance messages, turns them into commands, and passes them to ConnectionToServer. Added IncomingHeartBeatService, which tracks the heart beat status of the connection. Added IncomingHeartBeatController, which accepts HeartBeat messages, turns them into commands, and passes them to IncomingHeartBeatService. Refactored ServerConnection a bit. #story[1095]
This commit is contained in:
+41
@@ -0,0 +1,41 @@
|
||||
package visualiser.Commands.ConnectionToServerCommands;
|
||||
|
||||
|
||||
import mock.exceptions.CommandConstructionException;
|
||||
import mock.model.commandFactory.Command;
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.JoinAcceptance;
|
||||
import visualiser.network.ConnectionToServer;
|
||||
|
||||
/**
|
||||
* Factory to create ConnectionToServer commands.
|
||||
*/
|
||||
public class ConnectionToServerCommandFactory {
|
||||
|
||||
/**
|
||||
* Generates a command to execute on server connection based on the type of {@link network.Messages.Enums.JoinAcceptanceEnum}.
|
||||
* @param message The message to turn into a command.
|
||||
* @param connectionToServer The connection for the command to operate on.
|
||||
* @return The command to execute the given action.
|
||||
* @throws CommandConstructionException Thrown if the command cannot be constructed.
|
||||
*/
|
||||
public static Command create(AC35Data message, ConnectionToServer connectionToServer) throws CommandConstructionException {
|
||||
|
||||
if (!(message instanceof JoinAcceptance)) {
|
||||
throw new CommandConstructionException("Message: " + message + " is not a JoinAcceptance message.");
|
||||
}
|
||||
|
||||
JoinAcceptance joinAcceptance = (JoinAcceptance) message;
|
||||
|
||||
|
||||
switch(joinAcceptance.getAcceptanceType()) {
|
||||
|
||||
case JOIN_SUCCESSFUL: return new JoinSuccessfulCommand(joinAcceptance, connectionToServer);
|
||||
case RACE_PARTICIPANTS_FULL: return new RaceParticipantsFullCommand(joinAcceptance, connectionToServer);
|
||||
case SERVER_FULL: return new ServerFullCommand(joinAcceptance, connectionToServer);
|
||||
|
||||
default: throw new CommandConstructionException("Could not create command for JoinAcceptance: " + joinAcceptance + ". Unknown JoinAcceptanceEnum.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package visualiser.Commands.ConnectionToServerCommands;
|
||||
|
||||
import mock.model.commandFactory.Command;
|
||||
import network.Messages.JoinAcceptance;
|
||||
import visualiser.enums.ConnectionToServerState;
|
||||
import visualiser.network.ConnectionToServer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
/**
|
||||
* Command created when a {@link network.Messages.Enums.JoinAcceptanceEnum#JOIN_SUCCESSFUL} {@link network.Messages.JoinAcceptance} message is received.
|
||||
*/
|
||||
public class JoinSuccessfulCommand implements Command {
|
||||
|
||||
/**
|
||||
* The message to operate on.
|
||||
*/
|
||||
private JoinAcceptance joinAcceptance;
|
||||
|
||||
/**
|
||||
* The context to operate on.
|
||||
*/
|
||||
private ConnectionToServer connectionToServer;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link JoinSuccessfulCommand}, which operates on a given {@link ConnectionToServer}.
|
||||
* @param joinAcceptance The message to operate on.
|
||||
* @param connectionToServer The context to operate on.
|
||||
*/
|
||||
public JoinSuccessfulCommand(JoinAcceptance joinAcceptance, ConnectionToServer connectionToServer) {
|
||||
this.joinAcceptance = joinAcceptance;
|
||||
this.connectionToServer = connectionToServer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
connectionToServer.setJoinAcceptance(joinAcceptance);
|
||||
|
||||
connectionToServer.setConnectionState(ConnectionToServerState.CONNECTED);
|
||||
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package visualiser.Commands.ConnectionToServerCommands;
|
||||
|
||||
import mock.model.commandFactory.Command;
|
||||
import network.Messages.JoinAcceptance;
|
||||
import visualiser.enums.ConnectionToServerState;
|
||||
import visualiser.network.ConnectionToServer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
/**
|
||||
* Command created when a {@link network.Messages.Enums.JoinAcceptanceEnum#RACE_PARTICIPANTS_FULL} {@link JoinAcceptance} message is received.
|
||||
*/
|
||||
public class RaceParticipantsFullCommand implements Command {
|
||||
|
||||
/**
|
||||
* The message to operate on.
|
||||
*/
|
||||
private JoinAcceptance joinAcceptance;
|
||||
|
||||
/**
|
||||
* The context to operate on.
|
||||
*/
|
||||
private ConnectionToServer connectionToServer;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link RaceParticipantsFullCommand}, which operates on a given {@link ConnectionToServer}.
|
||||
* @param joinAcceptance The message to operate on.
|
||||
* @param connectionToServer The context to operate on.
|
||||
*/
|
||||
public RaceParticipantsFullCommand(JoinAcceptance joinAcceptance, ConnectionToServer connectionToServer) {
|
||||
this.joinAcceptance = joinAcceptance;
|
||||
this.connectionToServer = connectionToServer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
connectionToServer.setJoinAcceptance(joinAcceptance);
|
||||
|
||||
connectionToServer.setConnectionState(ConnectionToServerState.DECLINED);
|
||||
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package visualiser.Commands.ConnectionToServerCommands;
|
||||
|
||||
import mock.model.commandFactory.Command;
|
||||
import network.Messages.JoinAcceptance;
|
||||
import visualiser.enums.ConnectionToServerState;
|
||||
import visualiser.network.ConnectionToServer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
/**
|
||||
* Command created when a {@link network.Messages.Enums.JoinAcceptanceEnum#SERVER_FULL} {@link JoinAcceptance} message is received.
|
||||
*/
|
||||
public class ServerFullCommand implements Command {
|
||||
|
||||
/**
|
||||
* The message to operate on.
|
||||
*/
|
||||
private JoinAcceptance joinAcceptance;
|
||||
|
||||
/**
|
||||
* The context to operate on.
|
||||
*/
|
||||
private ConnectionToServer connectionToServer;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link ServerFullCommand}, which operates on a given {@link ConnectionToServer}.
|
||||
* @param joinAcceptance The message to operate on.
|
||||
* @param connectionToServer The context to operate on.
|
||||
*/
|
||||
public ServerFullCommand(JoinAcceptance joinAcceptance, ConnectionToServer connectionToServer) {
|
||||
this.joinAcceptance = joinAcceptance;
|
||||
this.connectionToServer = connectionToServer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
connectionToServer.setJoinAcceptance(joinAcceptance);
|
||||
|
||||
connectionToServer.setConnectionState(ConnectionToServerState.DECLINED);
|
||||
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package visualiser.Commands.IncomingHeartBeatCommands;
|
||||
|
||||
import mock.model.commandFactory.Command;
|
||||
import network.Messages.HeartBeat;
|
||||
import network.Messages.JoinAcceptance;
|
||||
import visualiser.enums.ConnectionToServerState;
|
||||
import visualiser.network.ConnectionToServer;
|
||||
import visualiser.network.IncomingHeartBeatService;
|
||||
|
||||
|
||||
/**
|
||||
* Command created when a {@link HeartBeat} message is received.
|
||||
*/
|
||||
public class IncomingHeartBeatCommand implements Command {
|
||||
|
||||
/**
|
||||
* The message to operate on.
|
||||
*/
|
||||
private HeartBeat heartBeat;
|
||||
|
||||
/**
|
||||
* The context to operate on.
|
||||
*/
|
||||
private IncomingHeartBeatService incomingHeartBeatService;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link IncomingHeartBeatCommand}, which operates on a given {@link IncomingHeartBeatService}.
|
||||
* @param heartBeat The message to operate on.
|
||||
* @param incomingHeartBeatService The context to operate on.
|
||||
*/
|
||||
public IncomingHeartBeatCommand(HeartBeat heartBeat, IncomingHeartBeatService incomingHeartBeatService) {
|
||||
this.heartBeat = heartBeat;
|
||||
this.incomingHeartBeatService = incomingHeartBeatService;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
incomingHeartBeatService.setLastHeartBeatSeqNum(heartBeat.getSequenceNumber());
|
||||
|
||||
incomingHeartBeatService.setLastHeartbeatTime(System.currentTimeMillis());
|
||||
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package visualiser.Commands.IncomingHeartBeatCommands;
|
||||
|
||||
|
||||
import mock.exceptions.CommandConstructionException;
|
||||
import mock.model.commandFactory.Command;
|
||||
import network.Messages.AC35Data;
|
||||
import network.Messages.HeartBeat;
|
||||
import visualiser.network.IncomingHeartBeatService;
|
||||
|
||||
/**
|
||||
* Factory to create IncomingHeartBeatService commands.
|
||||
*/
|
||||
public class IncomingHeartBeatCommandFactory {
|
||||
|
||||
/**
|
||||
* Generates a command on an IncomingHeartBeatService.
|
||||
* @param message The message to turn into a command.
|
||||
* @param incomingHeartBeatService The context for the command to operate on.
|
||||
* @return The command to execute the given action.
|
||||
* @throws CommandConstructionException Thrown if the command cannot be constructed.
|
||||
*/
|
||||
public static Command create(AC35Data message, IncomingHeartBeatService incomingHeartBeatService) throws CommandConstructionException {
|
||||
|
||||
if (!(message instanceof HeartBeat)) {
|
||||
throw new CommandConstructionException("Message: " + message + " is not a HeartBeat message.");
|
||||
}
|
||||
|
||||
HeartBeat heartBeat = (HeartBeat) message;
|
||||
|
||||
return new IncomingHeartBeatCommand(heartBeat, incomingHeartBeatService);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user