Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
thrift-client.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_RPC_THRIFT_CLIENT_H
17 #define IMPALA_RPC_THRIFT_CLIENT_H
18 
19 #include <ostream>
20 #include <sstream>
21 #include <boost/shared_ptr.hpp>
22 #include <common/status.h>
23 #include <thrift/Thrift.h>
24 #include <thrift/transport/TSocket.h>
25 #include <thrift/transport/TSSLSocket.h>
26 #include <thrift/transport/TBufferTransports.h>
27 #include <thrift/protocol/TBinaryProtocol.h>
28 #include <sstream>
29 #include <gflags/gflags.h>
30 
32 #include "transport/TSasl.h"
33 #include "rpc/authentication.h"
34 #include "rpc/thrift-server.h"
35 #include "gen-cpp/Types_types.h"
36 
37 DECLARE_string(principal);
38 DECLARE_string(hostname);
39 
40 namespace impala {
43  public:
45  Close();
46  }
47 
48  const TNetworkAddress& address() const { return address_; }
49 
52  Status Open();
53 
56  Status OpenWithRetry(uint32_t num_retries, uint64_t wait_ms);
57 
59  void Close();
60 
62  void setRecvTimeout(int32_t ms) { socket_->setRecvTimeout(ms); }
63 
65  void setSendTimeout(int32_t ms) { socket_->setSendTimeout(ms); }
66 
67  protected:
68  ThriftClientImpl(const std::string& ipaddress, int port, bool ssl)
69  : address_(MakeNetworkAddress(ipaddress, port)), ssl_(ssl) {
71  }
72 
76 
78  TNetworkAddress address_;
79 
81  bool ssl_;
82 
84 
87  boost::shared_ptr<sasl::TSasl> sasl_client_;
88 
90  boost::shared_ptr<apache::thrift::transport::TSocket> socket_;
91  boost::shared_ptr<apache::thrift::transport::TTransport> transport_;
92  boost::shared_ptr<apache::thrift::protocol::TBinaryProtocol> protocol_;
93 };
94 
95 
99 template <class InterfaceType>
101  public:
109  ThriftClient(const std::string& ipaddress, int port,
110  const std::string& service_name = "", AuthProvider* auth_provider = NULL,
111  bool ssl = false);
112 
114  InterfaceType* iface() { return iface_.get(); }
115 
116  private:
117  boost::shared_ptr<InterfaceType> iface_;
118 
120 };
121 
122 template <class InterfaceType>
123 ThriftClient<InterfaceType>::ThriftClient(const std::string& ipaddress, int port,
124  const std::string& service_name,
125  AuthProvider* auth_provider, bool ssl)
126  : ThriftClientImpl(ipaddress, port, ssl),
127  iface_(new InterfaceType(protocol_)),
128  auth_provider_(auth_provider) {
129 
130  transport_.reset(new apache::thrift::transport::TBufferedTransport(socket_));
131 
132  if (auth_provider_ == NULL) {
134  }
135 
136  auth_provider_->WrapClientTransport(address_.hostname, transport_, service_name,
137  &transport_);
138 
139  protocol_.reset(new apache::thrift::protocol::TBinaryProtocol(transport_));
140  iface_.reset(new InterfaceType(protocol_));
141 }
142 
143 }
144 #endif
DECLARE_string(principal)
bool ssl_
True if ssl encryption is enabled on this connection.
Definition: thrift-client.h:81
virtual Status WrapClientTransport(const std::string &hostname, boost::shared_ptr< apache::thrift::transport::TTransport > raw_transport, const std::string &service_name, boost::shared_ptr< apache::thrift::transport::TTransport > *wrapped_transport)=0
boost::shared_ptr< apache::thrift::transport::TSocket > socket_
All shared pointers, because Thrift requires them to be.
Definition: thrift-client.h:90
AuthProvider * GetInternalAuthProvider()
boost::shared_ptr< apache::thrift::transport::TTransport > transport_
Definition: thrift-client.h:91
InterfaceType * iface()
Returns the object used to actually make RPCs against the remote server.
AuthProvider * auth_provider_
TNetworkAddress MakeNetworkAddress(const string &hostname, int port)
Definition: network-util.cc:96
ThriftClientImpl(const std::string &ipaddress, int port, bool ssl)
Definition: thrift-client.h:68
void setSendTimeout(int32_t ms)
Set send timeout on the underlying TSocket.
Definition: thrift-client.h:65
boost::shared_ptr< InterfaceType > iface_
TNetworkAddress address_
Address of the server this client communicates with.
Definition: thrift-client.h:78
Status OpenWithRetry(uint32_t num_retries, uint64_t wait_ms)
boost::shared_ptr< sasl::TSasl > sasl_client_
Definition: thrift-client.h:87
Super class for templatized thrift clients.
Definition: thrift-client.h:42
static AuthManager * GetInstance()
boost::shared_ptr< apache::thrift::protocol::TBinaryProtocol > protocol_
Definition: thrift-client.h:92
const TNetworkAddress & address() const
Definition: thrift-client.h:48
ThriftClient(const std::string &ipaddress, int port, const std::string &service_name="", AuthProvider *auth_provider=NULL, bool ssl=false)
void Close()
Close the connection with the remote server. May be called repeatedly.
void setRecvTimeout(int32_t ms)
Set receive timeout on the underlying TSocket.
Definition: thrift-client.h:62