native_client.cpp 2.52 KB
Newer Older
1 2 3 4 5 6 7
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11 12 13 14 15 16
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
17 18 19 20

// A thrift client sending requests to server every 1 second.

#include <gflags/gflags.h>
21 22 23 24 25 26
#include "gen-cpp/EchoService.h"
#include "gen-cpp/echo_types.h"
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>

27 28
#include <butil/logging.h>

29 30 31 32 33 34
// _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0
#ifndef THRIFT_STDCXX
 #if defined(_THRIFT_STDCXX_H_)
 # define THRIFT_STDCXX apache::thrift::stdcxx
 #else
 # define THRIFT_STDCXX boost
35
 # include <boost/make_shared.hpp>
36 37 38
 #endif
#endif

39 40
DEFINE_string(server, "0.0.0.0", "IP Address of server");
DEFINE_int32(port, 8019, "Port of server");
41 42 43

int main(int argc, char **argv) {

44 45 46
    // Parse gflags. We recommend you to use gflags as well.
    google::ParseCommandLineFlags(&argc, &argv, true);

47
    THRIFT_STDCXX::shared_ptr<apache::thrift::transport::TSocket> socket(
wangxuefeng's avatar
wangxuefeng committed
48
        new apache::thrift::transport::TSocket(FLAGS_server, FLAGS_port));
49
    THRIFT_STDCXX::shared_ptr<apache::thrift::transport::TTransport> transport(
wangxuefeng's avatar
wangxuefeng committed
50
        new apache::thrift::transport::TFramedTransport(socket));
51
    THRIFT_STDCXX::shared_ptr<apache::thrift::protocol::TProtocol> protocol(
wangxuefeng's avatar
wangxuefeng committed
52
        new apache::thrift::protocol::TBinaryProtocol(transport));
53 54 55 56 57

    example::EchoServiceClient client(protocol);
    transport->open();

    example::EchoRequest req;
58 59
    req.__set_data("hello");
    req.__set_need_by_proxy(10);
60 61

    example::EchoResponse res;
62

63
    while (1) {
64 65
        try {
            client.Echo(res, req);
66
            LOG(INFO) << "Req=" << req << " Res=" << res;
67 68 69
        } catch (std::exception& e) {
            LOG(ERROR) << "Fail to rpc, " << e.what();
        }
70
        sleep(1);
71
    }
72
    transport->close();
73

74
    return 0;
75
}