blob: 02e7b17ce8c4fd23f3a1da39f1c6d07359321cde [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Grochowskia8eec712014-09-04 00:56:27 +020020#include <iostream>
Mark Slee07a3aab2007-03-07 05:45:10 +000021
Roger Meier49ff8b12012-04-13 09:12:31 +000022#include <thrift/protocol/TBinaryProtocol.h>
23#include <thrift/transport/TSocket.h>
24#include <thrift/transport/TTransportUtils.h>
Mark Slee07a3aab2007-03-07 05:45:10 +000025
26#include "../gen-cpp/Calculator.h"
27
28using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000029using namespace apache::thrift;
30using namespace apache::thrift::protocol;
31using namespace apache::thrift::transport;
Mark Slee07a3aab2007-03-07 05:45:10 +000032
33using namespace tutorial;
34using namespace shared;
35
Konrad Grochowski157872d2014-11-06 19:55:28 +010036int main() {
cyy316723a2019-01-05 16:35:14 +080037 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 Slee07a3aab2007-03-07 05:45:10 +000040 CalculatorClient client(protocol);
41
42 try {
43 transport->open();
David Reiss0c90f6f2008-02-06 22:18:40 +000044
Mark Slee07a3aab2007-03-07 05:45:10 +000045 client.ping();
CJCombrink4a280d52024-03-14 19:57:41 +010046 cout << "ping()" << '\n';
David Reiss0c90f6f2008-02-06 22:18:40 +000047
CJCombrink4a280d52024-03-14 19:57:41 +010048 cout << "1 + 1 = " << client.add(1, 1) << '\n';
Mark Slee07a3aab2007-03-07 05:45:10 +000049
50 Work work;
Bryan Duxbury833ae492010-09-27 17:26:02 +000051 work.op = Operation::DIVIDE;
Mark Slee07a3aab2007-03-07 05:45:10 +000052 work.num1 = 1;
53 work.num2 = 0;
54
55 try {
Roger Meier585b7b82012-10-24 21:12:47 +000056 client.calculate(1, work);
CJCombrink4a280d52024-03-14 19:57:41 +010057 cout << "Whoa? We can divide by zero!" << '\n';
Konrad Grochowskia8eec712014-09-04 00:56:27 +020058 } catch (InvalidOperation& io) {
CJCombrink4a280d52024-03-14 19:57:41 +010059 cout << "InvalidOperation: " << io.why << '\n';
60 // or using generated operator<<: cout << io << '\n';
61 // or by using std::exception native method what(): cout << io.what() << '\n';
Mark Slee07a3aab2007-03-07 05:45:10 +000062 }
63
Bryan Duxbury833ae492010-09-27 17:26:02 +000064 work.op = Operation::SUBTRACT;
Mark Slee07a3aab2007-03-07 05:45:10 +000065 work.num1 = 15;
66 work.num2 = 10;
67 int32_t diff = client.calculate(1, work);
CJCombrink4a280d52024-03-14 19:57:41 +010068 cout << "15 - 10 = " << diff << '\n';
David Reiss0c90f6f2008-02-06 22:18:40 +000069
Mark Slee07a3aab2007-03-07 05:45:10 +000070 // Note that C++ uses return by reference for complex types to avoid
71 // costly copy construction
72 SharedStruct ss;
73 client.getStruct(ss, 1);
CJCombrink4a280d52024-03-14 19:57:41 +010074 cout << "Received log: " << ss << '\n';
Mark Slee07a3aab2007-03-07 05:45:10 +000075
76 transport->close();
Konrad Grochowskia8eec712014-09-04 00:56:27 +020077 } catch (TException& tx) {
CJCombrink4a280d52024-03-14 19:57:41 +010078 cout << "ERROR: " << tx.what() << '\n';
Mark Slee07a3aab2007-03-07 05:45:10 +000079 }
Mark Slee07a3aab2007-03-07 05:45:10 +000080}