-  Fixed regularPacketsTest unit test, which checks how regularly packets are sent from C2S thread.
- Removed misused GameClient in main server thread.

tags: #story[1124]
This commit is contained in:
Haoming Yin
2017-08-17 11:46:14 +12:00
parent d7a290478d
commit 7bda2bc580
3 changed files with 44 additions and 48 deletions
@@ -21,7 +21,6 @@ import seng302.model.PolarTable;
import seng302.model.ServerYacht;
import seng302.model.mark.CompoundMark;
import seng302.utilities.GeoUtility;
import seng302.visualiser.GameClient;
/**
* A class describing the overall server, which creates and collects server threads for each client
@@ -48,8 +47,6 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
private ServerSocket serverSocket = null;
private ArrayList<ServerToClientThread> serverToClientThreads = new ArrayList<>();
private GameClient gameClient;
public MainServerThread() {
new GameState("localhost");
try {
@@ -270,25 +267,17 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
terminated = true;
}
/**
* Pass GameClient to main server thread so it can access the properties inside.
*
* @param gameClient gameClient
*/
public void setGameClient(GameClient gameClient) {
this.gameClient = gameClient;
}
/**
* Initialise boats to specific spaced out geopoints behind starting line.
*/
private void initialiseBoatPositions() {
// Getting the start line compound marks
CompoundMark cm = gameClient.getCourseData().getCompoundMarks().get(1);
GeoPoint startMark1 = new GeoPoint(cm.getMarks().get(0).getLat(),
cm.getMarks().get(0).getLng());
GeoPoint startMark2 = new GeoPoint(cm.getMarks().get(1).getLat(),
cm.getMarks().get(1).getLng());
// if (gameClient== null) {
// return;
// }
CompoundMark cm = GameState.getMarkOrder().getMarkOrder().get(0);
GeoPoint startMark1 = cm.getSubMark(1);
GeoPoint startMark2 = cm.getSubMark(2);
// Calculating midpoint
Double perpendicularAngle = GeoUtility.getBearing(startMark1, startMark2);
@@ -130,7 +130,6 @@ public class GameClient {
});
this.lobbyController = lobbyController;
server.setGameClient(this);
}
private void loadStartScreen() {
@@ -1,9 +1,15 @@
package seng302.visualiser.ClientToServerTests;
import java.util.ArrayList;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import seng302.gameServer.GameStages;
import seng302.gameServer.GameState;
import seng302.gameServer.MainServerThread;
import seng302.gameServer.messages.BoatAction;
import seng302.model.ServerYacht;
import seng302.visualiser.ClientToServerThread;
/**
@@ -16,39 +22,39 @@ public class RegularPacketsTest {
@Before
public void setup() throws Exception {
// new GameState("localhost");
// serverThread = new MainServerThread();
// clientThread = new ClientToServerThread("localhost", 4942);
// GameState.setCurrentStage(GameStages.RACING);
new GameState("localhost");
serverThread = new MainServerThread();
clientThread = new ClientToServerThread("localhost", 4942);
GameState.setCurrentStage(GameStages.RACING);
}
@Test
public void packetsSentAtRegularIntervals () throws Exception {
// final double TEST_DISTANCE = 10.0;
// serverThread.startGame();
// SleepThreadMaxDelay();
// ServerYacht yacht = new ArrayList<>(GameState.getYachts().values()).get(0);
// double startAngle = yacht.getHeading();
// long startTime = System.currentTimeMillis();
// clientThread.sendBoatAction(BoatAction.UPWIND);
// Thread.sleep(200);
// while (Math.abs(yacht.getHeading() - startAngle) < TEST_DISTANCE) {
// Thread.sleep(1);
// }
// clientThread.sendBoatAction(BoatAction.MAINTAIN_HEADING);
// long endTime = System.currentTimeMillis();
// SleepThreadMaxDelay();
// //Allowed to be two loops of delay due to loop delay and processing delay at client + server ends.
// Assert.assertEquals(
// TEST_DISTANCE / ServerYacht.TURN_STEP * ClientToServerThread.PACKET_SENDING_INTERVAL_MS,
// (endTime - startTime), 2 * ClientToServerThread.PACKET_SENDING_INTERVAL_MS);
final double TEST_DISTANCE = 10.0;
serverThread.startGame();
SleepThreadMaxDelay();
ServerYacht yacht = new ArrayList<>(GameState.getYachts().values()).get(0);
double startAngle = yacht.getHeading();
long startTime = System.currentTimeMillis();
clientThread.sendBoatAction(BoatAction.UPWIND); //start sending
Thread.sleep(200);
while (Math.abs(yacht.getHeading() - startAngle) < TEST_DISTANCE) {
Thread.sleep(1);
}
clientThread.sendBoatAction(BoatAction.MAINTAIN_HEADING); //stop sending
long endTime = System.currentTimeMillis();
SleepThreadMaxDelay();
//Allowed to be two loops of delay due to loop delay and processing delay at client + server ends.
Assert.assertEquals(
TEST_DISTANCE / ServerYacht.TURN_STEP * ClientToServerThread.PACKET_SENDING_INTERVAL_MS,
(endTime - startTime), 2 * ClientToServerThread.PACKET_SENDING_INTERVAL_MS);
}
// @Test
// public void testArbitraryPacketSent() throws Exception {
// serverThread.startGame();
// SleepThreadMaxDelay();
// Yacht yacht = new ArrayList<>(GameState.getYachts().values()).get(0);
// ServerYacht yacht = new ArrayList<>(GameState.getYachts().values()).get(0);
// boolean startState = yacht.getSailIn();
// clientThread.sendBoatAction(BoatAction.SAILS_IN);
// SleepThreadMaxDelay();
@@ -61,15 +67,17 @@ public class RegularPacketsTest {
* @throws Exception Thrown if thread crashes or something
*/
private void SleepThreadMaxDelay() throws Exception {
Thread.sleep(200);
Thread.sleep(100);
}
@After
public void teardown () throws Exception {
// clientThread.setSocketToClose();
// serverThread.terminate();
// GameState.setCurrentStage(GameStages.LOBBYING);
// for (int i = 0; i<20; i++)
// SleepThreadMaxDelay(); //Make sure socket is closed and toolkit remade.
public void teardown() {
try {
clientThread.setSocketToClose();
serverThread.terminate();
GameState.setCurrentStage(GameStages.LOBBYING);
} catch (Exception e) {
System.out.println("Caught expected exception.");
}
}
}