File indexing completed on 2024-02-25 05:07:08

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 */
0010 #include "transfergroupscheduler.h"
0011 
0012 #include "kget.h"
0013 #include "settings.h"
0014 #include "transfergrouphandler.h"
0015 
0016 #include "kget_debug.h"
0017 #include <QDebug>
0018 
0019 TransferGroupScheduler::TransferGroupScheduler(QObject *parent)
0020     : Scheduler(parent)
0021     , m_downloadLimit(0)
0022     , m_uploadLimit(0)
0023 {
0024 }
0025 
0026 TransferGroupScheduler::~TransferGroupScheduler()
0027 {
0028 }
0029 
0030 void TransferGroupScheduler::calculateSpeedLimits()
0031 {
0032     calculateDownloadLimit();
0033     calculateUploadLimit();
0034 }
0035 
0036 void TransferGroupScheduler::calculateDownloadLimit()
0037 {
0038     int n = KGet::allTransferGroups().count();
0039     int pool = 0; // We create a pool where we have some KiB/s to go to other groups...
0040     QList<TransferGroupHandler *> transfergroupsNeedSpeed;
0041     foreach (TransferGroupHandler *handler, KGet::allTransferGroups()) {
0042         if (!Settings::speedLimit())
0043             handler->setDownloadLimit(handler->downloadLimit(Transfer::VisibleSpeedLimit), Transfer::InvisibleSpeedLimit);
0044         else if (handler->transfers().count() < 1) {
0045             pool = pool + downloadLimit() / n;
0046         } else if (downloadLimit() == 0 && handler->downloadLimit(Transfer::VisibleSpeedLimit) != 0)
0047             continue;
0048         else if (downloadLimit() == 0 && handler->downloadLimit(Transfer::VisibleSpeedLimit) == 0)
0049             handler->setDownloadLimit(0, Transfer::InvisibleSpeedLimit);
0050         else if (handler->downloadLimit(Transfer::VisibleSpeedLimit) < downloadLimit() / n && handler->downloadLimit(Transfer::VisibleSpeedLimit) != 0)
0051             /*If the handler's visible download limit is under the new one,
0052                            we move the KiB/s which are different to the pool*/
0053             pool = pool + (downloadLimit() / n - handler->downloadLimit(Transfer::VisibleSpeedLimit));
0054         else if (handler->downloadSpeed() + 10 < downloadLimit() / n) {
0055             /*When the downloadSpeed of the handler is under the new downloadLimit + 10 then we
0056                     set the downloadLimit to the downloadSpeed + 10*/
0057             pool = pool + downloadLimit() / n - handler->downloadSpeed() + 10;
0058             handler->setDownloadLimit(handler->downloadSpeed() + 10, Transfer::InvisibleSpeedLimit);
0059         } else {
0060             handler->setDownloadLimit(downloadLimit() / n, Transfer::InvisibleSpeedLimit);
0061             transfergroupsNeedSpeed.append(handler);
0062         }
0063     }
0064     foreach (TransferGroupHandler *handler, transfergroupsNeedSpeed) {
0065         handler->setDownloadLimit(downloadLimit() / n + pool / transfergroupsNeedSpeed.count(), Transfer::InvisibleSpeedLimit);
0066     }
0067 }
0068 
0069 void TransferGroupScheduler::calculateUploadLimit()
0070 {
0071     int n = KGet::allTransferGroups().count();
0072     qCDebug(KGET_DEBUG) << n;
0073     int pool = 0; // We create a pool where we have some KiB/s to go to other groups...
0074     QList<TransferGroupHandler *> transfergroupsNeedSpeed;
0075     foreach (TransferGroupHandler *handler, KGet::allTransferGroups()) {
0076         if (!Settings::speedLimit())
0077             handler->setUploadLimit(handler->uploadLimit(Transfer::VisibleSpeedLimit), Transfer::InvisibleSpeedLimit);
0078         else if (handler->transfers().count() < 1)
0079             pool = pool + uploadLimit() / n;
0080         else if (uploadLimit() == 0 && handler->uploadLimit(Transfer::VisibleSpeedLimit) != 0)
0081             continue;
0082         else if (uploadLimit() == 0 && handler->uploadLimit(Transfer::VisibleSpeedLimit) == 0)
0083             handler->setUploadLimit(0, Transfer::InvisibleSpeedLimit);
0084         else if (handler->uploadLimit(Transfer::VisibleSpeedLimit) < uploadLimit() / n && handler->uploadLimit(Transfer::VisibleSpeedLimit) != 0)
0085             /*If the handler's visible download limit is under the new one,
0086                            we move the KiB/s which are different to the pool*/
0087             pool = pool + (uploadLimit() / n - handler->uploadLimit(Transfer::VisibleSpeedLimit));
0088         else if (handler->uploadSpeed() + 10 < uploadLimit() / n) {
0089             /*When the downloadSpeed of the handler is under the new downloadLimit + 10 then we
0090                     set the downloadLimit to the downloadSpeed + 10*/
0091             pool = pool + uploadLimit() / n - handler->uploadSpeed() + 10;
0092             handler->setUploadLimit(handler->uploadSpeed() + 10, Transfer::InvisibleSpeedLimit);
0093         } else {
0094             handler->setUploadLimit(uploadLimit() / n, Transfer::InvisibleSpeedLimit);
0095             transfergroupsNeedSpeed.append(handler);
0096         }
0097     }
0098     foreach (TransferGroupHandler *handler, transfergroupsNeedSpeed) {
0099         handler->setUploadLimit(uploadLimit() / n + pool / transfergroupsNeedSpeed.count(), Transfer::InvisibleSpeedLimit);
0100     }
0101 }
0102 
0103 void TransferGroupScheduler::setDownloadLimit(int limit)
0104 {
0105     m_downloadLimit = limit;
0106     calculateDownloadLimit();
0107 }
0108 
0109 void TransferGroupScheduler::setUploadLimit(int limit)
0110 {
0111     m_uploadLimit = limit;
0112     calculateUploadLimit();
0113 }
0114 
0115 #include "moc_transfergroupscheduler.cpp"