| 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()" << '\n'; |
| 47 | |
| 48 | cout << "1 + 1 = " << client.add(1, 1) << '\n'; |
| 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!" << '\n'; |
| 58 | } catch (InvalidOperation& io) { |
| 59 | cout << "InvalidOperation: " << io.why << '\n'; |
| 60 | // or using generated operator<<: cout << io << '\n'; |
| 61 | // or by using std::exception native method what(): cout << io.what() << '\n'; |
| 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 << '\n'; |
| 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 << '\n'; |
| 75 | |
| 76 | transport->close(); |
| 77 | } catch (TException& tx) { |
| 78 | cout << "ERROR: " << tx.what() << '\n'; |
| 79 | } |
| 80 | } |