jobserv/src/main/java/JobServ/JobServServer.java

165 lines
5.6 KiB
Java
Raw Normal View History

2019-05-18 13:05:02 -07:00
/*
* JobServServer
*
* v1.0
*
* May 18, 2019
*/
2019-05-16 14:49:47 -07:00
package JobServ;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
2019-05-18 15:28:36 -07:00
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyServerBuilder;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslProvider;
2019-05-16 14:49:47 -07:00
import java.io.IOException;
import java.util.logging.Logger;
2019-05-18 13:05:02 -07:00
/*
* The JobServServer class implements the JobServ protobuf API
* It does this by extending the gRPC stub code.
* Additionally, JobServServer starts and manages a daemon
* Which accepts incoming connections from client.
*/
2019-05-16 14:49:47 -07:00
public class JobServServer {
private static final Logger logger = Logger.getLogger(JobServServer.class.getName());
private Server server;
2019-05-18 15:28:36 -07:00
private final int port;
private final String certChainFilePath;
private final String privateKeyFilePath;
private final String trustCertCollectionFilePath;
public JobServServer(int port,
String certChainFilePath,
String privateKeyFilePath,
String trustCertCollectionFilePath) {
this.port = port;
this.certChainFilePath = certChainFilePath;
this.privateKeyFilePath = privateKeyFilePath;
this.trustCertCollectionFilePath = trustCertCollectionFilePath;
}
private SslContextBuilder getSslContextBuilder() {
SslContextBuilder sslClientContextBuilder = SslContextBuilder.forServer(new File(certChainFilePath),
new File(privateKeyFilePath));
sslClientContextBuilder.trustManager(new File(trustCertCollectionFilePath));
sslClientContextBuilder.clientAuth(ClientAuth.REQUIRE);
return GrpcSslContexts.configure(sslClientContextBuilder);
2019-05-16 14:49:47 -07:00
// starts the GRPC API Server
private void start() throws IOException {
2019-05-17 01:28:26 -07:00
// TODO: this should be passed in from a configuration manager
2019-05-18 15:28:36 -07:00
server = ServerBuilder.forPort(this.port)
2019-05-17 01:35:55 -07:00
.addService(new ShellServerImpl())
2019-05-18 15:28:36 -07:00
.sslContext(getSslContextBuilder().build())
2019-05-17 01:28:26 -07:00
.build()
.start();
2019-05-18 15:28:36 -07:00
logger.info("Server initialized!");
2019-05-17 01:35:55 -07:00
Runtime.getRuntime().addShutdownHook(new Thread() {
2019-05-17 01:28:26 -07:00
@Override
public void run() {
// JVM shutdown might break logger functionality
// so investigate this....
logger.info("Shutting down server");
JobServServer.this.stop();
}
});
2019-05-16 14:49:47 -07:00
}
private void stop() {
2019-05-17 01:28:26 -07:00
if (server != null) {
server.shutdown();
}
2019-05-16 14:49:47 -07:00
}
2019-05-18 12:20:12 -07:00
2019-05-16 14:49:47 -07:00
// spinlock for main()
private void blockUntilShutdown() throws InterruptedException {
2019-05-17 01:28:26 -07:00
if (server != null) {
server.awaitTermination();
}
2019-05-16 14:49:47 -07:00
}
2019-05-17 01:28:26 -07:00
// Main function. starts GRPC server and spins until server is shutdown
2019-05-16 14:49:47 -07:00
public static void main(String[] args) throws IOException, InterruptedException {
2019-05-18 15:28:36 -07:00
int port = 8448; // TODO: port and key/cert files should be handled by a config manager
if(args.length < 3) {
System.out.println("Usage: ./jobserv-server certchain privatekey truststore");
return;
}
final JobServServer server = new JobServServer(8448, args[0], args[1], args[2]);
2019-05-17 01:28:26 -07:00
server.start();
server.blockUntilShutdown();
2019-05-16 14:49:47 -07:00
}
// wrap around stub code generated by GRPC
2019-05-17 01:35:55 -07:00
static class ShellServerImpl extends ShellServerGrpc.ShellServerImplBase {
2019-05-18 13:05:02 -07:00
2019-05-17 01:28:26 -07:00
@Override
public void getStatus(PIDMessage request,
StreamObserver<StatusMessage> responder) {
2019-05-17 01:28:26 -07:00
// TODO: Implement job controller, check request type, do the thing
StatusMessage reply = StatusMessage.newBuilder()
2019-05-18 12:20:12 -07:00
.setIsRunning(true)
2019-05-17 01:28:26 -07:00
.build();
responder.onNext(reply);
responder.onCompleted();
}
2019-05-18 13:05:02 -07:00
2019-05-17 01:28:26 -07:00
@Override
public void getOutput(PIDMessage request,
StreamObserver<OutputMessage> responder) {
2019-05-17 01:28:26 -07:00
// TODO: Implement job controller, get output, do the thing
OutputMessage reply = OutputMessage.newBuilder()
.setOutput("test data")
2019-05-17 01:28:26 -07:00
.build();
responder.onNext(reply);
responder.onCompleted();
}
2019-05-18 13:05:02 -07:00
2019-05-17 01:28:26 -07:00
@Override
public void newJob(NewJobMessage request,
StreamObserver<PIDMessage> responder) {
2019-05-17 01:28:26 -07:00
// TODO: Implement job controller, Start Job, do the thing
PIDMessage reply = PIDMessage.newBuilder()
.setPid(-1)
.build();
responder.onNext(reply);
responder.onCompleted();
}
@Override
public void getReturn(PIDMessage request,
StreamObserver<ReturnMessage> responder) {
// TODO: Implement job controller, get return code
ReturnMessage reply = ReturnMessage.newBuilder()
2019-05-18 12:20:12 -07:00
.setProcessReturnCode(277)
.build();
responder.onNext(reply);
responder.onCompleted();
}
@Override
public void killJob(PIDMessage request,
2019-05-18 12:20:12 -07:00
StreamObserver<StatusMessage> responder) {
// TODO: implement job controller, do the thing
// TODO: kill job here
2019-05-18 12:20:12 -07:00
StatusMessage reply = StatusMessage.newBuilder()
.setIsRunning(false)
2019-05-17 01:28:26 -07:00
.build();
responder.onNext(reply);
responder.onCompleted();
}
2019-05-16 14:49:47 -07:00
}
}