TrackerRequestSessionFactoryImpl.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 "MultiTrackerRequestSession.h"
00025 #include "SingleTrackerRequestSession.h"
00026 #include "TrackerRequestSessionFactoryImpl.h"
00027 #include "Imports.cpp"
00028 
00030 
00036 TrackerRequestSessionFactoryImpl::TrackerRequestSessionFactoryImpl (
00037                 TrackerRequestSessionFactoryStatus *trackerRequestFactorySessionStatus)
00038     : status_ (trackerRequestFactorySessionStatus),
00039       sessionsStorage_()
00040 {
00041 }
00042 
00044 
00048 TrackerRequestSessionFactoryImpl::~TrackerRequestSessionFactoryImpl()
00049 {
00050     foreach (TrackerRequestSession *session, sessionsStorage_.keys())
00051         destroySession (session);
00052 }
00053 
00055 
00067 TrackerRequestSession *TrackerRequestSessionFactoryImpl::createSession (
00068                                                         const Torrent &torrent)
00069 {
00070     // Create data needed in the session ctor
00071     // \todo Implement real HttpRequestSession and TrackerRequestSessionStatus
00072     // instance creation.
00073     auto_ptr <HttpRequestSession> httpRequestSession;
00074     auto_ptr <TrackerRequestSessionStatus> trackerRequestSessionStatus;
00075     TrackerRequestSessionData sessionData (
00076         httpRequestSession.get(), trackerRequestSessionStatus.get());
00077 
00078     // Choose the correct session type and create an instance of it
00079     auto_ptr <TrackerRequestSession> session;
00080     if (!torrent.announceList().isEmpty()) {
00081         session.reset (new MultiTrackerRequestSession (httpRequestSession.get(),
00082             torrent.announceList(), trackerRequestSessionStatus.get()));
00083     } else if (torrent.announce() != Uri()) {
00084         session.reset (new SingleTrackerRequestSession (httpRequestSession.get(),
00085             torrent.announce(), trackerRequestSessionStatus.get()));
00086     } else {
00087         Q_ASSERT (false); // Invalid (empty) torrent passed!
00088     }
00089 
00090     // Store the session with its data for later usage
00091     sessionsStorage_.insert (session.get(), sessionData);
00092 
00093     // Release the session data (they're now owned by the sessionsStorage_)
00094     httpRequestSession.release();
00095     trackerRequestSessionStatus.release();
00096 
00097     // Session is now owned by the sessionsStorage_ too, so we can release it
00098     return session.release();
00099 }
00100 
00102 
00114 void TrackerRequestSessionFactoryImpl::destroySession (
00115                                                 TrackerRequestSession *session)
00116 {
00117     Q_ASSERT (isHoldedSession (session));
00118 
00119     TrackerRequestSessionData sessionData = sessionsStorage_.take (session);
00120     delete sessionData.first;
00121     delete sessionData.second;
00122     delete session;
00123 }
00124 
00126 
00130 uint TrackerRequestSessionFactoryImpl::holdedSessionsCount() const
00131 {
00132     return sessionsStorage_.size();
00133 }
00134 
00136 
00145 void TrackerRequestSessionFactoryImpl::trackerRequestSessionEstablished (
00146                                                 TrackerRequestSession *session)
00147 {
00148     Q_ASSERT (isHoldedSession (session));
00149 
00150     status_->trackerRequestSessionFactorySessionEstablished (session);
00151 }
00152 
00154 
00164 void TrackerRequestSessionFactoryImpl::trackerRequestSessionError (
00165                     TrackerRequestSession *session, const QString &errorMessage)
00166 {
00167     Q_ASSERT (isHoldedSession (session));
00168 
00169     status_->trackerRequestSessionFactorySessionError (session, errorMessage);
00170 }
00171 
00173 
00183 void TrackerRequestSessionFactoryImpl::trackerRequestSessionResponseRecieved (
00184         TrackerRequestSession *session, const TrackerResponse &trackerResponse)
00185 {
00186     Q_ASSERT (isHoldedSession (session));
00187 
00188     status_->trackerRequestSessionFactoryResponseRecieved (session,
00189         trackerResponse);
00190 }
00191 
00193 
00202 void TrackerRequestSessionFactoryImpl::trackerRequestSessionClosing (
00203                                                 TrackerRequestSession *session)
00204 {
00205     Q_ASSERT (isHoldedSession (session));
00206 
00207     status_->trackerRequestSessionFactorySessionClosing (session);
00208 }
00209 
00211 
00220 void TrackerRequestSessionFactoryImpl::trackerRequestSessionClosed (
00221                                                 TrackerRequestSession *session)
00222 {
00223     Q_ASSERT (isHoldedSession (session));
00224 
00225     status_->trackerRequestSessionFactorySessionClosed (session);
00226 }
00227 
00229 
00234 bool TrackerRequestSessionFactoryImpl::isHoldedSession (
00235                                         TrackerRequestSession *session) const
00236 {
00237     return sessionsStorage_.contains (session);
00238 }
00239