Added port number randomization

- Servers all run on different ports, so multiple servers can run on one host
- Port is displayed in lobby

Tags: #story[1281]
This commit is contained in:
Michael Rausch
2017-09-28 15:57:59 +13:00
parent 8810554ce9
commit a05a41d5ec
10 changed files with 75 additions and 79 deletions
@@ -19,7 +19,7 @@ import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
public class DiscoveryServerClient { public class DiscoveryServerClient {
private final Integer UPDATE_INTERVAL_MS = 700; private final Integer UPDATE_INTERVAL_MS = 1000;
private static String roomCode = null; private static String roomCode = null;
private Timer serverListingUpdateTimer; private Timer serverListingUpdateTimer;
@@ -35,6 +35,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
private Logger logger = LoggerFactory.getLogger(MainServerThread.class); private Logger logger = LoggerFactory.getLogger(MainServerThread.class);
private static final int PORT = 4942; private static final int PORT = 4942;
private static int selectedPort = PORT;
private static final Integer CLIENT_UPDATES_PER_SECOND = 60; private static final Integer CLIENT_UPDATES_PER_SECOND = 60;
private boolean terminated; private boolean terminated;
@@ -65,7 +66,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
.setMapName(regattaXMLData.getCourseName()) .setMapName(regattaXMLData.getCourseName())
.setCapacity(capacity) .setCapacity(capacity)
.setNumberOfPlayers(numPlayers - 1) .setNumberOfPlayers(numPlayers - 1)
.registerGame(PORT, regattaXMLData.getRegattaName()); .registerGame(selectedPort, regattaXMLData.getRegattaName());
} catch (IOException e) { } catch (IOException e) {
logger.warn("Could not register server"); logger.warn("Could not register server");
} }
@@ -74,7 +75,10 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
public MainServerThread() { public MainServerThread() {
new GameState(); new GameState();
try { try {
serverSocket = new ServerSocket(PORT); serverSocket = new ServerSocket(0);
selectedPort = serverSocket.getLocalPort();
System.out.println("selectedPort = " + selectedPort);
} catch (IOException e) { } catch (IOException e) {
logger.trace("IO error in server thread handler upon trying to make new server socket", logger.trace("IO error in server thread handler upon trying to make new server socket",
0); 0);
@@ -459,4 +463,8 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
public boolean hasStarted() { public boolean hasStarted() {
return hasStarted; return hasStarted;
} }
public int getPortNumber() {
return selectedPort;
}
} }
@@ -165,7 +165,7 @@ public class GameClient {
} }
try { try {
startClientToServerThread(ipAddress, 4942); startClientToServerThread(ipAddress, server.getPortNumber());
} catch (IOException e) { } catch (IOException e) {
showConnectionError("Cannot connect to server as host"); showConnectionError("Cannot connect to server as host");
} }
@@ -199,8 +199,10 @@ public class GameClient {
this.lobbyController = ViewManager.getInstance().goToLobby(false); this.lobbyController = ViewManager.getInstance().goToLobby(false);
lobbyController.setPortNumber(""+server.getPortNumber());
ViewManager.getInstance().setPlayerList(clientLobbyList); ViewManager.getInstance().setPlayerList(clientLobbyList);
return new ServerDescription(serverName, regattaData.getCourseName(), GameState.getNumberOfPlayers(), GameState.getCapacity(), ipAddress, 4942); return new ServerDescription(serverName, regattaData.getCourseName(), GameState.getNumberOfPlayers(), GameState.getCapacity(), ipAddress, server.getPortNumber());
} }
private void tearDownConnection() { private void tearDownConnection() {
@@ -71,6 +71,8 @@ public class LobbyController implements Initializable {
@FXML @FXML
private Label roomLabel; private Label roomLabel;
@FXML @FXML
private Label portNumber;
@FXML
private Pane speedTokenPane, handlingTokenPane, windWalkerTokenPane, bumperTokenPane, randomTokenPane; private Pane speedTokenPane, handlingTokenPane, windWalkerTokenPane, bumperTokenPane, randomTokenPane;
//---------FXML END---------// //---------FXML END---------//
@@ -85,6 +87,8 @@ public class LobbyController implements Initializable {
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
roomLabel.setText(""); roomLabel.setText("");
portNumber.setText("");
this.playerBoats = ViewManager.getInstance().getGameClient().getAllBoatsMap(); this.playerBoats = ViewManager.getInstance().getGameClient().getAllBoatsMap();
if (this.playersColor == null) { if (this.playersColor == null) {
@@ -376,4 +380,8 @@ public class LobbyController implements Initializable {
public void setRoomCode(String roomCode) { public void setRoomCode(String roomCode) {
roomLabel.setText("Room: " + roomCode); roomLabel.setText("Room: " + roomCode);
} }
public void setPortNumber(String p){
portNumber.setText("Port: " + p);
}
} }
@@ -115,7 +115,7 @@ public class ServerCreationController implements Initializable {
*/ */
private void createServer() { private void createServer() {
ServerDescription serverDescription = ViewManager.getInstance().getGameClient() ServerDescription serverDescription = ViewManager.getInstance().getGameClient()
.runAsHost("localhost", 4941, serverName.getText(), (int) maxPlayersSlider .runAsHost("localhost", 0, serverName.getText(), (int) maxPlayersSlider
.getValue(), mapMaker.getCurrentRacePath(), (int) legsSlider.getValue(), pickupsCheckBox.isSelected()); .getValue(), mapMaker.getCurrentRacePath(), (int) legsSlider.getValue(), pickupsCheckBox.isSelected());
if (serverDescription == null){ if (serverDescription == null){
+32 -54
View File
@@ -66,6 +66,11 @@
<GridPane.margin> <GridPane.margin>
<Insets right="20.0" top="10.0" /> <Insets right="20.0" top="10.0" />
</GridPane.margin> </GridPane.margin>
</Label>
<Label fx:id="portNumber" text="Port: 4191" GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="20.0" top="-15.0" />
</GridPane.margin>
</Label> </Label>
</children> </children>
<columnConstraints> <columnConstraints>
@@ -80,12 +85,9 @@
</GridPane> </GridPane>
<GridPane GridPane.rowIndex="1"> <GridPane GridPane.rowIndex="1">
<columnConstraints> <columnConstraints>
<ColumnConstraints hgrow="NEVER" maxWidth="-Infinity" minWidth="-Infinity" <ColumnConstraints hgrow="NEVER" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="115.0" />
prefWidth="115.0"/> <ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="337.0" prefWidth="430.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" <ColumnConstraints hgrow="NEVER" maxWidth="350.0" minWidth="350.0" prefWidth="350.0" />
minWidth="337.0" prefWidth="430.0"/>
<ColumnConstraints hgrow="NEVER" maxWidth="350.0" minWidth="350.0"
prefWidth="350.0"/>
</columnConstraints> </columnConstraints>
<rowConstraints> <rowConstraints>
<RowConstraints vgrow="SOMETIMES" /> <RowConstraints vgrow="SOMETIMES" />
@@ -110,20 +112,16 @@
<Insets bottom="15.0" left="7.0" right="7.0" top="15.0" /> <Insets bottom="15.0" left="7.0" right="7.0" top="15.0" />
</GridPane.margin> </GridPane.margin>
</AnchorPane> </AnchorPane>
<GridPane prefHeight="370.0" prefWidth="189.0" styleClass="tokenGridView" <GridPane prefHeight="370.0" prefWidth="189.0" styleClass="tokenGridView" vgap="5.0">
vgap="5.0">
<children> <children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Tokens" <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Tokens" GridPane.halignment="CENTER">
GridPane.halignment="CENTER">
<font> <font>
<Font name="System Bold" size="18.0" /> <Font name="System Bold" size="18.0" />
</font> </font>
</Text> </Text>
<StackPane prefHeight="150.0" prefWidth="200.0" <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="1">
GridPane.rowIndex="1">
<children> <children>
<Label alignment="BOTTOM_CENTER" text="Speed" <Label alignment="BOTTOM_CENTER" text="Speed" StackPane.alignment="BOTTOM_CENTER">
StackPane.alignment="BOTTOM_CENTER">
<font> <font>
<Font size="12.0" /> <Font size="12.0" />
</font> </font>
@@ -131,15 +129,12 @@
<Insets bottom="5.0" /> <Insets bottom="5.0" />
</padding> </padding>
</Label> </Label>
<Pane fx:id="speedTokenPane" prefHeight="999.0" <Pane fx:id="speedTokenPane" prefHeight="999.0" prefWidth="200.0" styleClass="tokenView" />
prefWidth="200.0" styleClass="tokenView"/>
</children> </children>
</StackPane> </StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0" <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="2">
GridPane.rowIndex="2">
<children> <children>
<Label alignment="BOTTOM_CENTER" text="Handling" <Label alignment="BOTTOM_CENTER" text="Handling" StackPane.alignment="BOTTOM_CENTER">
StackPane.alignment="BOTTOM_CENTER">
<font> <font>
<Font size="12.0" /> <Font size="12.0" />
</font> </font>
@@ -147,15 +142,12 @@
<Insets bottom="5.0" /> <Insets bottom="5.0" />
</padding> </padding>
</Label> </Label>
<Pane fx:id="handlingTokenPane" prefHeight="999.0" <Pane fx:id="handlingTokenPane" prefHeight="999.0" prefWidth="200.0" styleClass="tokenView" />
prefWidth="200.0" styleClass="tokenView"/>
</children> </children>
</StackPane> </StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0" <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="3">
GridPane.rowIndex="3">
<children> <children>
<Label alignment="BOTTOM_CENTER" text="Wind Walker" <Label alignment="BOTTOM_CENTER" text="Wind Walker" StackPane.alignment="BOTTOM_CENTER">
StackPane.alignment="BOTTOM_CENTER">
<font> <font>
<Font size="12.0" /> <Font size="12.0" />
</font> </font>
@@ -163,15 +155,12 @@
<Insets bottom="5.0" /> <Insets bottom="5.0" />
</padding> </padding>
</Label> </Label>
<Pane fx:id="windWalkerTokenPane" prefHeight="999.0" <Pane fx:id="windWalkerTokenPane" prefHeight="999.0" prefWidth="200.0" styleClass="tokenView" />
prefWidth="200.0" styleClass="tokenView"/>
</children> </children>
</StackPane> </StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0" <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="4">
GridPane.rowIndex="4">
<children> <children>
<Label alignment="BOTTOM_CENTER" text="Bumper" <Label alignment="BOTTOM_CENTER" text="Bumper" StackPane.alignment="BOTTOM_CENTER">
StackPane.alignment="BOTTOM_CENTER">
<font> <font>
<Font size="12.0" /> <Font size="12.0" />
</font> </font>
@@ -179,15 +168,12 @@
<Insets bottom="5.0" /> <Insets bottom="5.0" />
</padding> </padding>
</Label> </Label>
<Pane fx:id="bumperTokenPane" prefHeight="999.0" <Pane fx:id="bumperTokenPane" prefHeight="999.0" prefWidth="200.0" styleClass="tokenView" />
prefWidth="200.0" styleClass="tokenView"/>
</children> </children>
</StackPane> </StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0" <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="5">
GridPane.rowIndex="5">
<children> <children>
<Label alignment="BOTTOM_CENTER" text="Random" <Label alignment="BOTTOM_CENTER" text="Random" StackPane.alignment="BOTTOM_CENTER">
StackPane.alignment="BOTTOM_CENTER">
<font> <font>
<Font size="12.0" /> <Font size="12.0" />
</font> </font>
@@ -195,31 +181,23 @@
<Insets bottom="5.0" /> <Insets bottom="5.0" />
</padding> </padding>
</Label> </Label>
<Pane fx:id="randomTokenPane" prefHeight="999.0" <Pane fx:id="randomTokenPane" prefHeight="999.0" prefWidth="60.0" styleClass="tokenView" />
prefWidth="60.0" styleClass="tokenView"/>
</children> </children>
</StackPane> </StackPane>
</children> </children>
<columnConstraints> <columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="80.0" minWidth="80.0" <ColumnConstraints hgrow="SOMETIMES" maxWidth="80.0" minWidth="80.0" prefWidth="80.0" />
prefWidth="80.0"/>
</columnConstraints> </columnConstraints>
<padding> <padding>
<Insets bottom="15.0" left="15.0" right="7.0" top="15.0" /> <Insets bottom="15.0" left="15.0" right="7.0" top="15.0" />
</padding> </padding>
<rowConstraints> <rowConstraints>
<RowConstraints maxHeight="116.0" minHeight="0.0" prefHeight="40.0" <RowConstraints maxHeight="116.0" minHeight="0.0" prefHeight="40.0" vgrow="NEVER" />
vgrow="NEVER"/> <RowConstraints maxHeight="285.0" minHeight="-Infinity" prefHeight="60.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="285.0" minHeight="-Infinity" <RowConstraints maxHeight="285.0" minHeight="-Infinity" prefHeight="60.0" vgrow="SOMETIMES" />
prefHeight="60.0" vgrow="SOMETIMES"/> <RowConstraints maxHeight="141.0" minHeight="-Infinity" prefHeight="60.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="285.0" minHeight="-Infinity" <RowConstraints maxHeight="285.0" minHeight="-Infinity" prefHeight="60.0" vgrow="SOMETIMES" />
prefHeight="60.0" vgrow="SOMETIMES"/> <RowConstraints maxHeight="285.0" minHeight="-Infinity" prefHeight="60.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="141.0" minHeight="-Infinity"
prefHeight="60.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="285.0" minHeight="-Infinity"
prefHeight="60.0" vgrow="SOMETIMES"/>
<RowConstraints maxHeight="285.0" minHeight="-Infinity"
prefHeight="60.0" vgrow="SOMETIMES"/>
</rowConstraints> </rowConstraints>
</GridPane> </GridPane>
</children> </children>
@@ -12,7 +12,7 @@ public class DisconnectionTest {
@Test @Test
public void testServerDisconnection () throws Exception { public void testServerDisconnection () throws Exception {
MainServerThread serverThread = new MainServerThread(); MainServerThread serverThread = new MainServerThread();
ClientToServerThread clientThread = new ClientToServerThread("localhost", 4942); ClientToServerThread clientThread = new ClientToServerThread("localhost", serverThread.getPortNumber());
Thread.sleep(1000); Thread.sleep(1000);
clientThread.addDisconnectionListener(message -> Assert.assertTrue(message != null)); clientThread.addDisconnectionListener(message -> Assert.assertTrue(message != null));
serverThread.terminate(); serverThread.terminate();
@@ -20,7 +20,7 @@ public class RegularPacketsTest {
public void setup() throws Exception { public void setup() throws Exception {
new GameState(); new GameState();
serverThread = new MainServerThread(); serverThread = new MainServerThread();
clientThread = new ClientToServerThread("localhost", 4942); clientThread = new ClientToServerThread("localhost", serverThread.getPortNumber());
GameState.setCurrentStage(GameStages.RACING); GameState.setCurrentStage(GameStages.RACING);
} }
+2 -2
View File
@@ -45,7 +45,7 @@ public class SendChatSteps {
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
host = new ClientToServerThread("localhost", 4942); host = new ClientToServerThread("localhost", mst.getPortNumber());
host.addStreamObserver(() -> { host.addStreamObserver(() -> {
while (host.getPacketQueue().peek() != null) { while (host.getPacketQueue().peek() != null) {
StreamPacket packet = host.getPacketQueue().poll(); StreamPacket packet = host.getPacketQueue().poll();
@@ -68,7 +68,7 @@ public class SendChatSteps {
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
client = new ClientToServerThread("localhost", 4942); client = new ClientToServerThread("localhost", mst.getPortNumber());
try { try {
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
+1 -1
View File
@@ -44,7 +44,7 @@ public class ToggleSailSteps {
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
client = new ClientToServerThread("localhost", 4942); client = new ClientToServerThread("localhost", mst.getPortNumber());
try { try {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {