add connect to server message
This commit is contained in:
parent
71d9eb27a5
commit
7712419544
@ -1,14 +1,17 @@
|
||||
package application.client;
|
||||
|
||||
import application.PropertiesLoader;
|
||||
import application.client.exception.CannotConnectException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Connector {
|
||||
private static final Connector instance = new Connector();
|
||||
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
||||
|
||||
private Socket client = null;
|
||||
private boolean isConnect = false;
|
||||
|
||||
private Connector() {
|
||||
}
|
||||
@ -18,10 +21,37 @@ public class Connector {
|
||||
}
|
||||
|
||||
public void connect(String ip) throws CannotConnectException {
|
||||
this.isConnect = false;
|
||||
try {
|
||||
this.client = new Socket(ip, 7332);
|
||||
this.client = new Socket(ip, Integer.parseInt(this.propertiesLoader.get("port")));
|
||||
this.checkIfConnect();
|
||||
} catch (IOException e) {
|
||||
throw new CannotConnectException();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkIfConnect() {
|
||||
String pattern = this.propertiesLoader.get("serverStatusConnect");
|
||||
|
||||
this.isConnect = pattern.equals(this.getMessage());
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
String message = null;
|
||||
try {
|
||||
System.out.println("Trying to get input stream");
|
||||
DataInputStream inputStream = new DataInputStream(this.client.getInputStream());
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
|
||||
BufferedReader br = new BufferedReader(inputStreamReader);
|
||||
message = br.readLine();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Message received: "+ message);
|
||||
return message;
|
||||
}
|
||||
|
||||
public boolean isConnect() {
|
||||
return this.isConnect;
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,14 @@ import application.client.Connector;
|
||||
import application.client.exception.CannotConnectException;
|
||||
import javafx.event.Event;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
|
||||
public class MainPageController {
|
||||
public Button connectButton = null;
|
||||
public TextField rawFieldWithIp = null;
|
||||
public Label errorLabel = null;
|
||||
public Label messageLabel = null;
|
||||
private Connector connector = Connector.getInstance();
|
||||
|
||||
public MainPageController() {
|
||||
@ -17,13 +19,16 @@ public class MainPageController {
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void connectButton(Event e) {
|
||||
public void connectToServer(Event e) {
|
||||
this.connectButton.setText("Connecting...");
|
||||
System.out.println(rawFieldWithIp.getText());;
|
||||
|
||||
try {
|
||||
this.connector.connect(this.rawFieldWithIp.getText());
|
||||
} catch (CannotConnectException ex) {
|
||||
this.errorLabel.setText("Cannot connect");
|
||||
}
|
||||
|
||||
this.messageLabel.setText(this.connector.isConnect() ? "Connected" : "Not connect");
|
||||
this.connectButton.setText("Connect");
|
||||
}
|
||||
}
|
||||
|
@ -1 +1,2 @@
|
||||
port=7332
|
||||
port=7332
|
||||
serverStatusConnect=CONNECT
|
@ -17,8 +17,8 @@
|
||||
</font>
|
||||
</Label>
|
||||
<TextField fx:id="rawFieldWithIp" layoutX="239.0" layoutY="82.0" promptText="Insert ip" />
|
||||
<Button layoutX="290.0" layoutY="122.0" mnemonicParsing="false" onAction="#connectButton" text="Connect" />
|
||||
<Label fx:id="errorLabel" alignment="CENTER" contentDisplay="CENTER" layoutX="15.0" layoutY="14.0" prefHeight="18.0" prefWidth="608.0" textFill="RED" />
|
||||
<Button fx:id="connectButton" layoutX="290.0" layoutY="122.0" mnemonicParsing="false" onAction="#connectToServer" text="Connect" />
|
||||
<Label fx:id="messageLabel" alignment="CENTER" contentDisplay="CENTER" layoutX="15.0" layoutY="14.0" prefHeight="18.0" prefWidth="608.0" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
</children>
|
||||
|
Loading…
Reference in New Issue
Block a user