NodeHandle.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00025 #include "NodeHandle.h"
00026
00027
00028 const NodeHandle NodeHandle::UNSPECIFIED_NODE;
00029
00030 std::ostream& operator<<(std::ostream& os, const NodeHandle& n)
00031 {
00032 if (n.ip.isUnspecified()) {
00033 os << "<addr unspec> ";
00034 } else {
00035 os << n.ip << ":" << n.port << " ";
00036 }
00037
00038 if (n.key.isUnspecified()) {
00039 os << "<key unspec>";
00040 } else {
00041 os << n.key;
00042 }
00043
00044 return os;
00045 };
00046
00047
00048
00049 NodeHandle::NodeHandle()
00050 {
00051 port = -1;
00052 key = OverlayKey();
00053 }
00054
00055
00056 NodeHandle::NodeHandle( const NodeHandle& handle )
00057 {
00058 key = handle.key;
00059 port = handle.port;
00060 ip = handle.ip;
00061 }
00062
00063 NodeHandle::NodeHandle( const TransportAddress& ta )
00064 {
00065 this->setAddress(ta.getAddress());
00066 this->setPort(ta.getPort());
00067 this->setKey(OverlayKey::UNSPECIFIED_KEY);
00068 }
00069
00070
00071 NodeHandle::NodeHandle( const OverlayKey& key,
00072 const IPvXAddress& ip, int port )
00073 {
00074 this->setAddress(ip);
00075 this->setPort(port);
00076 this->setKey(key);
00077 }
00078
00079 NodeHandle::NodeHandle( const OverlayKey& key, const TransportAddress& ta )
00080 {
00081 this->setAddress(ta.getAddress());
00082 this->setPort(ta.getPort());
00083 this->setKey(key);
00084 }
00085
00086
00087 bool NodeHandle::isUnspecified() const
00088 {
00089 return (ip.isUnspecified() || key.isUnspecified());
00090 }
00091
00092
00093 NodeHandle& NodeHandle::operator=(const NodeHandle& rhs)
00094 {
00095 this->key = rhs.key;
00096 this->ip = rhs.ip;
00097 this->port = rhs.port;
00098
00099 return *this;
00100 }
00101
00102
00103 bool NodeHandle::operator==(const NodeHandle& rhs) const
00104 {
00105 assertUnspecified(rhs);
00106 return (this->port == rhs.port && this->ip == rhs.ip &&
00107 this->key == rhs.key);
00108 }
00109
00110
00111 bool NodeHandle::operator!=(const NodeHandle& rhs) const
00112 {
00113 assertUnspecified( rhs );
00114 return !operator==(rhs);
00115 }
00116
00117
00118 bool NodeHandle::operator<(const NodeHandle &rhs) const
00119 {
00120 assertUnspecified(rhs);
00121 int cmp = key.compareTo(rhs.key);
00122 if (cmp < 0) {
00123 return true;
00124 } else if (cmp > 0) {
00125 return false;
00126 } else if (ip < rhs.ip) {
00127 return true;
00128 } else if (rhs.ip < ip) {
00129 return false;
00130 } else if (port < rhs.port) {
00131 return true;
00132 }
00133
00134 return false;
00135 }
00136
00137
00138 bool NodeHandle::operator>(const NodeHandle &rhs) const
00139 {
00140 assertUnspecified(rhs);
00141 int cmp = key.compareTo(rhs.key);
00142 if (cmp > 0) {
00143 return true;
00144 } else if (cmp < 0) {
00145 return false;
00146 } else if (rhs.ip < ip) {
00147 return true;
00148 } else if (ip < rhs.ip) {
00149 return false;
00150 } else if (port > rhs.port) {
00151 return true;
00152 }
00153
00154 return false;
00155 }
00156
00157
00158 bool NodeHandle::operator<=(const NodeHandle &rhs) const
00159 {
00160 return !operator>(rhs);
00161 }
00162
00163
00164 bool NodeHandle::operator>=(const NodeHandle &rhs) const
00165 {
00166 return !operator<(rhs);
00167 }
00168
00169
00170 void NodeHandle::setKey( const OverlayKey& key )
00171 {
00172 this->key = key;
00173 }
00174
00175
00176 const OverlayKey& NodeHandle::getKey() const
00177 {
00178 return key;
00179 }
00180
00181
00182
00183 inline void NodeHandle::assertUnspecified( const NodeHandle& handle ) const
00184 {
00185 if ( this->isUnspecified() || handle.isUnspecified() )
00186 opp_error("NodeHandle: Trying to compare unspecified NodeHandle!");
00187 }
00188
00189
00190 void NodeHandle::netPack(cCommBuffer *b)
00191 {
00192
00193 doPacking(b,this->ip);
00194 doPacking(b,this->key);
00195 doPacking(b,this->port);
00196 }
00197
00198 void NodeHandle::netUnpack(cCommBuffer *b)
00199 {
00200
00201 doUnpacking(b,this->ip);
00202 doUnpacking(b,this->key);
00203 doUnpacking(b,this->port);
00204 }
00205
00206 TransportAddress* NodeHandle::dup() const
00207 {
00208 return new NodeHandle(*this);
00209 }