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.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<String> packet) throws Exception{
if (packet.get(0).startsWith("DROP")){
throw new Exception("All transfers finished");
jobInProgress = false;
return;
}
File requestedFile;
@ -214,14 +219,13 @@ public class NETCommunications extends CancellableRunnable {
}
if (packet.get(0).startsWith("GET")) {
for (String line: packet) {
if (! line.toLowerCase().startsWith("range")) //todo: fix
continue;
if (line.toLowerCase().startsWith("range")){
parseGETrange(requestedFile, reqFileName, reqFileSize, line);
return;
}
}
}
}
private void parseGETrange(File file, String fileName, long fileSize, String rangeDirective) throws Exception{
try {

View file

@ -304,7 +304,9 @@ public class FrontController implements Initializable {
* It's button listener when transmission in progress
* */
private void stopBtnAction(){
if (workThread != null && workThread.isAlive()){
if (workThread == null || ! workThread.isAlive())
return;
usbNetCommunications.cancel();
if (usbNetCommunications instanceof NETCommunications){
@ -315,7 +317,6 @@ public class FrontController implements Initializable {
catch (Exception ignore){ }
}
}
}
/**
* Drag-n-drop support (dragOver consumer)
* */
@ -358,26 +359,24 @@ public class FrontController implements Initializable {
usbNetPane.setDisable(isActive);
return;
}
if (isActive) {
selectNspBtn.setDisable(true);
selectSplitNspBtn.setDisable(true);
selectNspBtn.setDisable(isActive);
selectSplitNspBtn.setDisable(isActive);
btnUpStopImage.getStyleClass().clear();
if (isActive) {
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");
}
/**