1 /* 2 * Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 */ 18 19 #pragma once 20 21 #include "forward.h" 22 #include "DownloadManagerListener.h" 23 #include "UserConnectionListener.h" 24 #include "QueueItem.h" 25 #include "TimerManager.h" 26 #include "Singleton.h" 27 #include "MerkleTree.h" 28 #include "Speaker.h" 29 30 namespace dcpp { 31 32 /** 33 * Singleton. Use its listener interface to update the download list 34 * in the user interface. 35 */ 36 class DownloadManager : public Speaker<DownloadManagerListener>, 37 private UserConnectionListener, private TimerManagerListener, 38 public Singleton<DownloadManager> 39 { 40 public: 41 42 /** @internal */ 43 void addConnection(UserConnectionPtr conn); 44 void checkIdle(const UserPtr& user); 45 46 /** @return Running average download speed in Bytes/s */ 47 int64_t getRunningAverage(); 48 49 /** @return Number of downloads. */ getDownloadCount()50 size_t getDownloadCount() { 51 Lock l(cs); 52 return downloads.size(); 53 } 54 55 bool startDownload(QueueItem::Priority prio); 56 private: 57 58 CriticalSection cs; 59 DownloadList downloads; 60 UserConnectionList idlers; 61 62 void removeConnection(UserConnectionPtr aConn); 63 void removeDownload(Download* aDown); 64 void fileNotAvailable(UserConnection* aSource); 65 void noSlots(UserConnection* aSource); 66 67 void logDownload(UserConnection* aSource, Download* d); 68 int64_t getResumePos(const string& file, const TigerTree& tt, int64_t startPos); 69 70 void failDownload(UserConnection* aSource, const string& reason); 71 72 friend class Singleton<DownloadManager>; 73 74 DownloadManager(); 75 virtual ~DownloadManager(); 76 77 void checkDownloads(UserConnection* aConn); 78 void startData(UserConnection* aSource, int64_t start, int64_t newSize, bool z); 79 void endData(UserConnection* aSource); 80 81 void onFailed(UserConnection* aSource, const string& aError); 82 83 // UserConnectionListener 84 virtual void on(Data, UserConnection*, const uint8_t*, size_t) noexcept; on(Failed,UserConnection * aSource,const string & aError)85 virtual void on(Failed, UserConnection* aSource, const string& aError) noexcept { onFailed(aSource, aError); } on(ProtocolError,UserConnection * aSource,const string & aError)86 virtual void on(ProtocolError, UserConnection* aSource, const string& aError) noexcept { onFailed(aSource, aError); } 87 virtual void on(MaxedOut, UserConnection*) noexcept; 88 virtual void on(FileNotAvailable, UserConnection*) noexcept; 89 virtual void on(Updated, UserConnection*) noexcept; 90 91 virtual void on(AdcCommand::SND, UserConnection*, const AdcCommand&) noexcept; 92 virtual void on(AdcCommand::STA, UserConnection*, const AdcCommand&) noexcept; 93 94 // TimerManagerListener 95 virtual void on(TimerManagerListener::Second, uint64_t aTick) noexcept; 96 }; 97 98 } // namespace dcpp 99