refactored out re-entrant code in client

This commit is contained in:
Aidan Hahn 2019-05-21 01:37:41 -07:00
parent 75c3dcda61
commit 5724953e9d
No known key found for this signature in database
GPG key ID: 327711E983899316
2 changed files with 41 additions and 51 deletions

View file

@ -220,15 +220,16 @@ public class JobServClient {
return; return;
} }
// start client
// fails if port is improperly formatted or if an ssl exception occurs
JobServClient client; JobServClient client;
try { try {
client = new JobServClient(args[3], Integer.parseInt(args[4]), args[2], args[1], args[0]); client = new JobServClient(args[3], Integer.parseInt(args[4]), args[2], args[1], args[0]);
// Likely bad port
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
System.out.println("Invalid Port"); System.out.println("Invalid Port");
return; return;
// bad cert or key format
} catch (SSLException e) { } catch (SSLException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
return; return;
@ -254,15 +255,8 @@ public class JobServClient {
break; break;
case "output": case "output":
if (args.length != 7) { candidatePid = getPidArg(args, 7);
System.out.println("Improper formatting, try client --help"); if (candidatePid < 0) {
break;
}
try {
candidatePid = Integer.parseInt(args[6]);
} catch (InputMismatchException e) {
System.out.println(args[6] + " is not a valid int, much less a valid pid");
break; break;
} }
@ -271,16 +265,8 @@ public class JobServClient {
break; break;
case "status": case "status":
if (args.length != 7) { candidatePid = getPidArg(args, 7);
System.out.println("Improper formatting, try client --help"); if (candidatePid < 0) {
break;
}
try {
candidatePid = Integer.parseInt(args[6]);
} catch (InputMismatchException e) {
System.out.println(args[6] + " is not a valid int, much less a valid pid");
break; break;
} }
@ -289,16 +275,8 @@ public class JobServClient {
break; break;
case "kill": case "kill":
if (args.length != 7) { candidatePid = getPidArg(args, 7);
System.out.println("Improper formatting, try client --help"); if (candidatePid < 0) {
break;
}
try {
candidatePid = Integer.parseInt(args[6]);
} catch (InputMismatchException e) {
System.out.println(args[6] + " is not a valid int, much less a valid pid");
break; break;
} }
@ -307,16 +285,8 @@ public class JobServClient {
break; break;
case "return": case "return":
if (args.length != 7) { candidatePid = getPidArg(args, 7);
System.out.println("Improper formatting, try client --help"); if (candidatePid < 0) {
break;
}
try {
candidatePid = Integer.parseInt(args[6]);
} catch (InputMismatchException e) {
System.out.println(args[6] + " is not a valid int, much less a valid pid");
break; break;
} }
@ -340,6 +310,28 @@ public class JobServClient {
} }
} }
/*
* getPidArg()
* reentrant code was found in all commands except newjob
* this function pulls the pid argument and wraps around the integer case
* returns -1 (an invalid PID) if bad index or unparsable int
*/
private static int getPidArg(String[] args, int index) {
if (args.length < index) {
System.out.println("Improper formatting, try client --help");
return -1;
}
try {
return Integer.parseInt(args[6]);
} catch (InputMismatchException e) {
System.out.println(args[6] + " is not a valid int, much less a valid pid");
return -1;
}
}
/* /*
* outputHelp() * outputHelp()
* writes help information about all commands in the shell to screen * writes help information about all commands in the shell to screen

View file

@ -69,8 +69,6 @@ public class JobServServer {
Runtime.getRuntime().addShutdownHook(new Thread() { Runtime.getRuntime().addShutdownHook(new Thread() {
@Override @Override
public void run() { public void run() {
// JVM shutdown might break logger functionality
// so investigate this....
logger.info("Shutting down server"); logger.info("Shutting down server");
JobServServer.this.stop(); JobServServer.this.stop();
} }