1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "peerwireclient.h"
43 #include "ratecontroller.h"
47 Q_GLOBAL_STATIC(RateController
, rateController
)
49 RateController
*RateController::instance()
51 return rateController();
54 void RateController::addSocket(PeerWireClient
*socket
)
56 connect(socket
, SIGNAL(readyToTransfer()), this, SLOT(scheduleTransfer()));
57 socket
->setReadBufferSize(downLimit
* 4);
62 void RateController::removeSocket(PeerWireClient
*socket
)
64 disconnect(socket
, SIGNAL(readyToTransfer()), this, SLOT(scheduleTransfer()));
65 socket
->setReadBufferSize(0);
66 sockets
.remove(socket
);
69 void RateController::setDownloadLimit(int bytesPerSecond
)
71 downLimit
= bytesPerSecond
;
72 foreach (PeerWireClient
*socket
, sockets
)
73 socket
->setReadBufferSize(downLimit
* 4);
76 void RateController::scheduleTransfer()
78 if (transferScheduled
)
80 transferScheduled
= true;
81 QTimer::singleShot(50, this, SLOT(transfer()));
84 void RateController::transfer()
86 transferScheduled
= false;
87 if (sockets
.isEmpty())
91 if (!stopWatch
.isNull())
92 msecs
= qMin(msecs
, stopWatch
.elapsed());
94 qint64 bytesToWrite
= (upLimit
* msecs
) / 1000;
95 qint64 bytesToRead
= (downLimit
* msecs
) / 1000;
96 if (bytesToWrite
== 0 && bytesToRead
== 0) {
101 QSet
<PeerWireClient
*> pendingSockets
;
102 foreach (PeerWireClient
*client
, sockets
) {
103 if (client
->canTransferMore())
104 pendingSockets
<< client
;
106 if (pendingSockets
.isEmpty())
111 bool canTransferMore
;
113 canTransferMore
= false;
114 qint64 writeChunk
= qMax
<qint64
>(1, bytesToWrite
/ pendingSockets
.size());
115 qint64 readChunk
= qMax
<qint64
>(1, bytesToRead
/ pendingSockets
.size());
117 QSetIterator
<PeerWireClient
*> it(pendingSockets
);
118 while (it
.hasNext() && (bytesToWrite
> 0 || bytesToRead
> 0)) {
119 PeerWireClient
*socket
= it
.next();
120 if (socket
->state() != QAbstractSocket::ConnectedState
) {
121 pendingSockets
.remove(socket
);
125 bool dataTransferred
= false;
126 qint64 available
= qMin
<qint64
>(socket
->socketBytesAvailable(), readChunk
);
128 qint64 readBytes
= socket
->readFromSocket(qMin
<qint64
>(available
, bytesToRead
));
130 bytesToRead
-= readBytes
;
131 dataTransferred
= true;
135 if (upLimit
* 2 > socket
->bytesToWrite()) {
136 qint64 chunkSize
= qMin
<qint64
>(writeChunk
, bytesToWrite
);
137 qint64 toWrite
= qMin(upLimit
* 2 - socket
->bytesToWrite(), chunkSize
);
139 qint64 writtenBytes
= socket
->writeToSocket(toWrite
);
140 if (writtenBytes
> 0) {
141 bytesToWrite
-= writtenBytes
;
142 dataTransferred
= true;
147 if (dataTransferred
&& socket
->canTransferMore())
148 canTransferMore
= true;
150 pendingSockets
.remove(socket
);
152 } while (canTransferMore
&& (bytesToWrite
> 0 || bytesToRead
> 0) && !pendingSockets
.isEmpty());
154 if (canTransferMore
|| bytesToWrite
== 0 || bytesToRead
== 0)