diff --git a/src/main/java/io/github/robertograham/tpp4j/DefaultGrpcController.java b/src/main/java/io/github/robertograham/tpp4j/DefaultGrpcController.java new file mode 100644 index 0000000..beb3338 --- /dev/null +++ b/src/main/java/io/github/robertograham/tpp4j/DefaultGrpcController.java @@ -0,0 +1,25 @@ +package io.github.robertograham.tpp4j; + +import com.hashicorp.goplugin.Empty; +import com.hashicorp.goplugin.GRPCControllerGrpc.GRPCControllerImplBase; +import io.grpc.stub.StreamObserver; + +final class DefaultGrpcController extends GRPCControllerImplBase { + + final ProviderServer providerServer; + + DefaultGrpcController(final ProviderServer providerServer) { + this.providerServer = providerServer; + } + + @Override + public void shutdown(final Empty request, final StreamObserver responseObserver) { + System.err.println("[INFO] Shutting down"); + + providerServer.stop(); + + responseObserver.onNext(Empty.newBuilder() + .build()); + responseObserver.onCompleted(); + } +} diff --git a/src/main/java/io/github/robertograham/tpp4j/ProviderServer.java b/src/main/java/io/github/robertograham/tpp4j/ProviderServer.java index 1e725b9..bc1fae6 100644 --- a/src/main/java/io/github/robertograham/tpp4j/ProviderServer.java +++ b/src/main/java/io/github/robertograham/tpp4j/ProviderServer.java @@ -1,9 +1,7 @@ package io.github.robertograham.tpp4j; -import io.grpc.Attributes; import io.grpc.Grpc; import io.grpc.Server; -import io.grpc.ServerTransportFilter; import io.grpc.TlsServerCredentials; import java.io.IOException; import java.io.StringWriter; @@ -60,12 +58,7 @@ void start() throws IOException, GeneralSecurityException { .clientAuth(TlsServerCredentials.ClientAuth.REQUIRE) .build()) .addService(new DefaultProvider()) - .addTransportFilter(new ServerTransportFilter() { - @Override - public void transportTerminated(final Attributes attributes) { - stop(); - } - }) + .addService(new DefaultGrpcController(this)) .build() .start(); } @@ -83,9 +76,8 @@ void blockUntilShutdown() throws InterruptedException { } } - private void stop() { + void stop() { if (server != null) { - System.err.println("[INFO] Shutting down provider server"); server.shutdown(); } } diff --git a/src/main/proto/plugin/grpc_controller.proto b/src/main/proto/plugin/grpc_controller.proto new file mode 100644 index 0000000..32f0a57 --- /dev/null +++ b/src/main/proto/plugin/grpc_controller.proto @@ -0,0 +1,16 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +syntax = "proto3"; +package plugin; +option go_package = "./plugin"; +option java_multiple_files = true; +option java_package = "com.hashicorp.goplugin"; + +message Empty { +} + +// The GRPCController is responsible for telling the plugin server to shutdown. +service GRPCController { + rpc Shutdown(Empty) returns (Empty); +}