TransportAddress.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 <omnetpp.h>
00026 
00027 #include "TransportAddress.h"
00028 
00029 // predefined transport address
00030 const TransportAddress TransportAddress::UNSPECIFIED_NODE;
00031 const std::vector<TransportAddress> TransportAddress::UNSPECIFIED_NODES;
00032 
00033 std::ostream& operator<<(std::ostream& os, const TransportAddress& n)
00034 {
00035     if (n.isUnspecified()) {
00036         os << "<unspec>";
00037     } else {
00038         os << n.ip << ":" << n.port;
00039     }
00040 
00041     return os;
00042 };
00043 
00044 
00045 //default-constructor
00046 TransportAddress::TransportAddress()
00047 {
00048     port = -1;
00049 }
00050 
00051 //copy constructor
00052 TransportAddress::TransportAddress( const TransportAddress& handle )
00053 {
00054     port = handle.port;
00055     ip = handle.ip;
00056 }
00057 
00058 
00059 //complete constructor
00060 TransportAddress::TransportAddress( const IPvXAddress& ip, int port)
00061 {
00062     this->ip = ip;
00063     this->port = port;
00064 }
00065 
00066 //public
00067 bool TransportAddress::isUnspecified() const
00068 {
00069     return (ip.isUnspecified() || (port == -1));
00070 }
00071 
00072 //public
00073 TransportAddress& TransportAddress::operator=(const TransportAddress& rhs)
00074 {
00075     this->ip = rhs.ip;
00076     this->port = rhs.port;
00077 
00078     return *this;
00079 }
00080 
00081 //public
00082 bool TransportAddress::operator==(const TransportAddress& rhs) const
00083 {
00084     assertUnspecified(rhs);
00085     return (this->ip == rhs.ip && this->port == rhs.port);
00086 }
00087 
00088 //public
00089 bool TransportAddress::operator!=(const TransportAddress& rhs) const
00090 {
00091     assertUnspecified(rhs);
00092     return !(this->ip == rhs.ip && this->port == rhs.port );
00093 }
00094 
00095 //public
00096 bool TransportAddress::operator<(const TransportAddress &rhs) const
00097 {
00098     assertUnspecified(rhs);
00099     return this->ip < rhs.ip;
00100 }
00101 
00102 //public
00103 bool TransportAddress::operator>(const TransportAddress &rhs) const
00104 {
00105     assertUnspecified(rhs);
00106     return !(this->ip < rhs.ip || this->ip == rhs.ip);
00107 }
00108 
00109 //public
00110 bool TransportAddress::operator<=(const TransportAddress &rhs) const
00111 {
00112     assertUnspecified(rhs);
00113     return this->ip < rhs.ip || this->ip == rhs.ip;
00114 }
00115 
00116 //public
00117 bool TransportAddress::operator>=(const TransportAddress &rhs) const
00118 {
00119     assertUnspecified(rhs);
00120     return !(this->ip < rhs.ip);
00121 }
00122 
00123 //public
00124 void TransportAddress::setAddress( const IPvXAddress& ip, int port )
00125 {
00126     this->ip = ip;
00127     if (port!=-1)
00128         this->port = port;
00129 }
00130 
00131 //public
00132 void TransportAddress::setPort( int port )
00133 {
00134     this->port = port;
00135 }
00136 
00137 //public
00138 const IPvXAddress& TransportAddress::getAddress() const
00139 {
00140     return ip;
00141 }
00142 
00143 //public
00144 int TransportAddress::getPort() const
00145 {
00146     return port;
00147 }
00148 
00149 //public
00150 size_t TransportAddress::hash() const
00151 {
00152     size_t iphash;
00153     if (ip.isIPv6()) {
00154         uint32_t* addr = (uint32_t*) ip.get6().words();
00155         iphash = (size_t)(addr[0]^addr[1]^addr[2]^addr[3]);
00156     } else {
00157         iphash = (size_t)ip.get4().getInt();
00158     }
00159 
00160     return (size_t)(iphash^port);
00161 }
00162 
00163 TransportAddress* TransportAddress::dup() const
00164 {
00165     return new TransportAddress(*this);
00166 }
00167 
00168 //private
00169 inline void TransportAddress::assertUnspecified( const TransportAddress& handle ) const
00170 {
00171     if ( this->isUnspecified() || handle.isUnspecified() )
00172         opp_error("TransportAddress: Trying to compare unspecified TransportAddress!");
00173 }

Generated on Wed Mar 25 16:32:50 2009 for OverSim by  doxygen 1.5.5