Proces Controller Object
This commit is contained in:
parent
37cd129b8b
commit
622da2d238
2 changed files with 131 additions and 31 deletions
|
|
@ -146,8 +146,8 @@ public class JobServClient {
|
||||||
/*
|
/*
|
||||||
* sends PID to server
|
* sends PID to server
|
||||||
* returns process exit code
|
* returns process exit code
|
||||||
* returns a 0-255 return code or 277 if still running
|
* returns a 0-255 return code or 256 if still running
|
||||||
* or 278 if error in API
|
* or 257 if error in API
|
||||||
*/
|
*/
|
||||||
public int getProcessReturn(int pid) {
|
public int getProcessReturn(int pid) {
|
||||||
logger.info("[+] Requesting return code of a job");
|
logger.info("[+] Requesting return code of a job");
|
||||||
|
|
@ -162,7 +162,7 @@ public class JobServClient {
|
||||||
response = blockingStub.getReturn(request);
|
response = blockingStub.getReturn(request);
|
||||||
} catch (StatusRuntimeException e) {
|
} catch (StatusRuntimeException e) {
|
||||||
logger.log(Level.WARNING, "(API Failure) Failed to get return code: " + e.getStatus());
|
logger.log(Level.WARNING, "(API Failure) Failed to get return code: " + e.getStatus());
|
||||||
return 278;
|
return 257;
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.getProcessReturnCode();
|
return response.getProcessReturnCode();
|
||||||
|
|
@ -218,7 +218,6 @@ public class JobServClient {
|
||||||
ManagedChannel channel = NettyChannelBuilder.forAddress(args[3], Integer.parseInt(args[4]))
|
ManagedChannel channel = NettyChannelBuilder.forAddress(args[3], Integer.parseInt(args[4]))
|
||||||
.sslContext(builder.build())
|
.sslContext(builder.build())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
client = new JobServClient(channel);
|
client = new JobServClient(channel);
|
||||||
|
|
||||||
// Likely bad port
|
// Likely bad port
|
||||||
|
|
|
||||||
101
src/main/java/JobServ/ProcessController.java
Normal file
101
src/main/java/JobServ/ProcessController.java
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
/*
|
||||||
|
* ProcessController
|
||||||
|
*
|
||||||
|
* v1.0
|
||||||
|
*
|
||||||
|
* May 22, 2019
|
||||||
|
*/
|
||||||
|
|
||||||
|
package JobServ;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ProcessController
|
||||||
|
* This class wraps a java Process object with metadata
|
||||||
|
* such as translated PID that exist for this specific API
|
||||||
|
* as well as general metadata like IO streams.
|
||||||
|
*/
|
||||||
|
class ProcessController {
|
||||||
|
// incremented in constructor
|
||||||
|
private static int nextPid = 0;
|
||||||
|
private int pid;
|
||||||
|
|
||||||
|
// TODO: add an api endpoint for streaming client input into
|
||||||
|
// interactive processes (out of scope for initial API)
|
||||||
|
private OutputStream output;
|
||||||
|
private InputStream input;
|
||||||
|
private Scanner outputScanner;
|
||||||
|
|
||||||
|
private Process process;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Constructor
|
||||||
|
* Takes a command and spawns it in a new process
|
||||||
|
* Redirects IO streams and assigns a fake PID
|
||||||
|
*/
|
||||||
|
public ProcessController(String command) throws IOException {
|
||||||
|
this.pid = ProcessController.nextPid;
|
||||||
|
ProcessController.nextPid += 1;
|
||||||
|
|
||||||
|
this.process = Runtime.exec(command);
|
||||||
|
this.output = this.process.getOutputStream();
|
||||||
|
this.input = this.process.getInputStream();
|
||||||
|
this.outputScanner = new Scanner(this.input);
|
||||||
|
this.outputScanner.useDelimieter("\\A");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getStatus()
|
||||||
|
* returns whether or not the process is running
|
||||||
|
* this isnt a very direct way of getting the information
|
||||||
|
* The alternative is to use reflection to get into the private UNIXProcess class
|
||||||
|
* for the PID and to check that against 'ps' or a similar command
|
||||||
|
*
|
||||||
|
* TODO: (for future release) return thread state
|
||||||
|
*/
|
||||||
|
public Boolean getStatus() {
|
||||||
|
try {
|
||||||
|
process.exitValue();
|
||||||
|
return true;
|
||||||
|
} catch (IllegalThreadStateException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getReturn()
|
||||||
|
* returns the exit code of the process
|
||||||
|
* or 256 if process is still running
|
||||||
|
* (unix/posix defines an exit code as a uint8, so 256 is fair game)
|
||||||
|
*/
|
||||||
|
public int getReturn() {
|
||||||
|
try {
|
||||||
|
return process.exitValue();
|
||||||
|
} catch (IllegalThreadStateException e) {
|
||||||
|
return 256;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getOutput()
|
||||||
|
* gets new output from stream
|
||||||
|
* (TODO: investigate whether this would better be done by )
|
||||||
|
*/
|
||||||
|
public String getOutput() {
|
||||||
|
String out = "";
|
||||||
|
while(scanner.hasNext()) {
|
||||||
|
out += scanner.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* kill()
|
||||||
|
* Cleans up resources and destroys process
|
||||||
|
*/
|
||||||
|
public void kill() {
|
||||||
|
this.input.close();
|
||||||
|
this.output.close();
|
||||||
|
process.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue