PeerInfo.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-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 "Qt.h"
00024 #include "PeerInfo.h"
00025 #include "Imports.cpp"
00026 
00028 
00033 PeerInfo::PeerInfo()
00034     : peerInfo()
00035 {
00036     peerInfo.setScheme (PeerSchemeName);
00037     peerInfo.setAuthority (QByteArray(), PeerNullHost, PeerNullPort);
00038     peerInfo.appendQueryItem (PeerIdQueryItemKeyName, PeerId().toQByteArray());
00039 }
00040 
00042 
00051 PeerInfo::PeerInfo (const QByteArray &host, uint port, const PeerId &peerId)
00052     : peerInfo()
00053 {
00054     Q_ASSERT (!host.isEmpty());
00055 
00056     peerInfo.setScheme (PeerSchemeName);
00057     peerInfo.setAuthority (QByteArray(), host, QByteArray::number (port));
00058     peerInfo.appendQueryItem (PeerIdQueryItemKeyName, peerId.toQByteArray());
00059 }
00060 
00062 
00065 PeerInfo::~PeerInfo()
00066 {
00067 }
00068 
00070 
00075 PeerInfo::PeerInfo (const PeerInfo &other)
00076     : peerInfo (other.peerInfo)
00077 {
00078 }
00079 
00081 
00085 PeerInfo & PeerInfo::operator= (const PeerInfo &other)
00086 {
00087     peerInfo = other.peerInfo;
00088     return *this;
00089 }
00090 
00092 
00096 bool PeerInfo::operator== (const PeerInfo &other) const
00097 {
00098     return peerInfo.host() == other.peerInfo.host() &&
00099         peerInfo.port() == other.peerInfo.port() &&
00100         peerInfo.queryItemValue (PeerIdQueryItemKeyName) ==
00101             other.peerInfo.queryItemValue (PeerIdQueryItemKeyName);
00102 }
00103 
00105 
00109 bool PeerInfo::operator!= (const PeerInfo &other) const
00110 {
00111     return !(*this == other);
00112 }
00113 
00115 PeerInfo::PeerId PeerInfo::peerId() const
00116 {
00117     QByteArray peerId = peerInfo.queryItemValue (PeerIdQueryItemKeyName);
00118     return !peerId.isNull() ? PeerId (peerId) : PeerId();
00119 }
00120 
00122 QByteArray PeerInfo::host() const
00123 {
00124     return peerInfo.host();
00125 }
00126 
00128 uint PeerInfo::port() const
00129 {
00130     return peerInfo.port().toUInt();
00131 }
00132 
00134 
00137 void PeerInfo::setPeerId (PeerId peerId)
00138 {
00139     Uri updatedPeerInfo (peerInfo);
00140     updatedPeerInfo.setQuery (QByteArray());
00141     updatedPeerInfo.appendQueryItem (PeerIdQueryItemKeyName,
00142         peerId.toQByteArray());
00143     peerInfo = updatedPeerInfo;
00144 }
00145 
00147 
00152 void PeerInfo::setHost (QByteArray host)
00153 {
00154     Q_ASSERT (!host.isEmpty());
00155     peerInfo.setAuthority (peerInfo.userInfo(), host, peerInfo.port());
00156 }
00157 
00159 
00162 void PeerInfo::setPort (uint port)
00163 {
00164     peerInfo.setAuthority (peerInfo.userInfo(),
00165                            peerInfo.host(),
00166                            QByteArray::number (port));
00167 }
00168 
00170 
00176 Uri PeerInfo::toUri() const
00177 {
00178     return peerInfo;
00179 }
00180 
00182 
00196 PeerInfo::PeerInfo PeerInfo::fromUri (const Uri &peerInfo)
00197 {
00198     Q_ASSERT (peerInfo.scheme() == PeerSchemeName);
00199 
00200     PeerInfo createdPeerInfo;
00201 
00202     if (!peerInfo.host().isNull())
00203         createdPeerInfo.setHost (peerInfo.host());
00204     if (!peerInfo.port().isNull())
00205         createdPeerInfo.setPort (peerInfo.port().toUInt());
00206     QByteArray peerId = peerInfo.queryItemValue (PeerIdQueryItemKeyName);
00207     if (!peerId.isNull())
00208         createdPeerInfo.setPeerId (PeerId (peerId));;
00209 
00210     return createdPeerInfo;
00211 }
00212 
00213 // Static constants definitions
00215 
00218 const char *PeerInfo::PeerSchemeName = "bt-peer";
00219 
00221 const char *PeerInfo::PeerIdQueryItemKeyName = "id";
00222 
00224 const char *PeerInfo::PeerNullPort = "0";
00225 
00227 const char *PeerInfo::PeerNullHost = "0.0.0.0";