• 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.AltsChannelCredentials;
20 import io.grpc.Grpc;
21 import io.grpc.ManagedChannel;
22 import io.grpc.examples.helloworld.GreeterGrpc;
23 import io.grpc.examples.helloworld.HelloReply;
24 import io.grpc.examples.helloworld.HelloRequest;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.TimeUnit;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 
31 /**
32  * An example gRPC client 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 HelloWorldAltsClient {
36   private static final Logger logger = Logger.getLogger(HelloWorldAltsClient.class.getName());
37   private String serverAddress = "localhost:10001";
38 
main(String[] args)39   public static void main(String[] args) throws InterruptedException {
40     new HelloWorldAltsClient().run(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 ("server".equals(key)) {
64         serverAddress = value;
65       } else {
66         System.err.println("Unknown argument: " + key);
67         usage = true;
68         break;
69       }
70     }
71     if (usage) {
72       HelloWorldAltsClient c = new HelloWorldAltsClient();
73       System.out.println(
74           "Usage: [ARGS...]"
75               + "\n"
76               + "\n  --server=SERVER_ADDRESS        Server address to connect to. Default "
77               + c.serverAddress);
78       System.exit(1);
79     }
80   }
81 
run(String[] args)82   private void run(String[] args) throws InterruptedException {
83     parseArgs(args);
84     ExecutorService executor = Executors.newFixedThreadPool(1);
85     ManagedChannel channel = Grpc.newChannelBuilder(serverAddress, AltsChannelCredentials.create())
86         .executor(executor).build();
87     try {
88       GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
89       HelloReply resp = stub.sayHello(HelloRequest.newBuilder().setName("Waldo").build());
90 
91       logger.log(Level.INFO, "Got {0}", resp);
92     } finally {
93       channel.shutdown();
94       channel.awaitTermination(1, TimeUnit.SECONDS);
95       // Wait until the channel has terminated, since tasks can be queued after the channel is
96       // shutdown.
97       executor.shutdown();
98     }
99   }
100 }
101