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 | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <unistd.h> |
| 22 | #include <sys/time.h> |
| 23 | |
| 24 | #include <protocol/TBinaryProtocol.h> |
| 25 | #include <transport/TSocket.h> |
| 26 | #include <transport/TTransportUtils.h> |
| 27 | |
| 28 | #include "../gen-cpp/Calculator.h" |
| 29 | |
| 30 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 31 | using namespace apache::thrift; |
| 32 | using namespace apache::thrift::protocol; |
| 33 | using namespace apache::thrift::transport; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace tutorial; |
| 36 | using namespace shared; |
| 37 | |
| 38 | using namespace boost; |
| 39 | |
| 40 | int main(int argc, char** argv) { |
| 41 | shared_ptr<TTransport> socket(new TSocket("localhost", 9090)); |
| 42 | shared_ptr<TTransport> transport(new TBufferedTransport(socket)); |
| 43 | shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); |
| 44 | CalculatorClient client(protocol); |
| 45 | |
| 46 | try { |
| 47 | transport->open(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 48 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 49 | client.ping(); |
| 50 | printf("ping()\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 51 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 52 | int32_t sum = client.add(1,1); |
| 53 | printf("1+1=%d\n", sum); |
| 54 | |
| 55 | Work work; |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame^] | 56 | work.op = Operation::DIVIDE; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 57 | work.num1 = 1; |
| 58 | work.num2 = 0; |
| 59 | |
| 60 | try { |
| 61 | int32_t quotient = client.calculate(1, work); |
| 62 | printf("Whoa? We can divide by zero!\n"); |
| 63 | } catch (InvalidOperation &io) { |
| 64 | printf("InvalidOperation: %s\n", io.why.c_str()); |
| 65 | } |
| 66 | |
Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame^] | 67 | work.op = Operation::SUBTRACT; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 68 | work.num1 = 15; |
| 69 | work.num2 = 10; |
| 70 | int32_t diff = client.calculate(1, work); |
| 71 | printf("15-10=%d\n", diff); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 72 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 73 | // Note that C++ uses return by reference for complex types to avoid |
| 74 | // costly copy construction |
| 75 | SharedStruct ss; |
| 76 | client.getStruct(ss, 1); |
| 77 | printf("Check log: %s\n", ss.value.c_str()); |
| 78 | |
| 79 | transport->close(); |
| 80 | } catch (TException &tx) { |
| 81 | printf("ERROR: %s\n", tx.what()); |
| 82 | } |
| 83 | |
| 84 | } |