0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CommAddress.h
Go to the documentation of this file.
1 /* -*- c++ -*-
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; version 3
9  * of the License.
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_COMMADDRESS_H
29 #define AsyncComm_COMMADDRESS_H
30 
31 #include <Common/InetAddr.h>
32 #include <Common/Logger.h>
33 #include <Common/String.h>
34 
35 #include <functional>
36 #include <set>
37 #include <unordered_map>
38 
39 namespace Hypertable {
40 
52  class CommAddress {
53  public:
54 
57  enum AddressType {
58  NONE=0,
61  };
62 
66 
70  CommAddress(const sockaddr_in addr) : m_type(INET) { inet=addr; }
71 
76  void set_proxy(const String &str) { proxy = str; m_type=PROXY; }
77 
82  void set_inet(sockaddr_in addr) { inet = addr; m_type=INET; }
83 
89  CommAddress &operator=(sockaddr_in addr)
90  { inet = addr; m_type=INET; return *this; }
91 
102  bool operator==(const CommAddress &other) const {
103  if (m_type != other.m_type)
104  return false;
105  if (m_type == PROXY)
106  return proxy.compare(other.proxy) == 0;
107  else if (m_type == INET)
108  return inet == other.inet;
109  return true;
110  }
111 
117  bool operator!=(const CommAddress &other) const {
118  return !(*this == other);
119  }
120 
132  bool operator<(const CommAddress &other) const {
133  if (m_type != other.m_type)
134  return m_type < other.m_type;
135  if (m_type == PROXY)
136  return proxy < other.proxy;
137  else if (m_type == INET)
138  return inet < other.inet;
139  HT_ASSERT(m_type == NONE && other.m_type == NONE);
140  return false;
141  }
142 
147  bool is_proxy() const { return m_type == PROXY; }
148 
153  bool is_inet() const { return m_type == INET; }
154 
159  bool is_set() const { return m_type == PROXY || m_type == INET; }
160 
164  void clear() { proxy=""; m_type=NONE; }
165 
173  String to_str() const;
174 
177 
178  private:
179  int32_t m_type;
180  };
181 
187  public:
197  size_t operator () (const CommAddress &addr) const {
198  if (addr.is_inet())
199  return (size_t)(addr.inet.sin_addr.s_addr ^ addr.inet.sin_port);
200  else if (addr.is_proxy())
201  return std::hash<std::string>()(addr.proxy);
202  return 0;
203  }
204  };
205 
207  template<typename TypeT, typename addr=CommAddress>
208  class CommAddressMap : public std::unordered_map<addr, TypeT, CommAddressHash> {
209  };
210 
212  typedef std::set<CommAddress> CommAddressSet;
214 } // namespace Hypertable
215 
216 #endif // AsyncComm_COMMADDRESS_H
AddressType
Enumeration for address type.
Definition: CommAddress.h:57
String proxy
Proxy name.
Definition: CommAddress.h:175
CommAddress & operator=(sockaddr_in addr)
Sets address type to CommAddress::INET and inet value to addr.
Definition: CommAddress.h:89
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
bool operator==(const CommAddress &other) const
Equality operator.
Definition: CommAddress.h:102
Parameterized hash map for mapping CommAddress to arbitrary type.
Definition: CommAddress.h:208
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
IPv4:port address type.
Definition: CommAddress.h:60
CommAddress(const sockaddr_in addr)
Constructs a CommAddress object of type CommAddress::INET.
Definition: CommAddress.h:70
#define HT_ASSERT(_e_)
Definition: Logger.h:396
CommAddress()
Constructs an uninitialized CommAddress object.
Definition: CommAddress.h:65
bool operator<(const CommAddress &other) const
Less than operator.
Definition: CommAddress.h:132
void set_inet(sockaddr_in addr)
Sets address type to CommAddress::INET and inet value to addr.
Definition: CommAddress.h:82
Hash function (functor) for CommAddress objets.
Definition: CommAddress.h:186
Encapsulate an internet address.
Definition: InetAddr.h:66
Logging routines and macros.
void set_proxy(const String &str)
Sets address type to CommAddress::PROXY and proxy name to p.
Definition: CommAddress.h:76
String to_str() const
Returns string representation of address.
Definition: CommAddress.cc:34
bool is_set() const
Returns true if address has been initialized.
Definition: CommAddress.h:159
bool is_inet() const
Returns true if address is of type CommAddress::INET.
Definition: CommAddress.h:153
Hypertable definitions
std::set< CommAddress > CommAddressSet
Set of CommAddress objects.
Definition: CommAddress.h:212
Internet address wrapper classes and utility functions.
bool operator!=(const CommAddress &other) const
Inequality operator.
Definition: CommAddress.h:117
A String class based on std::string.
InetAddr inet
IPv4:port address.
Definition: CommAddress.h:176
size_t operator()(const CommAddress &addr) const
Parenthesis operator with a single CommAddress parameter.
Definition: CommAddress.h:197
bool is_proxy() const
Returns true if address is of type CommAddress::PROXY.
Definition: CommAddress.h:147
int32_t m_type
Address type.
Definition: CommAddress.h:179
Address abstraction to hold either proxy name or IPv4:port address.
Definition: CommAddress.h:52
void clear()
Clears address to uninitialized state.
Definition: CommAddress.h:164