File: transfergroupscheduler.cpp

package info (click to toggle)
kget 4%3A16.08.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,440 kB
  • ctags: 4,323
  • sloc: cpp: 37,020; xml: 341; python: 290; perl: 41; sh: 11; makefile: 4
file content (125 lines) | stat: -rw-r--r-- 5,335 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* This file is part of the KDE project

   Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>

   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.
*/
#include "transfergroupscheduler.h"

#include "kget.h"
#include "transfergrouphandler.h"
#include "settings.h"

TransferGroupScheduler::TransferGroupScheduler(QObject *parent)
  : Scheduler(parent),
    m_downloadLimit(0),
    m_uploadLimit(0)
{
}

TransferGroupScheduler::~TransferGroupScheduler()
{
}

void TransferGroupScheduler::calculateSpeedLimits()
{
    calculateDownloadLimit();
    calculateUploadLimit();
}

void TransferGroupScheduler::calculateDownloadLimit()
{
    int n = KGet::allTransferGroups().count();
    int pool = 0;//We create a pool where we have some KiB/s to go to other groups...
    QList<TransferGroupHandler*> transfergroupsNeedSpeed;
    foreach (TransferGroupHandler *handler, KGet::allTransferGroups())
    {
        if (!Settings::speedLimit())
            handler->setDownloadLimit(handler->downloadLimit(Transfer::VisibleSpeedLimit), Transfer::InvisibleSpeedLimit);
        else if (handler->transfers().count() < 1)
        {
            pool = pool + downloadLimit() / n;
        }
        else if (downloadLimit() == 0 && handler->downloadLimit(Transfer::VisibleSpeedLimit) != 0)
            continue;
        else if (downloadLimit() == 0 && handler->downloadLimit(Transfer::VisibleSpeedLimit) == 0)
            handler->setDownloadLimit(0, Transfer::InvisibleSpeedLimit);
        else if (handler->downloadLimit(Transfer::VisibleSpeedLimit) < downloadLimit() / n 
                                               && handler->downloadLimit(Transfer::VisibleSpeedLimit) != 0)
            /*If the handler's visible download limit is under the new one, 
                           we move the KiB/s which are different to the pool*/
            pool = pool + (downloadLimit() / n - handler->downloadLimit(Transfer::VisibleSpeedLimit));
        else if (handler->downloadSpeed() + 10 < downloadLimit() / n)
        {
            /*When the downloadSpeed of the handler is under the new downloadLimit + 10 then we 
                    set the downloadLimit to the downloadSpeed + 10*/
            pool = pool + downloadLimit() / n - handler->downloadSpeed() + 10;
            handler->setDownloadLimit(handler->downloadSpeed() + 10, Transfer::InvisibleSpeedLimit);
        }
        else
        {
            handler->setDownloadLimit(downloadLimit() / n, Transfer::InvisibleSpeedLimit);
            transfergroupsNeedSpeed.append(handler);
        }
    }
    foreach (TransferGroupHandler *handler, transfergroupsNeedSpeed)
    {
        handler->setDownloadLimit(downloadLimit() / n + pool / transfergroupsNeedSpeed.count(), Transfer::InvisibleSpeedLimit);
    }
}

void TransferGroupScheduler::calculateUploadLimit()
{
    int n = KGet::allTransferGroups().count();
    kDebug(5001) << n;
    int pool = 0;//We create a pool where we have some KiB/s to go to other groups...
    QList<TransferGroupHandler*> transfergroupsNeedSpeed;
    foreach (TransferGroupHandler *handler, KGet::allTransferGroups())
    {
        if (!Settings::speedLimit())
            handler->setUploadLimit(handler->uploadLimit(Transfer::VisibleSpeedLimit), Transfer::InvisibleSpeedLimit);
        else if (handler->transfers().count() < 1)
            pool = pool + uploadLimit() / n;
        else if (uploadLimit() == 0 && handler->uploadLimit(Transfer::VisibleSpeedLimit) != 0)
            continue;
        else if (uploadLimit() == 0 && handler->uploadLimit(Transfer::VisibleSpeedLimit) == 0)
            handler->setUploadLimit(0, Transfer::InvisibleSpeedLimit);
        else if (handler->uploadLimit(Transfer::VisibleSpeedLimit) < uploadLimit() / n && handler->uploadLimit(Transfer::VisibleSpeedLimit) != 0)
            /*If the handler's visible download limit is under the new one, 
                           we move the KiB/s which are different to the pool*/
            pool = pool + (uploadLimit() / n - handler->uploadLimit(Transfer::VisibleSpeedLimit));       
        else if (handler->uploadSpeed() + 10 < uploadLimit() / n)
        {
            /*When the downloadSpeed of the handler is under the new downloadLimit + 10 then we 
                    set the downloadLimit to the downloadSpeed + 10*/
            pool = pool + uploadLimit() / n - handler->uploadSpeed() + 10;
            handler->setUploadLimit(handler->uploadSpeed() + 10, Transfer::InvisibleSpeedLimit);
        }
        else
        {
            handler->setUploadLimit(uploadLimit() / n, Transfer::InvisibleSpeedLimit);
            transfergroupsNeedSpeed.append(handler);
        }
    }
    foreach (TransferGroupHandler *handler, transfergroupsNeedSpeed)
    {
        handler->setUploadLimit(uploadLimit() / n + pool / transfergroupsNeedSpeed.count(), Transfer::InvisibleSpeedLimit);
    }
}

void TransferGroupScheduler::setDownloadLimit(int limit)
{
    m_downloadLimit = limit;
    calculateDownloadLimit();
}

void TransferGroupScheduler::setUploadLimit(int limit)
{
    m_uploadLimit = limit;
    calculateUploadLimit();
}

#include "transfergroupscheduler.moc"