| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
// Generated code
|
|
|
21 |
import tutorial.*;
|
|
|
22 |
import shared.*;
|
|
|
23 |
|
|
|
24 |
import org.apache.thrift.TException;
|
|
|
25 |
import org.apache.thrift.transport.TTransport;
|
|
|
26 |
import org.apache.thrift.transport.TSocket;
|
|
|
27 |
import org.apache.thrift.transport.TTransportException;
|
|
|
28 |
import org.apache.thrift.protocol.TBinaryProtocol;
|
|
|
29 |
import org.apache.thrift.protocol.TProtocol;
|
|
|
30 |
|
|
|
31 |
import java.util.AbstractMap;
|
|
|
32 |
import java.util.HashMap;
|
|
|
33 |
import java.util.HashSet;
|
|
|
34 |
import java.util.ArrayList;
|
|
|
35 |
|
|
|
36 |
public class JavaClient {
|
|
|
37 |
public static void main(String [] args) {
|
|
|
38 |
try {
|
|
|
39 |
|
|
|
40 |
TTransport transport = new TSocket("localhost", 9090);
|
|
|
41 |
TProtocol protocol = new TBinaryProtocol(transport);
|
|
|
42 |
Calculator.Client client = new Calculator.Client(protocol);
|
|
|
43 |
|
|
|
44 |
transport.open();
|
|
|
45 |
|
|
|
46 |
client.ping();
|
|
|
47 |
System.out.println("ping()");
|
|
|
48 |
|
|
|
49 |
int sum = client.add(1,1);
|
|
|
50 |
System.out.println("1+1=" + sum);
|
|
|
51 |
|
|
|
52 |
Work work = new Work();
|
|
|
53 |
|
|
|
54 |
work.op = Operation.DIVIDE;
|
|
|
55 |
work.num1 = 1;
|
|
|
56 |
work.num2 = 0;
|
|
|
57 |
try {
|
|
|
58 |
int quotient = client.calculate(1, work);
|
|
|
59 |
System.out.println("Whoa we can divide by 0");
|
|
|
60 |
} catch (InvalidOperation io) {
|
|
|
61 |
System.out.println("Invalid operation: " + io.why);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
work.op = Operation.SUBTRACT;
|
|
|
65 |
work.num1 = 15;
|
|
|
66 |
work.num2 = 10;
|
|
|
67 |
try {
|
|
|
68 |
int diff = client.calculate(1, work);
|
|
|
69 |
System.out.println("15-10=" + diff);
|
|
|
70 |
} catch (InvalidOperation io) {
|
|
|
71 |
System.out.println("Invalid operation: " + io.why);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
SharedStruct log = client.getStruct(1);
|
|
|
75 |
System.out.println("Check log: " + log.value);
|
|
|
76 |
|
|
|
77 |
transport.close();
|
|
|
78 |
|
|
|
79 |
} catch (TException x) {
|
|
|
80 |
x.printStackTrace();
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
}
|