Frontend complete
This commit is contained in:
parent
863c69affa
commit
65f084591a
5 changed files with 130 additions and 107 deletions
|
@ -2,6 +2,7 @@ package nsusbloader.Controllers;
|
|||
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.concurrent.Task;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.*;
|
||||
|
@ -20,7 +21,6 @@ import nsusbloader.USB.UsbCommunications;
|
|||
import java.io.File;
|
||||
import java.net.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
|
@ -54,7 +54,7 @@ public class NSLMainController implements Initializable {
|
|||
@FXML
|
||||
private Label nsIpLbl;
|
||||
|
||||
private UsbCommunications usbCommunications;
|
||||
private Task<Void> usbNetCommunications;
|
||||
private Thread workThread;
|
||||
|
||||
private String previouslyOpenedPath;
|
||||
|
@ -200,8 +200,8 @@ public class NSLMainController implements Initializable {
|
|||
for (File item: nspToUpload)
|
||||
logArea.appendText(" "+item.getAbsolutePath()+"\n");
|
||||
}
|
||||
usbCommunications = new UsbCommunications(nspToUpload, choiceProtocol.getSelectionModel().getSelectedItem());
|
||||
workThread = new Thread(usbCommunications);
|
||||
usbNetCommunications = new UsbCommunications(nspToUpload, choiceProtocol.getSelectionModel().getSelectedItem());
|
||||
workThread = new Thread(usbNetCommunications);
|
||||
workThread.setDaemon(true);
|
||||
workThread.start();
|
||||
}
|
||||
|
@ -212,7 +212,6 @@ public class NSLMainController implements Initializable {
|
|||
String nsIP = nsIpTextField.getText();
|
||||
|
||||
List<File> nspToUpload;
|
||||
if (!SettingsTabController.getExpertModeSelected()) {
|
||||
if ((nspToUpload = tableFilesListController.getFilesForUpload()) == null) {
|
||||
logArea.setText(resourceBundle.getString("logsNoFolderFileSelected"));
|
||||
return;
|
||||
|
@ -222,15 +221,23 @@ public class NSLMainController implements Initializable {
|
|||
for (File item: nspToUpload)
|
||||
logArea.appendText(" "+item.getAbsolutePath()+"\n");
|
||||
}
|
||||
NETCommunications netCommunications = new NETCommunications(nspToUpload, nsIP); // TODO: move somewhere
|
||||
workThread = new Thread(netCommunications);
|
||||
if (!SettingsTabController.getExpertModeSelected())
|
||||
usbNetCommunications = new NETCommunications(nspToUpload, nsIP, false, "", "", "");
|
||||
else {
|
||||
usbNetCommunications = new NETCommunications(
|
||||
nspToUpload,
|
||||
nsIP,
|
||||
SettingsTabController.getNotServeSelected(),
|
||||
SettingsTabController.getAutoIpSelected()?"":SettingsTabController.getHostIp(),
|
||||
SettingsTabController.getRandPortSelected()?"":SettingsTabController.getHostPort(),
|
||||
SettingsTabController.getNotServeSelected()?SettingsTabController.getHostExtra():""
|
||||
);
|
||||
}
|
||||
|
||||
workThread = new Thread(usbNetCommunications);
|
||||
workThread.setDaemon(true);
|
||||
workThread.start();
|
||||
}
|
||||
else {
|
||||
// TODO; pass to another constructor
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
@ -238,7 +245,7 @@ public class NSLMainController implements Initializable {
|
|||
* */
|
||||
private void stopBtnAction(){
|
||||
if (workThread != null && workThread.isAlive()){
|
||||
usbCommunications.cancel(false); // TODO: add something abstract to use also for network
|
||||
usbNetCommunications.cancel(false); // TODO: add something abstract to use also for network
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
|
|
@ -50,11 +50,14 @@ public class NSLRowModel {
|
|||
case FAILED:
|
||||
this.status = "Failed";
|
||||
break;
|
||||
case UNKNOWN:
|
||||
case INDETERMINATE:
|
||||
this.status = "...";
|
||||
break;
|
||||
case UNKNOWN:
|
||||
this.status = "Unknown";
|
||||
break;
|
||||
case INCORRECT_FILE_FAILED:
|
||||
this.status = "Failed: Incorrect file";
|
||||
this.status = "Failed: Bad file";
|
||||
markForUpload = false;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -225,7 +225,7 @@ public class NSTableViewController implements Initializable {
|
|||
for (NSLRowModel model: rowsObsLst){
|
||||
if (model.isMarkForUpload()){
|
||||
files.add(model.getNspFile());
|
||||
model.setStatus(EFileStatus.UNKNOWN);
|
||||
model.setStatus(EFileStatus.INDETERMINATE);
|
||||
}
|
||||
}
|
||||
if (!files.isEmpty()) {
|
||||
|
|
|
@ -10,9 +10,6 @@ import java.net.*;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
/*
|
||||
* Add option: don't serve replies
|
||||
* */
|
||||
|
||||
public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
||||
|
||||
|
@ -33,13 +30,15 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
|||
/**
|
||||
* Simple constructor that everybody uses
|
||||
* */
|
||||
public NETCommunications(List<File> filesList, String switchIP){
|
||||
this.doNotServeRequests = false;
|
||||
public NETCommunications(List<File> filesList, String switchIP, boolean doNotServeRequests, String hostIPaddr, String hostPortNum, String extras){
|
||||
this.doNotServeRequests = doNotServeRequests;
|
||||
if (doNotServeRequests)
|
||||
this.extras = extras;
|
||||
else
|
||||
this.extras = "";
|
||||
this.logPrinter = new LogPrinter();
|
||||
this.switchIP = switchIP;
|
||||
this.logPrinter = new LogPrinter();
|
||||
this.nspMap = new HashMap<>();
|
||||
logPrinter.print("\tPrepare chain", EMsgType.INFO);
|
||||
// Collect and encode NSP files list
|
||||
try {
|
||||
for (File nspFile : filesList)
|
||||
|
@ -47,39 +46,37 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
|||
}
|
||||
catch (UnsupportedEncodingException uee){
|
||||
isValid = false;
|
||||
logPrinter.print("NET: Unsupported encoding for 'URLEncoder'. Internal issue you can't fix. Please report.", EMsgType.FAIL);
|
||||
logPrinter.print("NET: Unsupported encoding for 'URLEncoder'. Internal issue you can't fix. Please report. . Returned:\n\t"+uee.getMessage(), EMsgType.FAIL);
|
||||
for (File nspFile : filesList)
|
||||
nspMap.put(nspFile.getName(), nspFile);
|
||||
close(EFileStatus.FAILED);
|
||||
return;
|
||||
}
|
||||
// Resolve IP
|
||||
if (hostIPaddr.isEmpty()) {
|
||||
DatagramSocket socket;
|
||||
try { // todo: check other method if internet unavaliable
|
||||
socket = new DatagramSocket();
|
||||
socket.connect(InetAddress.getByName("8.8.8.8"), 10002); // Google
|
||||
hostIP = socket.getLocalAddress().getHostAddress();
|
||||
socket.close();
|
||||
}
|
||||
catch (SocketException | UnknownHostException e){
|
||||
logPrinter.print("NET: Can't get your computer IP using Google DNS server", EMsgType.INFO);
|
||||
} catch (SocketException | UnknownHostException e) {
|
||||
logPrinter.print("NET: Can't get your computer IP using Google DNS server. Returned:\n\t"+e.getMessage(), EMsgType.INFO);
|
||||
try {
|
||||
socket = new DatagramSocket();
|
||||
socket.connect(InetAddress.getByName("193.0.14.129"), 10002); // RIPE NCC
|
||||
hostIP = socket.getLocalAddress().getHostAddress();
|
||||
socket.close();
|
||||
}
|
||||
catch (SocketException | UnknownHostException e1){
|
||||
logPrinter.print("NET: Can't get your computer IP using RIPE NCC root server", EMsgType.INFO);
|
||||
} catch (SocketException | UnknownHostException e1) {
|
||||
logPrinter.print("NET: Can't get your computer IP using RIPE NCC root server. Returned:\n\t"+e1.getMessage(), EMsgType.INFO);
|
||||
try {
|
||||
socket = new DatagramSocket();
|
||||
socket.connect(InetAddress.getByName("people.com.cn"), 10002); // Renmin Ribao
|
||||
hostIP = socket.getLocalAddress().getHostAddress();
|
||||
socket.close();
|
||||
}
|
||||
catch (SocketException | UnknownHostException e2){
|
||||
logPrinter.print("NET: Can't get your computer IP using Renmin Ribao server", EMsgType.FAIL);
|
||||
logPrinter.print("Try using 'Expert mode' and set IP by yourself", EMsgType.INFO);
|
||||
} catch (SocketException | UnknownHostException e2) {
|
||||
logPrinter.print("NET: Can't get your computer IP using Renmin Ribao server. Returned:\n\t"+e2.getMessage(), EMsgType.FAIL);
|
||||
logPrinter.print("Try using 'Expert mode' and set IP manually.", EMsgType.INFO);
|
||||
try {
|
||||
Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
|
@ -90,8 +87,8 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
|||
logPrinter.print("Check for: " + i.getHostAddress(), EMsgType.INFO);
|
||||
}
|
||||
}
|
||||
} catch (SocketException socketException) {
|
||||
logPrinter.print("Can't determine possible variants.", EMsgType.FAIL);
|
||||
} catch (SocketException socketException) { // Good block.
|
||||
logPrinter.print("Can't determine possible variants. Returned:\n\t"+socketException.getMessage(), EMsgType.FAIL);
|
||||
}
|
||||
isValid = false;
|
||||
close(EFileStatus.FAILED);
|
||||
|
@ -100,7 +97,14 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
|||
}
|
||||
}
|
||||
logPrinter.print("NET: Your IP detected as: " + hostIP, EMsgType.PASS);
|
||||
// Detect port
|
||||
}
|
||||
else {
|
||||
this.hostIP = hostIPaddr;
|
||||
logPrinter.print("NET: Your IP defined as: " + hostIP, EMsgType.PASS);
|
||||
}
|
||||
|
||||
// Get port
|
||||
if (hostPortNum.isEmpty()) {
|
||||
Random portRandomizer = new Random();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
try {
|
||||
|
@ -108,33 +112,40 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
|||
serverSocket = new ServerSocket(hostPort); //System.out.println(serverSocket.getInetAddress()); 0.0.0.0
|
||||
logPrinter.print("NET: Your port detected as: " + hostPort, EMsgType.PASS);
|
||||
break;
|
||||
}
|
||||
catch (IOException ioe){
|
||||
} catch (IOException ioe) {
|
||||
if (i == 4) {
|
||||
logPrinter.print("NET: Can't find good port", EMsgType.FAIL);
|
||||
logPrinter.print("Try using 'Expert mode' and set port by yourself.", EMsgType.INFO);
|
||||
isValid = false;
|
||||
close(EFileStatus.FAILED);
|
||||
return;
|
||||
}
|
||||
else
|
||||
} else
|
||||
logPrinter.print("NET: Can't use port " + hostPort + "\nLooking for another one.", EMsgType.WARNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
this.hostPort = Integer.parseInt(hostPortNum);
|
||||
logPrinter.print("NET: Using defined port number: " + hostPort, EMsgType.PASS);
|
||||
}
|
||||
catch (NumberFormatException exeption){ // Literally never happens.
|
||||
isValid = false;
|
||||
logPrinter.print("NET: Can't use port defined in settings: " + hostPortNum + "\nIt's not a valid number!", EMsgType.WARNING);
|
||||
close(EFileStatus.FAILED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
isValid = true;
|
||||
}
|
||||
/**
|
||||
* Do it hard way (expert mode selected)
|
||||
* Override cancel block to close connection by ourselves
|
||||
* */
|
||||
public NETCommunications(List<File> filesList, String switchIP, boolean doNotServeRequests, String hostIP, int hostPort, String extras){
|
||||
this.doNotServeRequests = doNotServeRequests;
|
||||
if (doNotServeRequests)
|
||||
this.extras = extras;
|
||||
else
|
||||
this.extras = "";
|
||||
this.switchIP = switchIP;
|
||||
@Override
|
||||
protected void cancelled() {
|
||||
this.close(EFileStatus.UNKNOWN);
|
||||
super.cancelled();
|
||||
}
|
||||
|
||||
/*
|
||||
Replace everything to ASCII (WEB representation)
|
||||
calculate
|
||||
|
@ -142,16 +153,10 @@ write in first 4 bytes
|
|||
* */
|
||||
@Override
|
||||
protected Void call() {
|
||||
if (!isValid)
|
||||
if (!isValid | isCancelled())
|
||||
return null;
|
||||
if (isCancelled()){
|
||||
logPrinter.print("NET: interrupted by user", EMsgType.INFO);
|
||||
close(EFileStatus.FAILED);
|
||||
return null;
|
||||
}
|
||||
|
||||
logPrinter.print("\tStart chain", EMsgType.INFO);
|
||||
// Get files list length
|
||||
// Create string that we'll send to TF and which initiates chain
|
||||
StringBuilder myStrBuilder;
|
||||
|
||||
myStrBuilder = new StringBuilder();
|
||||
|
@ -179,9 +184,17 @@ write in first 4 bytes
|
|||
handShakeSocket.close();
|
||||
}
|
||||
catch (IOException uhe){
|
||||
uhe.printStackTrace(); // TODO: FIX: could be [UnknownHostException]
|
||||
logPrinter.print("NET: Unable to connect to NS and send files list. Returned:\n\t"+uhe.getMessage(), EMsgType.FAIL);
|
||||
close(EFileStatus.UNKNOWN);
|
||||
return null;
|
||||
}
|
||||
// Check if we should serve requests
|
||||
if (this.doNotServeRequests){
|
||||
logPrinter.print("NET: List of files transferred. Replies won't be served.", EMsgType.PASS);
|
||||
close(EFileStatus.UNKNOWN);
|
||||
return null;
|
||||
}
|
||||
logPrinter.print("NET: Initiation files list has been sent to NS.", EMsgType.PASS);
|
||||
|
||||
// Go transfer
|
||||
try {
|
||||
|
@ -198,8 +211,8 @@ write in first 4 bytes
|
|||
String line;
|
||||
LinkedList<String> tcpPackeet = new LinkedList<>();
|
||||
while ((line = br.readLine()) != null) {
|
||||
if (isCancelled()) // TODO: notice everywhere
|
||||
break;
|
||||
// if (isCancelled()) // TODO: notice everywhere
|
||||
// break;
|
||||
System.out.println(line); // TODO: remove DBG
|
||||
if (line.trim().isEmpty()) { // If TCP packet is ended
|
||||
handleRequest(tcpPackeet, pw); // Proceed required things
|
||||
|
@ -210,13 +223,12 @@ write in first 4 bytes
|
|||
}
|
||||
System.out.println("\nDone!"); // reopen client sock
|
||||
clientSocket.close();
|
||||
serverSocket.close();
|
||||
}
|
||||
catch (IOException ioe){
|
||||
ioe.printStackTrace(); // TODO: fix
|
||||
System.out.println("client sock closed");
|
||||
}
|
||||
logPrinter.update(nspMap, EFileStatus.UNKNOWN);
|
||||
logPrinter.close();
|
||||
if (!isCancelled())
|
||||
close(EFileStatus.UNKNOWN);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -271,11 +283,12 @@ write in first 4 bytes
|
|||
* Close when done
|
||||
* */
|
||||
private void close(EFileStatus status){
|
||||
System.out.println("called");
|
||||
try {
|
||||
serverSocket.close();
|
||||
logPrinter.print("NET: Closing server socket.", EMsgType.PASS);
|
||||
}
|
||||
catch (IOException ioe){
|
||||
catch (IOException | NullPointerException ioe){
|
||||
logPrinter.print("NET: Closing server socket failed. Sometimes it's not an issue.", EMsgType.WARNING);
|
||||
}
|
||||
if (status != null) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package nsusbloader.NSLDataTypes;
|
||||
|
||||
public enum EFileStatus {
|
||||
UPLOADED, INCORRECT_FILE_FAILED, FAILED, UNKNOWN
|
||||
UPLOADED, INCORRECT_FILE_FAILED, FAILED, INDETERMINATE, UNKNOWN
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue