TrackerManagerImpl.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 "TrackerManagerImpl.h"
00025 #include "Imports.cpp"
00026 
00028 
00040 TrackerManagerImpl::TrackerManagerImpl (
00041                         TrackerRequestSessionFactory *peersRequestSessionFactory,
00042                         Timer *trackerUpdateTimer,
00043                         TrackerManagerStatus *trackerManagerStatus)
00044     : sessionFactory_ (peersRequestSessionFactory), timer_ (trackerUpdateTimer),
00045       status_ (trackerManagerStatus), session_ (0), state_ (Stopped),
00046       torrent_(), updateInterval_ (0), trackerId_()
00047 {
00048 }
00049 
00051 
00054 TrackerManagerImpl::~TrackerManagerImpl()
00055 {
00056 }
00057 
00059 
00068 void TrackerManagerImpl::start (const Torrent &torrent)
00069 {
00070     state_ = Started;
00071     torrent_ = torrent;
00072     session_ = sessionFactory_->createSession (torrent_);
00073     session_->open();
00074 }
00075 
00077 
00084 void TrackerManagerImpl::stop()
00085 {
00086     state_ = Stopping;
00087     timer_->stop();
00088     if (session_ != 0)
00089         session_->close();
00090 }
00091 
00093 
00100 void TrackerManagerImpl::updateTime()
00101 {
00102     Q_ASSERT (state_ == Started);
00103 
00104     timer_->stop();
00105     session_->sendRequest (createTrackerRequest());
00106     timer_->start (0, updateInterval_);
00107 }
00108 
00110 
00117 void TrackerManagerImpl::trackerRequestSessionFactorySessionEstablished (
00118                                                 TrackerRequestSession *session)
00119 {
00120     Q_ASSERT (session_ == session);
00121     Q_ASSERT (state_ == Started);
00122 
00123     session_->sendRequest (createTrackerRequest());
00124 }
00125 
00127 
00134 void TrackerManagerImpl::trackerRequestSessionFactorySessionError (
00135                                                 TrackerRequestSession *session,
00136                                                 const QString &errorMessage)
00137 {
00138     Q_ASSERT (session_ == session);
00139     Q_ASSERT (state_ != Stopped);
00140 
00141     status_->trackerManagerError (errorMessage);
00142 }
00143 
00145 
00152 void TrackerManagerImpl::trackerRequestSessionFactoryResponseRecieved (
00153                                                 TrackerRequestSession *session,
00154                                                 const TrackerResponse &response)
00155 {
00156     Q_ASSERT (session_ == session);
00157     Q_ASSERT (state_ == Started);
00158 
00159     timer_->stop();
00160 
00161     // Update necessary info (that must be stored) from the response
00162     updateInterval_ = response.interval();
00163     if (!response.trackerId().isEmpty())
00164         trackerId_ = response.trackerId();
00165 
00166     status_->trackerManagerFoundPeers (response.peers());
00167     timer_->start (0, updateInterval_);
00168 }
00169 
00171 
00179 void TrackerManagerImpl::trackerRequestSessionFactorySessionClosing (
00180                                                 TrackerRequestSession *session)
00181 {
00182     Q_ASSERT (session_ == session);
00183     Q_ASSERT (state_ != Stopped);
00184 
00185     state_ = Stopping;
00186     timer_->stop();
00187 }
00188 
00190 
00198 void TrackerManagerImpl::trackerRequestSessionFactorySessionClosed (
00199                                                 TrackerRequestSession *session)
00200 {
00201     Q_ASSERT (session_ == session);
00202     Q_ASSERT (state_ != Stopped);
00203 
00204     state_ = Stopped;
00205     timer_->stop();
00206     sessionFactory_->destroySession (session_);
00207 }
00208 
00210 
00214 TrackerRequest TrackerManagerImpl::createTrackerRequest() const
00215 {
00216     return TrackerRequest (
00217         Uri::fromUnencoded ("http://localhost/announce.php"),
00218         PeerInfo ("1.2.3.4", 6881, PeerInfo::PeerId ("-CA0610-574938205849")),
00219         Torrent::InfoHash ("z53bf64bd05hfn2daqml"),
00220         4096,
00221         2048,
00222         63488,
00223         false,
00224         TrackerRequest::Empty,
00225         0,
00226         QByteArray(),
00227         trackerId_);
00228 }