Merge commit 'remotes/trunk'
[amiethrift.git] / tutorial / cpp / CppClient.cpp
blobb971160628493b8040b892ad312fe8c347e8c021
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/time.h>
5 #include <protocol/TBinaryProtocol.h>
6 #include <transport/TSocket.h>
7 #include <transport/TTransportUtils.h>
9 #include "../gen-cpp/Calculator.h"
11 using namespace std;
12 using namespace facebook::thrift;
13 using namespace facebook::thrift::protocol;
14 using namespace facebook::thrift::transport;
16 using namespace tutorial;
17 using namespace shared;
19 using namespace boost;
21 int main(int argc, char** argv) {
22 shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
23 shared_ptr<TTransport> transport(new TBufferedTransport(socket));
24 shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
25 CalculatorClient client(protocol);
27 try {
28 transport->open();
30 client.ping();
31 printf("ping()\n");
33 int32_t sum = client.add(1,1);
34 printf("1+1=%d\n", sum);
36 Work work;
37 work.op = DIVIDE;
38 work.num1 = 1;
39 work.num2 = 0;
41 try {
42 int32_t quotient = client.calculate(1, work);
43 printf("Whoa? We can divide by zero!\n");
44 } catch (InvalidOperation &io) {
45 printf("InvalidOperation: %s\n", io.why.c_str());
48 work.op = SUBTRACT;
49 work.num1 = 15;
50 work.num2 = 10;
51 int32_t diff = client.calculate(1, work);
52 printf("15-10=%d\n", diff);
54 // Note that C++ uses return by reference for complex types to avoid
55 // costly copy construction
56 SharedStruct ss;
57 client.getStruct(ss, 1);
58 printf("Check log: %s\n", ss.value.c_str());
60 transport->close();
61 } catch (TException &tx) {
62 printf("ERROR: %s\n", tx.what());