From 1910092c88dd1e677bd289507782aa664ed0d118 Mon Sep 17 00:00:00 2001 From: "Peter Fichtner (pfichtner)" Date: Sat, 16 Sep 2023 10:19:24 +0200 Subject: [PATCH] house keeping --- .../fourwins/tournament/RealTournamentIT.java | 8 ++-- .../ase/fourwins/udp/server/UdpServer.java | 37 ++++++++++--------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/core/src/test/java/org/ase/fourwins/tournament/RealTournamentIT.java b/core/src/test/java/org/ase/fourwins/tournament/RealTournamentIT.java index e88f9ea2..1c95facc 100644 --- a/core/src/test/java/org/ase/fourwins/tournament/RealTournamentIT.java +++ b/core/src/test/java/org/ase/fourwins/tournament/RealTournamentIT.java @@ -1,11 +1,13 @@ package org.ase.fourwins.tournament; import static java.util.function.Function.identity; +import static java.util.function.Predicate.not; import static java.util.stream.Collectors.toList; import static net.jqwik.api.Arbitraries.frequency; import static net.jqwik.api.Arbitraries.strings; import static org.ase.fourwins.board.Board.Score.LOSE; import static org.ase.fourwins.board.Board.Score.WIN; +import static org.ase.fourwins.tournament.DefaultTournament.CoffeebreakGame.COFFEE_BREAK_WIN_MESSAGE; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertThat; @@ -21,7 +23,6 @@ import org.ase.fourwins.board.mockplayers.ColumnTrackingMockPlayer; import org.ase.fourwins.board.mockplayers.PlayerMock; import org.ase.fourwins.game.Player; -import org.ase.fourwins.tournament.DefaultTournament.CoffeebreakGame; import org.ase.fourwins.tournament.listener.TournamentListener; import net.jqwik.api.Arbitraries; @@ -41,8 +42,7 @@ public class RealTournamentIT { static final int MAX_SEASONS = 8; - static final Predicate isCoffeeBreak = s -> CoffeebreakGame.COFFEE_BREAK_WIN_MESSAGE - .equals(s.getReason()); + static final Predicate isNotCoffeeBreak = not(s -> COFFEE_BREAK_WIN_MESSAGE.equals(s.getReason())); private TournamentListener scoreListener; @@ -120,7 +120,7 @@ public void seasonEnded() { } }; tournament.addTournamentListener(scoreListener); - Stream states = playSeasons(tournament, players, seasons).filter(isCoffeeBreak.negate()); + Stream states = playSeasons(tournament, players, seasons).filter(isNotCoffeeBreak); assertThat(seasonEndedCalls.get(), is(seasons)); return states; } diff --git a/udp/src/main/java/org/ase/fourwins/udp/server/UdpServer.java b/udp/src/main/java/org/ase/fourwins/udp/server/UdpServer.java index f99a1b63..b29201a0 100644 --- a/udp/src/main/java/org/ase/fourwins/udp/server/UdpServer.java +++ b/udp/src/main/java/org/ase/fourwins/udp/server/UdpServer.java @@ -1,17 +1,18 @@ package org.ase.fourwins.udp.server; import static java.lang.System.currentTimeMillis; +import static java.util.Arrays.stream; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; +import static java.util.stream.Stream.concat; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; -import java.util.Arrays; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; @@ -29,6 +30,7 @@ import java.util.stream.Stream; import org.ase.fourwins.board.Board.GameState; +import org.ase.fourwins.board.Board.Score; import org.ase.fourwins.board.BoardInfo; import org.ase.fourwins.game.Player; import org.ase.fourwins.tournament.Tournament; @@ -61,7 +63,7 @@ public class UdpServer { private final byte[] buf = new byte[1024]; private final Lock lock = new ReentrantLock(); - private final Condition playerRegistered = lock.newCondition(); + private final Condition newPlayerRegistered = lock.newCondition(); private volatile boolean keepSeasonRunning = true; private DatagramSocket socket; @@ -96,7 +98,7 @@ String getResponse(String delimiter, String uuid) throws TimeoutException { timeouts = 0; String[] splitted = response.split(delimiter); if (splitted.length > 1 && splitted[splitted.length - 1].equals(uuid)) { - return Arrays.stream(splitted).limit(splitted.length - 1).collect(joining(delimiter)); + return stream(splitted).limit(splitted.length - 1).collect(joining(delimiter)); } } } catch (InterruptedException e) { @@ -104,6 +106,11 @@ String getResponse(String delimiter, String uuid) throws TimeoutException { } } + void send(String command, Object... parts) { + send(concat(Stream.of(command), stream(parts)).map(e -> e == null ? "" : String.valueOf(e)) + .collect(joining(";"))); + } + synchronized void send(String message) { try { byte[] bytes = message.getBytes(); @@ -167,22 +174,18 @@ protected int nextColumn() { @Override public boolean joinGame(String opposite, BoardInfo boardInfo) { - playerInfo.send("NEW GAME;" + opposite); + playerInfo.send("NEW GAME", opposite); return true; } @Override protected void tokenWasInserted(String token, int column) { - playerInfo.send("TOKEN INSERTED;" + token + ";" + column); + playerInfo.send("TOKEN INSERTED", token, column); super.tokenWasInserted(token, column); } public void gameEnded(GameState state) { - String reason = state.getReason(); - Object token = state.getToken(); - String message = Stream.of("RESULT", state.getScore(), token, reason) - .map(e -> e == null ? "" : String.valueOf(e)).collect(joining(";")); - playerInfo.send(message); + playerInfo.send("RESULT", state.getScore(), state.getToken(), state.getReason()); super.gameEnded(state); } @@ -196,7 +199,7 @@ protected void playSeasonsForever(Tournament tournament) { if (players.size() < minPlayers) { try { lock.lock(); - playerRegistered.await(5, SECONDS); + newPlayerRegistered.await(5, SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { @@ -312,7 +315,7 @@ private Predicate inetAddressAndName(InetAddress clientIp, String } private Predicate inetAddress(InetAddress inetAddress) { - return i -> i.getAdressInfo().equals(inetAddress); + return i -> Objects.equals(i.getAdressInfo(), inetAddress); } private Predicate port(int port) { @@ -320,11 +323,11 @@ private Predicate port(int port) { } private Predicate name(String name) { - return i -> i.getName().equals(name); + return i -> Objects.equals(i.getName(), name); } - private Optional findBy(Predicate p) { - return players.keySet().stream().filter(p).findFirst(); + private Optional findBy(Predicate predicate) { + return players.keySet().stream().filter(predicate).findFirst(); } private void handleRegisterCommand(UdpPlayerInfo playerInfo) { @@ -336,10 +339,10 @@ private void handleRegisterCommand(UdpPlayerInfo playerInfo) { System.out.println( "Player " + playerInfo.getName() + " registered, we now have " + players.size() + " player(s)"); - playerInfo.send("WELCOME;" + playerInfo.getName()); + playerInfo.send("WELCOME", playerInfo.getName()); try { lock.lock(); - playerRegistered.signal(); + newPlayerRegistered.signal(); } finally { lock.unlock(); }