aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJiangtao Li <jiangtao@google.com>2018-02-15 09:28:00 -0800
committerCarl Mastrangelo <notcarl@google.com>2018-02-15 09:28:00 -0800
commite7f2f1dedd8587cbb4bcfa07ec7ecc2a66f1f7d9 (patch)
tree454b95df04f808c17aa3261d02e54d18e9904299 /examples
parenta45d07bcb51e4f73b897a58d9734793ac2672220 (diff)
downloadgrpc-grpc-java-e7f2f1dedd8587cbb4bcfa07ec7ecc2a66f1f7d9.tar.gz
alts: add gRPC ALTS
Diffstat (limited to 'examples')
-rw-r--r--examples/BUILD.bazel24
-rw-r--r--examples/build.gradle17
-rw-r--r--examples/pom.xml5
-rw-r--r--examples/src/main/java/io/grpc/examples/alts/HelloWorldAltsClient.java98
-rw-r--r--examples/src/main/java/io/grpc/examples/alts/HelloWorldAltsServer.java99
5 files changed, 243 insertions, 0 deletions
diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel
index 9a97cc669..4d6900497 100644
--- a/examples/BUILD.bazel
+++ b/examples/BUILD.bazel
@@ -71,6 +71,7 @@ java_library(
"@com_google_guava_guava//jar",
"@com_google_protobuf//:protobuf_java",
"@com_google_protobuf//:protobuf_java_util",
+ "@grpc_java//alts",
"@grpc_java//core",
"@grpc_java//netty",
"@grpc_java//protobuf",
@@ -98,6 +99,29 @@ java_binary(
)
java_binary(
+ name = "hello-world-alts-client",
+ testonly = 1,
+ main_class = "io.grpc.examples.alts.HelloWorldAltsClient",
+ runtime_deps = [
+ ":examples",
+ "@grpc_java//alts",
+ "@grpc_java//netty",
+ ],
+)
+
+java_binary(
+ name = "hello-world-alts-server",
+ testonly = 1,
+ main_class = "io.grpc.examples.alts.HelloWorldAltsServer",
+ runtime_deps = [
+ ":examples",
+ "@grpc_java//alts",
+ "@grpc_java//netty",
+ ],
+
+)
+
+java_binary(
name = "route-guide-client",
testonly = 1,
main_class = "io.grpc.examples.routeguide.RouteGuideClient",
diff --git a/examples/build.gradle b/examples/build.gradle
index 10e1871ee..02236461e 100644
--- a/examples/build.gradle
+++ b/examples/build.gradle
@@ -27,6 +27,7 @@ def nettyTcNativeVersion = '2.0.7.Final'
dependencies {
compile "com.google.api.grpc:proto-google-common-protos:1.0.0"
+ compile "io.grpc:grpc-alts:${grpcVersion}"
compile "io.grpc:grpc-netty:${grpcVersion}"
compile "io.grpc:grpc-protobuf:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"
@@ -101,6 +102,20 @@ task helloWorldClient(type: CreateStartScripts) {
classpath = jar.outputs.files + project.configurations.runtime
}
+task helloWorldAltsServer(type: CreateStartScripts) {
+ mainClassName = 'io.grpc.examples.alts.HelloWorldAltsServer'
+ applicationName = 'hello-world-alts-server'
+ outputDir = new File(project.buildDir, 'tmp')
+ classpath = jar.outputs.files + project.configurations.runtime
+}
+
+task helloWorldAltsClient(type: CreateStartScripts) {
+ mainClassName = 'io.grpc.examples.alts.HelloWorldAltsClient'
+ applicationName = 'hello-world-alts-client'
+ outputDir = new File(project.buildDir, 'tmp')
+ classpath = jar.outputs.files + project.configurations.runtime
+}
+
task helloWorldTlsServer(type: CreateStartScripts) {
mainClassName = 'io.grpc.examples.helloworldtls.HelloWorldServerTls'
applicationName = 'hello-world-tls-server'
@@ -127,6 +142,8 @@ applicationDistribution.into('bin') {
from(routeGuideClient)
from(helloWorldServer)
from(helloWorldClient)
+ from(helloWorldAltsServer)
+ from(helloWorldAltsClient)
from(helloWorldTlsServer)
from(helloWorldTlsClient)
from(compressingHelloWorldClient)
diff --git a/examples/pom.xml b/examples/pom.xml
index 3f0922b31..b9cc5ddc5 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -32,6 +32,11 @@
</dependency>
<dependency>
<groupId>io.grpc</groupId>
+ <artifactId>grpc-alts</artifactId>
+ <version>${grpc.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
<version>${grpc.version}</version>
<scope>test</scope>
diff --git a/examples/src/main/java/io/grpc/examples/alts/HelloWorldAltsClient.java b/examples/src/main/java/io/grpc/examples/alts/HelloWorldAltsClient.java
new file mode 100644
index 000000000..8ef005b36
--- /dev/null
+++ b/examples/src/main/java/io/grpc/examples/alts/HelloWorldAltsClient.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2018, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.grpc.examples.alts;
+
+import io.grpc.alts.AltsChannelBuilder;
+import io.grpc.ManagedChannel;
+import io.grpc.examples.helloworld.GreeterGrpc;
+import io.grpc.examples.helloworld.HelloReply;
+import io.grpc.examples.helloworld.HelloRequest;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * An example gRPC client that uses ALTS. Shows how to do a Unary RPC. This example can only be run
+ * on Google Cloud Platform.
+ */
+public final class HelloWorldAltsClient {
+ private static final Logger logger = Logger.getLogger(HelloWorldAltsClient.class.getName());
+ private String serverAddress = "localhost:10001";
+
+ public static void main(String[] args) throws InterruptedException {
+ new HelloWorldAltsClient().run(args);
+ }
+
+ private void parseArgs(String[] args) {
+ boolean usage = false;
+ for (String arg : args) {
+ if (!arg.startsWith("--")) {
+ System.err.println("All arguments must start with '--': " + arg);
+ usage = true;
+ break;
+ }
+ String[] parts = arg.substring(2).split("=", 2);
+ String key = parts[0];
+ if ("help".equals(key)) {
+ usage = true;
+ break;
+ }
+ if (parts.length != 2) {
+ System.err.println("All arguments must be of the form --arg=value");
+ usage = true;
+ break;
+ }
+ String value = parts[1];
+ if ("server".equals(key)) {
+ serverAddress = value;
+ } else {
+ System.err.println("Unknown argument: " + key);
+ usage = true;
+ break;
+ }
+ }
+ if (usage) {
+ HelloWorldAltsClient c = new HelloWorldAltsClient();
+ System.out.println(
+ "Usage: [ARGS...]"
+ + "\n"
+ + "\n --server=SERVER_ADDRESS Server address to connect to. Default "
+ + c.serverAddress);
+ System.exit(1);
+ }
+ }
+
+ private void run(String[] args) throws InterruptedException {
+ parseArgs(args);
+ ExecutorService executor = Executors.newFixedThreadPool(1);
+ ManagedChannel channel = AltsChannelBuilder.forTarget(serverAddress).executor(executor).build();
+ try {
+ GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
+ HelloReply resp = stub.sayHello(HelloRequest.newBuilder().setName("Waldo").build());
+
+ logger.log(Level.INFO, "Got {0}", resp);
+ } finally {
+ channel.shutdown();
+ channel.awaitTermination(1, TimeUnit.SECONDS);
+ // Wait until the channel has terminated, since tasks can be queued after the channel is
+ // shutdown.
+ executor.shutdown();
+ }
+ }
+}
diff --git a/examples/src/main/java/io/grpc/examples/alts/HelloWorldAltsServer.java b/examples/src/main/java/io/grpc/examples/alts/HelloWorldAltsServer.java
new file mode 100644
index 000000000..24931115c
--- /dev/null
+++ b/examples/src/main/java/io/grpc/examples/alts/HelloWorldAltsServer.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2018, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.grpc.examples.alts;
+
+import io.grpc.alts.AltsServerBuilder;
+import io.grpc.Server;
+import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase;
+import io.grpc.examples.helloworld.HelloReply;
+import io.grpc.examples.helloworld.HelloRequest;
+import io.grpc.stub.StreamObserver;
+import java.io.IOException;
+import java.util.concurrent.Executors;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * An example gRPC server that uses ALTS. Shows how to do a Unary RPC. This example can only be run
+ * on Google Cloud Platform.
+ */
+public final class HelloWorldAltsServer extends GreeterImplBase {
+ private static final Logger logger = Logger.getLogger(HelloWorldAltsServer.class.getName());
+ private Server server;
+ private int port = 10001;
+
+ public static void main(String[] args) throws IOException, InterruptedException {
+ new HelloWorldAltsServer().start(args);
+ }
+
+ private void parseArgs(String[] args) {
+ boolean usage = false;
+ for (String arg : args) {
+ if (!arg.startsWith("--")) {
+ System.err.println("All arguments must start with '--': " + arg);
+ usage = true;
+ break;
+ }
+ String[] parts = arg.substring(2).split("=", 2);
+ String key = parts[0];
+ if ("help".equals(key)) {
+ usage = true;
+ break;
+ }
+ if (parts.length != 2) {
+ System.err.println("All arguments must be of the form --arg=value");
+ usage = true;
+ break;
+ }
+ String value = parts[1];
+ if ("port".equals(key)) {
+ port = Integer.parseInt(value);
+ } else {
+ System.err.println("Unknown argument: " + key);
+ usage = true;
+ break;
+ }
+ }
+ if (usage) {
+ HelloWorldAltsServer s = new HelloWorldAltsServer();
+ System.out.println(
+ "Usage: [ARGS...]"
+ + "\n"
+ + "\n --port=PORT Server port to bind to. Default "
+ + s.port);
+ System.exit(1);
+ }
+ }
+
+ private void start(String[] args) throws IOException, InterruptedException {
+ parseArgs(args);
+ server =
+ AltsServerBuilder.forPort(port)
+ .addService(this)
+ .executor(Executors.newFixedThreadPool(1))
+ .build();
+ server.start();
+ logger.log(Level.INFO, "Started on {0}", port);
+ server.awaitTermination();
+ }
+
+ @Override
+ public void sayHello(HelloRequest request, StreamObserver<HelloReply> observer) {
+ observer.onNext(HelloReply.newBuilder().setMessage("Hello, " + request.getName()).build());
+ observer.onCompleted();
+ }
+}