Refactor connecting to server
This commit is contained in:
parent
d52c1fd55d
commit
04c76eb49f
@ -14,7 +14,7 @@ public class Main extends Application {
|
||||
public void start(Stage primaryStage) throws Exception{
|
||||
ResourceLoader loader = ResourceLoader.getInstance();
|
||||
|
||||
Parent root = FXMLLoader.load(loader.getResource("mainPage.fxml"));
|
||||
Parent root = FXMLLoader.load(loader.getResource("main_page.fxml"));
|
||||
primaryStage.setTitle("FTP SAR");
|
||||
primaryStage.setScene(new Scene(root));
|
||||
primaryStage.show();
|
||||
|
@ -1,72 +0,0 @@
|
||||
package application.client;
|
||||
|
||||
import application.PropertiesLoader;
|
||||
import application.client.exception.CannotConnectException;
|
||||
|
||||
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() {
|
||||
}
|
||||
|
||||
public static Connector getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void connect(String ip) throws CannotConnectException {
|
||||
this.isConnect = false;
|
||||
try {
|
||||
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 void getFileList() {
|
||||
try {
|
||||
DataOutputStream outputStream = new DataOutputStream(this.client.getOutputStream());
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
|
||||
|
||||
bufferedWriter.write("GET_ALL_FILES");
|
||||
bufferedWriter.flush();
|
||||
|
||||
this.getMessage();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnect() {
|
||||
return this.isConnect;
|
||||
}
|
||||
}
|
43
src/application/client/server/Server.java
Normal file
43
src/application/client/server/Server.java
Normal file
@ -0,0 +1,43 @@
|
||||
package application.client.server;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Server {
|
||||
private String ip;
|
||||
private int port;
|
||||
private ArrayList<String> fileList = new ArrayList<String>();
|
||||
|
||||
public Server (String ip, int port) {
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public ArrayList<String> getFileList() {
|
||||
return fileList;
|
||||
}
|
||||
|
||||
public void setFileList(ArrayList<String> fileList) {
|
||||
this.fileList = fileList;
|
||||
}
|
||||
|
||||
public void addToFileList(String file) {
|
||||
this.fileList.add(file);
|
||||
}
|
||||
}
|
108
src/application/client/server/ServerConnector.java
Normal file
108
src/application/client/server/ServerConnector.java
Normal file
@ -0,0 +1,108 @@
|
||||
package application.client.server;
|
||||
|
||||
import application.PropertiesLoader;
|
||||
import application.client.exception.CannotConnectException;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ServerConnector {
|
||||
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
||||
|
||||
public ServerConnector() {
|
||||
}
|
||||
|
||||
public boolean readFileList(Server server) {
|
||||
try {
|
||||
Socket socket = new Socket(server.getIp(), server.getPort());
|
||||
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(dataOutputStream);
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
|
||||
|
||||
String command = "GET_ALL_FILES";
|
||||
|
||||
bufferedWriter.write(command, 0, command.length());
|
||||
bufferedWriter.flush();
|
||||
bufferedWriter.close();
|
||||
socket.close();
|
||||
|
||||
|
||||
|
||||
socket = new Socket(server.getIp(), server.getPort());
|
||||
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(dataInputStream);
|
||||
BufferedReader reader = new BufferedReader(inputStreamReader);
|
||||
|
||||
while (true) {
|
||||
String file = reader.readLine();
|
||||
System.out.println(file);
|
||||
|
||||
if (file == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
server.addToFileList(file);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// public static Connector getInstance() {
|
||||
// return instance;
|
||||
// }
|
||||
//
|
||||
// public void connect(String ip) throws CannotConnectException {
|
||||
// this.isConnect = false;
|
||||
// try {
|
||||
// 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 void getFileList() {
|
||||
// try {
|
||||
// DataOutputStream outputStream = new DataOutputStream(this.client.getOutputStream());
|
||||
// OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
|
||||
// BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
|
||||
//
|
||||
// bufferedWriter.write("GET_ALL_FILES");
|
||||
// bufferedWriter.flush();
|
||||
//
|
||||
// this.getMessage();
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public boolean isConnect() {
|
||||
// return this.isConnect;
|
||||
// }
|
||||
}
|
31
src/application/client/server/ServerFactory.java
Normal file
31
src/application/client/server/ServerFactory.java
Normal file
@ -0,0 +1,31 @@
|
||||
package application.client.server;
|
||||
|
||||
import application.client.validator.IpValidator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ServerFactory {
|
||||
|
||||
public Server create(String ip, int port) {
|
||||
if (!this.validate(ip)) {
|
||||
return null;
|
||||
};
|
||||
|
||||
Server server = this.createServer(ip, port);
|
||||
|
||||
ServerConnector connector = new ServerConnector();
|
||||
connector.readFileList(server);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
private Server createServer(String ip, int port) {
|
||||
return new Server(ip, port);
|
||||
}
|
||||
|
||||
public boolean validate(String ip) {
|
||||
IpValidator validator = new IpValidator();
|
||||
|
||||
return validator.validate(ip);
|
||||
}
|
||||
}
|
16
src/application/client/validator/IpValidator.java
Normal file
16
src/application/client/validator/IpValidator.java
Normal file
@ -0,0 +1,16 @@
|
||||
package application.client.validator;
|
||||
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
public class IpValidator implements Validator {
|
||||
public static final String REGEX = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
|
||||
|
||||
@Override
|
||||
public boolean validate(Object value) {
|
||||
if (value instanceof String) {
|
||||
return ((String) value).matches(REGEX);
|
||||
} else {
|
||||
throw new InvalidParameterException("Must be type string");
|
||||
}
|
||||
}
|
||||
}
|
5
src/application/client/validator/Validator.java
Normal file
5
src/application/client/validator/Validator.java
Normal file
@ -0,0 +1,5 @@
|
||||
package application.client.validator;
|
||||
|
||||
public interface Validator {
|
||||
public boolean validate(Object value);
|
||||
}
|
@ -1,41 +1,22 @@
|
||||
package application.client.view.mainPage;
|
||||
|
||||
import application.client.Connector;
|
||||
import application.client.exception.CannotConnectException;
|
||||
import application.client.server.ServerConnector;
|
||||
import application.client.server.ServerFactory;
|
||||
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 Button getFileListButton = null;
|
||||
public TextField rawFieldWithIp = null;
|
||||
public Label messageLabel = null;
|
||||
private Connector connector = Connector.getInstance();
|
||||
|
||||
@FXML public TextField ipField;
|
||||
|
||||
public MainPageController() {
|
||||
System.out.println("Controller work!");
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void connectToServer(Event e) {
|
||||
this.connectButton.setText("Connecting...");
|
||||
System.out.println(rawFieldWithIp.getText());;
|
||||
public void tryAddServer(Event e) {
|
||||
ServerFactory serverFactory = new ServerFactory();
|
||||
|
||||
try {
|
||||
this.connector.connect(this.rawFieldWithIp.getText());
|
||||
} catch (CannotConnectException ex) {
|
||||
}
|
||||
|
||||
this.messageLabel.setText(this.connector.isConnect() ? "Connected" : "Not connect");
|
||||
this.connectButton.setText("Connect");
|
||||
this.getFileListButton.setDisable(false);
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void getFileList(Event e) {
|
||||
this.connector.getFileList();
|
||||
serverFactory.create(ipField.getText(), 7332);
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?import javafx.scene.text.Font?>
|
||||
|
||||
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.client.view.mainPage.MainPageController">
|
||||
<children>
|
||||
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
|
||||
<children>
|
||||
<Label alignment="CENTER" layoutX="197.0" layoutY="45.0" style=" " text="FTP SAR - Sender And Reciver" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
|
||||
<font>
|
||||
<Font size="18.0" />
|
||||
</font>
|
||||
</Label>
|
||||
<TextField fx:id="rawFieldWithIp" layoutX="239.0" layoutY="82.0" promptText="Insert ip" />
|
||||
<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" />
|
||||
<Button fx:id="getFileListButton" disable="true" layoutX="283.0" layoutY="166.0" mnemonicParsing="false" onAction="#getFileList" text="Get file list" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
</children>
|
||||
</VBox>
|
20
src/resource/main_page.fxml
Normal file
20
src/resource/main_page.fxml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.Pane?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
|
||||
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.client.view.mainPage.MainPageController">
|
||||
<children>
|
||||
<Pane prefHeight="70.0" prefWidth="640.0">
|
||||
<children>
|
||||
<Button layoutX="529.0" layoutY="22.0" mnemonicParsing="false" onAction="#tryAddServer" text="Add server" />
|
||||
<TextField fx:id="ipField" layoutX="344.0" layoutY="22.0" />
|
||||
<Label layoutX="65.0" layoutY="27.0" text="SAR - Send and Revice" />
|
||||
<Label alignment="CENTER" layoutX="345.0" layoutY="48.0" prefHeight="16.0" prefWidth="170.0" textAlignment="JUSTIFY" textFill="RED" />
|
||||
</children>
|
||||
</Pane>
|
||||
</children>
|
||||
</VBox>
|
Loading…
Reference in New Issue
Block a user