Skip to content

Commit

Permalink
Squashed 'airdcpp-core/' changes from 55f7b49..657f08d
Browse files Browse the repository at this point in the history
657f08d API: less expensive file download state events
e9bb8a2 Add simple download progress tracking for viewed files

git-subtree-dir: airdcpp-core
git-subtree-split: 657f08d6497626b54929ca390931d0c5a68b6f25
  • Loading branch information
maksis committed Jan 26, 2016
1 parent 9442d97 commit 8f58efb
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 22 deletions.
58 changes: 51 additions & 7 deletions airdcpp/TrackableDownloadItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace dcpp {
if (downloads.empty()) {
newState = completedDownloads ? STATE_DOWNLOADED : STATE_DOWNLOAD_PENDING;
} else {
auto hasRunning = boost::find(downloads | map_values, true).base() != downloads.end();
auto hasRunning = boost::find_if(downloads | map_values, PathInfo::IsRunning()).base() != downloads.end();
newState = hasRunning ? STATE_DOWNLOADING : STATE_DOWNLOAD_PENDING;
}
}
Expand All @@ -55,13 +55,13 @@ namespace dcpp {
onStateChanged();
}

void TrackableDownloadItem::onAddedQueue(const string& aPath) noexcept {
void TrackableDownloadItem::onAddedQueue(const string& aPath, int64_t aSize) noexcept {
bool first = false;

{
WLock l(cs);
first = downloads.empty();
downloads.emplace(aPath, false);
downloads.emplace(aPath, PathInfo(aSize));
}

if (first) {
Expand Down Expand Up @@ -108,26 +108,70 @@ namespace dcpp {
return ret;
}

void TrackableDownloadItem::onDownloadStateChanged(const Download* aDownload, bool aFailed) noexcept {
void TrackableDownloadItem::onRunningStateChanged(const Download* aDownload, bool aFailed) noexcept {
{
RLock l(cs);
auto d = downloads.find(aDownload->getPath());
if (d == downloads.end()) {
return;
}

d->second = !aFailed;
auto& di = d->second;
di.running = !aFailed;
}

updateState();
}

double TrackableDownloadItem::PathInfo::getDownloadedPercentage() const noexcept {
return size > 0 ? (static_cast<double>(downloaded) * 100.0) / static_cast<double>(size) : 0;
}

string TrackableDownloadItem::formatRunningStatus() const noexcept {
auto p = find_if(downloads | map_values, PathInfo::IsRunning());

if (p.base() != downloads.end() && p->trackProgress()) {
if (p->downloaded == -1) {
return STRING(DOWNLOAD_STARTING);
}

return STRING_F(RUNNING_PCT, p->getDownloadedPercentage());
}

return "Downloading";
}

string TrackableDownloadItem::getStatusString() const noexcept {
switch (state) {
case TrackableDownloadItem::STATE_DOWNLOAD_PENDING: return "Download pending";
case TrackableDownloadItem::STATE_DOWNLOADING: return formatRunningStatus();
case TrackableDownloadItem::STATE_DOWNLOADED: return STRING(DOWNLOADED);
}

dcassert(0);
return "";
}

void TrackableDownloadItem::on(DownloadManagerListener::Failed, const Download* aDownload, const string& /*aReason*/) noexcept {
onDownloadStateChanged(aDownload, true);
onRunningStateChanged(aDownload, true);
}

void TrackableDownloadItem::on(DownloadManagerListener::Starting, const Download* aDownload) noexcept {
onDownloadStateChanged(aDownload, false);
onRunningStateChanged(aDownload, false);
}

void TrackableDownloadItem::onProgress(const string& aDir, int64_t aDownloadedBytes) noexcept {
{
RLock l(cs);
auto i = downloads.find(aDir);
if (i == downloads.end()) {
return;
}

i->second.downloaded = aDownloadedBytes;
}

updateState();
}

} // namespace dcpp
31 changes: 28 additions & 3 deletions airdcpp/TrackableDownloadItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ namespace dcpp {
{
public:
virtual void onRemovedQueue(const string& aDir, bool aFinished) noexcept;
virtual void onAddedQueue(const string& aDir) noexcept;

// Leave the size unspecified if there is no tracking for download progress
virtual void onAddedQueue(const string& aDir, int64_t aSize = -1) noexcept;

virtual void onProgress(const string& aDir, int64_t aDownloadedBytes) noexcept;

TrackableDownloadItem(bool aDownloaded) noexcept;
~TrackableDownloadItem() noexcept;
Expand All @@ -53,6 +57,8 @@ namespace dcpp {
StringList getDownloads() const noexcept;

IGETSET(time_t, timeFinished, TimeFinished, 0);

string getStatusString() const noexcept;
protected:
virtual void onStateChanged() noexcept = 0;

Expand All @@ -61,13 +67,32 @@ namespace dcpp {
State state = STATE_DOWNLOAD_PENDING;

void updateState() noexcept;
void onDownloadStateChanged(const Download* aDownload, bool aFailed) noexcept;
void onRunningStateChanged(const Download* aDownload, bool aFailed) noexcept;

void on(DownloadManagerListener::Failed, const Download* aDownload, const string& aReason) noexcept;
void on(DownloadManagerListener::Starting, const Download* aDownload) noexcept;

struct PathInfo {
struct IsRunning {
bool operator()(const PathInfo& d) const {
return d.running;
}
};

PathInfo(int64_t aSize) : size(aSize) { }

bool running = false;
int64_t size = -1;
int64_t downloaded = -1;

bool trackProgress() const noexcept { return size != -1; }
double getDownloadedPercentage() const noexcept;
};

mutable SharedMutex cs;
map<string, bool> downloads;
map<string, PathInfo> downloads;

string formatRunningStatus() const noexcept;
};

} // namespace dcpp
Expand Down
3 changes: 0 additions & 3 deletions airdcpp/ViewFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace dcpp {
ViewFile::ViewFile(const string& aTarget, const TTHValue& aTTH, bool aIsText, bool aIsLocalFile, UpdateF&& aUpdateFunction) noexcept :
TrackableDownloadItem(aIsLocalFile), path(aTarget), tth(aTTH), updateFunction(aUpdateFunction), text(aIsText), localFile(aIsLocalFile) {

if (!aIsLocalFile) {
onAddedQueue(path);
}
}

ViewFile::~ViewFile() noexcept {
Expand Down
26 changes: 20 additions & 6 deletions airdcpp/ViewFileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ namespace dcpp {
return aQI->isSet(QueueItem::FLAG_CLIENT_VIEW) && !aQI->isSet(QueueItem::FLAG_USER_LIST) && !aQI->isSet(QueueItem::FLAG_OPEN);
}

void ViewFileManager::on(QueueManagerListener::StatusUpdated, const QueueItemPtr& aQI) noexcept {
if (!isViewedItem(aQI)) {
return;
}

auto file = getFile(aQI->getTTH());
if (file) {
file->onProgress(aQI->getTarget(), aQI->getDownloadedBytes());
}
}

void ViewFileManager::on(QueueManagerListener::Removed, const QueueItemPtr& aQI, bool finished) noexcept {
if (finished || !isViewedItem(aQI)) {
return;
Expand All @@ -66,16 +77,19 @@ namespace dcpp {
}

void ViewFileManager::on(QueueManagerListener::Added, QueueItemPtr& aQI) noexcept {
if (!aQI->isSet(QueueItem::FLAG_CLIENT_VIEW) || aQI->isSet(QueueItem::FLAG_USER_LIST)) {
if (!isViewedItem(aQI)) {
return;
}

createFile(aQI->getTarget(), aQI->getTTH(), aQI->isSet(QueueItem::FLAG_TEXT), false);
auto file = createFile(aQI->getTarget(), aQI->getTTH(), aQI->isSet(QueueItem::FLAG_TEXT), false);
if (file) {
file->onAddedQueue(aQI->getTarget(), aQI->getSize());
}
}

ViewFilePtr ViewFileManager::createFile(const string& aFileName, const TTHValue& aTTH, bool aIsText, bool aIsLocalFile) noexcept {
auto file = make_shared<ViewFile>(aFileName, aTTH, aIsText, aIsLocalFile,
std::bind(&ViewFileManager::onFileUpdated, this, std::placeholders::_1));
std::bind(&ViewFileManager::onFileStateUpdated, this, std::placeholders::_1));

{
WLock l(cs);
Expand All @@ -86,10 +100,10 @@ namespace dcpp {
return file;
}

void ViewFileManager::onFileUpdated(const TTHValue& aTTH) noexcept {
void ViewFileManager::onFileStateUpdated(const TTHValue& aTTH) noexcept {
auto file = getFile(aTTH);
if (file) {
fire(ViewFileManagerListener::FileUpdated(), file);
fire(ViewFileManagerListener::FileStateUpdated(), file);
}
}

Expand Down Expand Up @@ -148,7 +162,7 @@ namespace dcpp {

bool ViewFileManager::addUserFileNotify(const string& aFileName, int64_t aSize, const TTHValue& aTTH, const HintedUser& aUser, bool aIsText) noexcept {
try {
if (ViewFileManager::getInstance()->addUserFileThrow(aFileName, aSize, aTTH, aUser, aIsText)) {
if (addUserFileThrow(aFileName, aSize, aTTH, aUser, aIsText)) {
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion airdcpp/ViewFileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ namespace dcpp {
void on(QueueManagerListener::Added, QueueItemPtr& aQI) noexcept;
void on(QueueManagerListener::Finished, const QueueItemPtr& qi, const string& dir, const HintedUser& aUser, int64_t aSpeed) noexcept;
void on(QueueManagerListener::Removed, const QueueItemPtr& qi, bool finished) noexcept;
void on(QueueManagerListener::StatusUpdated, const QueueItemPtr& aQI) noexcept;

void onFileUpdated(const TTHValue& aTTH) noexcept;
void onFileStateUpdated(const TTHValue& aTTH) noexcept;

friend class Singleton<ViewFileManager>;

Expand Down
4 changes: 2 additions & 2 deletions airdcpp/ViewFileManagerListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ namespace dcpp {
template<int I> struct X { enum { TYPE = I }; };

typedef X<0> FileAdded;
typedef X<1> FileUpdated;
typedef X<1> FileStateUpdated;
typedef X<2> FileClosed;
typedef X<3> FileFinished;
typedef X<4> FileRead;

virtual void on(FileAdded, const ViewFilePtr&) noexcept { }
virtual void on(FileUpdated, const ViewFilePtr&) noexcept { }
virtual void on(FileStateUpdated, const ViewFilePtr&) noexcept { }
virtual void on(FileClosed, const ViewFilePtr&) noexcept { }
virtual void on(FileFinished, const ViewFilePtr&) noexcept { }
virtual void on(FileRead, const ViewFilePtr&) noexcept { }
Expand Down

0 comments on commit 8f58efb

Please sign in to comment.