David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 20 | // Generated code |
| 21 | import tutorial.*; |
| 22 | import shared.*; |
| 23 | |
Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame] | 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; |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 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 { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 39 | |
| 40 | TTransport transport = new TSocket("localhost", 9090); |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 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 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 73 | |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 74 | SharedStruct log = client.getStruct(1); |
| 75 | System.out.println("Check log: " + log.value); |
| 76 | |
| 77 | transport.close(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 78 | |
Mark Slee | cb39f08 | 2007-04-10 02:30:30 +0000 | [diff] [blame] | 79 | } catch (TException x) { |
| 80 | x.printStackTrace(); |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | } |