Cleaned up the information needed for client connection.
This commit is contained in:
parent
da5ab070a8
commit
89ec1ddccc
|
@ -1,6 +1,7 @@
|
||||||
package nl.andrewl.aos2_client;
|
package nl.andrewl.aos2_client;
|
||||||
|
|
||||||
import nl.andrewl.aos2_client.config.ClientConfig;
|
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.control.InputHandler;
|
||||||
import nl.andrewl.aos2_client.model.Chat;
|
import nl.andrewl.aos2_client.model.Chat;
|
||||||
import nl.andrewl.aos2_client.model.ClientPlayer;
|
import nl.andrewl.aos2_client.model.ClientPlayer;
|
||||||
|
@ -26,10 +27,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
public class Client implements Runnable {
|
public class Client implements Runnable {
|
||||||
private final String host;
|
public final ConnectConfig connectConfig;
|
||||||
private final int port;
|
public final ClientConfig config;
|
||||||
private final String username;
|
|
||||||
private final ClientConfig config;
|
|
||||||
|
|
||||||
private final CommunicationHandler communicationHandler;
|
private final CommunicationHandler communicationHandler;
|
||||||
private final InputHandler inputHandler;
|
private final InputHandler inputHandler;
|
||||||
|
@ -46,11 +45,9 @@ public class Client implements Runnable {
|
||||||
private final Chat chat;
|
private final Chat chat;
|
||||||
private final Queue<Runnable> mainThreadActions;
|
private final Queue<Runnable> mainThreadActions;
|
||||||
|
|
||||||
public Client(ClientConfig config, String host, int port, String username) {
|
public Client(ClientConfig config, ConnectConfig connectConfig) {
|
||||||
this.host = host;
|
|
||||||
this.port = port;
|
|
||||||
this.username = username;
|
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
this.connectConfig = connectConfig;
|
||||||
this.camera = new Camera();
|
this.camera = new Camera();
|
||||||
this.players = new ConcurrentHashMap<>();
|
this.players = new ConcurrentHashMap<>();
|
||||||
this.teams = new ConcurrentHashMap<>();
|
this.teams = new ConcurrentHashMap<>();
|
||||||
|
@ -81,7 +78,7 @@ public class Client implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
communicationHandler.establishConnection(host, port, username);
|
communicationHandler.establishConnection();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Couldn't connect to the server: " + e.getMessage());
|
System.err.println("Couldn't connect to the server: " + e.getMessage());
|
||||||
return;
|
return;
|
||||||
|
@ -279,6 +276,7 @@ public class Client implements Runnable {
|
||||||
String host = args[0].trim();
|
String host = args[0].trim();
|
||||||
int port = Integer.parseInt(args[1]);
|
int port = Integer.parseInt(args[1]);
|
||||||
String username = args[2].trim();
|
String username = args[2].trim();
|
||||||
|
ConnectConfig connectCfg = new ConnectConfig(host, port, username, false);
|
||||||
|
|
||||||
List<Path> configPaths = Config.getCommonConfigPaths();
|
List<Path> configPaths = Config.getCommonConfigPaths();
|
||||||
configPaths.add(0, Path.of("client.yaml")); // Add this first so we create client.yaml if needed.
|
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()));
|
configPaths.add(Path.of(args[3].trim()));
|
||||||
}
|
}
|
||||||
ClientConfig clientConfig = Config.loadConfig(ClientConfig.class, configPaths, new ClientConfig(), "default-config.yaml");
|
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();
|
client.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.item.ItemStack;
|
||||||
import nl.andrewl.aos_core.model.world.World;
|
import nl.andrewl.aos_core.model.world.World;
|
||||||
import nl.andrewl.aos_core.model.world.WorldIO;
|
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.ConnectAcceptMessage;
|
||||||
import nl.andrewl.aos_core.net.connect.ConnectRejectMessage;
|
import nl.andrewl.aos_core.net.connect.ConnectRejectMessage;
|
||||||
import nl.andrewl.aos_core.net.connect.ConnectRequestMessage;
|
import nl.andrewl.aos_core.net.connect.ConnectRequestMessage;
|
||||||
|
@ -42,18 +43,18 @@ public class CommunicationHandler {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void establishConnection(String host, int port, String username) throws IOException {
|
public void establishConnection() throws IOException {
|
||||||
if (socket != null && !socket.isClosed()) {
|
if (socket != null && !socket.isClosed()) {
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
InetAddress address = InetAddress.getByName(host);
|
InetAddress address = InetAddress.getByName(client.connectConfig.host());
|
||||||
System.out.printf("Connecting to server at %s, port %d, with username \"%s\"...%n", address, port, username);
|
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);
|
socket.setSoTimeout(1000);
|
||||||
in = Net.getInputStream(socket.getInputStream());
|
in = Net.getInputStream(socket.getInputStream());
|
||||||
out = Net.getOutputStream(socket.getOutputStream());
|
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);
|
Message response = Net.read(in);
|
||||||
socket.setSoTimeout(0);
|
socket.setSoTimeout(0);
|
||||||
if (response instanceof ConnectRejectMessage rejectMessage) {
|
if (response instanceof ConnectRejectMessage rejectMessage) {
|
||||||
|
@ -61,7 +62,7 @@ public class CommunicationHandler {
|
||||||
}
|
}
|
||||||
if (response instanceof ConnectAcceptMessage acceptMessage) {
|
if (response instanceof ConnectAcceptMessage acceptMessage) {
|
||||||
this.clientId = acceptMessage.clientId();
|
this.clientId = acceptMessage.clientId();
|
||||||
client.setMyPlayer(new ClientPlayer(clientId, username));
|
client.setMyPlayer(new ClientPlayer(clientId, client.connectConfig.username()));
|
||||||
receiveInitialData();
|
receiveInitialData();
|
||||||
establishDatagramConnection();
|
establishDatagramConnection();
|
||||||
new Thread(new TcpReceiver(in, client::onMessageReceived).withShutdownHook(this::shutdown)).start();
|
new Thread(new TcpReceiver(in, client::onMessageReceived).withShutdownHook(this::shutdown)).start();
|
||||||
|
|
|
@ -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
|
||||||
|
) {}
|
Loading…
Reference in New Issue