/* * 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_CLIENTUTIL_H_ #define THRIFT_CLIENTUTIL_H_ 1 #include "thrift/lib/cpp/transport/TBufferTransports.h" #include "thrift/lib/cpp/transport/TSocket.h" #include "thrift/lib/cpp/protocol/TBinaryProtocol.h" namespace apache { namespace thrift { namespace util { /* * Versions that accept a host and port */ template ClientT* createClient(const std::string& host, uint16_t port) { boost::shared_ptr socket( new transport::TSocket(host, port)); // We could specialize this to not create a wrapper transport when // TransportT is TTransport or TSocket. However, everyone should always // use a TFramedTransport or TBufferedTransport wrapper for performance // reasons. boost::shared_ptr transport(new TransportT(socket)); boost::shared_ptr protocol( new ProtocolT(transport)); transport->open(); return new ClientT(protocol); } template boost::shared_ptr createClientPtr(const std::string& host, uint16_t port) { return boost::shared_ptr( createClient(host, port)); } template ClientT* createClient(const std::string& host, uint16_t port, bool useFramed = true) { if (useFramed) { return createClient( host, port); } else { return createClient( host, port); } } template ClientT* createClient(const std::string& host, uint16_t port, bool useFramed = true) { return createClient >( host, port, useFramed); } template boost::shared_ptr createClientPtr(const std::string& host, uint16_t port, bool useFramed = true) { return boost::shared_ptr( createClient(host, port, useFramed)); } template boost::shared_ptr createClientPtr(const std::string& host, uint16_t port, bool useFramed = true) { return boost::shared_ptr( createClient(host, port, useFramed)); } /* * Versions that accept TSocketAddress */ template ClientT* createClient(const transport::TSocketAddress* address) { boost::shared_ptr socket( new transport::TSocket(address)); // We could specialize this to not create a wrapper transport when // TransportT is TTransport or TSocket. However, everyone should always // use a TFramedTransport or TBufferedTransport wrapper for performance // reasons. boost::shared_ptr transport(new TransportT(socket)); boost::shared_ptr protocol( new ProtocolT(transport)); transport->open(); return new ClientT(protocol); } template boost::shared_ptr createClientPtr( const transport::TSocketAddress* address) { return boost::shared_ptr( createClient(address)); } template ClientT* createClient(const transport::TSocketAddress* address, bool useFramed = true) { if (useFramed) { return createClient( address); } else { return createClient( address); } } template ClientT* createClient(const transport::TSocketAddress* address, bool useFramed = true) { return createClient >( address, useFramed); } template boost::shared_ptr createClientPtr( const transport::TSocketAddress* address, bool useFramed = true) { return boost::shared_ptr( createClient(address, useFramed)); } template boost::shared_ptr createClientPtr( const transport::TSocketAddress* address, bool useFramed = true) { return boost::shared_ptr( createClient(address, useFramed)); } /* * Versions that accept TSocketAddress and socket options */ template ClientT* createClient(const transport::TSocketAddress* address, const transport::TSocket::Options& options) { boost::shared_ptr socket( new transport::TSocket(address)); socket->setSocketOptions(options); // We could specialize this to not create a wrapper transport when // TransportT is TTransport or TSocket. However, everyone should always // use a TFramedTransport or TBufferedTransport wrapper for performance // reasons. boost::shared_ptr transport(new TransportT(socket)); boost::shared_ptr protocol( new ProtocolT(transport)); transport->open(); return new ClientT(protocol); } template boost::shared_ptr createClientPtr( const transport::TSocketAddress* address, const transport::TSocket::Options& options) { return boost::shared_ptr( createClient(address), options); } template ClientT* createClient(const transport::TSocketAddress* address, const transport::TSocket::Options& options, bool useFramed = true) { if (useFramed) { return createClient( address, options); } else { return createClient( address, options); } } template ClientT* createClient(const transport::TSocketAddress* address, const transport::TSocket::Options& options, bool useFramed = true ) { return createClient >( address, options, useFramed); } template boost::shared_ptr createClientPtr( const transport::TSocketAddress* address, const transport::TSocket::Options& options, bool useFramed = true) { return boost::shared_ptr( createClient(address, options, useFramed)); } template boost::shared_ptr createClientPtr( const transport::TSocketAddress* address, const transport::TSocket::Options& options, bool useFramed = true) { return boost::shared_ptr( createClient(address, options, useFramed)); } }}} // apache::thrift::util #endif // THRIFT_CLIENTUTIL_H_