Archived
1
0
Fork 0

Add connection to server

This commit is contained in:
Ziedelth 2021-12-08 09:02:25 +01:00
parent 2ebe7a68ae
commit 00effd8b55
6 changed files with 187 additions and 16 deletions

21
pom.xml
View file

@ -34,21 +34,33 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.1</version>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>22.0.0</version>
</dependency>
</dependencies>
<build>
<defaultGoal>package</defaultGoal>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>src/test/</testSourceDirectory>
<resources>
<resource>
<targetPath>resources</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -73,6 +85,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="fr.univ.lyon1.gui.controller.ConnectGuiController">
<Label text="Connexion">
<font>
<Font name="System Bold" size="18.0"/>
</font>
</Label>
<StackPane prefHeight="10.0" prefWidth="200.0"/>
<HBox alignment="CENTER" prefHeight="50.0" prefWidth="200.0">
<Label text="Adresse :"/>
<TextField fx:id="addressTextField">
<HBox.margin>
<Insets left="10.0"/>
</HBox.margin>
</TextField>
</HBox>
<HBox alignment="CENTER" layoutX="10.0" layoutY="174.0" prefHeight="50.0" prefWidth="200.0">
<Label text="Port :"/>
<TextField fx:id="portTextField">
<HBox.margin>
<Insets left="10.0"/>
</HBox.margin>
</TextField>
</HBox>
<Button mnemonicParsing="false" text="Se connecter" onAction="#connect" fx:id="connectButton">
<VBox.margin>
<Insets top="20.0"/>
</VBox.margin>
</Button>
</VBox>

View file

@ -12,15 +12,13 @@ import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
public class ClientPanel extends Parent {
private TextArea textToSend = new TextArea();
private ScrollPane scrollReceivedText = new ScrollPane();
private TextFlow receivedText = new TextFlow();
private Button sendBtn = new Button();
private Button clearBtn = new Button();
private final TextArea textToSend = new TextArea();
private final TextFlow receivedText = new TextFlow();
private final MainGui gui;
ClientPanel(MainGui gui) {
this.gui = gui;
ScrollPane scrollReceivedText = new ScrollPane();
scrollReceivedText.setLayoutX(20);
scrollReceivedText.setLayoutY(20);
scrollReceivedText.setPrefWidth(400);
@ -29,13 +27,14 @@ public class ClientPanel extends Parent {
scrollReceivedText.vvalueProperty().bind(receivedText.heightProperty());
this.getChildren().add(scrollReceivedText);
Button sendBtn = new Button();
sendBtn.setPrefWidth(80);
int btnMargin = 20;
textToSend.setLayoutX(scrollReceivedText.getLayoutX());
textToSend.setLayoutY(scrollReceivedText.getLayoutY() + scrollReceivedText.getPrefHeight() + 20);
textToSend.setPrefWidth(400-sendBtn.getPrefWidth()-btnMargin);
textToSend.setPrefWidth(400- sendBtn.getPrefWidth()-btnMargin);
textToSend.setPrefHeight(100);
this.getChildren().add(textToSend);
textToSend.setOnKeyPressed(this::textToSendKeyPressed);
@ -49,6 +48,7 @@ public class ClientPanel extends Parent {
this.getChildren().add(sendBtn);
sendBtn.setOnAction(this::sendBtnAction);
Button clearBtn = new Button();
clearBtn.setText("Clear");
clearBtn.setLayoutX(sendBtn.getLayoutX());
clearBtn.setPrefWidth(sendBtn.getPrefWidth());

View file

@ -1,22 +1,47 @@
package fr.univ.lyon1.gui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class MainGui extends Application {
private ClientPanel clientPanel;
@Nullable
private ClientGUI client;
@Override
public void start(Stage stage) throws Exception {
List<String> parameters = this.getParameters().getUnnamed();
public void start(Stage stage) {
try {
@NotNull
final List<String> parameters = this.getParameters().getUnnamed();
if (parameters.size() != 2) {
// TODO: Error
System.out.println("Error");
FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("connect_gui.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
// stage.getIcons().add(new Image(Objects.requireNonNull(HelloApplication.class.getResourceAsStream("icon.jpg"))));
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
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
}
} catch (Exception exception) {
System.out.println(exception.getMessage());
exception.printStackTrace();
return;
}
stage.setTitle("Chat client");
stage.setWidth(440);
@ -28,16 +53,21 @@ public class MainGui extends Application {
stage.setScene(scene);
stage.show();
if (client != null) {
client.run();
}
}
public static void main(String[] args) {
Application.launch(MainGui.class, args);
}
public void sendMessage(String msg) {
if (client != null) {
client.sendMessage(msg);
}
}
public void receiveMessage(String msg) {
clientPanel.addMessage(msg);

View file

@ -0,0 +1,82 @@
package fr.univ.lyon1.gui.controller;
import javafx.event.ActionEvent;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.net.Socket;
public class ConnectGuiController {
public TextField addressTextField;
public TextField portTextField;
public Button connectButton;
/**
* Called by submit button in resources/connect_gui.fxml
* @param actionEvent Action Event
*/
public void connect(ActionEvent actionEvent) {
this.connectButton.setDisable(true);
@NotNull
final String address = this.addressTextField.getText();
@NotNull
final String port = this.portTextField.getText();
System.out.println("Checking if port is valid...");
// Check if the port is a number and if it's an available port
if (this.isNumber(port) && this.isAvailablePort(Integer.parseInt(port))) {
final int iPort = Integer.parseInt(port);
System.out.println("Port valid, next step...");
System.out.println("Checking if connection is available...");
// Check if the connection is available
if (this.isAccessible(address, iPort)) {
} else {
System.out.println("Connection not available");
this.showDialog("Erreur de connexion", "Impossible de se connecter au serveur, veuillez vérifier les informations saisies");
this.connectButton.setDisable(false);
}
} else {
this.showDialog("Erreur", "Veuillez saisir un numéro de port valide");
this.connectButton.setDisable(false);
}
}
private void showDialog(String title, String description) {
@NotNull
final Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle(title);
alert.setContentText(description);
alert.show();
}
private boolean isNumber(String text) {
try {
Integer.parseInt(text);
return true;
} catch (NumberFormatException ignored) {
return false;
}
}
private boolean isAvailablePort(int port) {
return !(port < 0 || port > 0xFFFF);
}
private boolean isAccessible(String address, int port) {
try {
@NotNull
final Socket socket = new Socket(address, port);
socket.setSoTimeout(5 * 1000);
return true;
} catch (IOException exception) {
return false;
}
}
}

View file

@ -1,9 +1,13 @@
module fr.univ.lyon1.gui {
module fr.univ.lyon {
requires javafx.controls;
requires javafx.fxml;
requires org.kordamp.bootstrapfx.core;
requires org.jetbrains.annotations;
opens fr.univ.lyon1.gui to javafx.fxml;
exports fr.univ.lyon1.common;
exports fr.univ.lyon1.gui;
exports fr.univ.lyon1.gui.controller;
}