2 * Copyright 2018 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.open.infc.grpc.client;
20 import java.util.concurrent.TimeUnit;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import org.open.infc.grpc.Args;
25 import org.open.infc.grpc.Input;
26 import org.open.infc.grpc.OpenInterfaceGrpc;
27 import org.open.infc.grpc.Output;
28 import org.open.infc.grpc.Result;
30 import io.grpc.ManagedChannel;
31 import io.grpc.ManagedChannelBuilder;
32 import io.grpc.StatusRuntimeException;
34 public class OpenInterfaceGrpcClient {
35 private static final Logger logger = LoggerFactory.getLogger(OpenInterfaceGrpcClient.class.getName());
37 private final ManagedChannel channel;
38 private final OpenInterfaceGrpc.OpenInterfaceBlockingStub blockingStub;
41 private int timeout = 60000;
43 public static class OpenInterfaceGrpcExecption extends Exception {
44 private static final long serialVersionUID = -8755636432217894246L;
46 private int errorCode = -1;
48 public OpenInterfaceGrpcExecption(int errorCode, String message) {
50 this.errorCode = errorCode;
54 public static class OpenInterfaceGrpcTimeoutExecption extends OpenInterfaceGrpcExecption {
55 private static int errorCode = 1;
57 public OpenInterfaceGrpcTimeoutExecption(String message) {
58 super(errorCode, message);
62 public OpenInterfaceGrpcClient(String host, int port) {
63 this(ManagedChannelBuilder.forAddress(host, port)
64 // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
65 // needing certificates.
70 public OpenInterfaceGrpcClient(String host, int port, int timeout) {
72 this.timeout = timeout;
75 OpenInterfaceGrpcClient(ManagedChannel channel) {
76 this.channel = channel;
77 blockingStub = OpenInterfaceGrpc.newBlockingStub(channel);
80 public void shutdown() throws InterruptedException {
81 channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
84 public Output invoke(Input input) throws OpenInterfaceGrpcTimeoutExecption {
85 logger.info("Input {}", input);
87 Output result = Output.newBuilder().build();
89 result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).invoke(input);
90 } catch (StatusRuntimeException e) {
91 logger.warn("RPC failed: {}", e.getStatus());
92 //Status{code=DEADLINE_EXCEEDED}
93 throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage());
95 logger.info("Output: {}", result);
99 public Result remoteCli(Args args) throws OpenInterfaceGrpcTimeoutExecption {
100 logger.info(args.toString());
102 Result result = Result.newBuilder().setExitCode(1).build();
104 result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).remoteCli(args);
105 } catch (StatusRuntimeException e) {
106 logger.warn("RPC failed: {}", e.getStatus());
107 //Status{code=DEADLINE_EXCEEDED}
108 throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage());
111 logger.info("Result: {}", result);