0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Comm.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 
28 #ifndef AsyncComm_Comm_h
29 #define AsyncComm_Comm_h
30 
31 #include "Clock.h"
32 #include "CommAddress.h"
33 #include "CommBuf.h"
35 #include "DispatchHandler.h"
36 #include "HandlerMap.h"
37 #include "RawSocketHandler.h"
38 
39 #include <atomic>
40 #include <mutex>
41 
44 namespace Hypertable {
45 
61  class Comm {
62  public:
63 
72  static Comm *instance() {
73  std::lock_guard<std::mutex> lock(ms_mutex);
74 
75  if (!ms_instance)
76  ms_instance = new Comm();
77 
78  return ms_instance;
79  }
80 
85  static void destroy();
86 
97  int register_socket(int sd, const CommAddress &addr,
98  RawSocketHandler *handler);
99 
125  int connect(const CommAddress &addr, const DispatchHandlerPtr &default_handler);
126 
153  int connect(const CommAddress &addr, const CommAddress &local_addr,
154  const DispatchHandlerPtr &default_handler);
155 
170  int set_alias(const InetAddr &addr, const InetAddr &alias);
171 
186  int add_proxy(const String &proxy, const String &hostname, const InetAddr &addr);
187 
198  int remove_proxy(const String &proxy);
199 
214  bool translate_proxy(const String &proxy, InetAddr *addr);
215 
219  void get_proxy_map(ProxyMapT &proxy_map);
220 
227  bool wait_for_proxy_load(Timer &timer);
228 
242  void listen(const CommAddress &addr, ConnectionHandlerFactoryPtr &chf);
243 
259  void listen(const CommAddress &addr, ConnectionHandlerFactoryPtr &chf,
260  const DispatchHandlerPtr &default_handler);
261 
307  int send_request(const CommAddress &addr, uint32_t timeout_ms,
308  CommBufPtr &cbuf, DispatchHandler *response_handler);
309 
333  int send_response(const CommAddress &addr, CommBufPtr &cbuf);
334 
356  void create_datagram_receive_socket(CommAddress &addr, int tos,
357  const DispatchHandlerPtr &handler);
358 
370  int send_datagram(const CommAddress &addr, const CommAddress &send_addr,
371  CommBufPtr &cbuf);
372 
391  int set_timer(uint32_t duration_millis, const DispatchHandlerPtr &handler);
392 
410  int set_timer_absolute(ClockT::time_point expire_time, const DispatchHandlerPtr &handler);
411 
418  void cancel_timer(const DispatchHandlerPtr &handler);
419 
426  void close_socket(const CommAddress &addr);
427 
435  void find_available_tcp_port(InetAddr &addr);
436 
444  void find_available_udp_port(InetAddr &addr);
445 
446  private:
447 
449  Comm();
450 
452  ~Comm();
453 
474  int send_request(IOHandlerData *data_handler, uint32_t timeout_ms,
475  CommBufPtr &cbuf, DispatchHandler *response_handler);
476 
497  int connect_socket(int sd, const CommAddress &addr,
498  const DispatchHandlerPtr &default_handler);
499 
501  static Comm *ms_instance;
502 
504  static std::atomic<uint32_t> ms_next_request_id;
505 
508 
511 
514 
517 
519  bool m_verbose {};
520  };
521 
524 }
525 
526 #endif // AsyncComm_Comm_h
static Comm * instance()
Creates/returns singleton instance of the Comm class.
Definition: Comm.h:72
Declarations for ConnectionHandlerFactory.
static std::mutex mutex
Definition: Logger.cc:43
int register_socket(int sd, const CommAddress &addr, RawSocketHandler *handler)
Registers an externally managed socket with comm event loop.
Definition: Comm.cc:109
static std::atomic< uint32_t > ms_next_request_id
Atomic integer used for assinging request IDs.
Definition: Comm.h:504
bool m_verbose
Verbose flag.
Definition: Comm.h:519
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
Declarations for RawSocketHandler.
int connect(const CommAddress &addr, const DispatchHandlerPtr &default_handler)
Establishes a TCP connection and attaches a default dispatch handler.
Definition: Comm.cc:134
chrono::time_point< fast_clock > time_point
Definition: fast_clock.h:42
Abstract base class for application dispatch handlers registered with AsyncComm.
bool wait_for_proxy_load(Timer &timer)
Waits until a CommHeader::FLAGS_BIT_PROXY_MAP_UPDATE message is received from the proxy master...
Definition: Comm.cc:235
int set_alias(const InetAddr &addr, const InetAddr &alias)
Sets an alias for a TCP connection.
Definition: Comm.cc:204
bool translate_proxy(const String &proxy, InetAddr *addr)
Translates a proxy name to an IP address.
Definition: Comm.cc:225
Declarations for CommAddress.
~Comm()
Destructor.
Definition: Comm.cc:91
int send_datagram(const CommAddress &addr, const CommAddress &send_addr, CommBufPtr &cbuf)
Sends a datagram to a remote address.
Definition: Comm.cc:437
static void destroy()
Destroys singleton instance of the Comm class.
Definition: Comm.cc:102
Abstract base class for application raw socket handlers registered with AsyncComm.
InetAddr m_local_addr
Local address initialized to primary interface and empty port.
Definition: Comm.h:516
Declaration of ClockT.
Declarations for DispatchHandler.
std::shared_ptr< HandlerMap > HandlerMapPtr
Smart pointer to HandlerMap.
Definition: HandlerMap.h:437
std::shared_ptr< ConnectionHandlerFactory > ConnectionHandlerFactoryPtr
Smart pointer to ConnectionHandlerFactory.
void find_available_tcp_port(InetAddr &addr)
Finds an unused TCP port starting from addr.
Definition: Comm.cc:511
Encapsulate an internet address.
Definition: InetAddr.h:66
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
std::unordered_map< String, ProxyAddressInfo > ProxyMapT
Forward mapping hash type from proxy name to ProxyAddressInfo.
Definition: ProxyMap.h:57
int connect_socket(int sd, const CommAddress &addr, const DispatchHandlerPtr &default_handler)
Creates a TCP socket connection.
Definition: Comm.cc:589
I/O handler for TCP sockets.
Definition: IOHandlerData.h:51
std::shared_ptr< Reactor > ReactorPtr
Shared smart pointer to Reactor.
Definition: Reactor.h:295
Declarations for HandlerMap.
ReactorPtr m_timer_reactor
Pointer to dedicated reactor for handling timer events.
Definition: Comm.h:513
Comm()
Private constructor (prevent non-singleton usage).
Definition: Comm.cc:74
int send_response(const CommAddress &addr, CommBufPtr &cbuf)
Sends a response message back over a connection.
Definition: Comm.cc:343
static Comm * ms_instance
Pointer to singleton instance of this class.
Definition: Comm.h:501
Hypertable definitions
void find_available_udp_port(InetAddr &addr)
Finds an unused UDP port starting from addr.
Definition: Comm.cc:547
Entry point to AsyncComm service.
Definition: Comm.h:61
static std::mutex ms_mutex
Mutex for serializing access to ms_instance
Definition: Comm.h:507
void get_proxy_map(ProxyMapT &proxy_map)
Returns the proxy map.
Definition: Comm.cc:231
Declarations for CommBuf.
HandlerMapPtr m_handler_map
Pointer to IOHandler map.
Definition: Comm.h:510
void create_datagram_receive_socket(CommAddress &addr, int tos, const DispatchHandlerPtr &handler)
Creates a socket for receiving datagrams and attaches handler as the default dispatch handler...
Definition: Comm.cc:367
A timer class to keep timeout states across AsyncComm related calls.
Definition: Timer.h:44
std::shared_ptr< DispatchHandler > DispatchHandlerPtr
Smart pointer to DispatchHandler.
void listen(const CommAddress &addr, ConnectionHandlerFactoryPtr &chf)
Creates listen (accept) socket on addr.
Definition: Comm.cc:209
int set_timer_absolute(ClockT::time_point expire_time, const DispatchHandlerPtr &handler)
Sets a timer for absolute time expire_time.
Definition: Comm.cc:475
int set_timer(uint32_t duration_millis, const DispatchHandlerPtr &handler)
Sets a timer for duration_millis milliseconds in the future.
Definition: Comm.cc:465
void cancel_timer(const DispatchHandlerPtr &handler)
Cancels all scheduled timers registered with the dispatch handler handler.
Definition: Comm.cc:483
void alias(const String &cmdline_opt, const String &file_opt, bool overwrite)
Setup command line option alias for config file option.
Definition: Config.cc:607
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
int add_proxy(const String &proxy, const String &hostname, const InetAddr &addr)
Adds a proxy name for a TCP connection.
Definition: Comm.cc:215
Address abstraction to hold either proxy name or IPv4:port address.
Definition: CommAddress.h:52
int remove_proxy(const String &proxy)
Removes a proxy name for a TCP connection.
Definition: Comm.cc:220