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; //NOSONAR
47 public OpenInterfaceGrpcExecption(int errorCode, String message) {
49 this.errorCode = errorCode;
53 public static class OpenInterfaceGrpcTimeoutExecption extends OpenInterfaceGrpcExecption {
54 private static final int ERROR_CODE = 1;
56 public OpenInterfaceGrpcTimeoutExecption(String message) {
57 super(ERROR_CODE, message);
61 public OpenInterfaceGrpcClient(String host, int port) {
62 this(ManagedChannelBuilder.forAddress(host, port)
63 // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
64 // needing certificates.
69 public OpenInterfaceGrpcClient(String host, int port, int timeout) {
71 this.timeout = timeout;
74 OpenInterfaceGrpcClient(ManagedChannel channel) {
75 this.channel = channel;
76 blockingStub = OpenInterfaceGrpc.newBlockingStub(channel);
79 public void shutdown() throws InterruptedException {
80 channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
83 public Output invoke(Input input) throws OpenInterfaceGrpcTimeoutExecption {
84 logger.info("Input {}", input);
86 Output result = Output.newBuilder().build();
88 result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).invoke(input);
89 } catch (StatusRuntimeException e) {
90 logger.warn("RPC failed: {}", e.getStatus());
91 //Status{code=DEADLINE_EXCEEDED}
92 throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage());
94 logger.info("Output: {}", result);
98 public Result remoteCli(Args args) throws OpenInterfaceGrpcTimeoutExecption {
99 logger.info("{}", args);
101 Result result = Result.newBuilder().setExitCode(1).build();
103 result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).remoteCli(args);
104 } catch (StatusRuntimeException e) {
105 logger.warn("RPC failed: {}", e.getStatus());
106 //Status{code=DEADLINE_EXCEEDED}
107 throw new OpenInterfaceGrpcTimeoutExecption(e.getMessage());
110 logger.info("Result: {}", result);