From 9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0 Mon Sep 17 00:00:00 2001 From: tpearson Date: Wed, 20 Jan 2010 02:37:40 +0000 Subject: Updated to final KDE3 ktorrent release (2.2.6) git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/ktorrent@1077377 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- plugins/stats/PeerMonitor.cc | 187 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 plugins/stats/PeerMonitor.cc (limited to 'plugins/stats/PeerMonitor.cc') diff --git a/plugins/stats/PeerMonitor.cc b/plugins/stats/PeerMonitor.cc new file mode 100644 index 0000000..a4cf449 --- /dev/null +++ b/plugins/stats/PeerMonitor.cc @@ -0,0 +1,187 @@ +/*************************************************************************** + * Copyright © 2007 by Krzysztof Kundzicz * + * athantor@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "PeerMonitor.h" + +namespace kt { + +PeerMonitor::PeerMonitor(kt::TorrentInterface * pTi, std::map * pM) : kt::MonitorInterface(), QObject(), pmTorIface(pTi), pmPeerMMgr(pM) +{ + +} + +PeerMonitor::~PeerMonitor() +{ +} + +void PeerMonitor::peerAdded (kt::PeerInterface *peer) +{ + QMutexLocker lock(&mtx); + + mPeers.push_back( peer ); +} + +void PeerMonitor::peerRemoved (kt::PeerInterface *peer) +{ + + QMutexLocker lock(&mtx); + + data_t::iterator it = std::find(mPeers.begin(), mPeers.end(), peer); + + if(it != mPeers.end()) + { + mPeers.erase(it); + // *it = 0; + } + +} + +void PeerMonitor::downloadStarted (kt::ChunkDownloadInterface *) +{ + +} + +void PeerMonitor::downloadRemoved (kt::ChunkDownloadInterface *) +{ + +} + +void PeerMonitor::stopped () +{ + QMutexLocker lock(&mtx); + + std::fill(mPeers.begin(), mPeers.end(), static_cast( 0 ) ); +// mPeers.clear(); +} + +void PeerMonitor::destroyed () +{ + if(pmPeerMMgr -> find(pmTorIface -> getInfoHash()) != pmPeerMMgr -> end() ) + { + pmTorIface -> setMonitor(0); + pmPeerMMgr -> erase(pmTorIface -> getInfoHash()); + delete this; + } + +} + +double PeerMonitor::LeechersUpSpeed() +{ + QMutexLocker lock(&mtx); + + double spd = 0.0; + + //without it'll segfault/SIGABRT on stop as in meantime the iterator from + // mPeers will be invalidated + + for( data_t::const_iterator it = mPeers.begin(); it != mPeers.end(); it++) + { + if((it != mPeers.end()) && *it && ( (*it) -> getStats().perc_of_file < 100.0) ) + { + spd += (*it) -> getStats().download_rate; + } + } + + return spd; +} + +double PeerMonitor::LeechersDownSpeed() +{ + QMutexLocker lock(&mtx); + + double spd = 0.0; + + + for( data_t::const_iterator it = mPeers.begin(); it != mPeers.end(); it++) + { + if((it != mPeers.end()) && *it && ( (*it) -> getStats().perc_of_file < 100.0) ) + { + spd += (*it) -> getStats().upload_rate; + } + } + + return spd; + +} + +double PeerMonitor::SeedersUpSpeed() +{ + QMutexLocker lock(&mtx); + + double spd = 0.0; + + + for( data_t::const_iterator it = mPeers.begin(); it != mPeers.end(); it++) + { + if((it != mPeers.end()) && *it && ( (*it) -> getStats().perc_of_file == 100.0) ) + { + spd += (*it) -> getStats().download_rate; + } + + } + + return spd; + +} + +uint64_t PeerMonitor::GetLeechers() +{ + QMutexLocker lock(&mtx); + + uint64_t l = 0; + + + for( data_t::const_iterator it = mPeers.begin(); it != mPeers.end(); it++) + { + if((it != mPeers.end()) && *it && ( (*it) -> getStats().perc_of_file != 100.0) ) + { + l++; + } + + } + + return l; +} + +uint64_t PeerMonitor::GetSeeders() +{ + QMutexLocker lock(&mtx); + + uint64_t s = 0; + + + for( data_t::const_iterator it = mPeers.begin(); it != mPeers.end(); it++) + { + if((it != mPeers.end()) && *it && ( (*it) -> getStats().perc_of_file == 100) ) + { + s++; + } + + } + + return s; +} + +kt::TorrentInterface * PeerMonitor::GetTorIface() const +{ + return pmTorIface; +} + +} //NS end -- cgit v1.2.1