Frontend complete

This commit is contained in:
Dmitry Isaenko 2019-03-19 17:44:39 +03:00
parent 863c69affa
commit 65f084591a
5 changed files with 130 additions and 107 deletions

View file

@ -2,6 +2,7 @@ package nsusbloader.Controllers;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.*; import javafx.scene.control.*;
@ -20,7 +21,6 @@ import nsusbloader.USB.UsbCommunications;
import java.io.File; import java.io.File;
import java.net.*; import java.net.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -54,7 +54,7 @@ public class NSLMainController implements Initializable {
@FXML @FXML
private Label nsIpLbl; private Label nsIpLbl;
private UsbCommunications usbCommunications; private Task<Void> usbNetCommunications;
private Thread workThread; private Thread workThread;
private String previouslyOpenedPath; private String previouslyOpenedPath;
@ -200,8 +200,8 @@ public class NSLMainController implements Initializable {
for (File item: nspToUpload) for (File item: nspToUpload)
logArea.appendText(" "+item.getAbsolutePath()+"\n"); logArea.appendText(" "+item.getAbsolutePath()+"\n");
} }
usbCommunications = new UsbCommunications(nspToUpload, choiceProtocol.getSelectionModel().getSelectedItem()); usbNetCommunications = new UsbCommunications(nspToUpload, choiceProtocol.getSelectionModel().getSelectedItem());
workThread = new Thread(usbCommunications); workThread = new Thread(usbNetCommunications);
workThread.setDaemon(true); workThread.setDaemon(true);
workThread.start(); workThread.start();
} }
@ -212,7 +212,6 @@ public class NSLMainController implements Initializable {
String nsIP = nsIpTextField.getText(); String nsIP = nsIpTextField.getText();
List<File> nspToUpload; List<File> nspToUpload;
if (!SettingsTabController.getExpertModeSelected()) {
if ((nspToUpload = tableFilesListController.getFilesForUpload()) == null) { if ((nspToUpload = tableFilesListController.getFilesForUpload()) == null) {
logArea.setText(resourceBundle.getString("logsNoFolderFileSelected")); logArea.setText(resourceBundle.getString("logsNoFolderFileSelected"));
return; return;
@ -222,15 +221,23 @@ public class NSLMainController implements Initializable {
for (File item: nspToUpload) for (File item: nspToUpload)
logArea.appendText(" "+item.getAbsolutePath()+"\n"); logArea.appendText(" "+item.getAbsolutePath()+"\n");
} }
NETCommunications netCommunications = new NETCommunications(nspToUpload, nsIP); // TODO: move somewhere if (!SettingsTabController.getExpertModeSelected())
workThread = new Thread(netCommunications); 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.setDaemon(true);
workThread.start(); workThread.start();
} }
else {
// TODO; pass to another constructor
}
}
} }
} }
/** /**
@ -238,7 +245,7 @@ public class NSLMainController implements Initializable {
* */ * */
private void stopBtnAction(){ private void stopBtnAction(){
if (workThread != null && workThread.isAlive()){ 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
} }
} }
/** /**

View file

@ -50,11 +50,14 @@ public class NSLRowModel {
case FAILED: case FAILED:
this.status = "Failed"; this.status = "Failed";
break; break;
case UNKNOWN: case INDETERMINATE:
this.status = "..."; this.status = "...";
break; break;
case UNKNOWN:
this.status = "Unknown";
break;
case INCORRECT_FILE_FAILED: case INCORRECT_FILE_FAILED:
this.status = "Failed: Incorrect file"; this.status = "Failed: Bad file";
markForUpload = false; markForUpload = false;
break; break;
} }

View file

@ -225,7 +225,7 @@ public class NSTableViewController implements Initializable {
for (NSLRowModel model: rowsObsLst){ for (NSLRowModel model: rowsObsLst){
if (model.isMarkForUpload()){ if (model.isMarkForUpload()){
files.add(model.getNspFile()); files.add(model.getNspFile());
model.setStatus(EFileStatus.UNKNOWN); model.setStatus(EFileStatus.INDETERMINATE);
} }
} }
if (!files.isEmpty()) { if (!files.isEmpty()) {

View file

@ -10,9 +10,6 @@ import java.net.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
/*
* Add option: don't serve replies
* */
public class NETCommunications extends Task<Void> { // todo: thows IOException? 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 * Simple constructor that everybody uses
* */ * */
public NETCommunications(List<File> filesList, String switchIP){ public NETCommunications(List<File> filesList, String switchIP, boolean doNotServeRequests, String hostIPaddr, String hostPortNum, String extras){
this.doNotServeRequests = false; this.doNotServeRequests = doNotServeRequests;
if (doNotServeRequests)
this.extras = extras;
else
this.extras = ""; this.extras = "";
this.logPrinter = new LogPrinter();
this.switchIP = switchIP; this.switchIP = switchIP;
this.logPrinter = new LogPrinter();
this.nspMap = new HashMap<>(); this.nspMap = new HashMap<>();
logPrinter.print("\tPrepare chain", EMsgType.INFO);
// Collect and encode NSP files list // Collect and encode NSP files list
try { try {
for (File nspFile : filesList) for (File nspFile : filesList)
@ -47,39 +46,37 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
} }
catch (UnsupportedEncodingException uee){ catch (UnsupportedEncodingException uee){
isValid = false; 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) for (File nspFile : filesList)
nspMap.put(nspFile.getName(), nspFile); nspMap.put(nspFile.getName(), nspFile);
close(EFileStatus.FAILED); close(EFileStatus.FAILED);
return; return;
} }
// Resolve IP // Resolve IP
if (hostIPaddr.isEmpty()) {
DatagramSocket socket; DatagramSocket socket;
try { // todo: check other method if internet unavaliable try { // todo: check other method if internet unavaliable
socket = new DatagramSocket(); socket = new DatagramSocket();
socket.connect(InetAddress.getByName("8.8.8.8"), 10002); // Google socket.connect(InetAddress.getByName("8.8.8.8"), 10002); // Google
hostIP = socket.getLocalAddress().getHostAddress(); hostIP = socket.getLocalAddress().getHostAddress();
socket.close(); socket.close();
} } catch (SocketException | UnknownHostException e) {
catch (SocketException | UnknownHostException e){ logPrinter.print("NET: Can't get your computer IP using Google DNS server. Returned:\n\t"+e.getMessage(), EMsgType.INFO);
logPrinter.print("NET: Can't get your computer IP using Google DNS server", EMsgType.INFO);
try { try {
socket = new DatagramSocket(); socket = new DatagramSocket();
socket.connect(InetAddress.getByName("193.0.14.129"), 10002); // RIPE NCC socket.connect(InetAddress.getByName("193.0.14.129"), 10002); // RIPE NCC
hostIP = socket.getLocalAddress().getHostAddress(); hostIP = socket.getLocalAddress().getHostAddress();
socket.close(); socket.close();
} } catch (SocketException | UnknownHostException e1) {
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);
logPrinter.print("NET: Can't get your computer IP using RIPE NCC root server", EMsgType.INFO);
try { try {
socket = new DatagramSocket(); socket = new DatagramSocket();
socket.connect(InetAddress.getByName("people.com.cn"), 10002); // Renmin Ribao socket.connect(InetAddress.getByName("people.com.cn"), 10002); // Renmin Ribao
hostIP = socket.getLocalAddress().getHostAddress(); hostIP = socket.getLocalAddress().getHostAddress();
socket.close(); socket.close();
} } catch (SocketException | UnknownHostException e2) {
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("NET: Can't get your computer IP using Renmin Ribao server", EMsgType.FAIL); logPrinter.print("Try using 'Expert mode' and set IP manually.", EMsgType.INFO);
logPrinter.print("Try using 'Expert mode' and set IP by yourself", EMsgType.INFO);
try { try {
Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) { while (enumeration.hasMoreElements()) {
@ -90,8 +87,8 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
logPrinter.print("Check for: " + i.getHostAddress(), EMsgType.INFO); logPrinter.print("Check for: " + i.getHostAddress(), EMsgType.INFO);
} }
} }
} catch (SocketException socketException) { } catch (SocketException socketException) { // Good block.
logPrinter.print("Can't determine possible variants.", EMsgType.FAIL); logPrinter.print("Can't determine possible variants. Returned:\n\t"+socketException.getMessage(), EMsgType.FAIL);
} }
isValid = false; isValid = false;
close(EFileStatus.FAILED); 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); 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(); Random portRandomizer = new Random();
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
try { 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 serverSocket = new ServerSocket(hostPort); //System.out.println(serverSocket.getInetAddress()); 0.0.0.0
logPrinter.print("NET: Your port detected as: " + hostPort, EMsgType.PASS); logPrinter.print("NET: Your port detected as: " + hostPort, EMsgType.PASS);
break; break;
} } catch (IOException ioe) {
catch (IOException ioe){
if (i == 4) { if (i == 4) {
logPrinter.print("NET: Can't find good port", EMsgType.FAIL); logPrinter.print("NET: Can't find good port", EMsgType.FAIL);
logPrinter.print("Try using 'Expert mode' and set port by yourself.", EMsgType.INFO); logPrinter.print("Try using 'Expert mode' and set port by yourself.", EMsgType.INFO);
isValid = false; isValid = false;
close(EFileStatus.FAILED); close(EFileStatus.FAILED);
return; return;
} } else
else
logPrinter.print("NET: Can't use port " + hostPort + "\nLooking for another one.", EMsgType.WARNING); 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; 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){ @Override
this.doNotServeRequests = doNotServeRequests; protected void cancelled() {
if (doNotServeRequests) this.close(EFileStatus.UNKNOWN);
this.extras = extras; super.cancelled();
else
this.extras = "";
this.switchIP = switchIP;
} }
/* /*
Replace everything to ASCII (WEB representation) Replace everything to ASCII (WEB representation)
calculate calculate
@ -142,16 +153,10 @@ write in first 4 bytes
* */ * */
@Override @Override
protected Void call() { protected Void call() {
if (!isValid) if (!isValid | isCancelled())
return null; return null;
if (isCancelled()){
logPrinter.print("NET: interrupted by user", EMsgType.INFO);
close(EFileStatus.FAILED);
return null;
}
logPrinter.print("\tStart chain", EMsgType.INFO); logPrinter.print("\tStart chain", EMsgType.INFO);
// Get files list length // Create string that we'll send to TF and which initiates chain
StringBuilder myStrBuilder; StringBuilder myStrBuilder;
myStrBuilder = new StringBuilder(); myStrBuilder = new StringBuilder();
@ -179,9 +184,17 @@ write in first 4 bytes
handShakeSocket.close(); handShakeSocket.close();
} }
catch (IOException uhe){ 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; 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 // Go transfer
try { try {
@ -198,8 +211,8 @@ write in first 4 bytes
String line; String line;
LinkedList<String> tcpPackeet = new LinkedList<>(); LinkedList<String> tcpPackeet = new LinkedList<>();
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
if (isCancelled()) // TODO: notice everywhere // if (isCancelled()) // TODO: notice everywhere
break; // break;
System.out.println(line); // TODO: remove DBG System.out.println(line); // TODO: remove DBG
if (line.trim().isEmpty()) { // If TCP packet is ended if (line.trim().isEmpty()) { // If TCP packet is ended
handleRequest(tcpPackeet, pw); // Proceed required things handleRequest(tcpPackeet, pw); // Proceed required things
@ -210,13 +223,12 @@ write in first 4 bytes
} }
System.out.println("\nDone!"); // reopen client sock System.out.println("\nDone!"); // reopen client sock
clientSocket.close(); clientSocket.close();
serverSocket.close();
} }
catch (IOException ioe){ catch (IOException ioe){
ioe.printStackTrace(); // TODO: fix System.out.println("client sock closed");
} }
logPrinter.update(nspMap, EFileStatus.UNKNOWN); if (!isCancelled())
logPrinter.close(); close(EFileStatus.UNKNOWN);
return null; return null;
} }
@ -271,11 +283,12 @@ write in first 4 bytes
* Close when done * Close when done
* */ * */
private void close(EFileStatus status){ private void close(EFileStatus status){
System.out.println("called");
try { try {
serverSocket.close(); serverSocket.close();
logPrinter.print("NET: Closing server socket.", EMsgType.PASS); 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); logPrinter.print("NET: Closing server socket failed. Sometimes it's not an issue.", EMsgType.WARNING);
} }
if (status != null) { if (status != null) {

View file

@ -1,5 +1,5 @@
package nsusbloader.NSLDataTypes; package nsusbloader.NSLDataTypes;
public enum EFileStatus { public enum EFileStatus {
UPLOADED, INCORRECT_FILE_FAILED, FAILED, UNKNOWN UPLOADED, INCORRECT_FILE_FAILED, FAILED, INDETERMINATE, UNKNOWN
} }