• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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.alts;
18 
19 import io.grpc.alts.AltsServerBuilder;
20 import io.grpc.Server;
21 import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase;
22 import io.grpc.examples.helloworld.HelloReply;
23 import io.grpc.examples.helloworld.HelloRequest;
24 import io.grpc.stub.StreamObserver;
25 import java.io.IOException;
26 import java.util.concurrent.Executors;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 
30 /**
31  * An example gRPC server that uses ALTS. Shows how to do a Unary RPC. This example can only be run
32  * on Google Cloud Platform.
33  */
34 public final class HelloWorldAltsServer extends GreeterImplBase {
35   private static final Logger logger = Logger.getLogger(HelloWorldAltsServer.class.getName());
36   private Server server;
37   private int port = 10001;
38 
main(String[] args)39   public static void main(String[] args) throws IOException, InterruptedException {
40     new HelloWorldAltsServer().start(args);
41   }
42 
parseArgs(String[] args)43   private void parseArgs(String[] args) {
44     boolean usage = false;
45     for (String arg : args) {
46       if (!arg.startsWith("--")) {
47         System.err.println("All arguments must start with '--': " + arg);
48         usage = true;
49         break;
50       }
51       String[] parts = arg.substring(2).split("=", 2);
52       String key = parts[0];
53       if ("help".equals(key)) {
54         usage = true;
55         break;
56       }
57       if (parts.length != 2) {
58         System.err.println("All arguments must be of the form --arg=value");
59         usage = true;
60         break;
61       }
62       String value = parts[1];
63       if ("port".equals(key)) {
64         port = Integer.parseInt(value);
65       } else {
66         System.err.println("Unknown argument: " + key);
67         usage = true;
68         break;
69       }
70     }
71     if (usage) {
72       HelloWorldAltsServer s = new HelloWorldAltsServer();
73       System.out.println(
74           "Usage: [ARGS...]"
75               + "\n"
76               + "\n  --port=PORT           Server port to bind to. Default "
77               + s.port);
78       System.exit(1);
79     }
80   }
81 
start(String[] args)82   private void start(String[] args) throws IOException, InterruptedException {
83     parseArgs(args);
84     server =
85         AltsServerBuilder.forPort(port)
86             .addService(this)
87             .executor(Executors.newFixedThreadPool(1))
88             .build();
89     server.start();
90     logger.log(Level.INFO, "Started on {0}", port);
91     server.awaitTermination();
92   }
93 
94   @Override
sayHello(HelloRequest request, StreamObserver<HelloReply> observer)95   public void sayHello(HelloRequest request, StreamObserver<HelloReply> observer) {
96     observer.onNext(HelloReply.newBuilder().setMessage("Hello, " + request.getName()).build());
97     observer.onCompleted();
98   }
99 }
100