Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 1 | #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 | |
| 11 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 12 | using namespace apache::thrift; |
| 13 | using namespace apache::thrift::protocol; |
| 14 | using namespace apache::thrift::transport; |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace tutorial; |
| 17 | using namespace shared; |
| 18 | |
| 19 | using namespace boost; |
| 20 | |
| 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); |
| 26 | |
| 27 | try { |
| 28 | transport->open(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 29 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 30 | client.ping(); |
| 31 | printf("ping()\n"); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 32 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 33 | 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 Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 53 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 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()); |
| 59 | |
| 60 | transport->close(); |
| 61 | } catch (TException &tx) { |
| 62 | printf("ERROR: %s\n", tx.what()); |
| 63 | } |
| 64 | |
| 65 | } |