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