0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ClientConnectionHandler.h
Go to the documentation of this file.
1 /*
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 this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #ifndef Hyperspace_ClientConnectionHandler_h
23 #define Hyperspace_ClientConnectionHandler_h
24 
25 #include <boost/thread/condition.hpp>
26 
27 #include <AsyncComm/Comm.h>
29 
30 #include <Common/Error.h>
31 #include <Common/InetAddr.h>
32 
33 #include <condition_variable>
34 #include <mutex>
35 
36 namespace Hyperspace {
37 
38  using namespace Hypertable;
39 
40  class Session;
41 
43  public:
44 
46 
47  ClientConnectionHandler(Comm *comm, Session *session, uint32_t timeout_ms);
48  virtual ~ClientConnectionHandler();
49 
50  virtual void handle(Hypertable::EventPtr &event_ptr);
51 
52  void set_session_id(uint64_t id) { m_session_id = id; }
53 
54  bool disconnected() {
55  std::lock_guard<std::recursive_mutex> lock(m_mutex);
56  return m_state == DISCONNECTED;
57  }
58 
59  int initiate_connection(struct sockaddr_in &addr) {
60  std::lock_guard<std::recursive_mutex> lock(m_mutex);
62  int error = m_comm->connect(addr, shared_from_this());
63 
64  if (error == Error::COMM_ALREADY_CONNECTED) {
65  HT_WARNF("Connection attempt to htHyperspace "
66  "at %s - %s", InetAddr::format(addr).c_str(),
67  Error::get_text(error));
68  error = Error::OK;
69  }
70 
71  if (error != Error::OK) {
72  HT_ERRORF("Problem establishing TCP connection with htHyperspace "
73  "at %s - %s", InetAddr::format(addr).c_str(),
74  Error::get_text(error));
75  m_comm->close_socket(addr);
77  }
78 
79  return error;
80  }
81 
82  void set_verbose_mode(bool verbose) { m_verbose = verbose; }
83 
85 
86  void close();
87 
88  private:
89  std::recursive_mutex m_mutex;
90  std::condition_variable_any m_cond;
91  Comm *m_comm {};
93  uint64_t m_session_id {};
94  int m_state {};
95  struct sockaddr_in m_master_addr;
96  uint32_t m_timeout_ms {};
97  bool m_verbose {};
99  };
100 
102  typedef std::shared_ptr<ClientConnectionHandler> ClientConnectionHandlerPtr;
103 
104 }
105 
106 #endif // Hyperspace_ClientConnectionHandler_h
#define HT_WARNF(msg,...)
Definition: Logger.h:290
int connect(const CommAddress &addr, const DispatchHandlerPtr &default_handler)
Establishes a TCP connection and attaches a default dispatch handler.
Definition: Comm.cc:134
Abstract base class for application dispatch handlers registered with AsyncComm.
int initiate_connection(struct sockaddr_in &addr)
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
std::shared_ptr< ClientConnectionHandler > ClientConnectionHandlerPtr
Shared smart pointer to ClientConnectionHandler.
Hyperspace definitions
Declarations for DispatchHandler.
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
void close_socket(const CommAddress &addr)
Closes the socket specified by the addr argument.
Definition: Comm.cc:488
String format(int sep= ':') const
Returns a string with a dotted notation ("127.0.0.1:8080") including the port.
Definition: InetAddr.h:132
Hyperspace session.
Definition: Session.h:148
Hypertable definitions
Entry point to AsyncComm service.
Definition: Comm.h:61
Declarations for Comm.
Internet address wrapper classes and utility functions.
#define HT_ERRORF(msg,...)
Definition: Logger.h:300
ClientConnectionHandler(Comm *comm, Session *session, uint32_t timeout_ms)
Error codes, Exception handling, error logging.
virtual void handle(Hypertable::EventPtr &event_ptr)
Callback method.