aboutsummaryrefslogtreecommitdiffstats
path: root/grpc/grpc-client/src
diff options
context:
space:
mode:
Diffstat (limited to 'grpc/grpc-client/src')
-rw-r--r--grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java12
-rw-r--r--grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java47
2 files changed, 56 insertions, 3 deletions
diff --git a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java
index 9a818d7a..e32110de 100644
--- a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java
+++ b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenInterfaceGrpcClient.java
@@ -37,6 +37,9 @@ public class OpenInterfaceGrpcClient {
private final ManagedChannel channel;
private final OpenInterfaceGrpc.OpenInterfaceBlockingStub blockingStub;
+ //10 seconds
+ private int timeout = 60000;
+
public static class OpenInterfaceGrpcExecption extends Exception {
private static final long serialVersionUID = -8755636432217894246L;
@@ -64,6 +67,11 @@ public class OpenInterfaceGrpcClient {
.build());
}
+ public OpenInterfaceGrpcClient(String host, int port, int timeout) {
+ this(host, port);
+ this.timeout = timeout;
+
+ }
OpenInterfaceGrpcClient(ManagedChannel channel) {
this.channel = channel;
blockingStub = OpenInterfaceGrpc.newBlockingStub(channel);
@@ -78,7 +86,7 @@ public class OpenInterfaceGrpcClient {
Output result = Output.newBuilder().build();
try {
- result = blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS).invoke(input);
+ result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).invoke(input);
} catch (StatusRuntimeException e) {
logger.warn("RPC failed: {0}", e.getStatus());
//Status{code=DEADLINE_EXCEEDED}
@@ -93,7 +101,7 @@ public class OpenInterfaceGrpcClient {
Result result = Result.newBuilder().setExitCode(1).build();
try {
- result = blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS).remoteCli(args);
+ result = blockingStub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).remoteCli(args);
} catch (StatusRuntimeException e) {
logger.warn("RPC failed: {0}", e.getStatus());
//Status{code=DEADLINE_EXCEEDED}
diff --git a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java
index fc0c2aac..724267e8 100644
--- a/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java
+++ b/grpc/grpc-client/src/main/java/org/open/infc/grpc/client/OpenRemoteCli.java
@@ -26,6 +26,51 @@ import org.open.infc.grpc.Output;
import org.open.infc.grpc.Result;
public class OpenRemoteCli {
+
+ private String host;
+ private int port;
+ private int timeout;
+ private String requestId;
+
+ public OpenRemoteCli(String host, int port, int timeout, String requestId) {
+ this.host = host;
+ this.port = port;
+ this.timeout = timeout;
+ this.requestId = requestId;
+
+ }
+
+ public Result run (List <String> args) throws Exception {
+ OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient(
+ host, port, timeout);
+ try {
+ Result result = client.remoteCli(Args.newBuilder().setRequestId(this.requestId).addAllArgs(args).build());
+ return result;
+ } finally {
+ client.shutdown();
+ }
+ }
+
+ public Output invoke (String product, String profile, String action, Map <String, String> params) throws Exception {
+ OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient(
+ host, port, timeout);
+ try {
+
+ Map <String, String> options = new HashMap<>();
+ options.put("product", product);
+ if (profile != null && !profile.isEmpty())
+ options.put("profile", profile);
+ params.put("format", "json");
+ Input input = Input.newBuilder().setAction(action).setRequestId(requestId).putAllOptions(options).putAllParams(params).build();
+
+ Output output = client.invoke(input);
+ return output;
+ } finally {
+ client.shutdown();
+ }
+ }
+
+ //Absolute the static methods
/**
* Runs CLI remotely
* @param host
@@ -48,7 +93,7 @@ public class OpenRemoteCli {
}
}
- /**
+ /**
* Runs commands as remote procedure call :)
* @param host
* @param port