• 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.AltsChannelBuilder;
20 import io.grpc.ManagedChannel;
21 import io.grpc.examples.helloworld.GreeterGrpc;
22 import io.grpc.examples.helloworld.HelloReply;
23 import io.grpc.examples.helloworld.HelloRequest;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.TimeUnit;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 
30 /**
31  * An example gRPC client 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 HelloWorldAltsClient {
35   private static final Logger logger = Logger.getLogger(HelloWorldAltsClient.class.getName());
36   private String serverAddress = "localhost:10001";
37 
main(String[] args)38   public static void main(String[] args) throws InterruptedException {
39     new HelloWorldAltsClient().run(args);
40   }
41 
parseArgs(String[] args)42   private void parseArgs(String[] args) {
43     boolean usage = false;
44     for (String arg : args) {
45       if (!arg.startsWith("--")) {
46         System.err.println("All arguments must start with '--': " + arg);
47         usage = true;
48         break;
49       }
50       String[] parts = arg.substring(2).split("=", 2);
51       String key = parts[0];
52       if ("help".equals(key)) {
53         usage = true;
54         break;
55       }
56       if (parts.length != 2) {
57         System.err.println("All arguments must be of the form --arg=value");
58         usage = true;
59         break;
60       }
61       String value = parts[1];
62       if ("server".equals(key)) {
63         serverAddress = value;
64       } else {
65         System.err.println("Unknown argument: " + key);
66         usage = true;
67         break;
68       }
69     }
70     if (usage) {
71       HelloWorldAltsClient c = new HelloWorldAltsClient();
72       System.out.println(
73           "Usage: [ARGS...]"
74               + "\n"
75               + "\n  --server=SERVER_ADDRESS        Server address to connect to. Default "
76               + c.serverAddress);
77       System.exit(1);
78     }
79   }
80 
run(String[] args)81   private void run(String[] args) throws InterruptedException {
82     parseArgs(args);
83     ExecutorService executor = Executors.newFixedThreadPool(1);
84     ManagedChannel channel = AltsChannelBuilder.forTarget(serverAddress).executor(executor).build();
85     try {
86       GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
87       HelloReply resp = stub.sayHello(HelloRequest.newBuilder().setName("Waldo").build());
88 
89       logger.log(Level.INFO, "Got {0}", resp);
90     } finally {
91       channel.shutdown();
92       channel.awaitTermination(1, TimeUnit.SECONDS);
93       // Wait until the channel has terminated, since tasks can be queued after the channel is
94       // shutdown.
95       executor.shutdown();
96     }
97   }
98 }
99