From 0654836a3ddc1fcf95e2d8c43c6a05dfd2f3634d Mon Sep 17 00:00:00 2001 From: ewensun Date: Mon, 19 Jan 2009 15:02:59 +0000 Subject: [PATCH] add a downloading queue in ftControl,max 2 files can be download at same time git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@933 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/ft/ftcontroller.cc | 52 +++++++++++++++++++++++----- libretroshare/src/ft/ftcontroller.h | 5 ++- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index 2af200ebc..30d7f7ee8 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -119,20 +119,28 @@ void ftController::run() /* tick the transferModules */ std::list done; std::list::iterator it; + std::map::iterator mit; + if (mDownloadingQueue.size() > 0) { RsStackMutex stack(ctrlMutex); /******* LOCKED ********/ - std::map::iterator it; - for(it = mDownloads.begin(); it != mDownloads.end(); it++) + for(it = mDownloadingQueue.begin(); it != mDownloadingQueue.end(); it++) { #ifdef CONTROL_DEBUG - std::cerr << "\tTicking: " << it->first; + std::cerr << "\tTicking: " << *it; std::cerr << std::endl; #endif - - if (it->second.mTransfer) - (it->second.mTransfer)->tick(); + mit = mDownloads.find(*it); + if (mit != mDownloads.end()) + { + if (mit->second.mTransfer) + (mit->second.mTransfer)->tick(); + } + else + { + mDownloadingQueue.erase(it); + } } } @@ -142,7 +150,9 @@ void ftController::run() completeFile(*it); } mDone.clear(); - + + /* adjust the Downloading queue*/ + checkDownloadQueue(); } } @@ -152,9 +162,26 @@ void ftController::run() /* Called every 10 seconds or so */ void ftController::checkDownloadQueue() { - /* */ - + uint32_t cnt = MAX_NUMBER_OF_DOWNLOADING_FILE - mDownloadingQueue.size(); + if (cnt <= 0) return; + + RsStackMutex stack(ctrlMutex); + std::map::iterator mit; + mit = mDownloads.begin(); + while (cnt-- > 0) + { + while ((mit != mDownloads.end()) && (mit->second.mState != ftFileControl::DOWNLOADING)) + { mit++ ; } + if (mit != mDownloads.end()) + { + mDownloadingQueue.push_back(mit->first); + } + else + { + cnt = 0; + } + } } bool ftController::FlagFileComplete(std::string hash) @@ -275,6 +302,7 @@ bool ftController::completeFile(std::string hash) callbackCode = fc->mCallbackCode; mDownloads.erase(it); + mDownloadingQueue.remove(it->first); } /******* UNLOCKED ********/ @@ -614,6 +642,10 @@ bool ftController::FileRequest(std::string fname, std::string hash, mDownloads[hash] = ftfc; mSlowQueue.push_back(hash); + if (mDownloadingQueue.size() < MAX_NUMBER_OF_DOWNLOADING_FILE) + { + mDownloadingQueue.push_back(hash); + } IndicateConfigChanged(); /* completed transfer -> save */ return true; @@ -725,6 +757,8 @@ bool ftController::FileCancel(std::string hash) //fc->mState = ftFileControl::ERROR_COMPLETION; mDownloads.erase(mit); + + mDownloadingQueue.remove(mit->first); IndicateConfigChanged(); /* completed transfer -> save */ return true; diff --git a/libretroshare/src/ft/ftcontroller.h b/libretroshare/src/ft/ftcontroller.h index 9350805b5..24e92c9e2 100644 --- a/libretroshare/src/ft/ftcontroller.h +++ b/libretroshare/src/ft/ftcontroller.h @@ -62,6 +62,8 @@ const uint32_t CB_CODE_MEDIA = 0x0004; const uint32_t FC_TRANSFER_COMPLETE = 0x0001; +const uint32_t MAX_NUMBER_OF_DOWNLOADING_FILE = 0x0002; //will be controller by user later + class ftFileControl { public: @@ -190,7 +192,8 @@ bool setPeerState(ftTransferModule *tm, std::string id, std::map mCompleted; - std::map mDownloads; + std::map mDownloads; //include downloading and pending downloading + std::list mDownloadingQueue; //only include downloading file hashs //std::map mTransfers; //std::map mFileCreators;