Backend complete. v0.3 ready for QA.
This commit is contained in:
parent
65f084591a
commit
53a8a2337f
9 changed files with 132 additions and 74 deletions
|
@ -114,7 +114,9 @@ public class SettingsController implements Initializable {
|
|||
}));
|
||||
pcPortTextField.setTextFormatter(new TextFormatter<Object>(change -> {
|
||||
if (change.getControlNewText().matches("^[0-9]{0,5}$")) {
|
||||
if ((Integer.parseInt(change.getControlNewText()) > 65535) || (Integer.parseInt(change.getControlNewText()) == 0)) {
|
||||
if (!change.getControlNewText().isEmpty()
|
||||
&& ((Integer.parseInt(change.getControlNewText()) > 65535) || (Integer.parseInt(change.getControlNewText()) == 0))
|
||||
) {
|
||||
ServiceWindow.getErrorNotification(resourceBundle.getString("windowTitleErrorPort"), resourceBundle.getString("windowBodyErrorPort"));
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -27,6 +27,9 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
|
|||
|
||||
private boolean isValid;
|
||||
private boolean doNotServeRequests;
|
||||
|
||||
private OutputStream currSockOS;
|
||||
private PrintWriter currSockPW;
|
||||
/**
|
||||
* Simple constructor that everybody uses
|
||||
* */
|
||||
|
@ -197,93 +200,154 @@ write in first 4 bytes
|
|||
logPrinter.print("NET: Initiation files list has been sent to NS.", EMsgType.PASS);
|
||||
|
||||
// Go transfer
|
||||
Socket clientSocket;
|
||||
System.out.println("0");
|
||||
work_routine:
|
||||
while (true){
|
||||
try {
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
System.out.println("0.5");
|
||||
clientSocket = serverSocket.accept();
|
||||
System.out.println("1");
|
||||
BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(clientSocket.getInputStream())
|
||||
);
|
||||
|
||||
InputStream is = clientSocket.getInputStream();
|
||||
InputStreamReader isr = new InputStreamReader(is);
|
||||
BufferedReader br = new BufferedReader(isr);
|
||||
|
||||
OutputStream os = clientSocket.getOutputStream();
|
||||
OutputStreamWriter osr = new OutputStreamWriter(os);
|
||||
PrintWriter pw = new PrintWriter(osr);
|
||||
currSockOS = clientSocket.getOutputStream();
|
||||
currSockPW = new PrintWriter(new OutputStreamWriter(currSockOS));
|
||||
|
||||
String line;
|
||||
LinkedList<String> tcpPackeet = new LinkedList<>();
|
||||
LinkedList<String> tcpPacket = 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
|
||||
tcpPackeet.clear(); // Clear data and wait for next TCP packet
|
||||
if (handleRequest(tcpPacket)) // Proceed required things
|
||||
break work_routine;
|
||||
tcpPacket.clear(); // Clear data and wait for next TCP packet
|
||||
}
|
||||
else
|
||||
tcpPackeet.add(line); // Otherwise collect data
|
||||
tcpPacket.add(line); // Otherwise collect data
|
||||
}
|
||||
System.out.println("\nDone!"); // reopen client sock
|
||||
System.out.println("\t\tPacket covered!\n"); // reopen client sock
|
||||
clientSocket.close();
|
||||
}
|
||||
catch (IOException ioe){
|
||||
System.out.println("client sock closed");
|
||||
catch (IOException ioe){ // If server socket closed, then client socket also closed.
|
||||
System.out.println("2");
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println("3");
|
||||
if (!isCancelled())
|
||||
close(EFileStatus.UNKNOWN);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 200 206 400 (inv range) 404 416 (416 Range Not Satisfiable )
|
||||
private void handleRequest(LinkedList<String> packet, PrintWriter pw){
|
||||
// 200 206 400 (inv range) 404 416 (Range Not Satisfiable )
|
||||
/**
|
||||
* Handle requests
|
||||
* @return true if failed
|
||||
* */
|
||||
private boolean handleRequest(LinkedList<String> packet){
|
||||
//private boolean handleRequest(LinkedList<String> packet, OutputStreamWriter pw){
|
||||
File requestedFile;
|
||||
if (packet.get(0).startsWith("HEAD")){
|
||||
requestedFile = nspMap.get(packet.get(0).replaceAll("(^[A-z\\s]+/)|(\\s+?.*$)", ""));
|
||||
if (requestedFile == null || !requestedFile.exists()){
|
||||
pw.write(NETPacket.getCode404());
|
||||
pw.flush();
|
||||
if (!requestedFile.exists() || requestedFile.length() == 0){ // well.. tell 404 if file exists with 0 length is against standard, but saves time
|
||||
currSockPW.write(NETPacket.getCode404());
|
||||
currSockPW.flush();
|
||||
logPrinter.print("NET: File "+requestedFile.getName()+" doesn't exists or have 0 size. Returning 404", EMsgType.FAIL);
|
||||
logPrinter.update(requestedFile, EFileStatus.FAILED);
|
||||
return true;
|
||||
}
|
||||
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("HEAD")){
|
||||
currSockPW.write(NETPacket.getCode200(requestedFile.length()));
|
||||
currSockPW.flush();
|
||||
logPrinter.print("NET: Replying for requested file: "+requestedFile.getName(), EMsgType.INFO);
|
||||
return false;
|
||||
}
|
||||
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")){
|
||||
for (String line: packet) {
|
||||
if (line.toLowerCase().startsWith("range")) { //todo: fix
|
||||
try {
|
||||
String[] rangeStr = line.toLowerCase().replaceAll("^range:\\s+?bytes=", "").split("-", 2);
|
||||
if (!rangeStr[0].isEmpty() && !rangeStr[1].isEmpty()){ // If both ranges defined: Read requested
|
||||
if (!rangeStr[0].isEmpty() && !rangeStr[1].isEmpty()) { // If both ranges defined: Read requested
|
||||
if (Long.parseLong(rangeStr[0]) > Long.parseLong(rangeStr[1])){ // If start bytes greater then end bytes
|
||||
currSockPW.write(NETPacket.getCode400());
|
||||
currSockPW.flush();
|
||||
logPrinter.print("NET: Requested range for "+requestedFile.getName()+" is incorrect. Returning 400", EMsgType.FAIL);
|
||||
logPrinter.update(requestedFile, EFileStatus.FAILED);
|
||||
return true;
|
||||
}
|
||||
if (writeToSocket(requestedFile, Long.parseLong(rangeStr[0]), Long.parseLong(rangeStr[1]))) // DO WRITE
|
||||
return true;
|
||||
|
||||
}
|
||||
else if (!rangeStr[0].isEmpty()){ // If only START defined: Read all
|
||||
|
||||
else if (!rangeStr[0].isEmpty()) { // If only START defined: Read all
|
||||
if (writeToSocket(requestedFile, Long.parseLong(rangeStr[0]), requestedFile.length())) // DO WRITE
|
||||
return true;
|
||||
}
|
||||
else if (!rangeStr[1].isEmpty()) { // If only END defined: Try to read last 500 bytes
|
||||
if (requestedFile.length() > 500){
|
||||
if (writeToSocket(requestedFile, requestedFile.length()-500, requestedFile.length())) // DO WRITE
|
||||
return true;
|
||||
}
|
||||
else { // If file smaller than 500 bytes
|
||||
currSockPW.write(NETPacket.getCode416());
|
||||
currSockPW.flush();
|
||||
logPrinter.print("NET: File size requested for "+requestedFile.getName()+" while actual size of it: "+requestedFile.length()+". Returning 416", EMsgType.FAIL);
|
||||
logPrinter.update(requestedFile, EFileStatus.FAILED);
|
||||
return true;
|
||||
}
|
||||
else if (!rangeStr[1].isEmpty()){ // If only END defined: Try to read last 500 bytes
|
||||
|
||||
}
|
||||
else {
|
||||
pw.write(NETPacket.getCode400());
|
||||
pw.flush();
|
||||
currSockPW.write(NETPacket.getCode400()); // If Range not defined: like "Range: bytes=-"
|
||||
currSockPW.flush();
|
||||
logPrinter.print("NET: Requested range for "+requestedFile.getName()+" is incorrect (empty start & end). Returning 400", EMsgType.FAIL);
|
||||
logPrinter.update(requestedFile, EFileStatus.FAILED);
|
||||
//logPrinter.print(); // TODO: INFORM
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch (NumberFormatException nfe){
|
||||
currSockPW.write(NETPacket.getCode400());
|
||||
currSockPW.flush();
|
||||
logPrinter.print("NET: Requested range for "+requestedFile.getName()+" has incorrect format. Returning 400\n\t"+nfe.getMessage(), EMsgType.FAIL);
|
||||
logPrinter.update(requestedFile, EFileStatus.FAILED);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Send files.
|
||||
* */
|
||||
private boolean writeToSocket(File file, long start, long end){
|
||||
currSockPW.write(NETPacket.getCode206(file.length(), start, end));
|
||||
currSockPW.flush();
|
||||
try{
|
||||
long count = end - start;
|
||||
|
||||
RandomAccessFile raf = new RandomAccessFile(file, "r");
|
||||
raf.seek(start);
|
||||
for (int i=0; i <= count; i++)
|
||||
currSockOS.write(raf.read());
|
||||
currSockOS.flush();
|
||||
raf.close();
|
||||
}
|
||||
catch (IOException ioe){
|
||||
logPrinter.print("IO Exception:\n "+ioe.getMessage(), EMsgType.FAIL);
|
||||
ioe.printStackTrace();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Close when done
|
||||
* */
|
||||
private void close(EFileStatus status){
|
||||
System.out.println("called");
|
||||
if (isCancelled())
|
||||
logPrinter.print("NET: Interrupted by user.", EMsgType.INFO);
|
||||
try {
|
||||
serverSocket.close();
|
||||
logPrinter.print("NET: Closing server socket.", EMsgType.PASS);
|
||||
|
|
|
@ -31,21 +31,21 @@ public class NETPacket {
|
|||
"Date: %s\r\n" +
|
||||
"Connection: close\r\n"+
|
||||
"Content-Type: text/html;charset=utf-8\r\n"+
|
||||
"Content-Length: 0\r\b\r\n";
|
||||
"Content-Length: 0\r\n\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";
|
||||
"Content-Length: 0\r\n\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";
|
||||
"Content-Length: 0\r\n\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);
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
<ChoiceBox fx:id="choiceProtocol" prefWidth="120.0" />
|
||||
<ChoiceBox fx:id="choiceNetUsb" prefWidth="75.0" />
|
||||
<Label fx:id="nsIpLbl" text="%NSIPlable" visible="false" />
|
||||
<TextField fx:id="nsIpTextField" prefWidth="135.0" promptText="XXX,XXX,XXX,XXX" visible="false" />
|
||||
<TextField fx:id="nsIpTextField" prefWidth="135.0" promptText="XXX.XXX.XXX.XXX" visible="false" />
|
||||
<Pane HBox.hgrow="ALWAYS" />
|
||||
<Button fx:id="switchThemeBtn" mnemonicParsing="false" />
|
||||
</items>
|
||||
|
@ -84,7 +84,7 @@
|
|||
</VBox>
|
||||
</content>
|
||||
<graphic>
|
||||
<SVGPath content="M19,3H14.82C14.4,1.84 13.3,1 12,1C10.7,1 9.6,1.84 9.18,3H5A2,2 0 0,0 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5A2,2 0 0,0 19,3M12,3A1,1 0 0,1 13,4A1,1 0 0,1 12,5A1,1 0 0,1 11,4A1,1 0 0,1 12,3M7,7H17V5H19V19H5V5H7V7Z" />
|
||||
<SVGPath content="M9,22A1,1 0 0,1 8,21V18H4A2,2 0 0,1 2,16V4C2,2.89 2.9,2 4,2H20A2,2 0 0,1 22,4V16A2,2 0 0,1 20,18H13.9L10.2,21.71C10,21.9 9.75,22 9.5,22V22H9M10,16V19.08L13.08,16H20V4H4V16H10M17,11H15V9H17V11M13,11H11V9H13V11M9,11H7V9H9V11Z" />
|
||||
</graphic>
|
||||
</Tab>
|
||||
</tabs>
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
</HBox>
|
||||
<HBox>
|
||||
<children>
|
||||
<TextField fx:id="pcIpTextField" promptText="XXX,XXX,XXX,XXX" />
|
||||
<TextField fx:id="pcIpTextField" promptText="XXX.XXX.XXX.XXX" />
|
||||
<Label text=":" />
|
||||
<TextField fx:id="pcPortTextField" promptText="0-65535" />
|
||||
<Label text="/" />
|
||||
|
|
|
@ -27,8 +27,6 @@ netTabValidateNSHostNameCb=Always validate NS IP input.
|
|||
windowBodyBadIp=Are you sure that you entered NS IP address correctly?
|
||||
windowTitleBadIp=IP address of NS most likely incorrect
|
||||
netTabExpertModeCb=Expert mode
|
||||
windowTitleExpectModeIncomplete=Check your settings
|
||||
windowBodyExpectModeIncomplete=You selected 'Expert Mode' in settings and have mistakes in parameters\nDouble check if settings valid.
|
||||
netTabHostPortLbl=port
|
||||
netTabAutoDetectIpCb=Auto-detect IP
|
||||
netTabRandSelectPortCb=Randomly get port
|
||||
|
|
|
@ -20,14 +20,12 @@ tableFileNameLbl=Nom de fichier
|
|||
tableStatusLbl=Statut
|
||||
contextMenuBtnDelete=Supprimer
|
||||
contextMenuBtnDeleteAll=Supprimer tout
|
||||
netTabHostIPLbl=IP de l'ordinateur <-FIX
|
||||
NSIPlable=IP de NS:
|
||||
netTabHostIPLbl=IP de l'ordinateur <-FIX?
|
||||
NSIPlable=IP de NS: <-FIX?
|
||||
netTabValidateNSHostNameCb=Always validate NS IP input. <-FIX
|
||||
windowTitleBadIp=IP address of NS most likely incorrect <-FIX
|
||||
windowBodyBadIp=Are you sure that you entered NS IP address correctly? <-FIX
|
||||
netTabExpertModeCb=Expert mode<- FIX
|
||||
windowTitleExpectModeIncomplete=Check your settings <- FIX
|
||||
windowBodyExpectModeIncomplete=You selected 'Expert Mode' in settings and have mistakes in parameters\nDouble check if settings valid. <- FIX
|
||||
netTabHostPortLbl=port <- fix
|
||||
netTabAutoDetectIpCb=Auto-detect IP <- FIX
|
||||
netTabRandSelectPortCb=Randomly get port <- FIX
|
||||
|
|
|
@ -28,8 +28,6 @@ netTabValidateNSHostNameCb=\u0412\u0441\u0435\u0433\u0434\u0430 \u043F\u0440\u04
|
|||
windowTitleBadIp=IP \u0430\u0434\u0440\u0435\u0441 NS \u043F\u043E\u0445\u043E\u0436\u0435 \u043D\u0435\u043F\u0440\u0430\u0432\u0438\u043B\u044C\u043D\u044B\u0439
|
||||
windowBodyBadIp=\u0412\u044B \u0443\u0432\u0435\u0440\u0435\u043D\u044B \u0447\u0442\u043E IP \u0430\u0434\u0440\u0435\u0441 NS \u0432\u0432\u0435\u0434\u0451\u043D \u0431\u0435\u0437 \u043E\u0448\u0438\u0431\u043E\u043A?
|
||||
netTabExpertModeCb=\u0420\u0435\u0436\u0438\u043C \u044D\u043A\u0441\u043F\u0435\u0440\u0442\u0430
|
||||
windowTitleExpectModeIncomplete=\u041F\u0440\u043E\u0432\u0435\u0440\u044C\u0442\u0435 \u043F\u0440\u0430\u0432\u0438\u043B\u044C\u043D\u043E\u0441\u0442\u044C \u043D\u0430\u0441\u0442\u0440\u043E\u0435\u043A
|
||||
windowBodyExpectModeIncomplete=\u0412\u044B \u0432\u044B\u0431\u0440\u0430\u043B\u0438 \u00AB\u0420\u0435\u0436\u0438\u043C \u044D\u043A\u0441\u043F\u0435\u0440\u0442\u0430\u00BB \u0432 \u043D\u0430\u0441\u0442\u0440\u043E\u0439\u043A\u0430\u0445 \u0438 \u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043B\u0438 \u043E\u0448\u0438\u0431\u043A\u0438 \u0432 \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u0430\u0445\n\u041F\u0440\u043E\u0432\u0435\u0440\u044C\u0442\u0435 \u0438\u0445 \u043F\u0440\u0430\u0432\u0438\u043B\u044C\u043D\u043E\u0441\u0442\u044C \u0435\u0449\u0451 \u0440\u0430\u0437.
|
||||
netTabHostPortLbl=\u043F\u043E\u0440\u0442
|
||||
netTabAutoDetectIpCb=\u0410\u0432\u0442\u043E\u043C\u0430\u0442\u0438\u0447\u0435\u0441\u043A\u0438 \u043E\u043F\u0440\u0435\u0434\u0435\u043B\u044F\u0442\u044C IP
|
||||
netTabRandSelectPortCb=\u041E\u043F\u0440\u0435\u0434\u0435\u043B\u044F\u0442\u044C \u043F\u043E\u0440\u0442 \u0441\u043B\u0443\u0447\u0430\u0439\u043D\u044B\u043C \u043E\u0431\u0440\u0430\u0437\u043E\u043C
|
||||
|
|
|
@ -27,8 +27,6 @@ netTabValidateNSHostNameCb=\u0417\u0430\u0432\u0436\u0434\u0438 \u043F\u0435\u04
|
|||
windowTitleBadIp=IP \u0430\u0434\u0440\u0435\u0441\u0430 NS \u0441\u043A\u043E\u0440\u0456\u0448 \u0437\u0430 \u0432\u0441\u0435 \u043D\u0435\u0432\u0456\u0440\u043D\u0430
|
||||
windowBodyBadIp=\u0412\u0438 \u0432\u043F\u0435\u0432\u043D\u0435\u043D\u0456 \u0449\u043E IP \u0430\u0434\u0440\u0435\u0441\u0430 NS \u0432\u0432\u0435\u0434\u0435\u043D\u0430 \u043F\u0440\u0430\u0432\u0438\u043B\u044C\u043D\u043E?
|
||||
netTabExpertModeCb=\u0420\u0435\u0436\u0438\u043C \u0435\u043A\u0441\u043F\u0435\u0440\u0442\u0430
|
||||
windowTitleExpectModeIncomplete=\u041F\u0435\u0440\u0435\u0432\u0456\u0440\u0442\u0435 \u043F\u0440\u0430\u0432\u0438\u043B\u044C\u043D\u0456\u0441\u0442\u044C \u043D\u0430\u043B\u0430\u0448\u0442\u0443\u0432\u0430\u043D\u044C
|
||||
windowBodyExpectModeIncomplete=\u0412\u0438 \u0432\u0438\u0431\u0440\u0430\u043B\u0438 "\u0420\u0435\u0436\u0438\u043C \u0454\u043A\u0441\u043F\u0435\u0440\u0442\u0430" \u0432 \u043D\u0430\u043B\u0430\u0448\u0442\u0443\u0432\u0430\u043D\u043D\u044F\u0445 \u0442\u0430 \u0437\u0440\u043E\u0431\u0438\u043B\u0438 \u043F\u043E\u043C\u0438\u043B\u043A\u0438 \u0432 \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u0430\u0445\n\u041F\u0435\u0440\u0435\u0432\u0456\u0440\u0442\u0435 \u0457\u0445 \u043F\u0440\u0430\u0432\u0438\u043B\u044C\u043D\u0456\u0441\u0442\u044C \u0449\u0435 \u0440\u0430\u0437.
|
||||
netTabHostPortLbl=\u043F\u043E\u0440\u0442
|
||||
netTabAutoDetectIpCb=\u0410\u0432\u0442\u043E\u043C\u0430\u0442\u0438\u0447\u043D\u043E \u0432\u0438\u0437\u043D\u0430\u0447\u0430\u0442\u0438 IP
|
||||
netTabRandSelectPortCb=\u0412\u0438\u0437\u043D\u0430\u0447\u0430\u0442\u0438 \u043F\u043E\u0440\u0442 \u0432\u0438\u043F\u0430\u0434\u043A\u043E\u0432\u0438\u043C \u0447\u0438\u043D\u043E\u043C
|
||||
|
|
Loading…
Reference in a new issue