]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/tutorial/cpp/CppClient.cpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / tutorial / cpp / CppClient.cpp
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
20 #include <iostream>
21
22 #include <thrift/protocol/TBinaryProtocol.h>
23 #include <thrift/transport/TSocket.h>
24 #include <thrift/transport/TTransportUtils.h>
25
26 #include "../gen-cpp/Calculator.h"
27
28 using namespace std;
29 using namespace apache::thrift;
30 using namespace apache::thrift::protocol;
31 using namespace apache::thrift::transport;
32
33 using namespace tutorial;
34 using namespace shared;
35
36 int main() {
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));
40 CalculatorClient client(protocol);
41
42 try {
43 transport->open();
44
45 client.ping();
46 cout << "ping()" << endl;
47
48 cout << "1 + 1 = " << client.add(1, 1) << endl;
49
50 Work work;
51 work.op = Operation::DIVIDE;
52 work.num1 = 1;
53 work.num2 = 0;
54
55 try {
56 client.calculate(1, work);
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;
61 // or by using std::exception native method what(): cout << io.what() << endl;
62 }
63
64 work.op = Operation::SUBTRACT;
65 work.num1 = 15;
66 work.num2 = 10;
67 int32_t diff = client.calculate(1, work);
68 cout << "15 - 10 = " << diff << endl;
69
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);
74 cout << "Received log: " << ss << endl;
75
76 transport->close();
77 } catch (TException& tx) {
78 cout << "ERROR: " << tx.what() << endl;
79 }
80 }