native_server.cpp 3.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (c) 2017 Baidu, Inc.
//
// Licensed 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
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

// A thrift server to receive EchoRequest and send back EchoResponse.

#include <gflags/gflags.h>

#include <butil/logging.h>

21
#include "gen-cpp/EchoService.h"
wangxuefeng's avatar
wangxuefeng committed
22

23 24 25 26 27 28 29
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/server/TNonblockingServer.h>
#include <thrift/concurrency/PosixThreadFactory.h>

30
// _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0
wangxuefeng's avatar
wangxuefeng committed
31
#include <thrift/TProcessor.h> // to include stdcxx.h if present
32 33 34
#ifndef THRIFT_STDCXX
 #if defined(_THRIFT_STDCXX_H_)
 # define THRIFT_STDCXX apache::thrift::stdcxx
wangxuefeng's avatar
wangxuefeng committed
35
 #include <thrift/transport/TNonblockingServerSocket.h>
36 37 38 39
 #else
 # define THRIFT_STDCXX boost
 #endif
#endif
40

41
DEFINE_int32(port, 8019, "Port of server");
42

43
class EchoServiceHandler : virtual public example::EchoServiceIf {
44
public:
wangxuefeng's avatar
wangxuefeng committed
45
    EchoServiceHandler() {}
46

wangxuefeng's avatar
wangxuefeng committed
47
    void Echo(example::EchoResponse& res, const example::EchoRequest& req) {
48
        // Process request, just attach a simple string.
wangxuefeng's avatar
wangxuefeng committed
49 50 51
        res.data = req.data + " world";
        return;
    }
52 53 54

};

55 56 57
int main(int argc, char *argv[]) {
    // Parse gflags. We recommend you to use gflags as well.
    google::ParseCommandLineFlags(&argc, &argv, true);
58

59 60
    THRIFT_STDCXX::shared_ptr<EchoServiceHandler> handler(new EchoServiceHandler());  
    THRIFT_STDCXX::shared_ptr<apache::thrift::concurrency::PosixThreadFactory> thread_factory(
61 62 63
        new apache::thrift::concurrency::PosixThreadFactory(
            apache::thrift::concurrency::PosixThreadFactory::ROUND_ROBIN,
            apache::thrift::concurrency::PosixThreadFactory::NORMAL, 1, false));
64

65
    THRIFT_STDCXX::shared_ptr<apache::thrift::server::TProcessor> processor(
wangxuefeng's avatar
wangxuefeng committed
66
        new example::EchoServiceProcessor(handler));
67
    THRIFT_STDCXX::shared_ptr<apache::thrift::protocol::TProtocolFactory> protocol_factory(
68
        new apache::thrift::protocol::TBinaryProtocolFactory());
69
    THRIFT_STDCXX::shared_ptr<apache::thrift::transport::TTransportFactory> transport_factory(
70
        new apache::thrift::transport::TBufferedTransportFactory());
71
    THRIFT_STDCXX::shared_ptr<apache::thrift::concurrency::ThreadManager> thread_mgr(
72
        apache::thrift::concurrency::ThreadManager::newSimpleThreadManager(2));
wangxuefeng's avatar
wangxuefeng committed
73

74 75 76 77
    thread_mgr->threadFactory(thread_factory);

    thread_mgr->start();

wangxuefeng's avatar
wangxuefeng committed
78 79 80 81
#if defined(_THRIFT_STDCXX_H_)
    THRIFT_STDCXX::shared_ptr<apache::thrift::transport::TNonblockingServerSocket> server_transport = 
        THRIFT_STDCXX::make_shared<apache::thrift::transport::TNonblockingServerSocket>(FLAGS_port);

82
    apache::thrift::server::TNonblockingServer server(processor,
83
        transport_factory, transport_factory, protocol_factory,
wangxuefeng's avatar
wangxuefeng committed
84 85 86 87 88 89
        protocol_factory, server_transport);
#else
    apache::thrift::server::TNonblockingServer server(processor,
        transport_factory, transport_factory, protocol_factory,
        protocol_factory, FLAGS_port);
#endif
90
    server.serve();  
wangxuefeng's avatar
wangxuefeng committed
91
    return 0;
92
}