0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ClientConnectionHandler.cc
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 #include "Common/Compat.h"
23 #include "Common/Error.h"
24 #include "Common/InetAddr.h"
25 #include "Common/System.h"
26 
28 #include "Master.h"
29 #include "Protocol.h"
30 #include "Session.h"
31 
32 using namespace Hyperspace;
33 using namespace Hypertable;
34 using namespace std;
35 
37  uint32_t timeout_ms)
38  : m_comm(comm), m_session(session), m_session_id(0), m_state(DISCONNECTED),
39  m_timeout_ms(timeout_ms), m_verbose(false), m_callbacks_enabled(true) {
40  memset(&m_master_addr, 0, sizeof(struct sockaddr_in));
41 }
42 
43 
49 }
50 
51 
53  lock_guard<recursive_mutex> lock(m_mutex);
54  int error;
55 
56  HT_DEBUGF("%s", event_ptr->to_str().c_str());
57 
58  if (event_ptr->type == Hypertable::Event::MESSAGE) {
59 
60  if (Protocol::response_code(event_ptr.get()) != Error::OK)
61  HT_FATALF("Connection handshake error: %s",
62  Protocol::string_format_message(event_ptr.get()).c_str());
63 
66 
68  }
69  else if (event_ptr->type == Hypertable::Event::DISCONNECT) {
70 
71  if (m_verbose) {
72  HT_WARNF("%s", event_ptr->to_str().c_str());
73  }
74 
77 
79  m_cond.notify_all();
80  }
81  else if (event_ptr->type == Hypertable::Event::CONNECTION_ESTABLISHED) {
82 
84 
85  memcpy(&m_master_addr, &event_ptr->addr, sizeof(struct sockaddr_in));
86 
88  cbp(Hyperspace::Protocol::create_handshake_request(m_session_id, System::exe_name));
89 
90  if ((error = m_comm->send_request(event_ptr->addr, m_timeout_ms, cbp, this))
91  != Error::OK) {
92  HT_FATALF("Problem sending handshake request - %s",
93  Error::get_text(error));
94  }
95  }
96  else
97  HT_FATALF("Unrecognized event - %s", event_ptr->to_str().c_str());
98 
99 }
100 
102  unique_lock<recursive_mutex> lock(m_mutex);
104  m_cond.wait(lock, [this](){ return m_state == DISCONNECTED; });
105 }
Retrieves system information (hardware, installation directory, etc)
#define HT_WARNF(msg,...)
Definition: Logger.h:290
static int32_t response_code(const Event *event)
Returns the response code from an event event generated in response to a request message.
Definition: Protocol.cc:39
static String string_format_message(const Event *event)
Returns error message decoded standard error MESSAGE generated in response to a request message...
Definition: Protocol.cc:51
Declarations for Protocol.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
STL namespace.
Connection established event.
Definition: Event.h:61
Hyperspace definitions
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
static String exe_name
The exe file name.
Definition: System.h:120
void close_socket(const CommAddress &addr)
Closes the socket specified by the addr argument.
Definition: Comm.cc:488
std::shared_ptr< CommBuf > CommBufPtr
Smart pointer to CommBuf.
Definition: CommBuf.h:305
Compatibility Macros for C/C++.
Connection disconnected event.
Definition: Event.h:62
Hyperspace session.
Definition: Session.h:148
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
#define HT_DEBUGF(msg,...)
Definition: Logger.h:260
Entry point to AsyncComm service.
Definition: Comm.h:61
int state_transition(int state)
Transions state (internal method)
Definition: Session.cc:1181
Internet address wrapper classes and utility functions.
Request/response message event.
Definition: Event.h:63
session is in jeopardy
Definition: Session.h:159
ClientConnectionHandler(Comm *comm, Session *session, uint32_t timeout_ms)
Error codes, Exception handling, error logging.
int send_request(const CommAddress &addr, uint32_t timeout_ms, CommBufPtr &cbuf, DispatchHandler *response_handler)
Sends a request message over a connection, expecting a response.
Definition: Comm.cc:300
virtual void handle(Hypertable::EventPtr &event_ptr)
Callback method.