Cleaned up the information needed for client connection.

This commit is contained in:
Andrew Lalis 2022-08-07 18:34:22 +02:00
parent da5ab070a8
commit 89ec1ddccc
3 changed files with 27 additions and 17 deletions

View File

@ -1,6 +1,7 @@
package nl.andrewl.aos2_client;
import nl.andrewl.aos2_client.config.ClientConfig;
import nl.andrewl.aos2_client.config.ConnectConfig;
import nl.andrewl.aos2_client.control.InputHandler;
import nl.andrewl.aos2_client.model.Chat;
import nl.andrewl.aos2_client.model.ClientPlayer;
@ -26,10 +27,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Client implements Runnable {
private final String host;
private final int port;
private final String username;
private final ClientConfig config;
public final ConnectConfig connectConfig;
public final ClientConfig config;
private final CommunicationHandler communicationHandler;
private final InputHandler inputHandler;
@ -46,11 +45,9 @@ public class Client implements Runnable {
private final Chat chat;
private final Queue<Runnable> mainThreadActions;
public Client(ClientConfig config, String host, int port, String username) {
this.host = host;
this.port = port;
this.username = username;
public Client(ClientConfig config, ConnectConfig connectConfig) {
this.config = config;
this.connectConfig = connectConfig;
this.camera = new Camera();
this.players = new ConcurrentHashMap<>();
this.teams = new ConcurrentHashMap<>();
@ -81,7 +78,7 @@ public class Client implements Runnable {
@Override
public void run() {
try {
communicationHandler.establishConnection(host, port, username);
communicationHandler.establishConnection();
} catch (IOException e) {
System.err.println("Couldn't connect to the server: " + e.getMessage());
return;
@ -279,6 +276,7 @@ public class Client implements Runnable {
String host = args[0].trim();
int port = Integer.parseInt(args[1]);
String username = args[2].trim();
ConnectConfig connectCfg = new ConnectConfig(host, port, username, false);
List<Path> configPaths = Config.getCommonConfigPaths();
configPaths.add(0, Path.of("client.yaml")); // Add this first so we create client.yaml if needed.
@ -286,7 +284,7 @@ public class Client implements Runnable {
configPaths.add(Path.of(args[3].trim()));
}
ClientConfig clientConfig = Config.loadConfig(ClientConfig.class, configPaths, new ClientConfig(), "default-config.yaml");
Client client = new Client(clientConfig, host, port, username);
Client client = new Client(clientConfig, connectCfg);
client.run();
}
}

View File

@ -8,7 +8,8 @@ import nl.andrewl.aos_core.model.Team;
import nl.andrewl.aos_core.model.item.ItemStack;
import nl.andrewl.aos_core.model.world.World;
import nl.andrewl.aos_core.model.world.WorldIO;
import nl.andrewl.aos_core.net.*;
import nl.andrewl.aos_core.net.TcpReceiver;
import nl.andrewl.aos_core.net.UdpReceiver;
import nl.andrewl.aos_core.net.connect.ConnectAcceptMessage;
import nl.andrewl.aos_core.net.connect.ConnectRejectMessage;
import nl.andrewl.aos_core.net.connect.ConnectRequestMessage;
@ -42,18 +43,18 @@ public class CommunicationHandler {
this.client = client;
}
public void establishConnection(String host, int port, String username) throws IOException {
public void establishConnection() throws IOException {
if (socket != null && !socket.isClosed()) {
socket.close();
}
InetAddress address = InetAddress.getByName(host);
System.out.printf("Connecting to server at %s, port %d, with username \"%s\"...%n", address, port, username);
InetAddress address = InetAddress.getByName(client.connectConfig.host());
System.out.printf("Connecting to server at %s, port %d, with username \"%s\"...%n", address, client.connectConfig.port(), client.connectConfig.username());
socket = new Socket(address, port);
socket = new Socket(address, client.connectConfig.port());
socket.setSoTimeout(1000);
in = Net.getInputStream(socket.getInputStream());
out = Net.getOutputStream(socket.getOutputStream());
Net.write(new ConnectRequestMessage(username, true), out);
Net.write(new ConnectRequestMessage(client.connectConfig.username(), client.connectConfig.spectator()), out);
Message response = Net.read(in);
socket.setSoTimeout(0);
if (response instanceof ConnectRejectMessage rejectMessage) {
@ -61,7 +62,7 @@ public class CommunicationHandler {
}
if (response instanceof ConnectAcceptMessage acceptMessage) {
this.clientId = acceptMessage.clientId();
client.setMyPlayer(new ClientPlayer(clientId, username));
client.setMyPlayer(new ClientPlayer(clientId, client.connectConfig.username()));
receiveInitialData();
establishDatagramConnection();
new Thread(new TcpReceiver(in, client::onMessageReceived).withShutdownHook(this::shutdown)).start();

View File

@ -0,0 +1,11 @@
package nl.andrewl.aos2_client.config;
/**
* The data that's needed by the client to initially establish a connection.
*/
public record ConnectConfig(
String host,
int port,
String username,
boolean spectator
) {}