Skip to content

Commit 3c89df9

Browse files
committed
Refactoring:
* Replace UI-friendly Tasks to Runnable in all TF/GL NET/USB processes; implement similar interface for this. * Add Apache Commons CLI to handle CLI. * Extend/update CLI commands keys/functions, descriptions etc. * Add draft CLI command for Tinfoil/Awoo Net-install mode.
1 parent 6b65c74 commit 3c89df9

19 files changed

+404
-109
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Sometimes I add new posts about this project [on my home page](https://developer
3030
* [usb4java](https://mvnrepository.com/artifact/org.usb4java/usb4java)
3131
* Few icons taken from: [materialdesignicons.com](http://materialdesignicons.com/)
3232
* Information, ideas and data from ['fusee-launcher'](https://github.com/reswitched/fusee-launcher) application
33+
* [Apache Commons CLI](https://commons.apache.org/proper/commons-cli/)
3334

3435
#### List of awesome contributors!
3536

@@ -203,4 +204,4 @@ Want to support development? Make a donation* (see below):
203204

204205
Thanks
205206

206-
Appreciate assistance and support of both [Vitaliy](https://github.com/SebastianUA) and [Konstantin](https://github.com/konstantin-kelemen). Without you all this magic would not have happened.
207+
Appreciate assistance and support of both [Vitaliy](https://github.com/SebastianUA) and [Konstantin](https://github.com/konstantin-kelemen). Without you all this magic would not have happened.

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
</issueManagement>
5151
<!-- openJFX Linux -->
5252
<dependencies>
53+
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
54+
<dependency>
55+
<groupId>commons-cli</groupId>
56+
<artifactId>commons-cli</artifactId>
57+
<version>1.4</version>
58+
<scope>compile</scope>
59+
</dependency>
5360
<dependency>
5461
<groupId>org.openjfx</groupId>
5562
<artifactId>javafx-controls</artifactId>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2019-2020 Dmitry Isaenko
3+
4+
This file is part of NS-USBloader.
5+
6+
NS-USBloader is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
NS-USBloader is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package nsusbloader.COM;
20+
21+
public interface ICommunications extends Runnable {
22+
void cancel();
23+
boolean isCancelled();
24+
}

src/main/java/nsusbloader/COM/NET/NETCommunications.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package nsusbloader.COM.NET;
2020

21-
import javafx.concurrent.Task;
21+
import nsusbloader.COM.ICommunications;
2222
import nsusbloader.ModelControllers.ILogPrinter;
2323
import nsusbloader.NSLDataTypes.EFileStatus;
2424
import nsusbloader.ModelControllers.Log;
@@ -32,7 +32,7 @@
3232
import java.nio.charset.StandardCharsets;
3333
import java.util.*;
3434

35-
public class NETCommunications extends Task<Void> { // todo: thows IOException?
35+
public class NETCommunications implements ICommunications { // todo: rewrite
3636

3737
private ILogPrinter logPrinter;
3838

@@ -52,10 +52,17 @@ public class NETCommunications extends Task<Void> { // todo: thows IOException?
5252

5353
private OutputStream currSockOS;
5454
private PrintWriter currSockPW;
55+
56+
private volatile boolean cancel;
5557
/**
5658
* Simple constructor that everybody uses
5759
* */
58-
public NETCommunications(List<File> filesList, String switchIP, boolean doNotServeRequests, String hostIPaddr, String hostPortNum, String extras){
60+
public NETCommunications(List<File> filesList,
61+
String switchIP,
62+
boolean doNotServeRequests,
63+
String hostIPaddr,
64+
String hostPortNum,
65+
String extras) {
5966
this.doNotServeRequests = doNotServeRequests;
6067
if (doNotServeRequests)
6168
this.extras = extras;
@@ -263,19 +270,21 @@ private void showAvalIpExamples(){
263270
logPrinter.print("Can't determine possible variants. Returned:\n\t"+socketException.getMessage(), EMsgType.FAIL);
264271
}
265272
}
266-
/**
267-
* Override cancel block to close connection by ourselves
268-
* */
269273
@Override
270-
protected void cancelled() {
271-
this.close(EFileStatus.UNKNOWN);
272-
super.cancelled();
274+
public boolean isCancelled(){
275+
return cancel;
276+
}
277+
278+
@Override
279+
public void cancel() {
280+
cancel = true;
273281
}
274282

275283
@Override
276-
protected Void call() {
284+
public void run() {
285+
277286
if (!isValid | isCancelled())
278-
return null;
287+
return;
279288
logPrinter.print("\tStart chain", EMsgType.INFO);
280289
// Create string that we'll send to TF and which initiates chain
281290
StringBuilder myStrBuilder;
@@ -307,13 +316,13 @@ protected Void call() {
307316
catch (IOException uhe){
308317
logPrinter.print("NET: Unable to connect to NS and send files list. Returned:\n\t"+uhe.getMessage(), EMsgType.FAIL);
309318
close(EFileStatus.UNKNOWN);
310-
return null;
319+
return;
311320
}
312321
// Check if we should serve requests
313322
if (this.doNotServeRequests){
314323
logPrinter.print("NET: List of files transferred. Replies won't be served.", EMsgType.PASS);
315324
close(EFileStatus.UNKNOWN);
316-
return null;
325+
return;
317326
}
318327
logPrinter.print("NET: Initiation files list has been sent to NS.", EMsgType.PASS);
319328

@@ -353,7 +362,7 @@ protected Void call() {
353362
}
354363
if ( ! isCancelled() )
355364
close(EFileStatus.UNKNOWN);
356-
return null;
365+
return;
357366
}
358367

359368
// 200 206 400 (inv range) 404 416 (Range Not Satisfiable )

src/main/java/nsusbloader/COM/USB/GoldLeaf.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javafx.application.Platform;
2222
import javafx.concurrent.Task;
2323
import javafx.stage.FileChooser;
24+
import nsusbloader.COM.ICommunications;
2425
import nsusbloader.MediatorControl;
2526
import nsusbloader.ModelControllers.ILogPrinter;
2627
import nsusbloader.NSLDataTypes.EMsgType;
@@ -69,7 +70,7 @@ class GoldLeaf extends TransferModule {
6970
// For using in CMD_SelectFile with SPEC:/ prefix
7071
private File selectedFile;
7172

72-
GoldLeaf(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Task<Void> task, ILogPrinter logPrinter, boolean nspFilter){
73+
GoldLeaf(DeviceHandle handler, LinkedHashMap<String, File> nspMap, ICommunications task, ILogPrinter logPrinter, boolean nspFilter){
7374
super(handler, nspMap, task, logPrinter);
7475

7576
final byte CMD_GetDriveCount = 1;

src/main/java/nsusbloader/COM/USB/GoldLeaf_05.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package nsusbloader.COM.USB;
2020

2121
import javafx.concurrent.Task;
22+
import nsusbloader.COM.ICommunications;
2223
import nsusbloader.ModelControllers.ILogPrinter;
2324
import nsusbloader.NSLDataTypes.EFileStatus;
2425
import nsusbloader.NSLDataTypes.EMsgType;
@@ -53,7 +54,7 @@ public class GoldLeaf_05 extends TransferModule {
5354
private RandomAccessFile raf; // NSP File
5455
private NSSplitReader nsr; // It'a also NSP File
5556

56-
GoldLeaf_05(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Task<Void> task, ILogPrinter logPrinter){
57+
GoldLeaf_05(DeviceHandle handler, LinkedHashMap<String, File> nspMap, ICommunications task, ILogPrinter logPrinter){
5758
super(handler, nspMap, task, logPrinter);
5859
status = EFileStatus.FAILED;
5960

src/main/java/nsusbloader/COM/USB/GoldLeaf_07.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javafx.concurrent.Task;
2323
import javafx.stage.FileChooser;
2424
import nsusbloader.COM.Helpers.NSSplitReader;
25+
import nsusbloader.COM.ICommunications;
2526
import nsusbloader.MediatorControl;
2627
import nsusbloader.ModelControllers.ILogPrinter;
2728
import nsusbloader.NSLDataTypes.EMsgType;
@@ -69,7 +70,7 @@ class GoldLeaf_07 extends TransferModule {
6970
// For using in CMD_SelectFile with SPEC:/ prefix
7071
private File selectedFile;
7172

72-
GoldLeaf_07(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Task<Void> task, ILogPrinter logPrinter, boolean nspFilter){
73+
GoldLeaf_07(DeviceHandle handler, LinkedHashMap<String, File> nspMap, ICommunications task, ILogPrinter logPrinter, boolean nspFilter){
7374
super(handler, nspMap, task, logPrinter);
7475

7576
final byte CMD_GetDriveCount = 0x00;

src/main/java/nsusbloader/COM/USB/TinFoil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package nsusbloader.COM.USB;
2020

2121
import javafx.concurrent.Task;
22+
import nsusbloader.COM.ICommunications;
2223
import nsusbloader.ModelControllers.ILogPrinter;
2324
import nsusbloader.NSLDataTypes.EFileStatus;
2425
import nsusbloader.NSLDataTypes.EMsgType;
@@ -48,7 +49,7 @@ class TinFoil extends TransferModule {
4849
/* byte[] magic = new byte[4];
4950
ByteBuffer bb = StandardCharsets.UTF_8.encode("TUC0").rewind().get(magic); // Let's rephrase this 'string' */
5051

51-
TinFoil(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Task<Void> task, ILogPrinter logPrinter){
52+
TinFoil(DeviceHandle handler, LinkedHashMap<String, File> nspMap, ICommunications task, ILogPrinter logPrinter){
5253
super(handler, nspMap, task, logPrinter);
5354
logPrinter.print("============= Tinfoil =============", EMsgType.INFO);
5455

src/main/java/nsusbloader/COM/USB/TransferModule.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package nsusbloader.COM.USB;
2020

2121
import javafx.concurrent.Task;
22+
import nsusbloader.COM.ICommunications;
2223
import nsusbloader.ModelControllers.ILogPrinter;
2324
import nsusbloader.NSLDataTypes.EFileStatus;
2425
import nsusbloader.NSLDataTypes.EMsgType;
@@ -33,9 +34,9 @@ public abstract class TransferModule {
3334
LinkedHashMap<String, File> nspMap;
3435
ILogPrinter logPrinter;
3536
DeviceHandle handlerNS;
36-
Task<Void> task;
37+
ICommunications task;
3738

38-
TransferModule(DeviceHandle handler, LinkedHashMap<String, File> nspMap, Task<Void> task, ILogPrinter printer){
39+
TransferModule(DeviceHandle handler, LinkedHashMap<String, File> nspMap, ICommunications task, ILogPrinter printer){
3940
this.handlerNS = handler;
4041
this.nspMap = nspMap;
4142
this.task = task;

src/main/java/nsusbloader/COM/USB/UsbCommunications.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package nsusbloader.COM.USB;
2020

2121
import javafx.concurrent.Task;
22+
import nsusbloader.COM.ICommunications;
2223
import nsusbloader.ModelControllers.ILogPrinter;
2324
import nsusbloader.ModelControllers.Log;
2425
import nsusbloader.NSLDataTypes.EFileStatus;
@@ -31,13 +32,15 @@
3132
import java.util.*;
3233

3334
// TODO: add filter option to show only NSP files
34-
public class UsbCommunications extends Task<Void> {
35+
public class UsbCommunications implements ICommunications {
3536

3637
private ILogPrinter logPrinter;
3738
private LinkedHashMap<String, File> nspMap;
3839
private String protocol;
3940
private boolean nspFilterForGl;
4041

42+
private volatile boolean cancel;
43+
4144
public UsbCommunications(List<File> nspList, String protocol, boolean filterNspFilesOnlyForGl){
4245
this.protocol = protocol;
4346
this.nspFilterForGl = filterNspFilesOnlyForGl;
@@ -48,14 +51,14 @@ public UsbCommunications(List<File> nspList, String protocol, boolean filterNspF
4851
}
4952

5053
@Override
51-
protected Void call() {
54+
public void run() {
5255
logPrinter.print("\tStart", EMsgType.INFO);
5356

5457
UsbConnect usbConnect = UsbConnect.connectHomebrewMode(logPrinter);
5558

5659
if (! usbConnect.isConnected()){
5760
close(EFileStatus.FAILED);
58-
return null;
61+
return;
5962
}
6063

6164
DeviceHandle handler = usbConnect.getNsHandler();
@@ -81,7 +84,7 @@ protected Void call() {
8184

8285
close(module.getStatus());
8386

84-
return null;
87+
return;
8588
}
8689

8790
/**
@@ -93,4 +96,13 @@ private void close(EFileStatus status){
9396
logPrinter.close();
9497
}
9598

99+
@Override
100+
public boolean isCancelled() {
101+
return cancel;
102+
}
103+
104+
@Override
105+
public void cancel() {
106+
cancel = true;
107+
}
96108
}

0 commit comments

Comments
 (0)