Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rolleander committed May 5, 2024
1 parent 50a15af commit 7080aea
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 81 deletions.
16 changes: 6 additions & 10 deletions src/main/java/com/broll/networklib/client/LobbyGameClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -127,15 +123,15 @@ public CompletableFuture<GameLobby> reconnectCheck(String ip) {
return updateLobby(runTask(new ReconnectTask(ip, clientAuthenticationKey)));
}

public CompletableFuture<LobbyDiscoveryResult> discoverLobbies() {
public CompletableFuture<ServerDiscoveryResult> discoverLobbies() {
return runTask(new LobbyDiscoveryTask(client, clientAuthenticationKey, version));
}

public CompletableFuture<LobbyListResult> listLobbies(String ip) {
public CompletableFuture<ServerResult> listLobbies(String ip) {
return runTask(new LobbyListTask(ip, clientAuthenticationKey, version));
}

public CompletableFuture<LobbyListResult> listLobbies() {
public CompletableFuture<ServerResult> listLobbies() {
return runTask(new LobbyListTask(clientAuthenticationKey, version));
}

Expand All @@ -144,7 +140,7 @@ public CompletableFuture<GameLobby> joinLobby(GameLobby lobby, String playerName
}

public CompletableFuture<GameLobby> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,7 +17,7 @@
import java.util.List;
import java.util.stream.Collectors;

public class LobbyLookupSite extends AbstractTaskSite<LobbyListResult> {
public class LobbyLookupSite extends AbstractTaskSite<ServerResult> {

private final static Logger Log = LoggerFactory.getLogger(LobbyLookupSite.class);

Expand All @@ -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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<ServerInformation> servers;
private GameLobby reconnected;

public ServerDiscoveryResult(List<ServerInformation> servers){
this.servers = servers;
}

public ServerDiscoveryResult(GameLobby reconnected){
this.reconnected = reconnected;
}

public List<ServerInformation> getServers() {
return servers;
}

public GameLobby getReconnectedLobby() {
return reconnected;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import java.util.List;

public class DiscoveredLobbies {
public class ServerInformation {

private String serverName;
private String serverIp;
private List<GameLobby> lobbies;

public DiscoveredLobbies(String serverName, String serverIp, List<GameLobby> lobbies) {
public ServerInformation(String serverName, String serverIp, List<GameLobby> lobbies) {
this.serverName = serverName;
this.serverIp = serverIp;
this.lobbies = lobbies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ public class CreateLobbyTask extends AbstractClientTask<GameLobby> {
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
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LobbyDiscoveryResult> {
public class LobbyDiscoveryTask extends AbstractClientTask<ServerDiscoveryResult> {
private GameClient basicClient;
private String version;

Expand All @@ -23,7 +23,7 @@ public LobbyDiscoveryTask(GameClient basicClient, ClientAuthenticationKey key, S

@Override
protected void run() {
List<DiscoveredLobbies> discoveredLobbies = new ArrayList<>();
List<ServerInformation> discoveredLobbies = new ArrayList<>();
List<String> servers = basicClient.discoverServers();
//also check localhost for servers
servers.add("localhost");
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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<LobbyListResult> {
public class LobbyListTask extends AbstractClientTask<ServerResult> {
private final static Logger Log = LoggerFactory.getLogger(LobbyLookupSite.class);

private String ip;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public class NT_LobbyCreate {
public String authenticationKey;
public Object settings;
public String lobbyName;
public String version;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

public class ServerSitesHandler extends MultiSitesHandler<ServerSite, NetworkConnection> {

public ServerSitesHandler(){
super();
}

public final ServerSite accessSite(NetworkConnection connectionContext, Class<ServerSite> siteClass) {
ServerSite site = getSiteInstances(connectionContext).get(siteClass);
if (site == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,6 +37,10 @@ public class MultiSitesHandler<T extends NetworkSite, C> extends AbstractSitesHa

private final Kryo kryo = new Kryo();

protected MultiSitesHandler(){
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
}

private synchronized <O> O clone(O o) {
return kryo.copyShallow(o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

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 {

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());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/broll/networklib/test/NetworkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected <T> T waitFor(CompletableFuture<T> future) {

public void joinLobby(LobbyGameClient client, ServerLobby serverLobby) {
String name = clients.get(client).playerName;
Optional<GameLobby> lobbyOptional = waitFor(client.listLobbies()).getLobbies().stream().filter(lobby -> lobby.getLobbyId() == serverLobby.getId()).findFirst();
Optional<GameLobby> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7080aea

Please sign in to comment.