From d2be62bb14d7513dfd23f9559a93e3de040781a0 Mon Sep 17 00:00:00 2001 From: MKDrwal Date: Fri, 13 Dec 2019 23:05:49 +0100 Subject: [PATCH] add refresh method --- server/serverv2.c | 1 + .../sar/controller/MainPageController.java | 8 +++++- .../sar/controller/ServerPanelController.java | 11 +++++++- src/main/java/ftp/sar/server/Server.java | 21 ++++++++++++++- .../java/ftp/sar/server/ServerConnector.java | 16 +++++++----- .../java/ftp/sar/server/ServerFactory.java | 24 +++--------------- src/main/resources/server_panel.fxml | 2 +- src/test/java/ftp/sar/server/TestServer.java | 12 ++++----- target/classes/ftp/sar/server/Server.class | Bin 1939 -> 2661 bytes .../ftp/sar/server/ServerConnector.class | Bin 3391 -> 3447 bytes .../ftp/sar/server/ServerFactory.class | Bin 1808 -> 1024 bytes 11 files changed, 58 insertions(+), 37 deletions(-) diff --git a/server/serverv2.c b/server/serverv2.c index d364a63..b132ea6 100644 --- a/server/serverv2.c +++ b/server/serverv2.c @@ -77,6 +77,7 @@ int main(int argc, char **argv) free(namelist); send(sdconnection, names_files, 512, 0); + printf("Done Sending"); goto start; } else if (strncmp("SEND", buff, 4) == 0) diff --git a/src/main/java/ftp/sar/controller/MainPageController.java b/src/main/java/ftp/sar/controller/MainPageController.java index 64253b4..60afdfc 100644 --- a/src/main/java/ftp/sar/controller/MainPageController.java +++ b/src/main/java/ftp/sar/controller/MainPageController.java @@ -1,6 +1,7 @@ package ftp.sar.controller; import ftp.sar.ResourceLoader; +import ftp.sar.exception.CannotConnectException; import ftp.sar.server.Server; import ftp.sar.server.ServerFactory; import javafx.event.Event; @@ -24,7 +25,12 @@ public class MainPageController { public void tryAddServer(Event e) { ServerFactory serverFactory = new ServerFactory(); - Server server = serverFactory.create(ipField.getText(), 7332); + Server server = null; + try { + server = serverFactory.create(ipField.getText(), 7332); + } catch (CannotConnectException ex) { + ex.printStackTrace(); + } ResourceLoader resourceLoader = ResourceLoader.getInstance(); try { FXMLLoader fxmlLoader = new FXMLLoader(resourceLoader.getResource("server_panel.fxml")); diff --git a/src/main/java/ftp/sar/controller/ServerPanelController.java b/src/main/java/ftp/sar/controller/ServerPanelController.java index 5d23570..8da08e3 100644 --- a/src/main/java/ftp/sar/controller/ServerPanelController.java +++ b/src/main/java/ftp/sar/controller/ServerPanelController.java @@ -1,6 +1,7 @@ package ftp.sar.controller; import ftp.sar.builder.FilePaneBuilder; +import ftp.sar.exception.CannotConnectException; import ftp.sar.server.Server; import javafx.event.Event; import javafx.fxml.FXML; @@ -11,6 +12,7 @@ import javafx.scene.layout.FlowPane; import javafx.scene.layout.Pane; import java.net.URL; +import java.util.ArrayList; import java.util.ResourceBundle; public class ServerPanelController implements Initializable { @@ -31,6 +33,12 @@ public class ServerPanelController implements Initializable { @FXML public void refreshList(Event e) { + try { + this.server.getConnector().readFileList(); + } catch (CannotConnectException ex) { + ex.printStackTrace(); + } + this.setFileList(); System.out.println("refresh"); } @@ -48,7 +56,8 @@ public class ServerPanelController implements Initializable { this.setFileList(); } - private void setFileList() { + public void setFileList() { + this.fileContainer.getChildren().clear(); for (String filename : this.server.getFileList()) { var pane = this.filePaneBuilder.getPane(filename); diff --git a/src/main/java/ftp/sar/server/Server.java b/src/main/java/ftp/sar/server/Server.java index 88f9d1c..c5777a0 100644 --- a/src/main/java/ftp/sar/server/Server.java +++ b/src/main/java/ftp/sar/server/Server.java @@ -1,7 +1,9 @@ package ftp.sar.server; import ftp.sar.controller.ServerPanelController; +import ftp.sar.exception.CannotConnectException; +import java.io.IOException; import java.net.Socket; import java.util.ArrayList; @@ -11,6 +13,16 @@ public class Server { private ArrayList fileList = new ArrayList<>(); private ServerPanelController serverPanelController; + public ServerConnector getConnector() { + return connector; + } + + public void setConnector(ServerConnector connector) { + this.connector = connector; + } + + private ServerConnector connector = new ServerConnector(this); + public Socket getSocket() { return socket; } @@ -21,9 +33,16 @@ public class Server { private Socket socket; - public Server (String ip, int port) { + public Server (String ip, int port) throws CannotConnectException { this.ip = ip; this.port = port; + + try { + this.socket = new Socket(ip, port); + this.connector.readFileList(); + } catch (IOException e) { + throw new CannotConnectException(); + } } public String getIp() { diff --git a/src/main/java/ftp/sar/server/ServerConnector.java b/src/main/java/ftp/sar/server/ServerConnector.java index 9a5b0a0..3af5811 100644 --- a/src/main/java/ftp/sar/server/ServerConnector.java +++ b/src/main/java/ftp/sar/server/ServerConnector.java @@ -7,6 +7,7 @@ import java.io.*; import java.util.ArrayList; public class ServerConnector { + private final Server server; private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance(); ArrayList filterFileList = new ArrayList() { @@ -17,14 +18,16 @@ public class ServerConnector { } }; - public ServerConnector() { + public ServerConnector(Server server) { + this.server = server; } - public boolean readFileList(Server server) throws CannotConnectException { - this.sendRequest(server, "GET_ALL_FILES"); + public boolean readFileList() throws CannotConnectException { + this.server.setFileList(new ArrayList()); + this.sendRequest("GET_ALL_FILES"); try { - DataInputStream dataInputStream = new DataInputStream(server.getSocket().getInputStream()); + DataInputStream dataInputStream = new DataInputStream(this.server.getSocket().getInputStream()); InputStreamReader inputStreamReader = new InputStreamReader(dataInputStream); BufferedReader reader = new BufferedReader(inputStreamReader); @@ -49,6 +52,8 @@ public class ServerConnector { } + this.sendRequest(""); + return true; } catch (Exception e) { e.printStackTrace(); @@ -58,11 +63,10 @@ public class ServerConnector { } private void sendRequest( - Server server, String message ) throws CannotConnectException { try { - DataOutputStream dataOutputStream = new DataOutputStream(server.getSocket().getOutputStream()); + DataOutputStream dataOutputStream = new DataOutputStream(this.server.getSocket().getOutputStream()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(dataOutputStream); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); diff --git a/src/main/java/ftp/sar/server/ServerFactory.java b/src/main/java/ftp/sar/server/ServerFactory.java index b00df9d..5b623c6 100644 --- a/src/main/java/ftp/sar/server/ServerFactory.java +++ b/src/main/java/ftp/sar/server/ServerFactory.java @@ -8,33 +8,15 @@ import java.net.Socket; public class ServerFactory { - public Server create(String ip, int port) { + public Server create(String ip, int port) throws CannotConnectException { if (!this.validate(ip)) { return null; } - Server server = this.createServer(ip, port); - - ServerConnector connector = new ServerConnector(); - try { - this.createConnection(server); - connector.readFileList(server); - } catch (CannotConnectException e) { - e.printStackTrace(); - } - - return server; + return this.createServer(ip, port); } - private void createConnection(Server server) throws CannotConnectException{ - try { - server.setSocket(new Socket(server.getIp(), server.getPort())); - } catch (IOException e) { - throw new CannotConnectException(); - } - } - - private Server createServer(String ip, int port) { + private Server createServer(String ip, int port) throws CannotConnectException { return new Server(ip, port); } diff --git a/src/main/resources/server_panel.fxml b/src/main/resources/server_panel.fxml index 8f48b96..912f09e 100644 --- a/src/main/resources/server_panel.fxml +++ b/src/main/resources/server_panel.fxml @@ -12,7 +12,7 @@