blob: 4218019b51b1caf6fc07c2e74935f21c735c845b [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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
Mark Slee07a3aab2007-03-07 05:45:10 +000020#include <stdio.h>
21#include <unistd.h>
22#include <sys/time.h>
23
Roger Meier49ff8b12012-04-13 09:12:31 +000024#include <thrift/protocol/TBinaryProtocol.h>
25#include <thrift/transport/TSocket.h>
26#include <thrift/transport/TTransportUtils.h>
Mark Slee07a3aab2007-03-07 05:45:10 +000027
28#include "../gen-cpp/Calculator.h"
29
30using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000031using namespace apache::thrift;
32using namespace apache::thrift::protocol;
33using namespace apache::thrift::transport;
Mark Slee07a3aab2007-03-07 05:45:10 +000034
35using namespace tutorial;
36using namespace shared;
37
Mark Slee07a3aab2007-03-07 05:45:10 +000038int main(int argc, char** argv) {
Roger Meier5f2d34e2013-11-16 16:43:41 +010039 boost::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
40 boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
41 boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
Mark Slee07a3aab2007-03-07 05:45:10 +000042 CalculatorClient client(protocol);
43
44 try {
45 transport->open();
David Reiss0c90f6f2008-02-06 22:18:40 +000046
Mark Slee07a3aab2007-03-07 05:45:10 +000047 client.ping();
48 printf("ping()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +000049
Mark Slee07a3aab2007-03-07 05:45:10 +000050 int32_t sum = client.add(1,1);
51 printf("1+1=%d\n", sum);
52
53 Work work;
Bryan Duxbury833ae492010-09-27 17:26:02 +000054 work.op = Operation::DIVIDE;
Mark Slee07a3aab2007-03-07 05:45:10 +000055 work.num1 = 1;
56 work.num2 = 0;
57
58 try {
Roger Meier585b7b82012-10-24 21:12:47 +000059 client.calculate(1, work);
Mark Slee07a3aab2007-03-07 05:45:10 +000060 printf("Whoa? We can divide by zero!\n");
61 } catch (InvalidOperation &io) {
62 printf("InvalidOperation: %s\n", io.why.c_str());
63 }
64
Bryan Duxbury833ae492010-09-27 17:26:02 +000065 work.op = Operation::SUBTRACT;
Mark Slee07a3aab2007-03-07 05:45:10 +000066 work.num1 = 15;
67 work.num2 = 10;
68 int32_t diff = client.calculate(1, work);
69 printf("15-10=%d\n", diff);
David Reiss0c90f6f2008-02-06 22:18:40 +000070
Mark Slee07a3aab2007-03-07 05:45:10 +000071 // Note that C++ uses return by reference for complex types to avoid
72 // costly copy construction
73 SharedStruct ss;
74 client.getStruct(ss, 1);
75 printf("Check log: %s\n", ss.value.c_str());
76
77 transport->close();
78 } catch (TException &tx) {
79 printf("ERROR: %s\n", tx.what());
80 }
81
82}