1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt.tool;
import com.yahoo.jrt.*;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
/**
* A generic rpc invoker for use by command line tools
*
* @author bratseth
*/
public class RpcInvoker {
private Value getArgument(Request request,String parameter) {
if (parameter.length()<=1 || parameter.charAt(1)!=':')
return new StringValue(parameter);
String value=parameter.substring(2);
switch (parameter.charAt(0)) {
case 'b':
return new Int8Value(Byte.parseByte(value));
case 'h':
return new Int16Value(Short.parseShort(value));
case 'i':
return new Int32Value(Integer.parseInt(value));
case 'l':
return new Int64Value(Long.parseLong(value));
case 'f':
return new FloatValue(Float.parseFloat(value));
case 'd':
return new DoubleValue(Double.parseDouble(value));
case 's':
return new StringValue(value);
}
throw new IllegalArgumentException("The first letter in '" + parameter + "' must be a type argument. " +
"There is no jrt type identified by '" + parameter.charAt(0) + "'");
}
protected Request createRequest(String method,List<String> arguments) {
Request request=new Request(method);
if (arguments!=null) {
for (String argument : arguments)
request.parameters().add(getArgument(request,argument));
}
return request;
}
/**
* Invokes a rpc method without throwing an exception
*
* @param connectspec the rpc server connection spec
* @param method the name of the method to invoke
* @param arguments the argument to the method, or null or an empty list if there are no arguments
*/
public void invoke(String connectspec,String method, List<String> arguments) {
Supervisor supervisor=null;
Target target=null;
try {
if (connectspec.indexOf('/')<0)
connectspec="tcp/" + connectspec;
supervisor=new Supervisor(new Transport());
target = supervisor.connect(new Spec(connectspec));
Request request=createRequest(method,arguments);
target.invokeSync(request,10.0);
if (request.isError()) {
System.err.println("error(" + request.errorCode() + "): " + request.errorMessage());
return;
}
Values returned=request.returnValues();
for (int i=0; i<returned.size(); i++) {
System.out.println(returned.get(i));
}
}
finally {
if (target!=null)
target.close();
if (supervisor!=null)
supervisor.transport().shutdown().join();
}
}
public static void main(String[] args) {
if (args.length<1) {
System.err.println("usage: invoke [-h <connectspec>] <method> [arguments]");
System.err.println(" Connectspec: This is on the form hostname:port, or tcp/hostname:port");
System.err.println(" if omitted, localhost:8086 is used");
System.err.println(" Arguments: each argument must be a string or on the form <type>:<value>");
System.err.println(" supported types: {'b','h','i','l','f','d','s'}");
System.exit(0);
}
List<String> arguments=new ArrayList<String>(Arrays.asList(args));
String connectSpec="localhost:8086";
if ("-h".equals(arguments.get(0)) && arguments.size()>=3) {
arguments.remove(0); // Consume -h
connectSpec=arguments.remove(0);
}
String method=arguments.remove(0);
new RpcInvoker().invoke(connectSpec,method,arguments);
}
}
|