Refactor 'network-stopper' related code

This commit is contained in:
Dmitry Isaenko 2020-10-06 02:19:08 +03:00
parent 45696881e2
commit 85a54cabc2
2 changed files with 27 additions and 24 deletions

View file

@ -25,7 +25,6 @@ import nsusbloader.ModelControllers.Log;
import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.NSLDataTypes.EMsgType; import nsusbloader.NSLDataTypes.EMsgType;
import nsusbloader.COM.Helpers.NSSplitReader; import nsusbloader.COM.Helpers.NSSplitReader;
import nsusbloader.RainbowHexDump;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
@ -53,6 +52,8 @@ public class NETCommunications extends CancellableRunnable {
private OutputStream currSockOS; private OutputStream currSockOS;
private PrintWriter currSockPW; private PrintWriter currSockPW;
private boolean jobInProgress = true;
/** /**
* Simple constructor that everybody uses * Simple constructor that everybody uses
* */ * */
@ -150,7 +151,7 @@ public class NETCommunications extends CancellableRunnable {
} }
private void serveRequestsLoop(){ private void serveRequestsLoop(){
try { try {
while (true){ while (jobInProgress){
clientSocket = serverSocket.accept(); clientSocket = serverSocket.accept();
BufferedReader br = new BufferedReader( BufferedReader br = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()) new InputStreamReader(clientSocket.getInputStream())
@ -178,7 +179,10 @@ public class NETCommunications extends CancellableRunnable {
else else
logPrinter.print(e.getMessage(), EMsgType.INFO); logPrinter.print(e.getMessage(), EMsgType.INFO);
close(EFileStatus.UNKNOWN); close(EFileStatus.UNKNOWN);
return;
} }
logPrinter.print("All transfers complete", EMsgType.PASS);
close(EFileStatus.UPLOADED);
} }
/** /**
* Handle requests * Handle requests
@ -186,7 +190,8 @@ public class NETCommunications extends CancellableRunnable {
* */ * */
private void handleRequest(LinkedList<String> packet) throws Exception{ private void handleRequest(LinkedList<String> packet) throws Exception{
if (packet.get(0).startsWith("DROP")){ if (packet.get(0).startsWith("DROP")){
throw new Exception("All transfers finished"); jobInProgress = false;
return;
} }
File requestedFile; File requestedFile;
@ -214,14 +219,13 @@ public class NETCommunications extends CancellableRunnable {
} }
if (packet.get(0).startsWith("GET")) { if (packet.get(0).startsWith("GET")) {
for (String line: packet) { for (String line: packet) {
if (! line.toLowerCase().startsWith("range")) //todo: fix if (line.toLowerCase().startsWith("range")){
continue;
parseGETrange(requestedFile, reqFileName, reqFileSize, line); parseGETrange(requestedFile, reqFileName, reqFileSize, line);
return; return;
} }
} }
} }
}
private void parseGETrange(File file, String fileName, long fileSize, String rangeDirective) throws Exception{ private void parseGETrange(File file, String fileName, long fileSize, String rangeDirective) throws Exception{
try { try {

View file

@ -304,7 +304,9 @@ public class FrontController implements Initializable {
* It's button listener when transmission in progress * It's button listener when transmission in progress
* */ * */
private void stopBtnAction(){ private void stopBtnAction(){
if (workThread != null && workThread.isAlive()){ if (workThread == null || ! workThread.isAlive())
return;
usbNetCommunications.cancel(); usbNetCommunications.cancel();
if (usbNetCommunications instanceof NETCommunications){ if (usbNetCommunications instanceof NETCommunications){
@ -315,7 +317,6 @@ public class FrontController implements Initializable {
catch (Exception ignore){ } catch (Exception ignore){ }
} }
} }
}
/** /**
* Drag-n-drop support (dragOver consumer) * Drag-n-drop support (dragOver consumer)
* */ * */
@ -358,26 +359,24 @@ public class FrontController implements Initializable {
usbNetPane.setDisable(isActive); usbNetPane.setDisable(isActive);
return; return;
} }
if (isActive) { selectNspBtn.setDisable(isActive);
selectNspBtn.setDisable(true); selectSplitNspBtn.setDisable(isActive);
selectSplitNspBtn.setDisable(true);
btnUpStopImage.getStyleClass().clear(); btnUpStopImage.getStyleClass().clear();
if (isActive) {
btnUpStopImage.getStyleClass().add("regionStop"); btnUpStopImage.getStyleClass().add("regionStop");
uploadStopBtn.setOnAction(e-> stopBtnAction()); uploadStopBtn.setOnAction(e-> stopBtnAction());
uploadStopBtn.setText(resourceBundle.getString("btn_Stop")); uploadStopBtn.setText(resourceBundle.getString("btn_Stop"));
uploadStopBtn.getStyleClass().remove("buttonUp"); uploadStopBtn.getStyleClass().clear();
uploadStopBtn.getStyleClass().add("buttonStop"); uploadStopBtn.getStyleClass().add("buttonStop");
return; return;
} }
selectNspBtn.setDisable(false);
selectSplitNspBtn.setDisable(false);
btnUpStopImage.getStyleClass().clear();
btnUpStopImage.getStyleClass().add("regionUpload"); btnUpStopImage.getStyleClass().add("regionUpload");
uploadStopBtn.setOnAction(e-> uploadBtnAction()); uploadStopBtn.setOnAction(e-> uploadBtnAction());
uploadStopBtn.setText(resourceBundle.getString("btn_Upload")); uploadStopBtn.setText(resourceBundle.getString("btn_Upload"));
uploadStopBtn.getStyleClass().remove("buttonStop"); uploadStopBtn.getStyleClass().clear();
uploadStopBtn.getStyleClass().add("buttonUp"); uploadStopBtn.getStyleClass().add("buttonUp");
} }
/** /**