Archived
1
0
Fork 0

Add load server configuration on application start

This commit is contained in:
Ziedelth 2021-12-08 09:59:07 +01:00
parent bb100489dc
commit 8e4d54b70f
2 changed files with 29 additions and 30 deletions

View file

@ -15,6 +15,12 @@ public record ServerConfiguration(@NotNull String address, int port) {
}
public static ServerConfiguration load() throws IOException, NumberFormatException {
// Check if file non exists, return error to launch server configuration
if (!file.exists()) {
System.out.println("File not exists");
throw new IOException("File not exists");
}
@NotNull final Properties properties = new Properties();
properties.load(new FileReader(file));
return new ServerConfiguration(properties.getProperty("address"), Integer.parseInt(properties.getProperty("port")));

View file

@ -1,5 +1,6 @@
package fr.univ.lyon1.gui;
import fr.univ.lyon1.common.ServerConfiguration;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
@ -9,7 +10,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.List;
public class MainGui extends Application {
private ClientPanel clientPanel;
@ -20,38 +20,31 @@ public class MainGui extends Application {
public void start(Stage stage) {
try {
@NotNull
final List<String> parameters = this.getParameters().getUnnamed();
final ServerConfiguration serverConfiguration = ServerConfiguration.load();
this.client = new ClientGUI(this, serverConfiguration.getAddress(), serverConfiguration.getPort());
if (parameters.size() != 2) {
// TODO: Error
System.out.println("Error");
stage.setTitle("Chat client");
stage.setWidth(440);
launchServerConfiguration(stage);
return;
} else {
client = new ClientGUI(this, parameters.get(0), Integer.parseInt(parameters.get(1)));
//ToDo: error management especially for bad server IP/port
//ToDo: Server IP/port enter by user on the GUI
this.clientPanel = new ClientPanel(this);
Group root = new Group();
root.getChildren().add(this.clientPanel);
Scene scene = new Scene(root, 600, 500);
stage.setScene(scene);
stage.show();
if (this.client != null) {
this.client.run();
}
} catch (IOException | InterruptedException e) {
// Launch server configuration
try {
launchServerConfiguration(stage);
} catch (IOException ex) {
// Can not launch server configuration, stop application
System.exit(1);
}
} catch (Exception exception) {
System.out.println(exception.getMessage());
exception.printStackTrace();
return;
}
stage.setTitle("Chat client");
stage.setWidth(440);
clientPanel = new ClientPanel(this);
Group root = new Group();
root.getChildren().add(clientPanel);
Scene scene = new Scene(root, 600, 500);
stage.setScene(scene);
stage.show();
if (client != null) {
client.run();
}
}