95 lines
2.5 KiB
Java
95 lines
2.5 KiB
Java
|
|
package JobServ;
|
||
|
|
|
||
|
|
import io.grpc.Server;
|
||
|
|
import io.grpc.ServerBuilder;
|
||
|
|
import io.grpc.stub.StreamObserver;
|
||
|
|
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.util.logging.Logger;
|
||
|
|
|
||
|
|
public class JobServServer {
|
||
|
|
private static final Logger logger = Logger.getLogger(JobServServer.class.getName());
|
||
|
|
|
||
|
|
private Server server;
|
||
|
|
|
||
|
|
// starts the GRPC API Server
|
||
|
|
private void start() throws IOException {
|
||
|
|
// TODO: this should be passed in from a configuration manager
|
||
|
|
int port = 8448;
|
||
|
|
server = ServerBUilder.forPort(port)
|
||
|
|
.addService(new JobServImpl())
|
||
|
|
.build()
|
||
|
|
.start();
|
||
|
|
logger.info("Server initialized without tls");
|
||
|
|
Runtime.getRuntime().addShutdownHook(new thread() {
|
||
|
|
@Override
|
||
|
|
public void run() {
|
||
|
|
// JVM shutdown might break logger functionality
|
||
|
|
// so investigate this....
|
||
|
|
logger.info("Shutting down server");
|
||
|
|
JobServServer.this.stop();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void stop() {
|
||
|
|
if (server != null) {
|
||
|
|
server.shutdown();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// spinlock for main()
|
||
|
|
private void blockUntilShutdown() throws InterruptedException {
|
||
|
|
if (server != null) {
|
||
|
|
server.awaitTermination();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Main funciton. starts GRPC server and spins until server is shutdown
|
||
|
|
public static void main(String[] args) throws IOException, InterruptedException {
|
||
|
|
final JobServServer server = new JobServServer();
|
||
|
|
server.start();
|
||
|
|
server.blockUntilShutdown();
|
||
|
|
}
|
||
|
|
|
||
|
|
// wrap around stub code generated by GRPC
|
||
|
|
static class JobServImpl extends JobServGrpc.JobServImplBase {
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void getStatusDetail(RequestMessage request,
|
||
|
|
StreamObserver<JobStatusMessage> responder) {
|
||
|
|
// TODO: Implement job controller, check request type, do the thing
|
||
|
|
JobStatusMessage reply = JobStatusMessage.newBuilder()
|
||
|
|
.setPID(-1)
|
||
|
|
.setProcessStatus(-1)
|
||
|
|
.build();
|
||
|
|
responder.onNext(reply);
|
||
|
|
responder.onCompleted();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void getJobOutput(RequestMessage request,
|
||
|
|
StreamObserver<OutputMessage> responder) {
|
||
|
|
// TODO: Implement job controller, get output, do the thing
|
||
|
|
OutputMessage reply = OutputMessage.newBuilder()
|
||
|
|
.setPID(-1)
|
||
|
|
.setProcessOutput("Hello World!")
|
||
|
|
.build();
|
||
|
|
responder.onNext(reply);
|
||
|
|
responder.onCompleted();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void makeNewJob(NewJobMessage request,
|
||
|
|
StreamObserver<JobStatusMessage> responder) {
|
||
|
|
// TODO: Implement job controller, Start Job, do the thing
|
||
|
|
JobStatusMessage reply = JobStatusMessage.newBuilder()
|
||
|
|
.setPID(-1)
|
||
|
|
.setProcessStatus(-1)
|
||
|
|
.build():
|
||
|
|
responder.onNext(reply);
|
||
|
|
responder.onCompleted();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|