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;
}
// start client
// fails if port is improperly formatted or if an ssl exception occurs
JobServClient client;
try {
client = new JobServClient(args[3], Integer.parseInt(args[4]), args[2], args[1], args[0]);
// Likely bad port
} catch (NumberFormatException e) {
System.out.println("Invalid Port");
return;
// bad cert or key format
} catch (SSLException e) {
System.out.println(e.getMessage());
return;
@ -254,71 +255,40 @@ public class JobServClient {
break;
case "output":
if (args.length != 7) {
System.out.println("Improper formatting, try client --help");
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;
}
candidatePid = getPidArg(args, 7);
if (candidatePid < 0) {
break;
}
String processOutput = client.getProcessOutput(candidatePid);
System.out.println(processOutput);
break;
case "status":
if (args.length != 7) {
System.out.println("Improper formatting, try client --help");
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;
}
candidatePid = getPidArg(args, 7);
if (candidatePid < 0) {
break;
}
Boolean processStatus = client.getProcessStatus(candidatePid);
System.out.printf("Process is currently running? %b\n", processStatus);
break;
case "kill":
if (args.length != 7) {
System.out.println("Improper formatting, try client --help");
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;
}
candidatePid = getPidArg(args, 7);
if (candidatePid < 0) {
break;
}
client.killProcess(candidatePid);
System.out.println("End process request recieved!");
break;
case "return":
if (args.length != 7) {
System.out.println("Improper formatting, try client --help");
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;
}
candidatePid = getPidArg(args, 7);
if (candidatePid < 0) {
break;
}
int returnCode = client.getProcessReturn(candidatePid);
@ -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()
* writes help information about all commands in the shell to screen