blob: a21125b2d2e1ea47a5a1efb2e0ecb3e438a49dfb [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#include <stdio.h>
2#include <unistd.h>
3#include <sys/time.h>
4
5#include <protocol/TBinaryProtocol.h>
6#include <transport/TSocket.h>
7#include <transport/TTransportUtils.h>
8
9#include "../gen-cpp/Calculator.h"
10
11using namespace std;
12using namespace facebook::thrift;
13using namespace facebook::thrift::protocol;
14using namespace facebook::thrift::transport;
15
16using namespace tutorial;
17using namespace shared;
18
19using namespace boost;
20
21int 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);
26
27 try {
28 transport->open();
David Reiss0c90f6f2008-02-06 22:18:40 +000029
Mark Slee07a3aab2007-03-07 05:45:10 +000030 client.ping();
31 printf("ping()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +000032
Mark Slee07a3aab2007-03-07 05:45:10 +000033 int32_t sum = client.add(1,1);
34 printf("1+1=%d\n", sum);
35
36 Work work;
37 work.op = DIVIDE;
38 work.num1 = 1;
39 work.num2 = 0;
40
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());
46 }
47
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);
David Reiss0c90f6f2008-02-06 22:18:40 +000053
Mark Slee07a3aab2007-03-07 05:45:10 +000054 // 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());
59
60 transport->close();
61 } catch (TException &tx) {
62 printf("ERROR: %s\n", tx.what());
63 }
64
65}