0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Client.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Hypertable. If not, see <http://www.gnu.org/licenses/>
18  */
19 
20 #ifndef Hypertable_ThriftBroker_Client_h
21 #define Hypertable_ThriftBroker_Client_h
22 
23 // Note: do NOT add any hypertable dependencies in this file
24 #include <protocol/TBinaryProtocol.h>
25 #include <transport/TSocket.h>
26 #include <transport/TTransportUtils.h>
27 
28 #include "gen-cpp/HqlService.h"
29 
30 #include <memory>
31 
32 namespace Hypertable {
33 namespace Thrift {
34 
35 using namespace apache::thrift;
36 using namespace apache::thrift::protocol;
37 using namespace apache::thrift::transport;
38 
39 // helper to initialize base class of Client
40 struct ClientHelper {
41  boost::shared_ptr<TSocket> socket;
42  boost::shared_ptr<TTransport> transport;
43  boost::shared_ptr<TProtocol> protocol;
44 
45  ClientHelper(const std::string &host, int port, int timeout_ms)
46  : socket(new TSocket(host, port)),
47  transport(new TFramedTransport(socket)),
48  protocol(new TBinaryProtocol(transport)) {
49 
50  socket->setConnTimeout(timeout_ms);
51  socket->setSendTimeout(timeout_ms);
52  socket->setRecvTimeout(timeout_ms);
53  }
54 };
55 
59  class Client : private ClientHelper, public ThriftGen::HqlServiceClient {
60  public:
61  Client(const std::string &host, int port, int timeout_ms = 300000,
62  bool open = true)
63  : ClientHelper(host, port, timeout_ms), HqlServiceClient(protocol),
64  m_do_close(false) {
65 
66  if (open) {
67  transport->open();
68  m_do_close = true;
69  }
70  }
71 
72  virtual ~Client() {
73  if (m_do_close) {
74  transport->close();
75  m_do_close = false;
76  }
77  }
78 
79  private:
80  bool m_do_close;
81  };
82 
84  typedef std::shared_ptr<Client> ClientPtr;
85 
86 }} // namespace Hypertable::Thrift
87 
88 #endif // Hypertable_ThriftBroker_Client_h
Client(const std::string &host, int port, int timeout_ms=300000, bool open=true)
Definition: Client.h:61
ClientHelper(const std::string &host, int port, int timeout_ms)
Definition: Client.h:45
boost::shared_ptr< TTransport > transport
Definition: Client.h:42
Hypertable definitions
boost::shared_ptr< TSocket > socket
Definition: Client.h:41
boost::shared_ptr< TProtocol > protocol
Definition: Client.h:43
std::shared_ptr< Client > ClientPtr
Smart pointer to client.
Definition: Client.h:84
A client for the ThriftBroker.
Definition: Client.h:59