2525import java .util .List ;
2626
2727// TODO: Add 'don't serve requests' option
28- // TODO: Refactor: remove duplicates; make logic flow more 'exception-driven'
2928public class TinfoilNet {
3029
30+ private String [] arguments ;
31+
3132 private String nsIp ;
3233
3334 private String hostIp = "" ;
@@ -36,49 +37,40 @@ public class TinfoilNet {
3637
3738 private int parseFileSince = 1 ;
3839
39- TinfoilNet ( String [] arguments ) throws InterruptedException {
40+ private List < File > filesList ;
4041
41- if (arguments == null ) {
42- showIncorrectCommandMessage ();
43- return ;
42+ TinfoilNet (String [] arguments ) throws InterruptedException , IncorrectSetupException {
43+ this .arguments = arguments ;
44+ checkArguments ();
45+ parseNsIP ();
46+ parseHostIPAndExtras ();
47+ parseFilesArguments ();
48+ runTinfoilNetBackend ();
49+ }
50+
51+ private void checkArguments () throws IncorrectSetupException {
52+ if (arguments == null || arguments .length == 0 ) {
53+ throw new IncorrectSetupException ("No arguments.\n " +
54+ "Try 'ns-usbloader -n help' for more information." );
4455 }
4556
4657 if (arguments .length == 1 ){
47- if (isHelp (arguments [0 ]))
58+ if (isHelpDirective (arguments [0 ])){
4859 showHelp ();
60+ return ;
61+ }
4962 else
50- showIncorrectCommandMessage ();
51- return ;
63+ throw new IncorrectSetupException ( "Not enough arguments. \n " +
64+ "Try 'ns-usbloader -n help' for more information." ) ;
5265 }
5366
54- if (arguments .length < 2 ) {
55- showIncorrectCommandMessage ();
56- return ;
67+ if (arguments .length == 2 && arguments [ 1 ]. startsWith ( "hostip=" )) {
68+ throw new IncorrectSetupException ( "Not enough arguments. \n " +
69+ "Try 'ns-usbloader -n help' for more information." ) ;
5770 }
58-
59- if (parseNsIP (arguments [0 ]))
60- return ;
61- parseHostIPAndExtras (arguments [1 ]);
62- if (checkArgumentsCount (arguments .length ))
63- return ;
64-
65- List <File > filesList = new ArrayList <>();
66- for (; parseFileSince < arguments .length ; parseFileSince ++)
67- filesList .add (new File (arguments [parseFileSince ]));
68-
69- NETCommunications netCommunications = new NETCommunications (
70- filesList ,
71- nsIp ,
72- false ,
73- hostIp ,
74- hostPortNum ,
75- hostExtras );
76- Thread netCommThread = new Thread (netCommunications );
77- netCommThread .start ();
78- netCommThread .join ();
7971 }
8072
81- private boolean isHelp (String argument ){
73+ private boolean isHelpDirective (String argument ){
8274 return argument .equals ("help" );
8375 }
8476 private void showHelp (){
@@ -89,42 +81,63 @@ private void showHelp(){
8981 + "\n \t nsip=<ip>\t \t \t Define NS IP address (mandatory)"
9082 + "\n \t hostip=<ip[:port][/extra]>\t Define this host IP address. Will be obtained automatically if not set." );
9183 }
92- private void showIncorrectCommandMessage (){
93- System .out .println ("Try 'ns-usbloader -n help' for more information." );
84+
85+ private void parseNsIP () throws IncorrectSetupException {
86+ String argument1 = arguments [0 ];
87+
88+ if (! argument1 .startsWith ("nsip=" ))
89+ throw new IncorrectSetupException ("First argument must be 'nsip=<ip_address>'\n " +
90+ "Try 'ns-usbloader -n help' for more information." );
91+
92+ nsIp = argument1 .replaceAll ("^nsip=" , "" );
93+
94+ if (nsIp .isEmpty ())
95+ throw new IncorrectSetupException ("No spaces allowed before or after 'nsip=<ip_address>' argument.\n " +
96+ "Try 'ns-usbloader -n help' for more information." );
9497 }
9598
96- private boolean parseNsIP (String argument1 ){
97- if (argument1 .startsWith ("nsip=" )){
98- nsIp = argument1 .replaceAll ("^nsip=" , "" );
99+ private void parseHostIPAndExtras (){
100+ String argument2 = arguments [1 ];
99101
100- if (nsIp .isEmpty ()) {
101- showIncorrectCommandMessage ();
102- return true ;
103- }
104- }
105- else {
106- showIncorrectCommandMessage ();
107- return true ;
108- }
109- return false ;
102+ if (! argument2 .startsWith ("hostip=" ))
103+ return ;
104+
105+ parseFileSince = 2 ;
106+ hostIp = argument2 .replaceAll ("(^hostip=)|(:.+?$)|(:$)" , "" );
107+
108+ if (argument2 .contains (":" ))
109+ hostPortNum = argument2 .replaceAll ("(^.+:)|(/.+?$)|(/$)" , "" );
110+
111+ if (argument2 .contains ("/" ))
112+ hostExtras = argument2 .replaceAll ("^[^/]*/" , "" );
110113 }
111- private void parseHostIPAndExtras (String argument2 ){
112- if (argument2 .startsWith ("hostip=" )){
113- parseFileSince = 2 ;
114- hostIp = argument2 .replaceAll ("(^hostip=)|(:.+?$)|(:$)" , "" );
115114
116- if (argument2 .contains (":" ))
117- hostPortNum = argument2 .replaceAll ("(^.+:)|(/.+?$)|(/$)" , "" );
115+ private void parseFilesArguments () throws IncorrectSetupException {
116+ filesList = new ArrayList <>();
117+ File file ;
118118
119- if (argument2 .contains ("/" ))
120- hostExtras = argument2 .replaceAll ("^[^/]*/" , "" );
119+ for (; parseFileSince < arguments .length ; parseFileSince ++) {
120+ file = new File (arguments [parseFileSince ]);
121+ if (file .exists ())
122+ filesList .add (file );
121123 }
122- }
123- private boolean checkArgumentsCount (int argumentsLength ){
124- if (argumentsLength == parseFileSince ){
125- showIncorrectCommandMessage ();
126- return true ;
124+
125+ if (filesList .size () == 0 ) {
126+ throw new IncorrectSetupException ("File(s) doesn't exists.\n " +
127+ "Try 'ns-usbloader -n help' for more information." );
127128 }
128- return false ;
129+ }
130+
131+ private void runTinfoilNetBackend () throws InterruptedException {
132+ NETCommunications netCommunications = new NETCommunications (
133+ filesList ,
134+ nsIp ,
135+ false ,
136+ hostIp ,
137+ hostPortNum ,
138+ hostExtras );
139+ Thread netCommThread = new Thread (netCommunications );
140+ netCommThread .start ();
141+ netCommThread .join ();
129142 }
130143}
0 commit comments