Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
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
 
20
#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
 
30
using namespace std;
31
using namespace apache::thrift;
32
using namespace apache::thrift::protocol;
33
using namespace apache::thrift::transport;
34
 
35
using namespace tutorial;
36
using namespace shared;
37
 
38
using namespace boost;
39
 
40
int 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();
48
 
49
    client.ping();
50
    printf("ping()\n");
51
 
52
    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);
72
 
73
    // 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
}