Refactor 'network-stopper' related code
This commit is contained in:
parent
45696881e2
commit
85a54cabc2
2 changed files with 27 additions and 24 deletions
|
@ -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,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue