mirror of
https://github.com/developersu/ns-usbloader.git
synced 2025-05-14 15:15:05 -04:00
v0.3 UI is ready
This commit is contained in:
parent
9bf4a76076
commit
31045683e4
12 changed files with 206 additions and 60 deletions
|
@ -5,6 +5,7 @@ import javafx.fxml.Initializable;
|
|||
import javafx.scene.control.CheckBox;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.VBox;
|
||||
import nsusbloader.AppPreferences;
|
||||
|
||||
|
||||
|
@ -16,30 +17,36 @@ public class SettingsController implements Initializable {
|
|||
@FXML
|
||||
private CheckBox validateNSHostNameCb;
|
||||
@FXML
|
||||
private TextField pcIpTextField;
|
||||
@FXML
|
||||
private CheckBox expertModeCb;
|
||||
@FXML
|
||||
private Label hostIpLbl;
|
||||
private CheckBox autoDetectIpCb;
|
||||
@FXML
|
||||
private CheckBox randPortCb;
|
||||
|
||||
@FXML
|
||||
private TextField pcIpTextField;
|
||||
@FXML
|
||||
private TextField pcPortTextField;
|
||||
@FXML
|
||||
private TextField pcPostfixTextField;
|
||||
|
||||
@FXML
|
||||
private CheckBox dontServeCb;
|
||||
|
||||
@FXML
|
||||
private VBox expertSettingsVBox;
|
||||
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
validateNSHostNameCb.setSelected(AppPreferences.getInstance().getNsIpValidationNeeded());
|
||||
|
||||
if (AppPreferences.getInstance().getExpertMode()) {
|
||||
expertModeCb.setSelected(true);
|
||||
hostIpLbl.setVisible(true);
|
||||
pcIpTextField.setVisible(true);
|
||||
}
|
||||
expertSettingsVBox.setDisable(AppPreferences.getInstance().getExpertMode());
|
||||
|
||||
expertModeCb.setOnAction(e->{
|
||||
if (expertModeCb.isSelected()){
|
||||
hostIpLbl.setVisible(true);
|
||||
pcIpTextField.setVisible(true);
|
||||
}
|
||||
else {
|
||||
hostIpLbl.setVisible(false);
|
||||
pcIpTextField.setVisible(false);
|
||||
}
|
||||
if (expertModeCb.isSelected())
|
||||
expertSettingsVBox.setDisable(false);
|
||||
else
|
||||
expertSettingsVBox.setDisable(true);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package nsusbloader.USB;
|
||||
package nsusbloader.ModelControllers;
|
||||
|
||||
import nsusbloader.NSLDataTypes.EFileStatus;
|
||||
import nsusbloader.NSLDataTypes.EMsgType;
|
||||
|
@ -14,7 +14,7 @@ public class LogPrinter {
|
|||
private BlockingQueue<Double> progressQueue;
|
||||
private HashMap<String, EFileStatus> statusMap; // BlockingQueue for literally one object. TODO: read more books ; replace to hashMap
|
||||
|
||||
LogPrinter(){
|
||||
public LogPrinter(){
|
||||
this.msgQueue = new LinkedBlockingQueue<>();
|
||||
this.progressQueue = new LinkedBlockingQueue<>();
|
||||
this.statusMap = new HashMap<>();
|
||||
|
@ -53,11 +53,22 @@ public class LogPrinter {
|
|||
progressQueue.put(value);
|
||||
}
|
||||
/**
|
||||
* When we're done
|
||||
* When we're done - update status
|
||||
* */
|
||||
public void updateAndClose(HashMap<String, File> nspMap, EFileStatus status){
|
||||
for (String fileName: nspMap.keySet())
|
||||
statusMap.put(fileName, status);
|
||||
public void update(HashMap<String, File> nspMap, EFileStatus status){
|
||||
for (File file: nspMap.values())
|
||||
statusMap.putIfAbsent(file.getName(), status);
|
||||
}
|
||||
/**
|
||||
* When we're done - update status
|
||||
* */
|
||||
public void update(File file, EFileStatus status){
|
||||
statusMap.putIfAbsent(file.getName(), status);
|
||||
}
|
||||
/**
|
||||
* When we're done - close it
|
||||
* */
|
||||
public void close(){
|
||||
msgConsumer.interrupt();
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package nsusbloader.USB;
|
||||
package nsusbloader.ModelControllers;
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import javafx.scene.control.ProgressBar;
|
|
@ -1,15 +1,13 @@
|
|||
package nsusbloader.NET;
|
||||
|
||||
import javafx.concurrent.Task;
|
||||
import nsusbloader.NSLMain;
|
||||
import nsusbloader.NSLDataTypes.EFileStatus;
|
||||
import nsusbloader.ModelControllers.LogPrinter;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
@ -19,6 +17,8 @@ import java.util.List;
|
|||
|
||||
public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
||||
|
||||
private LogPrinter logPrinter;
|
||||
|
||||
private String hostIP;
|
||||
private int hostPort;
|
||||
private String switchIP;
|
||||
|
@ -29,6 +29,7 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
|||
private ServerSocket serverSocket;
|
||||
|
||||
public NETCommunications(List<File> filesList, String switchIP){
|
||||
this.logPrinter = new LogPrinter();
|
||||
this.switchIP = switchIP;
|
||||
this.nspMap = new HashMap<>();
|
||||
try {
|
||||
|
@ -117,6 +118,8 @@ write in first 4 bytes
|
|||
String line;
|
||||
LinkedList<String> tcpPackeet = new LinkedList<>();
|
||||
while ((line = br.readLine()) != null) {
|
||||
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
|
||||
|
@ -132,22 +135,56 @@ write in first 4 bytes
|
|||
catch (IOException ioe){
|
||||
ioe.printStackTrace(); // TODO: fix
|
||||
}
|
||||
|
||||
logPrinter.update(nspMap, EFileStatus.UNKNOWN);
|
||||
logPrinter.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
// 200 206 400 (inv range) 404
|
||||
// 200 206 400 (inv range) 404 416 (416 Range Not Satisfiable )
|
||||
private void handleRequest(LinkedList<String> packet, PrintWriter pw){
|
||||
File requestedFile;
|
||||
if (packet.get(0).startsWith("HEAD")){
|
||||
File requestedFile = nspMap.get(packet.get(0).replaceAll("(^[A-z\\s]+/)|(\\s+?.*$)", ""));
|
||||
requestedFile = nspMap.get(packet.get(0).replaceAll("(^[A-z\\s]+/)|(\\s+?.*$)", ""));
|
||||
if (requestedFile == null || !requestedFile.exists()){
|
||||
return; //todo: send 404
|
||||
pw.write(NETPacket.getCode404());
|
||||
pw.flush();
|
||||
logPrinter.update(requestedFile, EFileStatus.FAILED);
|
||||
}
|
||||
else {
|
||||
pw.write(NETPacket.getCode200(requestedFile.length()));
|
||||
pw.flush();
|
||||
System.out.println(requestedFile.getAbsolutePath()+"\n"+NETPacket.getCode200(requestedFile.length()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (packet.get(0).startsWith("GET")) {
|
||||
requestedFile = nspMap.get(packet.get(0).replaceAll("(^[A-z\\s]+/)|(\\s+?.*$)", ""));
|
||||
if (requestedFile == null || !requestedFile.exists()){
|
||||
pw.write(NETPacket.getCode404());
|
||||
pw.flush();
|
||||
logPrinter.update(requestedFile, EFileStatus.FAILED);
|
||||
}
|
||||
else {
|
||||
for (String line: packet)
|
||||
if (line.toLowerCase().startsWith("range")){
|
||||
String[] rangeStr = line.toLowerCase().replaceAll("^range:\\s+?bytes=", "").split("-", 2);
|
||||
if (!rangeStr[0].isEmpty() && !rangeStr[1].isEmpty()){ // If both ranges defined: Read requested
|
||||
|
||||
}
|
||||
else if (!rangeStr[0].isEmpty()){ // If only START defined: Read all
|
||||
|
||||
}
|
||||
else if (!rangeStr[1].isEmpty()){ // If only END defined: Try to read last 500 bytes
|
||||
|
||||
}
|
||||
else {
|
||||
pw.write(NETPacket.getCode400());
|
||||
pw.flush();
|
||||
logPrinter.update(requestedFile, EFileStatus.FAILED);
|
||||
//logPrinter.print(); // TODO: INFORM
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,12 +25,40 @@ public class NETPacket {
|
|||
"Content-Range: bytes %d-%d/%d\r\n"+
|
||||
"Content-Length: %d\r\n"+
|
||||
"Last-Modified: Mon, 18 Mar 2019 12:57:33 GMT\r\n\r\n";
|
||||
|
||||
private static final String CODE_400 =
|
||||
"HTTP/1.0 400 invalid range\r\n"+
|
||||
"Server: NS-USBloader-"+NSLMain.appVersion+"\r\n" +
|
||||
"Date: %s\r\n" +
|
||||
"Connection: close\r\n"+
|
||||
"Content-Type: text/html;charset=utf-8\r\n"+
|
||||
"Content-Length: 0\r\b\r\n";
|
||||
private static final String CODE_404 =
|
||||
"HTTP/1.0 404 Not Found\r\n"+
|
||||
"Server: NS-USBloader-"+NSLMain.appVersion+"\r\n" +
|
||||
"Date: %s\r\n" +
|
||||
"Connection: close\r\n"+
|
||||
"Content-Type: text/html;charset=utf-8\r\n"+
|
||||
"Content-Length: 0\r\b\r\n";
|
||||
private static final String CODE_416 =
|
||||
"HTTP/1.0 416 Requested Range Not Satisfiable\r\n"+
|
||||
"Server: NS-USBloader-"+NSLMain.appVersion+"\r\n" +
|
||||
"Date: %s\r\n" +
|
||||
"Connection: close\r\n"+
|
||||
"Content-Type: text/html;charset=utf-8\r\n"+
|
||||
"Content-Length: 0\r\b\r\n";
|
||||
public static String getCode200(long nspFileSize){
|
||||
return String.format(CODE_200, ZonedDateTime.now(ZoneId.of("GMT")).format(DateTimeFormatter.RFC_1123_DATE_TIME), nspFileSize-1, nspFileSize, nspFileSize);
|
||||
}
|
||||
public static String getCode206(long nspFileSize, long startPos, long endPos){
|
||||
return String.format(CODE_206, ZonedDateTime.now(ZoneId.of("GMT")).format(DateTimeFormatter.RFC_1123_DATE_TIME), startPos, endPos, nspFileSize, endPos-startPos+1);
|
||||
}
|
||||
|
||||
public static String getCode404(){
|
||||
return String.format(CODE_404, ZonedDateTime.now(ZoneId.of("GMT")).format(DateTimeFormatter.RFC_1123_DATE_TIME));
|
||||
}
|
||||
public static String getCode416(){
|
||||
return String.format(CODE_416, ZonedDateTime.now(ZoneId.of("GMT")).format(DateTimeFormatter.RFC_1123_DATE_TIME));
|
||||
}
|
||||
public static String getCode400(){
|
||||
return String.format(CODE_400, ZonedDateTime.now(ZoneId.of("GMT")).format(DateTimeFormatter.RFC_1123_DATE_TIME));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package nsusbloader.USB.PFS;
|
||||
|
||||
import nsusbloader.USB.LogPrinter;
|
||||
import nsusbloader.ModelControllers.LogPrinter;
|
||||
import nsusbloader.NSLDataTypes.EMsgType;
|
||||
import nsusbloader.ServiceWindow;
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package nsusbloader.USB;
|
||||
|
||||
import javafx.concurrent.Task;
|
||||
import nsusbloader.ModelControllers.LogPrinter;
|
||||
import nsusbloader.NSLDataTypes.EFileStatus;
|
||||
import nsusbloader.NSLDataTypes.EMsgType;
|
||||
import nsusbloader.USB.PFS.PFSProvider;
|
||||
|
@ -707,7 +708,8 @@ public class UsbCommunications extends Task<Void> {
|
|||
}
|
||||
|
||||
// Report status and close
|
||||
logPrinter.updateAndClose(nspMap, status);
|
||||
logPrinter.update(nspMap, status);
|
||||
logPrinter.close();
|
||||
}
|
||||
/**
|
||||
* Sending any byte array to USB device
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue