From 337a6e73e88684a274f32dffdf642d456169a7cc Mon Sep 17 00:00:00 2001 From: magoeke Date: Sun, 23 Apr 2023 22:01:46 +0200 Subject: [PATCH] Created project for gRPC task. --- task05/pom.xml | 143 ++++++++++++++++++ .../java/de/hftstuttgart/vs/HelloClient.java | 55 +++++++ .../java/de/hftstuttgart/vs/HelloServer.java | 75 +++++++++ task05/src/main/proto/helloworld.proto | 24 +++ 4 files changed, 297 insertions(+) create mode 100644 task05/pom.xml create mode 100644 task05/src/main/java/de/hftstuttgart/vs/HelloClient.java create mode 100644 task05/src/main/java/de/hftstuttgart/vs/HelloServer.java create mode 100644 task05/src/main/proto/helloworld.proto diff --git a/task05/pom.xml b/task05/pom.xml new file mode 100644 index 0000000..0912c2e --- /dev/null +++ b/task05/pom.xml @@ -0,0 +1,143 @@ + + 4.0.0 + de.hftstuttgart.vs + task05 + jar + + 0.0.1-SNAPSHOT + + + UTF-8 + 1.54.1 + 3.22.3 + 3.22.3 + + 1.8 + 1.8 + + + + + + io.grpc + grpc-bom + ${grpc.version} + pom + import + + + + + + + io.grpc + grpc-netty-shaded + runtime + + + com.google.protobuf + protobuf-java-util + ${protobuf.version} + + + io.grpc + grpc-protobuf + + + io.grpc + grpc-services + + + io.grpc + grpc-stub + + + io.grpc + grpc-api + + + + com.google.code.gson + gson + 2.9.0 + + + com.google.guava + guava + 31.1-jre + + + org.apache.tomcat + annotations-api + 6.0.53 + provided + + + io.grpc + grpc-testing + test + + + junit + junit + 4.13.2 + test + + + org.mockito + mockito-core + 3.4.0 + test + + + + + + + kr.motd.maven + os-maven-plugin + 1.7.1 + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + 0.6.1 + + com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} + grpc-java + io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} + + + + + compile + compile-custom + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + 1.4.1 + + + enforce + + enforce + + + + + + + + + + + + diff --git a/task05/src/main/java/de/hftstuttgart/vs/HelloClient.java b/task05/src/main/java/de/hftstuttgart/vs/HelloClient.java new file mode 100644 index 0000000..f3dc05b --- /dev/null +++ b/task05/src/main/java/de/hftstuttgart/vs/HelloClient.java @@ -0,0 +1,55 @@ +package de.hftstuttgart.vs; + + +import io.grpc.Channel; +import io.grpc.Grpc; +import io.grpc.InsecureChannelCredentials; +import io.grpc.ManagedChannel; +import io.grpc.StatusRuntimeException; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class HelloClient { + + private static final Logger logger = Logger.getLogger(HelloClient.class.getName()); + + private final GreeterGrpc.GreeterBlockingStub blockingStub; + + public HelloClient(Channel channel) { + blockingStub = GreeterGrpc.newBlockingStub(channel); + } + + public void greet(String name) { + logger.info("Will try to greet " + name + " ..."); + HelloRequest request = HelloRequest.newBuilder().setName(name).build(); + HelloReply response; + try { + response = blockingStub + .sayHello(request); + } catch (StatusRuntimeException e) { + logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); + return; + } + logger.info("Greeting: " + response.getMessage()); + } + + public static void main(String[] args) throws Exception { + String user = "world"; + String target = "localhost:50051"; + + ManagedChannel channel = Grpc + .newChannelBuilder(target, InsecureChannelCredentials.create()) + .build(); + + try { + HelloClient client = new HelloClient(channel); + client.greet(user); + } finally { + channel + .shutdownNow() + .awaitTermination(5, TimeUnit.SECONDS); + } + } + +} diff --git a/task05/src/main/java/de/hftstuttgart/vs/HelloServer.java b/task05/src/main/java/de/hftstuttgart/vs/HelloServer.java new file mode 100644 index 0000000..be02807 --- /dev/null +++ b/task05/src/main/java/de/hftstuttgart/vs/HelloServer.java @@ -0,0 +1,75 @@ +package de.hftstuttgart.vs; + +import io.grpc.Grpc; +import io.grpc.InsecureServerCredentials; +import io.grpc.Server; +import io.grpc.stub.StreamObserver; + +import java.io.IOException; +import java.util.logging.Logger; +import java.util.concurrent.TimeUnit; + +public class HelloServer { + + private static final Logger logger = Logger.getLogger(HelloServer.class.getName()); + + private Server server; + + private void start() throws IOException { + /* The port on which the server should run */ + int port = 50051; + server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create()) + .addService(new GreeterImpl()) + .build() + .start(); + logger.info("Server started, listening on " + port); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + // Use stderr here since the logger may have been reset by its JVM shutdown hook. + System.err.println("*** shutting down gRPC server since JVM is shutting down"); + try { + HelloServer.this.stop(); + } catch (InterruptedException e) { + e.printStackTrace(System.err); + } + System.err.println("*** server shut down"); + } + }); + } + + private void stop() throws InterruptedException { + if (server != null) { + server.shutdown().awaitTermination(30, TimeUnit.SECONDS); + } + } + + /** + * Await termination on the main thread since the grpc library uses daemon threads. + */ + private void blockUntilShutdown() throws InterruptedException { + if (server != null) { + server.awaitTermination(); + } + } + + /** + * Main launches the server from the command line. + */ + public static void main(String[] args) throws IOException, InterruptedException { + final HelloServer server = new HelloServer(); + server.start(); + server.blockUntilShutdown(); + } + + static class GreeterImpl extends GreeterGrpc.GreeterImplBase { + + @Override + public void sayHello(HelloRequest req, StreamObserver responseObserver) { + HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } + } + +} diff --git a/task05/src/main/proto/helloworld.proto b/task05/src/main/proto/helloworld.proto new file mode 100644 index 0000000..6727471 --- /dev/null +++ b/task05/src/main/proto/helloworld.proto @@ -0,0 +1,24 @@ +syntax = "proto3"; + +option java_multiple_files = true; +option java_package = "de.hftstuttgart.vs"; +option java_outer_classname = "HelloWorldProto"; +option objc_class_prefix = "HLW"; + +package helloworld; + +// The greeting service definition. +service Greeter { + // Sends a greeting + rpc SayHello (HelloRequest) returns (HelloReply) {} +} + +// The request message containing the user's name. +message HelloRequest { + string name = 1; +} + +// The response message containing the greetings +message HelloReply { + string message = 1; +}