ZG  "1.00
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
ZGPeerID.h
Go to the documentation of this file.
1 #ifndef ZGPeerID_h
2 #define ZGPeerID_h
3 
4 #include <string.h> // for strchr()
5 #include "support/Flattenable.h"
6 #include "util/String.h"
7 #include "zg/ZGNameSpace.h"
8 
9 namespace zg
10 {
11 
12 enum {ZG_PEER_ID_TYPE = 2053597540};
18 class ZGPeerID : public PseudoFlattenable
19 {
20 public:
22  ZGPeerID() : _highBits(0), _lowBits(0) {/* empty */}
23 
28  ZGPeerID(uint64 highBits, uint64 lowBits) : _highBits(highBits), _lowBits(lowBits) {/* empty */}
29 
33  bool operator == (const ZGPeerID & rhs) const {return ((_highBits == rhs._highBits)&&(_lowBits == rhs._lowBits));}
34 
38  bool operator != (const ZGPeerID & rhs) const {return !(*this==rhs);}
39 
43  bool operator < (const ZGPeerID & rhs) const {return ((_highBits < rhs._highBits)||((_highBits == rhs._highBits)&&(_lowBits < rhs._lowBits)));}
44 
48  bool operator >= (const ZGPeerID & rhs) const {return !(*this<rhs);}
49 
53  bool operator > (const ZGPeerID & rhs) const {return ((_highBits > rhs._highBits)||((_highBits == rhs._highBits)&&(_lowBits > rhs._lowBits)));}
54 
58  bool operator <= (const ZGPeerID & rhs) const {return !(*this>rhs);}
59 
63  ZGPeerID & operator = (const ZGPeerID & rhs) {_highBits = rhs._highBits; _lowBits = rhs._lowBits; return *this;}
64 
66  bool IsValid() const {return ((_highBits != 0)||(_lowBits != 0));}
67 
71  void FromString(const String & s)
72  {
73  _highBits = _lowBits = 0;
74 
75  const char * colon = strchr(s(), ':');
76  if (colon)
77  {
78  _highBits = Atoxll(s());
79  _lowBits = Atoxll(colon+1);
80  }
81  }
82 
84  String ToString() const
85  {
86  char buf[256];
87  muscleSprintf(buf, XINT64_FORMAT_SPEC ":" XINT64_FORMAT_SPEC, _highBits, _lowBits);
88  return buf;
89  }
90 
92  static MUSCLE_CONSTEXPR bool IsFixedSize() {return true;}
93 
95  static MUSCLE_CONSTEXPR uint32 TypeCode() {return ZG_PEER_ID_TYPE;}
96 
98  static MUSCLE_CONSTEXPR bool AllowsTypeCode(uint32 tc) {return (TypeCode()==tc);}
99 
101  static MUSCLE_CONSTEXPR uint32 FlattenedSize() {return 2*sizeof(uint64);}
102 
104  uint32 CalculateChecksum() const {return CalculateChecksumForUint64(_highBits) + (3*CalculateChecksumForUint64(_lowBits));}
105 
109  void Flatten(uint8 * buffer) const
110  {
111  muscleCopyOut(&buffer[0*sizeof(uint64)], B_HOST_TO_LENDIAN_INT64(_highBits));
112  muscleCopyOut(&buffer[1*sizeof(uint64)], B_HOST_TO_LENDIAN_INT64(_lowBits));
113  }
114 
120  status_t Unflatten(const uint8 * buffer, uint32 size)
121  {
122  if (size >= FlattenedSize())
123  {
124  _highBits = B_LENDIAN_TO_HOST_INT64(muscleCopyIn<uint64>(&buffer[0*sizeof(int64)]));
125  _lowBits = B_LENDIAN_TO_HOST_INT64(muscleCopyIn<uint64>(&buffer[1*sizeof(int64)]));
126  return B_NO_ERROR;
127  }
128  else return B_ERROR;
129  }
130 
134  uint32 HashCode() const {return CalculateChecksum();}
135 
136 private:
137  uint64 _highBits;
138  uint64 _lowBits;
139 };
140 
141 }; // end namespace zg
142 
143 #endif
ZGPeerID()
Default constructor – sets the ZGPeerID to its invalid state (all zeroes)
Definition: ZGPeerID.h:22
ZGPeerID & operator=(const ZGPeerID &rhs)
Assignment operator.
Definition: ZGPeerID.h:63
static MUSCLE_CONSTEXPR uint32 TypeCode()
Part of the Flattenable pseudo-interface: Returns ZG_PEER_ID_TYPE.
Definition: ZGPeerID.h:95
bool operator>=(const ZGPeerID &rhs) const
Comparison operator; returns true iff this ZGPeerID is greater than or equal to (rhs) ...
Definition: ZGPeerID.h:48
bool operator>(const ZGPeerID &rhs) const
Comparison operator; returns true iff this ZGPeerID is greater than (rhs)
Definition: ZGPeerID.h:53
void Flatten(uint8 *buffer) const
Copies this point into an endian-neutral flattened buffer.
Definition: ZGPeerID.h:109
static MUSCLE_CONSTEXPR bool IsFixedSize()
Part of the Flattenable pseudo-interface: Returns true.
Definition: ZGPeerID.h:92
String ToString() const
Returns a String representation of this peer ID (e.g.
Definition: ZGPeerID.h:84
uint32 HashCode() const
This is implemented so that if ZGPeerID is used as the key in a Hashtable, the HashCode() method will...
Definition: ZGPeerID.h:134
The zg namespace contains the public API of the ZG library.
bool operator==(const ZGPeerID &rhs) const
Equality operator; returns true iff this ZGPeerID is equal to (rhs)
Definition: ZGPeerID.h:33
bool operator<(const ZGPeerID &rhs) const
Comparison operator; returns true iff this ZGPeerID is less than (rhs)
Definition: ZGPeerID.h:43
bool IsValid() const
Returns true iff the ZGPeerID has any non-zero bits in it.
Definition: ZGPeerID.h:66
bool operator!=(const ZGPeerID &rhs) const
Inequality operator; returns true iff this ZGPeerID is not equal to (rhs)
Definition: ZGPeerID.h:38
static MUSCLE_CONSTEXPR bool AllowsTypeCode(uint32 tc)
Returns true iff (tc) equals ZG_PEER_ID_TYPE.
Definition: ZGPeerID.h:98
A ZGPeerID is a 128-bit integer that uniquely represents a particular peer in a ZG system...
Definition: ZGPeerID.h:18
bool operator<=(const ZGPeerID &rhs) const
Comparison operator; returns true iff this ZGPeerID is less than or equal to (rhs) ...
Definition: ZGPeerID.h:58
ZGPeerID(uint64 highBits, uint64 lowBits)
Explicit constructor.
Definition: ZGPeerID.h:28
static MUSCLE_CONSTEXPR uint32 FlattenedSize()
Part of the Flattenable pseudo-interface: 2*sizeof(uint64)
Definition: ZGPeerID.h:101
void FromString(const String &s)
Sets this peer ID from the specified String representation (in the format used by ToString())...
Definition: ZGPeerID.h:71
status_t Unflatten(const uint8 *buffer, uint32 size)
Restores this point from an endian-neutral flattened buffer.
Definition: ZGPeerID.h:120
uint32 CalculateChecksum() const
Returns a 32-bit checksum for this object.
Definition: ZGPeerID.h:104