blob: a3f17feecce2a8e170aaa5477646826ecae30846 [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
24#include <protocol/TBinaryProtocol.h>
25#include <transport/TSocket.h>
26#include <transport/TTransportUtils.h>
27
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
38using namespace boost;
39
40int main(int argc, char** argv) {
41 shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
42 shared_ptr<TTransport> transport(new TBufferedTransport(socket));
43 shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
44 CalculatorClient client(protocol);
45
46 try {
47 transport->open();
David Reiss0c90f6f2008-02-06 22:18:40 +000048
Mark Slee07a3aab2007-03-07 05:45:10 +000049 client.ping();
50 printf("ping()\n");
David Reiss0c90f6f2008-02-06 22:18:40 +000051
Mark Slee07a3aab2007-03-07 05:45:10 +000052 int32_t sum = client.add(1,1);
53 printf("1+1=%d\n", sum);
54
55 Work work;
56 work.op = DIVIDE;
57 work.num1 = 1;
58 work.num2 = 0;
59
60 try {
61 int32_t quotient = client.calculate(1, work);
62 printf("Whoa? We can divide by zero!\n");
63 } catch (InvalidOperation &io) {
64 printf("InvalidOperation: %s\n", io.why.c_str());
65 }
66
67 work.op = SUBTRACT;
68 work.num1 = 15;
69 work.num2 = 10;
70 int32_t diff = client.calculate(1, work);
71 printf("15-10=%d\n", diff);
David Reiss0c90f6f2008-02-06 22:18:40 +000072
Mark Slee07a3aab2007-03-07 05:45:10 +000073 // Note that C++ uses return by reference for complex types to avoid
74 // costly copy construction
75 SharedStruct ss;
76 client.getStruct(ss, 1);
77 printf("Check log: %s\n", ss.value.c_str());
78
79 transport->close();
80 } catch (TException &tx) {
81 printf("ERROR: %s\n", tx.what());
82 }
83
84}