0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ServerConnectionHandler.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/Config.h"
24 #include "Common/Error.h"
25 #include "Common/StringExt.h"
26 
28 #include "AsyncComm/Comm.h"
29 
30 #include "Protocol.h"
53 
54 using namespace std;
55 using namespace Hypertable;
56 using namespace Hyperspace;
57 using namespace Serialization;
58 using namespace Error;
59 
60 ServerConnectionHandler::ServerConnectionHandler(ApplicationQueuePtr &app_queue,
61  MasterPtr &master)
62  : m_app_queue(app_queue), m_master(master), m_session_id(0) {
64  m_maintenance_interval = Config::properties->get_i32("Hyperspace.Maintenance.Interval");
65 }
66 
67 
68 
69 /*
70  *
71  */
73 
74  //HT_INFOF("%s", event->to_str().c_str());
75 
76  if (event->type == Hypertable::Event::MESSAGE) {
77  ApplicationHandler *handler = 0;
78 
79  try {
80 
81  // sanity check command code
82  if (event->header.command >= Protocol::COMMAND_MAX)
83  HT_THROWF(Error::PROTOCOL_ERROR, "Invalid command (%llu)",
84  (Llu)event->header.command);
85 
86  // if this is not the current replication master then try to return
87  // addr of current master
88  if (!m_master->is_master())
90  m_master->get_current_master());
91 
92  switch (event->header.command) {
93  case Protocol::COMMAND_HANDSHAKE:
94  {
95  const uint8_t *decode = event->payload;
96  size_t decode_remain = event->payload_len;
97 
98  m_session_id = decode_i64(&decode, &decode_remain);
99  if (m_session_id == 0)
100  HT_THROW(Error::PROTOCOL_ERROR, "Bad session id: 0");
101  handler = new RequestHandlerHandshake(m_comm, m_master.get(),
102  m_session_id, event);
103 
104  }
105  break;
107  handler = new RequestHandlerOpen(m_comm, m_master.get(),
108  m_session_id, event);
109  break;
111  handler = new RequestHandlerClose(m_comm, m_master.get(),
112  m_session_id, event);
113  break;
115  handler = new RequestHandlerMkdir(m_comm, m_master.get(),
116  m_session_id, event);
117  break;
119  handler = new RequestHandlerDelete(m_comm, m_master.get(),
120  m_session_id, event);
121  break;
123  handler = new RequestHandlerAttrSet(m_comm, m_master.get(),
124  m_session_id, event);
125  break;
127  handler = new RequestHandlerAttrGet(m_comm, m_master.get(),
128  m_session_id, event);
129  break;
131  handler = new RequestHandlerAttrIncr(m_comm, m_master.get(),
132  m_session_id, event);
133  break;
135  handler = new RequestHandlerAttrExists(m_comm, m_master.get(),
136  m_session_id, event);
137  break;
139  handler = new RequestHandlerAttrList(m_comm, m_master.get(),
140  m_session_id, event);
141  break;
143  handler = new RequestHandlerAttrDel(m_comm, m_master.get(),
144  m_session_id, event);
145  break;
147  handler = new RequestHandlerExists(m_comm, m_master.get(),
148  m_session_id, event);
149  break;
151  handler = new RequestHandlerReaddir(m_comm, m_master.get(),
152  m_session_id, event);
153  break;
155  handler = new RequestHandlerReaddirAttr(m_comm, m_master.get(),
156  m_session_id, event);
157  break;
159  handler = new RequestHandlerReadpathAttr(m_comm, m_master.get(),
160  m_session_id, event);
161  break;
163  handler = new RequestHandlerLock(m_comm, m_master.get(),
164  m_session_id, event);
165  break;
167  handler = new RequestHandlerRelease(m_comm, m_master.get(),
168  m_session_id, event);
169  break;
171  handler = new RequestHandlerStatus(m_comm, m_master.get(), event);
172  break;
174  handler = new RequestHandlerShutdown(m_comm, m_master.get(),
175  m_session_id, event);
176  break;
177  default:
178  HT_THROWF(Error::PROTOCOL_ERROR, "Unimplemented command (%llu)",
179  (Llu)event->header.command);
180  }
181  m_app_queue->add(handler);
182  }
183  catch (Exception &e) {
184  ResponseCallback cb(m_comm, event);
185  HT_ERROR_OUT << e << HT_END;
186  String errmsg = format("%s - %s", e.what(), Error::get_text(e.code()));
187  cb.error(Error::PROTOCOL_ERROR, errmsg);
188  }
189  }
190  else if (event->type == Hypertable::Event::CONNECTION_ESTABLISHED) {
191  HT_INFOF("%s", event->to_str().c_str());
192  }
193  else if (event->type == Hypertable::Event::DISCONNECT) {
195  cout << flush;
196  }
197  else if (event->type == Hypertable::Event::TIMER) {
198  int error;
199  try {
201  }
202  catch (Exception &e) {
203  HT_ERROR_OUT << e << HT_END;
204  }
205 
206  if ((error = m_comm->set_timer(m_maintenance_interval, shared_from_this())) != Error::OK)
207  HT_FATALF("Problem setting timer - %s", Error::get_text(error));
208 
209  }
210 
211  else {
212  HT_INFOF("%s", event->to_str().c_str());
213  }
214 
215 }
static Comm * instance()
Creates/returns singleton instance of the Comm class.
Definition: Comm.h:72
virtual void handle(EventPtr &event)
Callback method.
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
Declarations for RequestHandlerAttrDel.
Declarations for Protocol.
long long unsigned int Llu
Shortcut for printf formats.
Definition: String.h:50
std::shared_ptr< Master > MasterPtr
Definition: Master.h:362
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
STL namespace.
Unmarshalls request parameters and calls Master::attr_del().
Connection established event.
Definition: Event.h:61
Hyperspace definitions
Declarations for RequestHandlerAttrExists.
uint64_t decode_i64(const uint8_t **bufp, size_t *remainp)
Decode a 64-bit integer in little-endian order.
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
Compatibility Macros for C/C++.
#define HT_END
Definition: Logger.h:220
Connection disconnected event.
Definition: Event.h:62
#define HT_ERROR_OUT
Definition: Logger.h:301
This class is used to generate and deliver standard responses back to a client.
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
virtual int error(int error, const String &msg)
Sends a standard error response back to the client.
Declarations for Comm.
#define HT_INFOF(msg,...)
Definition: Logger.h:272
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
Request/response message event.
Definition: Event.h:63
Timer event
Definition: Event.h:65
This is a generic exception class for Hypertable.
Definition: Error.h:314
Base clase for application handlers.
int set_timer(uint32_t duration_millis, const DispatchHandlerPtr &handler)
Sets a timer for duration_millis milliseconds in the future.
Definition: Comm.cc:465
Declarations for ApplicationQueue.
Configuration settings.
Unmarshalls request parameters and calls Master::attr_exists().
std::shared_ptr< ApplicationQueue > ApplicationQueuePtr
Shared smart pointer to ApplicationQueue object.
String extensions and helpers: sets, maps, append operators etc.
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
int code() const
Returns the error code.
Definition: Error.h:391