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;
public class DiscoveryServerClient {
private final Integer UPDATE_INTERVAL_MS = 700;
private final Integer UPDATE_INTERVAL_MS = 1000;
private static String roomCode = null;
private Timer serverListingUpdateTimer;
@@ -35,6 +35,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
private Logger logger = LoggerFactory.getLogger(MainServerThread.class);
private static final int PORT = 4942;
private static int selectedPort = PORT;
private static final Integer CLIENT_UPDATES_PER_SECOND = 60;
private boolean terminated;
@@ -65,7 +66,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
.setMapName(regattaXMLData.getCourseName())
.setCapacity(capacity)
.setNumberOfPlayers(numPlayers - 1)
.registerGame(PORT, regattaXMLData.getRegattaName());
.registerGame(selectedPort, regattaXMLData.getRegattaName());
} catch (IOException e) {
logger.warn("Could not register server");
}
@@ -74,7 +75,10 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
public MainServerThread() {
new GameState();
try {
serverSocket = new ServerSocket(PORT);
serverSocket = new ServerSocket(0);
selectedPort = serverSocket.getLocalPort();
System.out.println("selectedPort = " + selectedPort);
} catch (IOException e) {
logger.trace("IO error in server thread handler upon trying to make new server socket",
0);
@@ -459,4 +463,8 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
public boolean hasStarted() {
return hasStarted;
}
public int getPortNumber() {
return selectedPort;
}
}
@@ -165,7 +165,7 @@ public class GameClient {
}
try {
startClientToServerThread(ipAddress, 4942);
startClientToServerThread(ipAddress, server.getPortNumber());
} catch (IOException e) {
showConnectionError("Cannot connect to server as host");
}
@@ -199,8 +199,10 @@ public class GameClient {
this.lobbyController = ViewManager.getInstance().goToLobby(false);
lobbyController.setPortNumber(""+server.getPortNumber());
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() {
@@ -71,6 +71,8 @@ public class LobbyController implements Initializable {
@FXML
private Label roomLabel;
@FXML
private Label portNumber;
@FXML
private Pane speedTokenPane, handlingTokenPane, windWalkerTokenPane, bumperTokenPane, randomTokenPane;
//---------FXML END---------//
@@ -85,6 +87,8 @@ public class LobbyController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
roomLabel.setText("");
portNumber.setText("");
this.playerBoats = ViewManager.getInstance().getGameClient().getAllBoatsMap();
if (this.playersColor == null) {
@@ -376,4 +380,8 @@ public class LobbyController implements Initializable {
public void setRoomCode(String 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() {
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());
if (serverDescription == null){
+46 -68
View File
@@ -66,6 +66,11 @@
<GridPane.margin>
<Insets right="20.0" top="10.0" />
</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>
</children>
<columnConstraints>
@@ -80,12 +85,9 @@
</GridPane>
<GridPane GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints hgrow="NEVER" maxWidth="-Infinity" minWidth="-Infinity"
prefWidth="115.0"/>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308"
minWidth="337.0" prefWidth="430.0"/>
<ColumnConstraints hgrow="NEVER" maxWidth="350.0" minWidth="350.0"
prefWidth="350.0"/>
<ColumnConstraints hgrow="NEVER" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="115.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="337.0" prefWidth="430.0" />
<ColumnConstraints hgrow="NEVER" maxWidth="350.0" minWidth="350.0" prefWidth="350.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="SOMETIMES" />
@@ -110,116 +112,92 @@
<Insets bottom="15.0" left="7.0" right="7.0" top="15.0" />
</GridPane.margin>
</AnchorPane>
<GridPane prefHeight="370.0" prefWidth="189.0" styleClass="tokenGridView"
vgap="5.0">
<GridPane prefHeight="370.0" prefWidth="189.0" styleClass="tokenGridView" vgap="5.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Tokens"
GridPane.halignment="CENTER">
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Tokens" GridPane.halignment="CENTER">
<font>
<Font name="System Bold" size="18.0"/>
<Font name="System Bold" size="18.0" />
</font>
</Text>
<StackPane prefHeight="150.0" prefWidth="200.0"
GridPane.rowIndex="1">
<StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="1">
<children>
<Label alignment="BOTTOM_CENTER" text="Speed"
StackPane.alignment="BOTTOM_CENTER">
<Label alignment="BOTTOM_CENTER" text="Speed" StackPane.alignment="BOTTOM_CENTER">
<font>
<Font size="12.0"/>
<Font size="12.0" />
</font>
<padding>
<Insets bottom="5.0"/>
<Insets bottom="5.0" />
</padding>
</Label>
<Pane fx:id="speedTokenPane" prefHeight="999.0"
prefWidth="200.0" styleClass="tokenView"/>
<Pane fx:id="speedTokenPane" prefHeight="999.0" prefWidth="200.0" styleClass="tokenView" />
</children>
</StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0"
GridPane.rowIndex="2">
<StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="2">
<children>
<Label alignment="BOTTOM_CENTER" text="Handling"
StackPane.alignment="BOTTOM_CENTER">
<Label alignment="BOTTOM_CENTER" text="Handling" StackPane.alignment="BOTTOM_CENTER">
<font>
<Font size="12.0"/>
<Font size="12.0" />
</font>
<padding>
<Insets bottom="5.0"/>
<Insets bottom="5.0" />
</padding>
</Label>
<Pane fx:id="handlingTokenPane" prefHeight="999.0"
prefWidth="200.0" styleClass="tokenView"/>
<Pane fx:id="handlingTokenPane" prefHeight="999.0" prefWidth="200.0" styleClass="tokenView" />
</children>
</StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0"
GridPane.rowIndex="3">
<StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="3">
<children>
<Label alignment="BOTTOM_CENTER" text="Wind Walker"
StackPane.alignment="BOTTOM_CENTER">
<Label alignment="BOTTOM_CENTER" text="Wind Walker" StackPane.alignment="BOTTOM_CENTER">
<font>
<Font size="12.0"/>
<Font size="12.0" />
</font>
<padding>
<Insets bottom="5.0"/>
<Insets bottom="5.0" />
</padding>
</Label>
<Pane fx:id="windWalkerTokenPane" prefHeight="999.0"
prefWidth="200.0" styleClass="tokenView"/>
<Pane fx:id="windWalkerTokenPane" prefHeight="999.0" prefWidth="200.0" styleClass="tokenView" />
</children>
</StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0"
GridPane.rowIndex="4">
<StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="4">
<children>
<Label alignment="BOTTOM_CENTER" text="Bumper"
StackPane.alignment="BOTTOM_CENTER">
<Label alignment="BOTTOM_CENTER" text="Bumper" StackPane.alignment="BOTTOM_CENTER">
<font>
<Font size="12.0"/>
<Font size="12.0" />
</font>
<padding>
<Insets bottom="5.0"/>
<Insets bottom="5.0" />
</padding>
</Label>
<Pane fx:id="bumperTokenPane" prefHeight="999.0"
prefWidth="200.0" styleClass="tokenView"/>
<Pane fx:id="bumperTokenPane" prefHeight="999.0" prefWidth="200.0" styleClass="tokenView" />
</children>
</StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0"
GridPane.rowIndex="5">
<StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="5">
<children>
<Label alignment="BOTTOM_CENTER" text="Random"
StackPane.alignment="BOTTOM_CENTER">
<Label alignment="BOTTOM_CENTER" text="Random" StackPane.alignment="BOTTOM_CENTER">
<font>
<Font size="12.0"/>
<Font size="12.0" />
</font>
<padding>
<Insets bottom="5.0"/>
<Insets bottom="5.0" />
</padding>
</Label>
<Pane fx:id="randomTokenPane" prefHeight="999.0"
prefWidth="60.0" styleClass="tokenView"/>
<Pane fx:id="randomTokenPane" prefHeight="999.0" prefWidth="60.0" styleClass="tokenView" />
</children>
</StackPane>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="80.0" minWidth="80.0"
prefWidth="80.0"/>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="80.0" minWidth="80.0" prefWidth="80.0" />
</columnConstraints>
<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>
<rowConstraints>
<RowConstraints maxHeight="116.0" minHeight="0.0" prefHeight="40.0"
vgrow="NEVER"/>
<RowConstraints maxHeight="285.0" minHeight="-Infinity"
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 maxHeight="116.0" minHeight="0.0" prefHeight="40.0" vgrow="NEVER" />
<RowConstraints maxHeight="285.0" minHeight="-Infinity" 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>
</GridPane>
</children>
@@ -236,7 +214,7 @@
</GridPane>
</children>
<stylesheets>
<URL value="@../css/Master.css"/>
<URL value="@../css/LobbyView.css"/>
<URL value="@../css/Master.css" />
<URL value="@../css/LobbyView.css" />
</stylesheets>
</StackPane>
@@ -12,7 +12,7 @@ public class DisconnectionTest {
@Test
public void testServerDisconnection () throws Exception {
MainServerThread serverThread = new MainServerThread();
ClientToServerThread clientThread = new ClientToServerThread("localhost", 4942);
ClientToServerThread clientThread = new ClientToServerThread("localhost", serverThread.getPortNumber());
Thread.sleep(1000);
clientThread.addDisconnectionListener(message -> Assert.assertTrue(message != null));
serverThread.terminate();
@@ -20,7 +20,7 @@ public class RegularPacketsTest {
public void setup() throws Exception {
new GameState();
serverThread = new MainServerThread();
clientThread = new ClientToServerThread("localhost", 4942);
clientThread = new ClientToServerThread("localhost", serverThread.getPortNumber());
GameState.setCurrentStage(GameStages.RACING);
}
+2 -2
View File
@@ -45,7 +45,7 @@ public class SendChatSteps {
} catch (InterruptedException ie) {
ie.printStackTrace();
}
host = new ClientToServerThread("localhost", 4942);
host = new ClientToServerThread("localhost", mst.getPortNumber());
host.addStreamObserver(() -> {
while (host.getPacketQueue().peek() != null) {
StreamPacket packet = host.getPacketQueue().poll();
@@ -68,7 +68,7 @@ public class SendChatSteps {
} catch (InterruptedException ie) {
ie.printStackTrace();
}
client = new ClientToServerThread("localhost", 4942);
client = new ClientToServerThread("localhost", mst.getPortNumber());
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
+1 -1
View File
@@ -44,7 +44,7 @@ public class ToggleSailSteps {
} catch (InterruptedException ie) {
ie.printStackTrace();
}
client = new ClientToServerThread("localhost", 4942);
client = new ClientToServerThread("localhost", mst.getPortNumber());
try {
Thread.sleep(100);
} catch (InterruptedException ie) {