NodeAddress.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Imports.h"
00024 #include "NodeAddress.h"
00025 
00026 using namespace Protocols::Transports;
00027 
00028 // If we had more special addresses, we could have put them all in an enum.
00029 const NodeAddress NodeAddress::Null = NodeAddress();
00030 
00031 NodeAddress::NodeAddress (const QHostAddress &hostAddress, quint16 hostPort)
00032  :  hostName_(), hostAddress_(), hostPort_ (hostPort)
00033 {
00034     setHostAddress (hostAddress);
00035 }
00036 
00037 NodeAddress::NodeAddress (const QString &hostName, quint16 hostPort)
00038  :  hostName_(), hostAddress_(), hostPort_ (hostPort)
00039 {
00040     setHostName (hostName);
00041 }
00042 
00044 
00053 NodeAddress::NodeAddress (const QString &addressPortString)
00054  :  hostName_(), hostAddress_(), hostPort_ (0)
00055 {
00057     // fot the IPv6 addresses as well!
00058     int pos = addressPortString.indexOf (':');
00059     if (pos != -1) {
00060         hostAddress_.setAddress (addressPortString.left (pos));
00061         if (hostAddress_.isNull())
00062             hostName_ = addressPortString.left (pos);
00063         int length = addressPortString.length();
00064         while (addressPortString.at (pos + 1).isSpace())
00065             pos++;
00066         int i = 0;
00067         for (i = pos + 1; i < length && addressPortString.at(i).isDigit(); i++)
00068             ;
00069         hostPort_ = addressPortString.mid (pos + 1, i - pos - 1).toInt();
00070     }
00071 }
00072 
00073 NodeAddress::~NodeAddress()
00074 {
00075 }
00076 
00077 bool NodeAddress::operator == (const NodeAddress &nodeAddress) const
00078 {
00079     return toString() == nodeAddress.toString();
00080 }
00081 
00082 bool NodeAddress::operator < (const NodeAddress &nodeAddress) const
00083 {
00084     // \todo Is that OK?
00085     return hostAddress_.toString() < nodeAddress.hostAddress_.toString();
00086 }
00087 
00088 uint Protocols::Transports::qHash (const NodeAddress &nodeAddress)
00089 {
00090     return ::qHash (nodeAddress.toString());
00091 }
00092 
00093 QString NodeAddress::toString() const
00094 {
00095     if (hostName_.length() > 0)
00096         return QString ("%1:%2").arg (hostName_).arg (hostPort_);
00097     else
00098         return QString ("%1:%2").arg (hostAddress_.toString()).arg (hostPort_);
00099 }
00100 
00101 QDataStream & Protocols::Transports::operator << (QDataStream &stream,
00102                                                  const NodeAddress &nodeAddress)
00103 {
00104     if (nodeAddress.hostName().length() > 0) {
00105         stream << static_cast <unsigned char> (1);
00106         stream << nodeAddress.hostName();
00107     } else {
00108         stream << static_cast <unsigned char> (0);
00109         stream << nodeAddress.hostAddress().toString();
00110     }
00111     stream << nodeAddress.hostPort();
00112     return stream;
00113 }
00114 
00115 QDataStream & Protocols::Transports::operator >> (QDataStream &stream,
00116                                                  NodeAddress &nodeAddress)
00117 {
00118     unsigned char isName = 0;
00119     QString address;
00120     quint16 port = static_cast <quint16> (-1);
00121 
00122     stream >> isName;
00123     stream >> address;
00124     stream >> port;
00125 
00126     if (isName == 1)
00127         nodeAddress.setHostName (address);
00128     else
00129         nodeAddress.setHostAddress (QHostAddress (address));
00130     nodeAddress.setHostPort (port);
00131     return stream;
00132 }
00133