|
| 1 | +/* |
| 2 | + * Copyright 2024 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.examples.dualstack; |
| 18 | + |
| 19 | +import io.grpc.Server; |
| 20 | +import io.grpc.ServerBuilder; |
| 21 | +import io.grpc.examples.helloworld.GreeterGrpc; |
| 22 | +import io.grpc.examples.helloworld.HelloReply; |
| 23 | +import io.grpc.examples.helloworld.HelloRequest; |
| 24 | +import io.grpc.netty.NettyServerBuilder; |
| 25 | +import io.grpc.stub.StreamObserver; |
| 26 | +import java.io.IOException; |
| 27 | +import java.net.InetSocketAddress; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.logging.Logger; |
| 32 | + |
| 33 | +/** |
| 34 | + * Starts 3 different greeter services each on its own port, but all for localhost. |
| 35 | + * The first service listens on both IPv4 and IPv6, |
| 36 | + * the second on just IPv4, and the third on just IPv6. |
| 37 | + */ |
| 38 | +public class DualStackServer { |
| 39 | + private static final Logger logger = Logger.getLogger(DualStackServer.class.getName()); |
| 40 | + private List<Server> servers; |
| 41 | + |
| 42 | + public static void main(String[] args) throws IOException, InterruptedException { |
| 43 | + final DualStackServer server = new DualStackServer(); |
| 44 | + server.start(); |
| 45 | + server.blockUntilShutdown(); |
| 46 | + } |
| 47 | + |
| 48 | + private void start() throws IOException { |
| 49 | + InetSocketAddress inetSocketAddress; |
| 50 | + |
| 51 | + servers = new ArrayList<>(); |
| 52 | + int[] serverPorts = ExampleDualStackNameResolver.SERVER_PORTS; |
| 53 | + for (int i = 0; i < serverPorts.length; i++ ) { |
| 54 | + String addressType; |
| 55 | + int port = serverPorts[i]; |
| 56 | + ServerBuilder<?> serverBuilder; |
| 57 | + switch (i) { |
| 58 | + case 0: |
| 59 | + serverBuilder = ServerBuilder.forPort(port); // bind to both IPv4 and IPv6 |
| 60 | + addressType = "both IPv4 and IPv6"; |
| 61 | + break; |
| 62 | + case 1: |
| 63 | + // bind to IPv4 only |
| 64 | + inetSocketAddress = new InetSocketAddress("127.0.0.1", port); |
| 65 | + serverBuilder = NettyServerBuilder.forAddress(inetSocketAddress); |
| 66 | + addressType = "IPv4 only"; |
| 67 | + break; |
| 68 | + case 2: |
| 69 | + // bind to IPv6 only |
| 70 | + inetSocketAddress = new InetSocketAddress("::1", port); |
| 71 | + serverBuilder = NettyServerBuilder.forAddress(inetSocketAddress); |
| 72 | + addressType = "IPv6 only"; |
| 73 | + break; |
| 74 | + default: |
| 75 | + throw new IllegalStateException("Unexpected value: " + i); |
| 76 | + } |
| 77 | + |
| 78 | + servers.add(serverBuilder |
| 79 | + .addService(new GreeterImpl(port, addressType)) |
| 80 | + .build() |
| 81 | + .start()); |
| 82 | + logger.info("Server started, listening on " + port); |
| 83 | + } |
| 84 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 85 | + System.err.println("*** shutting down gRPC server since JVM is shutting down"); |
| 86 | + try { |
| 87 | + DualStackServer.this.stop(); |
| 88 | + } catch (InterruptedException e) { |
| 89 | + e.printStackTrace(System.err); |
| 90 | + } |
| 91 | + System.err.println("*** server shut down"); |
| 92 | + })); |
| 93 | + } |
| 94 | + |
| 95 | + private void stop() throws InterruptedException { |
| 96 | + for (Server server : servers) { |
| 97 | + server.shutdown().awaitTermination(30, TimeUnit.SECONDS); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void blockUntilShutdown() throws InterruptedException { |
| 102 | + for (Server server : servers) { |
| 103 | + server.awaitTermination(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + static class GreeterImpl extends GreeterGrpc.GreeterImplBase { |
| 108 | + |
| 109 | + int port; |
| 110 | + String addressType; |
| 111 | + |
| 112 | + public GreeterImpl(int port, String addressType) { |
| 113 | + this.port = port; |
| 114 | + this.addressType = addressType; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { |
| 119 | + String msg = String.format("Hello %s from server<%d> type: %s", |
| 120 | + req.getName(), this.port, addressType); |
| 121 | + HelloReply reply = HelloReply.newBuilder().setMessage(msg).build(); |
| 122 | + responseObserver.onNext(reply); |
| 123 | + responseObserver.onCompleted(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments