I want to add lib/cpp/src/thrift/*TThriftHelper*.h attached, but I can't
build it to test:

bison-3.0
autoconf-2.69
automake-1.15
libtool-2.4
openssl-1.0.2a
boost_1_57_0
libevent-2.0.22-stable
m4-1.4.17
SUSE Linux Enterprise Server 10 (x86_64)
gcc (GCC) 4.1.2

My forked branch: https://github.com/eyjian/thrift

Error message:
# dos2unix *.sh
# chmod +x *.sh
# ./bootstrap.sh
.*ibtoolize*: AC_CONFIG_MACRO_DIR([./aclocal]) *conflicts *with
ACLOCAL_AMFLAGS=-I ./aclocal
/*
 * 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
 *
 *   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.
 */

#ifndef _THRIFT_HELPER_H_
#define _THRIFT_HELPER_H_ 1

#include <arpa/inet.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TNonblockingServer.h>
#include <thrift/transport/TSocketPool.h>
#include <thrift/transport/TTransportException.h>
#include <thrift/Thrift.h>

/**
 * Simplify the use of thrift by TThriftServerHelper and TThriftClientHelper
 *
 * Example:
 * service EchoService
 * {
 *   void hello();
 * }
 *
 * Server:
 * #include <thrift/TThriftHelper.h>
 * int main()
 * {
 *   using namespace apache;
 *
 *   thrift::TThriftServerHelper<EchoHandler, EchoServiceProcessor> echoServerHelper_;
 *   echoServerHelper_.run(2015)
 *   return 0;
 * }
 *
 * Client:
 * #include <thrift/TThriftHelper.h>
 * int main()
 * {
 *   TThriftClientHelper<EchoServiceClient> echoClientHelper_("127.0.0.1", 2015);
 *   echoClientHelper_.connect();
 *   echoClientHelper_->hello();
 *   echoClientHelper_.close();
 *
 *   return 0;
 * }
 */

namespace apache {
namespace thrift {

inline bool thrift_not_connected(
        transport::TTransportException::TTransportExceptionType type)
{
    return (transport::TTransportException::NOT_OPEN == type)
        || (transport::TTransportException::END_OF_FILE == type);
}

/**
 * Simplify the server usage
 */
template <class ServiceHandler_, class ServiceProcessor_>
class TThriftServerHelper {
public:
  void stop();
  bool run(int port, size_t numThreads);

private:
  boost::shared_ptr<ServiceHandler_> handler_;
  boost::shared_ptr<TProcessor> processor_;
  boost::shared_ptr<protocol::TProtocolFactory> protocolFactory_;
  boost::shared_ptr<server::ThreadManager> threadManager_;
  boost::shared_ptr<concurrency::PosixThreadFactory> threadFactory_;
  boost::shared_ptr<server::TServer> server_;
};

/**
 * Simplify the client usage
 */
template <class ServiceClient_>
class TThriftClientHelper
{
public:
  TThriftClientHelper(const std::string& host, int port, int timeout = 1000);
  ~TThriftClientHelper();
  void connect();
  void close();

  ServiceClient_* operator ->() const
  {
    return serviceClient_.get();
  }

  ServiceClient_* get()
  {
    return serviceClient_.get();
  }

private:
  std::string host_;
  int port_;
  int timeout_;
  boost::shared_ptr<transport::TSocketPool> sockPool_;
  boost::shared_ptr<transport::TTransport> socket_;
  boost::shared_ptr<transport::TFramedTransport> transport_;
  boost::shared_ptr<protocol::TProtocol> protocol_;
  boost::shared_ptr<ServiceClient_> serviceClient_;
};

template <class ServiceHandler_, class ServiceProcessor_>
void TThriftServerHelper<ServiceHandler_, ServiceProcessor_>::stop()
{
    server_->stop();
}

template <class ServiceHandler_, class ServiceProcessor_>
bool TThriftServerHelper<ServiceHandler_, ServiceProcessor_>::run(int port, size_t numThreads)
{
  handler_.reset(new ServiceHandler_);
  processor_.reset(new ServiceProcessor_(handler_));
  protocolFactory_.reset(new protocol::TBinaryProtocolFactory());
  threadManager_ = server::ThreadManager::newSimpleThreadManager(numThreads);
  threadFactory_.reset(new concurrency::PosixThreadFactory());

  threadManager_->threadFactory(threadFactory_);
  threadManager_->start();

  server_.reset(new server::TNonblockingServer(processor_,
                                               protocolFactory_,
                                               port,
                                               threadManager_));

  server_->run();
}

template <class ServiceClient_>
TThriftClientHelper<ServiceClient_>::TThriftClientHelper(
        const std::string& host, int port, int timeout)
        : host_(host),
          port_(port),
          timeout_(timeout)
{
  sockPool_.reset(new thrift::transport::TSocketPool());
  sockPool_->addServer(host, port);
  sockPool_->setConnTimeout(timeout);
  sockPool_->setRecvTimeout(timeout);
  sockPool_->setSendTimeout(timeout);

  socket_ = sockPool_;
  transport_.reset(new transport::TFramedTransport(socket_));
  protocol_.reset(new protocol::TBinaryProtocol(transport_));

  serviceClient_.reset(new ServiceClient_(protocol_));
}

template <class ServiceClient_>
TThriftClientHelper<ServiceClient_>::~TThriftClientHelper()
{
  close();
}

template <class ServiceClient_>
void TThriftClientHelper<ServiceClient_>::connect()
{
  if (!transport_->isOpen())
  {
    transport_->open();
  }
}

template <class ServiceClient_>
void TThriftClientHelper<ServiceClient_>::close()
{
  if (transport_->isOpen())
  {
    transport_->close();
  }
}

}
} // apache::thrift

#endif // _THRIFT_HELPER_H_

Reply via email to