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