I3SubIdentifier.cc

Go to the documentation of this file.
00001 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016 //
00017 
00025 #include "I3SubIdentifier.h"
00026 
00027 
00028 I3SubIdentifier::I3SubIdentifier() :
00029         type(Invalid)
00030 {
00031 }
00032 
00033 void I3SubIdentifier::setIPAddress(const I3IPAddress &address)
00034 {
00035     type = IPAddress;
00036     ipAddress = address;
00037     identifier.clear();
00038 }
00039 
00040 void I3SubIdentifier::setIdentifier(const I3Identifier &id)
00041 {
00042     type = Identifier;
00043 
00044     I3IPAddress zero; // initialized to zero
00045     ipAddress = zero; // set to clear
00046 
00047     identifier = id;
00048 }
00049 
00050 I3SubIdentifier::IdentifierType I3SubIdentifier::getType() const
00051 {
00052     return type;
00053 }
00054 
00055 I3IPAddress &I3SubIdentifier::getIPAddress()
00056 {
00057     if (type != IPAddress) {
00058         // error!
00059     }
00060     return ipAddress;
00061 }
00062 
00063 I3Identifier &I3SubIdentifier::getIdentifier()
00064 {
00065     if (type != Identifier) {
00066         // error!
00067     };
00068     return identifier;
00069 }
00070 
00071 int I3SubIdentifier::compareTo(const I3SubIdentifier &id) const
00072 {
00073     if (type != id.type) {
00074         return type - id.type;
00075     } else if (type == Identifier) {
00076         return identifier.compareTo(id.identifier);
00077     } else if (type == IPAddress) {
00078         if (ipAddress.getAddress() < id.ipAddress.getAddress()) return -1;
00079         else if (ipAddress.getAddress() == id.ipAddress.getAddress()) return ipAddress.getPort() - id.ipAddress.getPort();
00080         else return 1;
00081     }
00082     return 0;
00083 }
00084 
00085 int I3SubIdentifier::length() const {
00086 //      return sizeof(type) + (type == IPAddress ? ipAddress.length() : identifier.length());
00087     return 1 + (type == IPAddress ? ipAddress.length() : identifier.length());
00088 }
00089 
00090 std::ostream& operator<<(std::ostream& os, const I3SubIdentifier &s) {
00091     if (s.type == I3SubIdentifier::Identifier) {
00092         os << s.identifier;
00093     } else {
00094         os << s.ipAddress;
00095     }
00096     return os;
00097 }
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3