0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ConnectionHandler.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 
26 
27 #include <Common/Compat.h>
28 
29 #include "ConnectionHandler.h"
30 #include "Client.h"
32 
34 
35 #include <Common/Config.h>
36 #include <Common/Error.h>
37 #include <Common/FileUtils.h>
38 #include <Common/String.h>
39 #include <Common/StringExt.h>
40 #include <Common/Serialization.h>
41 
42 using namespace Hypertable;
43 using namespace Hypertable::FsBroker::Lib;
44 using namespace Config;
45 
47 
48  if (event->type == Event::MESSAGE) {
49 
50  //event->display()
51 
52  try {
53  if (event->header.command ==
55  uint16_t flags = 0;
56  ResponseCallback cb(m_comm, event);
57  if (event->payload_len >= 2) {
58  const uint8_t *ptr = event->payload;
59  size_t remain = event->payload_len;
60  flags = Serialization::decode_i16(&ptr, &remain);
61  }
62  if ((flags & Client::SHUTDOWN_FLAG_IMMEDIATE) != 0)
63  m_app_queue->shutdown();
64  m_broker->shutdown(&cb);
65  if (has("pidfile"))
66  FileUtils::unlink(get_str("pidfile"));
67  quick_exit(EXIT_SUCCESS);
68  }
69  m_app_queue->add(Request::Handler::Factory::create(m_comm, m_broker.get(),
70  event));
71  }
72  catch (Exception &e) {
73  ResponseCallback cb(m_comm, event);
74  HT_ERROR_OUT << e << HT_END;
75  String errmsg = format("%s - %s", e.what(), Error::get_text(e.code()));
76  cb.error(Error::PROTOCOL_ERROR, errmsg);
77  }
78  }
79  else if (event->type == Event::DISCONNECT) {
80  HT_DEBUGF("%s : Closing all open handles from %s", event->to_str().c_str(),
81  event->addr.format().c_str());
82  OpenFileMap &ofmap = m_broker->get_open_file_map();
83  ofmap.remove_all(event->addr);
84  }
85  else {
86  HT_DEBUGF("%s", event->to_str().c_str());
87  }
88 
89 }
90 
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
static bool unlink(const String &fname)
Unlinks (deletes) a file or directory.
Definition: FileUtils.cc:427
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
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
void remove_all(struct sockaddr_in &addr)
Definition: OpenFileMap.h:86
static ApplicationHandler * create(Comm *comm, Broker *broker, EventPtr &event)
Constructs a handler class from a request event.
Definition: Factory.cc:57
bool has(const String &name)
Check existence of a configuration value.
Definition: Config.h:57
virtual void handle(EventPtr &event)
Handles incoming events from client connections.
File system utility functions.
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
uint16_t decode_i16(const uint8_t **bufp, size_t *remainp)
Decode a 16-bit integer in little-endian order.
Compatibility Macros for C/C++.
#define HT_END
Definition: Logger.h:220
Functions to serialize/deserialize primitives to/from a memory buffer.
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_DEBUGF(msg,...)
Definition: Logger.h:260
virtual int error(int error, const String &msg)
Sends a standard error response back to the client.
Request/response message event.
Definition: Event.h:63
This is a generic exception class for Hypertable.
Definition: Error.h:314
A String class based on std::string.
Declarations for ConnectionHandler.
Declarations for ApplicationQueue.
Configuration settings.
File system broker framework and client library.
Definition: Broker.h:44
String extensions and helpers: sets, maps, append operators etc.
Error codes, Exception handling, error logging.
Declarations for FsBroker request handler Factory.
Declarations for Client.
int code() const
Returns the error code.
Definition: Error.h:391