| 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 |  | 
| Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 20 | #include <iostream> | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 21 |  | 
| Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 22 | #include <thrift/protocol/TBinaryProtocol.h> | 
|  | 23 | #include <thrift/transport/TSocket.h> | 
|  | 24 | #include <thrift/transport/TTransportUtils.h> | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | #include "../gen-cpp/Calculator.h" | 
|  | 27 |  | 
|  | 28 | using namespace std; | 
| T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 29 | using namespace apache::thrift; | 
|  | 30 | using namespace apache::thrift::protocol; | 
|  | 31 | using namespace apache::thrift::transport; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 32 |  | 
|  | 33 | using namespace tutorial; | 
|  | 34 | using namespace shared; | 
|  | 35 |  | 
| Konrad Grochowski | 157872d | 2014-11-06 19:55:28 +0100 | [diff] [blame] | 36 | int main() { | 
| cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 37 | std::shared_ptr<TTransport> socket(new TSocket("localhost", 9090)); | 
|  | 38 | std::shared_ptr<TTransport> transport(new TBufferedTransport(socket)); | 
|  | 39 | std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 40 | CalculatorClient client(protocol); | 
|  | 41 |  | 
|  | 42 | try { | 
|  | 43 | transport->open(); | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 44 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 45 | client.ping(); | 
| Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 46 | cout << "ping()" << endl; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 47 |  | 
| Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 48 | cout << "1 + 1 = " << client.add(1, 1) << endl; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 49 |  | 
|  | 50 | Work work; | 
| Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 51 | work.op = Operation::DIVIDE; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 52 | work.num1 = 1; | 
|  | 53 | work.num2 = 0; | 
|  | 54 |  | 
|  | 55 | try { | 
| Roger Meier | 585b7b8 | 2012-10-24 21:12:47 +0000 | [diff] [blame] | 56 | client.calculate(1, work); | 
| Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 57 | cout << "Whoa? We can divide by zero!" << endl; | 
|  | 58 | } catch (InvalidOperation& io) { | 
|  | 59 | cout << "InvalidOperation: " << io.why << endl; | 
|  | 60 | // or using generated operator<<: cout << io << endl; | 
| Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 61 | // or by using std::exception native method what(): cout << io.what() << endl; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Bryan Duxbury | 833ae49 | 2010-09-27 17:26:02 +0000 | [diff] [blame] | 64 | work.op = Operation::SUBTRACT; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 65 | work.num1 = 15; | 
|  | 66 | work.num2 = 10; | 
|  | 67 | int32_t diff = client.calculate(1, work); | 
| Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 68 | cout << "15 - 10 = " << diff << endl; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 69 |  | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 70 | // Note that C++ uses return by reference for complex types to avoid | 
|  | 71 | // costly copy construction | 
|  | 72 | SharedStruct ss; | 
|  | 73 | client.getStruct(ss, 1); | 
| Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 74 | cout << "Received log: " << ss << endl; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 75 |  | 
|  | 76 | transport->close(); | 
| Konrad Grochowski | a8eec71 | 2014-09-04 00:56:27 +0200 | [diff] [blame] | 77 | } catch (TException& tx) { | 
|  | 78 | cout << "ERROR: " << tx.what() << endl; | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 79 | } | 
| Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 80 | } |