NodeHandle.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00025 #include "NodeHandle.h"
00026 
00027 // predefined node handle
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 //default-constructor
00049 NodeHandle::NodeHandle()
00050 {
00051     port = -1;
00052     key = OverlayKey(); // unspecified key, note: OverlayKey::UNSPECIFIED_KEY might not be initialized here
00053 }
00054 
00055 //copy constructor
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 //complete constructor
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 //public
00087 bool NodeHandle::isUnspecified() const
00088 {
00089     return (ip.isUnspecified() || key.isUnspecified());
00090 }
00091 
00092 //public
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 //public
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 //public
00111 bool NodeHandle::operator!=(const NodeHandle& rhs) const
00112 {
00113     assertUnspecified( rhs );
00114     return !operator==(rhs);
00115 }
00116 
00117 //public
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 //public
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 //public
00158 bool NodeHandle::operator<=(const NodeHandle &rhs) const
00159 {
00160     return !operator>(rhs);
00161 }
00162 
00163 //public
00164 bool NodeHandle::operator>=(const NodeHandle &rhs) const
00165 {
00166     return !operator<(rhs);
00167 }
00168 
00169 //public
00170 void NodeHandle::setKey( const OverlayKey& key )
00171 {
00172     this->key = key;
00173 }
00174 
00175 //public
00176 const OverlayKey& NodeHandle::getKey() const
00177 {
00178     return key;
00179 }
00180 
00181 
00182 //private
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     //cMessage::netPack(b);
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     //cMessage::netUnpack(b);
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 }
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3