From: <arn...@us...> - 2006-10-07 15:02:29
|
Revision: 664 http://svn.sourceforge.net/dcplusplus/?rev=664&view=rev Author: arnetheduck Date: 2006-10-07 08:02:06 -0700 (Sat, 07 Oct 2006) Log Message: ----------- more cleanup Modified Paths: -------------- dcplusplus/trunk/changelog.txt dcplusplus/trunk/client/ClientManager.cpp dcplusplus/trunk/client/ConnectionManager.cpp dcplusplus/trunk/client/DownloadManager.cpp dcplusplus/trunk/client/DownloadManager.h dcplusplus/trunk/client/QueueManager.cpp dcplusplus/trunk/client/QueueManager.h dcplusplus/trunk/client/UploadManager.cpp dcplusplus/trunk/client/UploadManager.h dcplusplus/trunk/client/UserConnection.cpp dcplusplus/trunk/windows/MainFrm.cpp Modified: dcplusplus/trunk/changelog.txt =================================================================== --- dcplusplus/trunk/changelog.txt 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/changelog.txt 2006-10-07 15:02:06 UTC (rev 664) @@ -4,6 +4,8 @@ * More code cleanup * Fixed trusted/untrusted upload view * Fixed crash on invalid remote command during upload +* [ADC] Improved GFI command support +* Lowest priority downloads are no longer started if there are other downloads running -- 0.697 2006-09-29 -- * [ADC] Fixed a few protocol issues Modified: dcplusplus/trunk/client/ClientManager.cpp =================================================================== --- dcplusplus/trunk/client/ClientManager.cpp 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/client/ClientManager.cpp 2006-10-07 15:02:06 UTC (rev 664) @@ -24,7 +24,6 @@ #include "ShareManager.h" #include "SearchManager.h" #include "CryptoManager.h" -#include "ConnectionManager.h" #include "FavoriteManager.h" #include "SimpleXML.h" #include "UserCommand.h" Modified: dcplusplus/trunk/client/ConnectionManager.cpp =================================================================== --- dcplusplus/trunk/client/ConnectionManager.cpp 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/client/ConnectionManager.cpp 2006-10-07 15:02:06 UTC (rev 664) @@ -131,9 +131,6 @@ ConnectionQueueItem::List removed; User::List idlers; - bool tooMany = ((SETTING(DOWNLOAD_SLOTS) != 0) && DownloadManager::getInstance()->getDownloadCount() >= (size_t)SETTING(DOWNLOAD_SLOTS)); - bool tooFast = ((SETTING(MAX_DOWNLOAD_SPEED) != 0 && DownloadManager::getInstance()->getAverageSpeed() >= (SETTING(MAX_DOWNLOAD_SPEED)*1024))); - { Lock l(cs); @@ -161,19 +158,15 @@ if( ((cqi->getLastAttempt() + 60*1000) < aTick) && !attemptDone ) { cqi->setLastAttempt(aTick); - if(!QueueManager::getInstance()->hasDownload(cqi->getUser())) { + QueueItem::Priority prio = QueueManager::getInstance()->hasDownload(cqi->getUser()); + + if(prio == QueueItem::PAUSED) { removed.push_back(cqi); continue; } - // Always start high-priority downloads unless we have 3 more than maxdownslots already... - bool startDown = !tooMany && !tooFast; - - if(!startDown) { - bool extraFull = (SETTING(DOWNLOAD_SLOTS) != 0) && (DownloadManager::getInstance()->getDownloadCount() >= (size_t)(SETTING(DOWNLOAD_SLOTS)+3)); - startDown = !extraFull && QueueManager::getInstance()->hasDownload(cqi->getUser(), QueueItem::HIGHEST); - } - + bool startDown = DownloadManager::getInstance()->startDownload(prio); + if(cqi->getState() == ConnectionQueueItem::WAITING) { if(startDown) { cqi->setState(ConnectionQueueItem::CONNECTING); Modified: dcplusplus/trunk/client/DownloadManager.cpp =================================================================== --- dcplusplus/trunk/client/DownloadManager.cpp 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/client/DownloadManager.cpp 2006-10-07 15:02:06 UTC (rev 664) @@ -22,9 +22,7 @@ #include "DownloadManager.h" #include "ResourceManager.h" -#include "ConnectionManager.h" #include "QueueManager.h" -#include "CryptoManager.h" #include "HashManager.h" #include "LogManager.h" @@ -33,7 +31,6 @@ #include "File.h" #include "FilteredFile.h" #include "MerkleCheckOutputStream.h" -#include "ClientManager.h" #include <limits> @@ -91,6 +88,12 @@ return cmd; } +void Download::getParams(const UserConnection& aSource, StringMap& params) { + Transfer::getParams(aSource, params); + params["target"] = getTarget(); + params["sfv"] = Util::toString(isSet(Download::FLAG_CRC32_OK) ? 1 : 0); +} + DownloadManager::DownloadManager() { TimerManager::getInstance()->addListener(this); } @@ -261,20 +264,36 @@ checkDownloads(conn); } -void DownloadManager::checkDownloads(UserConnection* aConn) { - dcassert(aConn->getDownload() == NULL); +bool DownloadManager::startDownload(QueueItem::Priority prio) { + size_t downloadCount = getDownloadCount(); - bool slotsFull = (SETTING(DOWNLOAD_SLOTS) != 0) && (getDownloadCount() >= (size_t)SETTING(DOWNLOAD_SLOTS)); - bool speedFull = (SETTING(MAX_DOWNLOAD_SPEED) != 0) && (getAverageSpeed() >= (SETTING(MAX_DOWNLOAD_SPEED)*1024)); + bool full = (SETTING(DOWNLOAD_SLOTS) != 0) && (downloadCount >= (size_t)SETTING(DOWNLOAD_SLOTS)); + full = full || (SETTING(MAX_DOWNLOAD_SPEED) != 0) && (getRunningAverage() >= (SETTING(MAX_DOWNLOAD_SPEED)*1024)); - if( slotsFull || speedFull ) { + if(full) { bool extraFull = (SETTING(DOWNLOAD_SLOTS) != 0) && (getDownloadCount() >= (size_t)(SETTING(DOWNLOAD_SLOTS)+3)); - if(extraFull || !QueueManager::getInstance()->hasDownload(aConn->getUser(), QueueItem::HIGHEST)) { - removeConnection(aConn); - return; + if(extraFull) { + return false; } + return prio == QueueItem::HIGHEST; } + if(downloadCount > 0) { + return prio != QueueItem::LOWEST; + } + + return true; +} + +void DownloadManager::checkDownloads(UserConnection* aConn) { + dcassert(aConn->getDownload() == NULL); + + QueueItem::Priority prio = QueueManager::getInstance()->hasDownload(aConn->getUser()); + if(!startDownload(prio)) { + removeConnection(aConn); + return; + } + Download* d = QueueManager::getInstance()->getDownload(*aConn, aConn->isSet(UserConnection::FLAG_SUPPORTS_TTHL)); if(!d) { @@ -731,10 +750,14 @@ return true; } -void Download::getParams(const UserConnection& aSource, StringMap& params) { - Transfer::getParams(aSource, params); - params["target"] = getTarget(); - params["sfv"] = Util::toString(isSet(Download::FLAG_CRC32_OK) ? 1 : 0); +int64_t DownloadManager::getRunningAverage() { + Lock l(cs); + int64_t avg = 0; + for(Download::Iter i = downloads.begin(); i != downloads.end(); ++i) { + Download* d = *i; + avg += d->getRunningAverage(); + } + return avg; } void DownloadManager::logDownload(UserConnection* aSource, Download* d) { Modified: dcplusplus/trunk/client/DownloadManager.h =================================================================== --- dcplusplus/trunk/client/DownloadManager.h 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/client/DownloadManager.h 2006-10-07 15:02:06 UTC (rev 664) @@ -30,8 +30,8 @@ #include "FilteredFile.h" #include "ZUtils.h" #include "MerkleTree.h" +#include "QueueItem.h" -class QueueItem; class ConnectionQueueItem; /** @@ -67,12 +67,7 @@ virtual ~Download(); - /** - * @remarks This function is only used from DownloadManager but its - * functionality could be useful in TransferView. - * - * @return Target filename without path. - */ + /** @return Target filename without path. */ string getTargetFileName() { return Util::getFileName(getTarget()); } @@ -99,14 +94,12 @@ private: Download(const Download&); - Download& operator=(const Download&); TigerTree tt; string pfs; }; - /** * Use this listener interface to get progress information for downloads. * @@ -172,21 +165,8 @@ void addConnection(UserConnection::Ptr conn); void checkIdle(const User::Ptr& user); - /** - * @remarks This is only used in the tray icons. In MainFrame this is - * calculated instead so there seems to be a little duplication of code. - * - * @return Average download speed in Bytes/s - */ - int getAverageSpeed() { - Lock l(cs); - int avg = 0; - for(Download::Iter i = downloads.begin(); i != downloads.end(); ++i) { - Download* d = *i; - avg += (int)d->getRunningAverage(); - } - return avg; - } + /** @return Running average download speed in Bytes/s */ + int64_t getRunningAverage(); /** @return Number of downloads. */ size_t getDownloadCount() { @@ -194,6 +174,7 @@ return downloads.size(); } + bool startDownload(QueueItem::Priority prio); private: enum { MOVER_LIMIT = 10*1024*1024 }; class FileMover : public Thread { Modified: dcplusplus/trunk/client/QueueManager.cpp =================================================================== --- dcplusplus/trunk/client/QueueManager.cpp 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/client/QueueManager.cpp 2006-10-07 15:02:06 UTC (rev 664) @@ -613,6 +613,18 @@ return sz; } +QueueItem::Priority QueueManager::hasDownload(const User::Ptr& aUser) throw() { + Lock l(cs); + if(pfsQueue.find(aUser->getCID()) != pfsQueue.end()) { + return QueueItem::HIGHEST; + } + QueueItem* qi = userQueue.getNext(aUser, QueueItem::LOWEST); + if(!qi) { + return QueueItem::PAUSED; + } + return qi->getPriority(); +} + typedef HASH_MULTIMAP<u_int32_t, QueueItem*> SizeMap; typedef SizeMap::iterator SizeIter; typedef pair<SizeIter, SizeIter> SizePair; Modified: dcplusplus/trunk/client/QueueManager.h =================================================================== --- dcplusplus/trunk/client/QueueManager.h 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/client/QueueManager.h 2006-10-07 15:02:06 UTC (rev 664) @@ -106,10 +106,8 @@ Download* getDownload(UserConnection& aSource, bool supportsTrees) throw(); void putDownload(Download* aDownload, bool finished) throw(); - bool hasDownload(const User::Ptr& aUser, QueueItem::Priority minPrio = QueueItem::LOWEST) throw() { - Lock l(cs); - return (pfsQueue.find(aUser->getCID()) != pfsQueue.end()) || (userQueue.getNext(aUser, minPrio) != NULL); - } + /** @return The highest priority download the user has, PAUSED may also mean no downloads */ + QueueItem::Priority hasDownload(const User::Ptr& aUser) throw(); int countOnlineSources(const string& aTarget); Modified: dcplusplus/trunk/client/UploadManager.cpp =================================================================== --- dcplusplus/trunk/client/UploadManager.cpp 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/client/UploadManager.cpp 2006-10-07 15:02:06 UTC (rev 664) @@ -214,7 +214,6 @@ if(partList) u->setFlag(Upload::FLAG_PARTIAL_LIST); - dcassert(aSource.getUpload() == NULL); uploads.push_back(u); if(!aSource.isSet(UserConnection::FLAG_HASSLOT)) { @@ -238,6 +237,16 @@ return true; } +int64_t UploadManager::getRunningAverage() { + Lock l(cs); + int64_t avg = 0; + for(Upload::Iter i = uploads.begin(); i != uploads.end(); ++i) { + Upload* u = *i; + avg += (int)u->getRunningAverage(); + } + return avg; +} + bool UploadManager::getAutoSlot() { /** A 0 in settings means disable */ if(SETTING(MIN_UPLOAD_SPEED) == 0) @@ -246,7 +255,7 @@ if(GET_TICK() < getLastGrant() + 30*1000) return false; /** Grant if upload speed is less than the threshold speed */ - return getAverageSpeed() < (SETTING(MIN_UPLOAD_SPEED)*1024); + return getRunningAverage() < (SETTING(MIN_UPLOAD_SPEED)*1024); } void UploadManager::removeUpload(Upload* aUpload) { @@ -362,7 +371,7 @@ void UploadManager::addFailedUpload(const UserConnection& source, string filename) { { Lock l(cs); - if (!count_if(waitingUsers.begin(), waitingUsers.end(), UserMatch(source.getUser()))) + if (!count_if(waitingUsers.begin(), waitingUsers.end(), CompareFirst<User::Ptr, u_int32_t>(source.getUser()))) waitingUsers.push_back(WaitingUser(source.getUser(), GET_TICK())); waitingFiles[source.getUser()].insert(filename); //files for which user's asked } @@ -373,7 +382,7 @@ void UploadManager::clearUserFiles(const User::Ptr& source) { Lock l(cs); //run this when a user's got a slot or goes offline. - UserList::iterator sit = find_if(waitingUsers.begin(), waitingUsers.end(), UserMatch(source)); + UserList::iterator sit = find_if(waitingUsers.begin(), waitingUsers.end(), CompareFirst<User::Ptr, u_int32_t>(source)); if (sit == waitingUsers.end()) return; FilesMap::iterator fit = waitingFiles.find(sit->first); Modified: dcplusplus/trunk/client/UploadManager.h =================================================================== --- dcplusplus/trunk/client/UploadManager.h 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/client/UploadManager.h 2006-10-07 15:02:06 UTC (rev 664) @@ -86,17 +86,9 @@ * @remarks This is only used in the tray icons. Could be used in * MainFrame too. * - * @return Average download speed in Bytes/s + * @return Running average download speed in Bytes/s */ - int getAverageSpeed() { - Lock l(cs); - int avg = 0; - for(Upload::Iter i = uploads.begin(); i != uploads.end(); ++i) { - Upload* u = *i; - avg += (int)u->getRunningAverage(); - } - return avg; - } + int64_t getRunningAverage(); /** @return Number of free slots. */ int getFreeSlots() { return max((SETTING(SLOTS) - running), 0); } @@ -133,14 +125,6 @@ typedef pair<User::Ptr, u_int32_t> WaitingUser; typedef list<WaitingUser> UserList; - struct UserMatch { - UserMatch(const User::Ptr& u) : u(u) { } - const User::Ptr& u; - bool operator()(const WaitingUser& wu) { return wu.first == u; } - private: - UserMatch& operator=(const UserMatch&); - }; - struct WaitingUserFresh { bool operator()(const WaitingUser& wu) { return wu.second > GET_TICK() - 5*60*1000; } }; Modified: dcplusplus/trunk/client/UserConnection.cpp =================================================================== --- dcplusplus/trunk/client/UserConnection.cpp 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/client/UserConnection.cpp 2006-10-07 15:02:06 UTC (rev 664) @@ -53,12 +53,11 @@ void Transfer::updateRunningAverage() { u_int32_t tick = GET_TICK(); - if(tick > lastTick) { + // Update 4 times/sec at most + if(tick > (lastTick + 250)) { u_int32_t diff = tick - lastTick; int64_t tot = getTotal(); - if(diff == 0) { - // No time passed, don't update runningAverage; - } else if( ((tick - getStart()) < AVG_PERIOD) ) { + if( ((tick - getStart()) < AVG_PERIOD) ) { runningAverage = getAverageSpeed(); } else { int64_t bdiff = tot - last; Modified: dcplusplus/trunk/windows/MainFrm.cpp =================================================================== --- dcplusplus/trunk/windows/MainFrm.cpp 2006-10-06 21:18:32 UTC (rev 663) +++ dcplusplus/trunk/windows/MainFrm.cpp 2006-10-07 15:02:06 UTC (rev 664) @@ -1011,9 +1011,9 @@ nid.hWnd = m_hWnd; nid.uID = 0; nid.uFlags = NIF_TIP; - _tcsncpy(nid.szTip, Text::toT("D: " + Util::formatBytes(DownloadManager::getInstance()->getAverageSpeed()) + "/s (" + + _tcsncpy(nid.szTip, Text::toT("D: " + Util::formatBytes(DownloadManager::getInstance()->getRunningAverage()) + "/s (" + Util::toString(DownloadManager::getInstance()->getDownloadCount()) + ")\r\nU: " + - Util::formatBytes(UploadManager::getInstance()->getAverageSpeed()) + "/s (" + + Util::formatBytes(UploadManager::getInstance()->getRunningAverage()) + "/s (" + Util::toString(UploadManager::getInstance()->getUploadCount()) + ")").c_str(), 64); ::Shell_NotifyIcon(NIM_MODIFY, &nid); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |