blob: f10c72557e21c5e4fe9ab70559f174f04d22cc80 [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>
James E. King, III82ae9572017-08-05 12:23:54 -040025#include <thrift/stdcxx.h>
Mark Slee07a3aab2007-03-07 05:45:10 +000026
27#include "../gen-cpp/Calculator.h"
28
29using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000030using namespace apache::thrift;
31using namespace apache::thrift::protocol;
32using namespace apache::thrift::transport;
Mark Slee07a3aab2007-03-07 05:45:10 +000033
34using namespace tutorial;
35using namespace shared;
36
Konrad Grochowski157872d2014-11-06 19:55:28 +010037int main() {
James E. King, III82ae9572017-08-05 12:23:54 -040038 stdcxx::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
39 stdcxx::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
40 stdcxx::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
Mark Slee07a3aab2007-03-07 05:45:10 +000041 CalculatorClient client(protocol);
42
43 try {
44 transport->open();
David Reiss0c90f6f2008-02-06 22:18:40 +000045
Mark Slee07a3aab2007-03-07 05:45:10 +000046 client.ping();
Konrad Grochowskia8eec712014-09-04 00:56:27 +020047 cout << "ping()" << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +000048
Konrad Grochowskia8eec712014-09-04 00:56:27 +020049 cout << "1 + 1 = " << client.add(1, 1) << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000050
51 Work work;
Bryan Duxbury833ae492010-09-27 17:26:02 +000052 work.op = Operation::DIVIDE;
Mark Slee07a3aab2007-03-07 05:45:10 +000053 work.num1 = 1;
54 work.num2 = 0;
55
56 try {
Roger Meier585b7b82012-10-24 21:12:47 +000057 client.calculate(1, work);
Konrad Grochowskia8eec712014-09-04 00:56:27 +020058 cout << "Whoa? We can divide by zero!" << endl;
59 } catch (InvalidOperation& io) {
60 cout << "InvalidOperation: " << io.why << endl;
61 // or using generated operator<<: cout << io << endl;
Konrad Grochowski3b115df2015-05-18 17:58:36 +020062 // or by using std::exception native method what(): cout << io.what() << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000063 }
64
Bryan Duxbury833ae492010-09-27 17:26:02 +000065 work.op = Operation::SUBTRACT;
Mark Slee07a3aab2007-03-07 05:45:10 +000066 work.num1 = 15;
67 work.num2 = 10;
68 int32_t diff = client.calculate(1, work);
Konrad Grochowskia8eec712014-09-04 00:56:27 +020069 cout << "15 - 10 = " << diff << endl;
David Reiss0c90f6f2008-02-06 22:18:40 +000070
Mark Slee07a3aab2007-03-07 05:45:10 +000071 // Note that C++ uses return by reference for complex types to avoid
72 // costly copy construction
73 SharedStruct ss;
74 client.getStruct(ss, 1);
Konrad Grochowskia8eec712014-09-04 00:56:27 +020075 cout << "Received log: " << ss << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000076
77 transport->close();
Konrad Grochowskia8eec712014-09-04 00:56:27 +020078 } catch (TException& tx) {
79 cout << "ERROR: " << tx.what() << endl;
Mark Slee07a3aab2007-03-07 05:45:10 +000080 }
Mark Slee07a3aab2007-03-07 05:45:10 +000081}