From 7080aeab9ccd0f2bbdf3aa53083a13fa7078c83b Mon Sep 17 00:00:00 2001 From: rolleander Date: Sun, 5 May 2024 14:51:42 +0200 Subject: [PATCH] fix tests --- .../networklib/client/LobbyGameClient.java | 16 +++++------ .../client/impl/LobbyLookupSite.java | 10 +++---- .../client/tasks/LobbyDiscoveryResult.java | 27 ------------------- .../client/tasks/ServerDiscoveryResult.java | 27 +++++++++++++++++++ ...redLobbies.java => ServerInformation.java} | 4 +-- ...LobbyListResult.java => ServerResult.java} | 14 +++++----- .../client/tasks/impl/CreateLobbyTask.java | 6 ++++- .../client/tasks/impl/LobbyDiscoveryTask.java | 18 ++++++------- .../client/tasks/impl/LobbyListTask.java | 14 ++-------- .../networklib/network/nt/NT_LobbyCreate.java | 1 + .../networklib/server/ServerSitesHandler.java | 4 +++ .../server/impl/ConnectionSite.java | 5 +++- .../networklib/site/MultiSitesHandler.java | 5 ++++ .../lobby/client/LobbyClientApplication.java | 8 +++--- .../broll/networklib/test/NetworkTest.java | 2 +- .../test/impl/LobbyConnectionTests.java | 2 +- 16 files changed, 82 insertions(+), 81 deletions(-) delete mode 100644 src/main/java/com/broll/networklib/client/tasks/LobbyDiscoveryResult.java create mode 100644 src/main/java/com/broll/networklib/client/tasks/ServerDiscoveryResult.java rename src/main/java/com/broll/networklib/client/tasks/{DiscoveredLobbies.java => ServerInformation.java} (85%) rename src/main/java/com/broll/networklib/client/tasks/{LobbyListResult.java => ServerResult.java} (50%) diff --git a/src/main/java/com/broll/networklib/client/LobbyGameClient.java b/src/main/java/com/broll/networklib/client/LobbyGameClient.java index c28e6af..6610fab 100644 --- a/src/main/java/com/broll/networklib/client/LobbyGameClient.java +++ b/src/main/java/com/broll/networklib/client/LobbyGameClient.java @@ -6,26 +6,22 @@ import com.broll.networklib.client.impl.GameLobby; import com.broll.networklib.client.impl.LobbyConnectionSite; import com.broll.networklib.client.tasks.AbstractClientTask; -import com.broll.networklib.client.tasks.DiscoveredLobbies; -import com.broll.networklib.client.tasks.LobbyDiscoveryResult; -import com.broll.networklib.client.tasks.LobbyListResult; +import com.broll.networklib.client.tasks.ServerDiscoveryResult; +import com.broll.networklib.client.tasks.ServerResult; import com.broll.networklib.client.tasks.impl.CreateLobbyTask; import com.broll.networklib.client.tasks.impl.JoinLobbyTask; import com.broll.networklib.client.tasks.impl.LobbyDiscoveryTask; import com.broll.networklib.client.tasks.impl.LobbyListTask; import com.broll.networklib.client.tasks.impl.ReconnectTask; import com.broll.networklib.network.IRegisterNetwork; -import com.broll.networklib.network.NetworkException; import com.broll.networklib.site.SiteReceiver; import com.esotericsoftware.kryo.Kryo; import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.util.Arrays; -import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; public class LobbyGameClient implements NetworkRegister { @@ -127,15 +123,15 @@ public CompletableFuture reconnectCheck(String ip) { return updateLobby(runTask(new ReconnectTask(ip, clientAuthenticationKey))); } - public CompletableFuture discoverLobbies() { + public CompletableFuture discoverLobbies() { return runTask(new LobbyDiscoveryTask(client, clientAuthenticationKey, version)); } - public CompletableFuture listLobbies(String ip) { + public CompletableFuture listLobbies(String ip) { return runTask(new LobbyListTask(ip, clientAuthenticationKey, version)); } - public CompletableFuture listLobbies() { + public CompletableFuture listLobbies() { return runTask(new LobbyListTask(clientAuthenticationKey, version)); } @@ -144,7 +140,7 @@ public CompletableFuture joinLobby(GameLobby lobby, String playerName } public CompletableFuture createLobby(String playerName, Object lobbySettings) { - return updateLobby(runTask(new CreateLobbyTask(playerName, lobbySettings, clientAuthenticationKey))); + return updateLobby(runTask(new CreateLobbyTask(playerName, lobbySettings, clientAuthenticationKey, version))); } public void clearClientAuthenticationKey() { diff --git a/src/main/java/com/broll/networklib/client/impl/LobbyLookupSite.java b/src/main/java/com/broll/networklib/client/impl/LobbyLookupSite.java index 3f716bc..d23654f 100644 --- a/src/main/java/com/broll/networklib/client/impl/LobbyLookupSite.java +++ b/src/main/java/com/broll/networklib/client/impl/LobbyLookupSite.java @@ -3,8 +3,8 @@ import com.broll.networklib.PackageReceiver; import com.broll.networklib.client.auth.ClientAuthenticationKey; import com.broll.networklib.client.tasks.AbstractTaskSite; -import com.broll.networklib.client.tasks.DiscoveredLobbies; -import com.broll.networklib.client.tasks.LobbyListResult; +import com.broll.networklib.client.tasks.ServerInformation; +import com.broll.networklib.client.tasks.ServerResult; import com.broll.networklib.network.nt.NT_ListLobbies; import com.broll.networklib.network.nt.NT_LobbyNoJoin; import com.broll.networklib.network.nt.NT_LobbyReconnected; @@ -17,7 +17,7 @@ import java.util.List; import java.util.stream.Collectors; -public class LobbyLookupSite extends AbstractTaskSite { +public class LobbyLookupSite extends AbstractTaskSite { private final static Logger Log = LoggerFactory.getLogger(LobbyLookupSite.class); @@ -38,12 +38,12 @@ public void receive(NT_ServerInformation info) { LobbyChange.updateLobbyInfo(lobby, lobbyInfo); return lobby; }).collect(Collectors.toList()); - complete(new LobbyListResult(new DiscoveredLobbies(info.serverName, ip, lobbies))); + complete(new ServerResult(new ServerInformation(info.serverName, ip, lobbies))); } @PackageReceiver public void reconnected(NT_LobbyReconnected reconnected) { - complete(new LobbyListResult(LobbyChange.reconnectedLobby(getClient(), reconnected))); + complete(new ServerResult(LobbyChange.reconnectedLobby(getClient(), reconnected))); } @PackageReceiver diff --git a/src/main/java/com/broll/networklib/client/tasks/LobbyDiscoveryResult.java b/src/main/java/com/broll/networklib/client/tasks/LobbyDiscoveryResult.java deleted file mode 100644 index 7ab9a7e..0000000 --- a/src/main/java/com/broll/networklib/client/tasks/LobbyDiscoveryResult.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.broll.networklib.client.tasks; - -import com.broll.networklib.client.impl.GameLobby; - -import java.util.List; - -public class LobbyDiscoveryResult { - - private List lobbies; - private GameLobby reconnected; - - public LobbyDiscoveryResult(List lobbies){ - this.lobbies = lobbies; - } - - public LobbyDiscoveryResult(GameLobby reconnected){ - this.reconnected = reconnected; - } - - public List getLobbies() { - return lobbies; - } - - public GameLobby getReconnectedLobby() { - return reconnected; - } -} diff --git a/src/main/java/com/broll/networklib/client/tasks/ServerDiscoveryResult.java b/src/main/java/com/broll/networklib/client/tasks/ServerDiscoveryResult.java new file mode 100644 index 0000000..8378bad --- /dev/null +++ b/src/main/java/com/broll/networklib/client/tasks/ServerDiscoveryResult.java @@ -0,0 +1,27 @@ +package com.broll.networklib.client.tasks; + +import com.broll.networklib.client.impl.GameLobby; + +import java.util.List; + +public class ServerDiscoveryResult { + + private List servers; + private GameLobby reconnected; + + public ServerDiscoveryResult(List servers){ + this.servers = servers; + } + + public ServerDiscoveryResult(GameLobby reconnected){ + this.reconnected = reconnected; + } + + public List getServers() { + return servers; + } + + public GameLobby getReconnectedLobby() { + return reconnected; + } +} diff --git a/src/main/java/com/broll/networklib/client/tasks/DiscoveredLobbies.java b/src/main/java/com/broll/networklib/client/tasks/ServerInformation.java similarity index 85% rename from src/main/java/com/broll/networklib/client/tasks/DiscoveredLobbies.java rename to src/main/java/com/broll/networklib/client/tasks/ServerInformation.java index 3fd8a72..bcb31cb 100644 --- a/src/main/java/com/broll/networklib/client/tasks/DiscoveredLobbies.java +++ b/src/main/java/com/broll/networklib/client/tasks/ServerInformation.java @@ -4,13 +4,13 @@ import java.util.List; -public class DiscoveredLobbies { +public class ServerInformation { private String serverName; private String serverIp; private List lobbies; - public DiscoveredLobbies(String serverName, String serverIp, List lobbies) { + public ServerInformation(String serverName, String serverIp, List lobbies) { this.serverName = serverName; this.serverIp = serverIp; this.lobbies = lobbies; diff --git a/src/main/java/com/broll/networklib/client/tasks/LobbyListResult.java b/src/main/java/com/broll/networklib/client/tasks/ServerResult.java similarity index 50% rename from src/main/java/com/broll/networklib/client/tasks/LobbyListResult.java rename to src/main/java/com/broll/networklib/client/tasks/ServerResult.java index bd1a0ff..4d68db9 100644 --- a/src/main/java/com/broll/networklib/client/tasks/LobbyListResult.java +++ b/src/main/java/com/broll/networklib/client/tasks/ServerResult.java @@ -2,22 +2,22 @@ import com.broll.networklib.client.impl.GameLobby; -public class LobbyListResult { +public class ServerResult { - private DiscoveredLobbies lobbies; + private ServerInformation server; private GameLobby reconnected; - public LobbyListResult(DiscoveredLobbies lobbies){ - this.lobbies = lobbies; + public ServerResult(ServerInformation server){ + this.server = server; } - public LobbyListResult(GameLobby reconnected){ + public ServerResult(GameLobby reconnected){ this.reconnected = reconnected; } - public DiscoveredLobbies getLobbies() { - return lobbies; + public ServerInformation getServer() { + return server; } public GameLobby getReconnectedLobby() { diff --git a/src/main/java/com/broll/networklib/client/tasks/impl/CreateLobbyTask.java b/src/main/java/com/broll/networklib/client/tasks/impl/CreateLobbyTask.java index f884751..a1a137a 100644 --- a/src/main/java/com/broll/networklib/client/tasks/impl/CreateLobbyTask.java +++ b/src/main/java/com/broll/networklib/client/tasks/impl/CreateLobbyTask.java @@ -16,11 +16,14 @@ public class CreateLobbyTask extends AbstractClientTask { private String playerName; private Object settings; + private String version; - public CreateLobbyTask(String playerName, Object lobbySettings, ClientAuthenticationKey authKey) { + + public CreateLobbyTask(String playerName, Object lobbySettings, ClientAuthenticationKey authKey, String version) { super(authKey); this.playerName = playerName; this.settings = lobbySettings; + this.version = version; } @Override @@ -39,6 +42,7 @@ public void create() { create.lobbyName = playerName + "'s Lobby"; create.authenticationKey = authKey.getSecret(); create.settings = settings; + create.version = version; client.sendTCP(create); } diff --git a/src/main/java/com/broll/networklib/client/tasks/impl/LobbyDiscoveryTask.java b/src/main/java/com/broll/networklib/client/tasks/impl/LobbyDiscoveryTask.java index 7ee8634..134cdd1 100644 --- a/src/main/java/com/broll/networklib/client/tasks/impl/LobbyDiscoveryTask.java +++ b/src/main/java/com/broll/networklib/client/tasks/impl/LobbyDiscoveryTask.java @@ -4,14 +4,14 @@ import com.broll.networklib.client.auth.ClientAuthenticationKey; import com.broll.networklib.client.impl.LobbyLookupSite; import com.broll.networklib.client.tasks.AbstractClientTask; -import com.broll.networklib.client.tasks.DiscoveredLobbies; -import com.broll.networklib.client.tasks.LobbyDiscoveryResult; -import com.broll.networklib.client.tasks.LobbyListResult; +import com.broll.networklib.client.tasks.ServerInformation; +import com.broll.networklib.client.tasks.ServerDiscoveryResult; +import com.broll.networklib.client.tasks.ServerResult; import java.util.ArrayList; import java.util.List; -public class LobbyDiscoveryTask extends AbstractClientTask { +public class LobbyDiscoveryTask extends AbstractClientTask { private GameClient basicClient; private String version; @@ -23,7 +23,7 @@ public LobbyDiscoveryTask(GameClient basicClient, ClientAuthenticationKey key, S @Override protected void run() { - List discoveredLobbies = new ArrayList<>(); + List discoveredLobbies = new ArrayList<>(); List servers = basicClient.discoverServers(); //also check localhost for servers servers.add("localhost"); @@ -32,12 +32,12 @@ protected void run() { LobbyLookupSite site = new LobbyLookupSite(); runOnTempClient(server, site); site.lookup(authKey, version); - LobbyListResult result = waitFor(site.getFuture()); + ServerResult result = waitFor(site.getFuture()); if(result.getReconnectedLobby()!=null){ - complete(new LobbyDiscoveryResult(result.getReconnectedLobby())); + complete(new ServerDiscoveryResult(result.getReconnectedLobby())); } - discoveredLobbies.add(result.getLobbies()); + discoveredLobbies.add(result.getServer()); }); - complete(new LobbyDiscoveryResult(discoveredLobbies)); + complete(new ServerDiscoveryResult(discoveredLobbies)); } } diff --git a/src/main/java/com/broll/networklib/client/tasks/impl/LobbyListTask.java b/src/main/java/com/broll/networklib/client/tasks/impl/LobbyListTask.java index a4a4733..28c3df2 100644 --- a/src/main/java/com/broll/networklib/client/tasks/impl/LobbyListTask.java +++ b/src/main/java/com/broll/networklib/client/tasks/impl/LobbyListTask.java @@ -1,24 +1,14 @@ package com.broll.networklib.client.tasks.impl; -import com.broll.networklib.PackageReceiver; import com.broll.networklib.client.auth.ClientAuthenticationKey; -import com.broll.networklib.client.impl.GameLobby; -import com.broll.networklib.client.impl.LobbyChange; import com.broll.networklib.client.impl.LobbyLookupSite; import com.broll.networklib.client.tasks.AbstractClientTask; -import com.broll.networklib.client.tasks.AbstractTaskSite; -import com.broll.networklib.client.tasks.DiscoveredLobbies; -import com.broll.networklib.client.tasks.LobbyListResult; -import com.broll.networklib.network.nt.NT_ServerInformation; +import com.broll.networklib.client.tasks.ServerResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -public class LobbyListTask extends AbstractClientTask { +public class LobbyListTask extends AbstractClientTask { private final static Logger Log = LoggerFactory.getLogger(LobbyLookupSite.class); private String ip; diff --git a/src/main/java/com/broll/networklib/network/nt/NT_LobbyCreate.java b/src/main/java/com/broll/networklib/network/nt/NT_LobbyCreate.java index ea1b9bf..a419271 100644 --- a/src/main/java/com/broll/networklib/network/nt/NT_LobbyCreate.java +++ b/src/main/java/com/broll/networklib/network/nt/NT_LobbyCreate.java @@ -5,5 +5,6 @@ public class NT_LobbyCreate { public String authenticationKey; public Object settings; public String lobbyName; + public String version; } diff --git a/src/main/java/com/broll/networklib/server/ServerSitesHandler.java b/src/main/java/com/broll/networklib/server/ServerSitesHandler.java index 4cd1298..a5dfbf4 100644 --- a/src/main/java/com/broll/networklib/server/ServerSitesHandler.java +++ b/src/main/java/com/broll/networklib/server/ServerSitesHandler.java @@ -4,6 +4,10 @@ public class ServerSitesHandler extends MultiSitesHandler { + public ServerSitesHandler(){ + super(); + } + public final ServerSite accessSite(NetworkConnection connectionContext, Class siteClass) { ServerSite site = getSiteInstances(connectionContext).get(siteClass); if (site == null) { diff --git a/src/main/java/com/broll/networklib/server/impl/ConnectionSite.java b/src/main/java/com/broll/networklib/server/impl/ConnectionSite.java index 6609af9..1599e12 100644 --- a/src/main/java/com/broll/networklib/server/impl/ConnectionSite.java +++ b/src/main/java/com/broll/networklib/server/impl/ConnectionSite.java @@ -44,7 +44,7 @@ public void setVersion(String version) { this.version = version; } - @ConnectionRestriction(RestrictionType.NOT_IN_LOBBY) + @ConnectionRestriction(RestrictionType.NONE) @PackageReceiver public void listLobbies(NT_ListLobbies list) { if (!checkJoiningClientVersion(list.version)) { @@ -126,6 +126,9 @@ public void switchLobby(NT_LobbyJoin join) { @ConnectionRestriction(RestrictionType.NOT_IN_LOBBY) @PackageReceiver public void createLobby(NT_LobbyCreate create) { + if (!checkJoiningClientVersion(create.version)) { + return; + } boolean reconnected = initPlayerConnection(create.playerName, create.authenticationKey); ServerLobby lobby = lobbyHandler.getLobbyCreationRequestHandler().createNewLobby(getPlayer(), create.lobbyName, create.settings); if (lobby != null) { diff --git a/src/main/java/com/broll/networklib/site/MultiSitesHandler.java b/src/main/java/com/broll/networklib/site/MultiSitesHandler.java index 75afe5f..4402da5 100644 --- a/src/main/java/com/broll/networklib/site/MultiSitesHandler.java +++ b/src/main/java/com/broll/networklib/site/MultiSitesHandler.java @@ -10,6 +10,7 @@ import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryonet.FrameworkMessage; +import org.objenesis.strategy.StdInstantiatorStrategy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,6 +37,10 @@ public class MultiSitesHandler extends AbstractSitesHa private final Kryo kryo = new Kryo(); + protected MultiSitesHandler(){ + kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()); + } + private synchronized O clone(O o) { return kryo.copyShallow(o); } diff --git a/src/test/java/com/broll/networklib/examples/lobby/client/LobbyClientApplication.java b/src/test/java/com/broll/networklib/examples/lobby/client/LobbyClientApplication.java index 4f354ce..37fc240 100644 --- a/src/test/java/com/broll/networklib/examples/lobby/client/LobbyClientApplication.java +++ b/src/test/java/com/broll/networklib/examples/lobby/client/LobbyClientApplication.java @@ -2,8 +2,6 @@ import com.broll.networklib.client.LobbyGameClient; import com.broll.networklib.client.impl.GameLobby; -import com.broll.networklib.client.impl.ILobbyUpdateListener; -import com.broll.networklib.client.impl.LobbyPlayer; import com.broll.networklib.examples.lobby.LobbyNetworkRegistry; public class LobbyClientApplication { @@ -11,9 +9,9 @@ public class LobbyClientApplication { public static void main(String[] args) { String playerName = "Peter"; LobbyGameClient client = new LobbyGameClient(new LobbyNetworkRegistry()); - client.listLobbies("localhost").thenCompose(lobbies -> { - System.out.println("# of listed lobbies: " + lobbies.getLobbies().size()); - GameLobby lobby = lobbies.getLobbies().get(0); + client.listLobbies("localhost").thenCompose(result -> { + System.out.println("# of listed lobbies: " + result.getServer().getLobbies().size()); + GameLobby lobby = result.getServer().getLobbies().get(0); return client.joinLobby(lobby, playerName); }).thenAccept(joinedLobby -> { System.out.println("joined lobby " + joinedLobby.getName()); diff --git a/src/test/java/com/broll/networklib/test/NetworkTest.java b/src/test/java/com/broll/networklib/test/NetworkTest.java index ad987b8..37bb599 100644 --- a/src/test/java/com/broll/networklib/test/NetworkTest.java +++ b/src/test/java/com/broll/networklib/test/NetworkTest.java @@ -133,7 +133,7 @@ protected T waitFor(CompletableFuture future) { public void joinLobby(LobbyGameClient client, ServerLobby serverLobby) { String name = clients.get(client).playerName; - Optional lobbyOptional = waitFor(client.listLobbies()).getLobbies().stream().filter(lobby -> lobby.getLobbyId() == serverLobby.getId()).findFirst(); + Optional lobbyOptional = waitFor(client.listLobbies()).getServer().getLobbies().stream().filter(lobby -> lobby.getLobbyId() == serverLobby.getId()).findFirst(); if(lobbyOptional.isPresent()){ GameLobby lobby = lobbyOptional.get(); Log.info("Client " + name + " discovered lobby " + lobby.getName()); diff --git a/src/test/java/com/broll/networklib/test/impl/LobbyConnectionTests.java b/src/test/java/com/broll/networklib/test/impl/LobbyConnectionTests.java index 153f113..c70158b 100644 --- a/src/test/java/com/broll/networklib/test/impl/LobbyConnectionTests.java +++ b/src/test/java/com/broll/networklib/test/impl/LobbyConnectionTests.java @@ -136,7 +136,7 @@ public void versionMismatch() { LobbyGameClient client = testClient("Tester"); client.setVersion("different"); //client should not be able to join - expectFailure(() -> joinLobby(client, lobby), "java.util.concurrent.ExecutionException: com.broll.networklib.network.NetworkException: java.util.concurrent.ExecutionException: java.lang.Exception: Could not join lobby: Version mismatch with server: null"); + expectFailure(() -> joinLobby(client, lobby), "java.util.concurrent.ExecutionException: com.broll.networklib.network.NetworkException: java.util.concurrent.ExecutionException: java.lang.Exception: Could not list lobbies: Version mismatch with server: null"); } @Test public void reconnect() throws ExecutionException, InterruptedException {