From 85a54cabc2165328d2e1bf586a4592aa92ae6fbb Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Tue, 6 Oct 2020 02:19:08 +0300 Subject: [PATCH] Refactor 'network-stopper' related code --- .../COM/NET/NETCommunications.java | 20 +++++++----- .../Controllers/FrontController.java | 31 +++++++++---------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/main/java/nsusbloader/COM/NET/NETCommunications.java b/src/main/java/nsusbloader/COM/NET/NETCommunications.java index cb03092..2f4b671 100644 --- a/src/main/java/nsusbloader/COM/NET/NETCommunications.java +++ b/src/main/java/nsusbloader/COM/NET/NETCommunications.java @@ -25,7 +25,6 @@ import nsusbloader.ModelControllers.Log; import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EMsgType; import nsusbloader.COM.Helpers.NSSplitReader; -import nsusbloader.RainbowHexDump; import java.io.*; import java.net.*; @@ -53,6 +52,8 @@ public class NETCommunications extends CancellableRunnable { private OutputStream currSockOS; private PrintWriter currSockPW; + + private boolean jobInProgress = true; /** * Simple constructor that everybody uses * */ @@ -150,7 +151,7 @@ public class NETCommunications extends CancellableRunnable { } private void serveRequestsLoop(){ try { - while (true){ + while (jobInProgress){ clientSocket = serverSocket.accept(); BufferedReader br = new BufferedReader( new InputStreamReader(clientSocket.getInputStream()) @@ -178,7 +179,10 @@ public class NETCommunications extends CancellableRunnable { else logPrinter.print(e.getMessage(), EMsgType.INFO); close(EFileStatus.UNKNOWN); + return; } + logPrinter.print("All transfers complete", EMsgType.PASS); + close(EFileStatus.UPLOADED); } /** * Handle requests @@ -186,7 +190,8 @@ public class NETCommunications extends CancellableRunnable { * */ private void handleRequest(LinkedList packet) throws Exception{ if (packet.get(0).startsWith("DROP")){ - throw new Exception("All transfers finished"); + jobInProgress = false; + return; } File requestedFile; @@ -214,11 +219,10 @@ public class NETCommunications extends CancellableRunnable { } if (packet.get(0).startsWith("GET")) { for (String line: packet) { - if (! line.toLowerCase().startsWith("range")) //todo: fix - continue; - - parseGETrange(requestedFile, reqFileName, reqFileSize, line); - return; + if (line.toLowerCase().startsWith("range")){ + parseGETrange(requestedFile, reqFileName, reqFileSize, line); + return; + } } } } diff --git a/src/main/java/nsusbloader/Controllers/FrontController.java b/src/main/java/nsusbloader/Controllers/FrontController.java index 96ddf07..3790f40 100644 --- a/src/main/java/nsusbloader/Controllers/FrontController.java +++ b/src/main/java/nsusbloader/Controllers/FrontController.java @@ -304,16 +304,17 @@ public class FrontController implements Initializable { * It's button listener when transmission in progress * */ private void stopBtnAction(){ - if (workThread != null && workThread.isAlive()){ - usbNetCommunications.cancel(); + if (workThread == null || ! workThread.isAlive()) + return; - if (usbNetCommunications instanceof NETCommunications){ - try{ - ((NETCommunications) usbNetCommunications).getServerSocket().close(); - ((NETCommunications) usbNetCommunications).getClientSocket().close(); - } - catch (Exception ignore){ } + usbNetCommunications.cancel(); + + if (usbNetCommunications instanceof NETCommunications){ + try{ + ((NETCommunications) usbNetCommunications).getServerSocket().close(); + ((NETCommunications) usbNetCommunications).getClientSocket().close(); } + catch (Exception ignore){ } } } /** @@ -358,26 +359,24 @@ public class FrontController implements Initializable { usbNetPane.setDisable(isActive); return; } + selectNspBtn.setDisable(isActive); + selectSplitNspBtn.setDisable(isActive); + btnUpStopImage.getStyleClass().clear(); + if (isActive) { - selectNspBtn.setDisable(true); - selectSplitNspBtn.setDisable(true); - btnUpStopImage.getStyleClass().clear(); btnUpStopImage.getStyleClass().add("regionStop"); uploadStopBtn.setOnAction(e-> stopBtnAction()); uploadStopBtn.setText(resourceBundle.getString("btn_Stop")); - uploadStopBtn.getStyleClass().remove("buttonUp"); + uploadStopBtn.getStyleClass().clear(); uploadStopBtn.getStyleClass().add("buttonStop"); return; } - selectNspBtn.setDisable(false); - selectSplitNspBtn.setDisable(false); - btnUpStopImage.getStyleClass().clear(); btnUpStopImage.getStyleClass().add("regionUpload"); uploadStopBtn.setOnAction(e-> uploadBtnAction()); uploadStopBtn.setText(resourceBundle.getString("btn_Upload")); - uploadStopBtn.getStyleClass().remove("buttonStop"); + uploadStopBtn.getStyleClass().clear(); uploadStopBtn.getStyleClass().add("buttonUp"); } /**