NodeAddress.h

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 #ifndef PROTOCOLS__TRANSPORTS__NODE_ADDRESS_H
00024 #define PROTOCOLS__TRANSPORTS__NODE_ADDRESS_H
00025 
00026 namespace Protocols {
00027 namespace Transports {
00028 
00030 
00046 class NodeAddress
00047 {
00048 public:
00049                             NodeAddress (const QHostAddress &hostAddress = QHostAddress::Null, quint16 hostPort = 0);
00050                             NodeAddress (const QString &hostName, quint16 hostPort);
00051                             NodeAddress (const QString &hostNameAndPort);
00052                             ~NodeAddress();
00053 
00054     bool                    operator == (const NodeAddress &nodeAddress) const;
00055     inline bool             operator != (const NodeAddress &nodeAddress) const;
00056     bool                    operator < (const NodeAddress &nodeAddress) const;
00057 
00058     inline bool             isNull() const;
00059 
00060     inline QString          hostName() const;
00061     inline QHostAddress     hostAddress() const;
00062     inline quint16          hostPort() const;
00063 
00064     inline void             setHostName (const QString &hostName);
00065     inline void             setHostAddress (const QHostAddress &hostAddress);
00066     inline void             setHostPort (quint16 hostPort);
00067 
00068     QString                 toString() const;
00069 
00070     static const NodeAddress    Null;
00071 
00072 private:
00073     QString                 hostName_;
00074     QHostAddress            hostAddress_;
00075     quint16                 hostPort_;
00076 };
00077 
00078 QDataStream & operator << (QDataStream &stream, const NodeAddress &nodeAddress);
00079 QDataStream & operator >> (QDataStream &stream, NodeAddress &nodeAddress);
00080 
00081 uint qHash (const NodeAddress &nodeAddress);
00082 
00084 // inline functions
00086 
00087 inline bool NodeAddress::operator != (const NodeAddress &nodeAddress) const
00088 { return ! operator == (nodeAddress); }
00089 
00090 inline bool NodeAddress::isNull() const
00091 { return hostAddress_.isNull() && hostName_.length() == 0; }
00092 
00093 inline QString NodeAddress::hostName() const
00094 { return hostName_; }
00095 
00096 inline QHostAddress NodeAddress::hostAddress() const
00097 { return hostAddress_; }
00098 
00099 inline quint16 NodeAddress::hostPort() const
00100 { return hostPort_; }
00101 
00102 // \todo If hostName happens to be an IP address string, set hostAddress_ instead?
00103 inline void NodeAddress::setHostName (const QString &hostName)
00104 { hostAddress_.setAddress (hostName);
00105   if (hostAddress_.isNull()) hostName_ = hostName;
00106   else hostName_ = ""; }
00107 
00108 // \todo Really reset the hostName when the address is set?
00109 inline void NodeAddress::setHostAddress (const QHostAddress &hostAddress)
00110 { hostName_ = ""; hostAddress_ = hostAddress; }
00111 
00112 inline void NodeAddress::setHostPort (quint16 hostPort)
00113 { hostPort_ = hostPort; }
00114 
00115 } // namespace Transports
00116 } // namespace Protocols
00117 
00118 #endif // PROTOCOLS__TRANSPORTS__NODE_ADDRESS_H