examples/network/torrent/ratecontroller.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     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
       
    14 ** this package.
       
    15 **
       
    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.
       
    23 **
       
    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.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "peerwireclient.h"
       
    43 #include "ratecontroller.h"
       
    44 
       
    45 #include <QtCore>
       
    46 
       
    47 Q_GLOBAL_STATIC(RateController, rateController)
       
    48 
       
    49 RateController *RateController::instance()
       
    50 {
       
    51     return rateController();
       
    52 }
       
    53 
       
    54 void RateController::addSocket(PeerWireClient *socket)
       
    55 {
       
    56     connect(socket, SIGNAL(readyToTransfer()), this, SLOT(scheduleTransfer()));
       
    57     socket->setReadBufferSize(downLimit * 4);
       
    58     sockets << socket;
       
    59     scheduleTransfer();
       
    60 }
       
    61 
       
    62 void RateController::removeSocket(PeerWireClient *socket)
       
    63 {
       
    64     disconnect(socket, SIGNAL(readyToTransfer()), this, SLOT(scheduleTransfer()));
       
    65     socket->setReadBufferSize(0);
       
    66     sockets.remove(socket);
       
    67 }
       
    68 
       
    69 void RateController::setDownloadLimit(int bytesPerSecond)
       
    70 {
       
    71     downLimit = bytesPerSecond;
       
    72     foreach (PeerWireClient *socket, sockets)
       
    73         socket->setReadBufferSize(downLimit * 4);
       
    74 }
       
    75 
       
    76 void RateController::scheduleTransfer()
       
    77 {
       
    78     if (transferScheduled)
       
    79         return;
       
    80     transferScheduled = true;
       
    81     QTimer::singleShot(50, this, SLOT(transfer()));
       
    82 }
       
    83 
       
    84 void RateController::transfer()
       
    85 {
       
    86     transferScheduled = false;
       
    87     if (sockets.isEmpty())
       
    88         return;
       
    89 
       
    90     int msecs = 1000;
       
    91     if (!stopWatch.isNull())
       
    92         msecs = qMin(msecs, stopWatch.elapsed());
       
    93 
       
    94     qint64 bytesToWrite = (upLimit * msecs) / 1000;
       
    95     qint64 bytesToRead = (downLimit * msecs) / 1000;
       
    96     if (bytesToWrite == 0 && bytesToRead == 0) {
       
    97         scheduleTransfer();
       
    98         return;
       
    99     }
       
   100 
       
   101     QSet<PeerWireClient *> pendingSockets;
       
   102     foreach (PeerWireClient *client, sockets) {
       
   103         if (client->canTransferMore())
       
   104             pendingSockets << client;
       
   105     }
       
   106     if (pendingSockets.isEmpty())
       
   107         return;
       
   108 
       
   109     stopWatch.start();
       
   110 
       
   111     bool canTransferMore;
       
   112     do {
       
   113         canTransferMore = false;
       
   114         qint64 writeChunk = qMax<qint64>(1, bytesToWrite / pendingSockets.size());
       
   115         qint64 readChunk = qMax<qint64>(1, bytesToRead / pendingSockets.size());
       
   116 
       
   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);
       
   122                 continue;
       
   123             }
       
   124 
       
   125             bool dataTransferred = false;
       
   126             qint64 available = qMin<qint64>(socket->socketBytesAvailable(), readChunk);
       
   127             if (available > 0) {
       
   128                 qint64 readBytes = socket->readFromSocket(qMin<qint64>(available, bytesToRead));
       
   129                 if (readBytes > 0) {
       
   130                     bytesToRead -= readBytes;
       
   131                     dataTransferred = true;
       
   132                 }
       
   133             }
       
   134 
       
   135             if (upLimit * 2 > socket->bytesToWrite()) {
       
   136                 qint64 chunkSize = qMin<qint64>(writeChunk, bytesToWrite);
       
   137                 qint64 toWrite = qMin(upLimit * 2 - socket->bytesToWrite(), chunkSize);
       
   138                 if (toWrite > 0) {
       
   139                     qint64 writtenBytes = socket->writeToSocket(toWrite);
       
   140                     if (writtenBytes > 0) {
       
   141                         bytesToWrite -= writtenBytes;
       
   142                         dataTransferred = true;
       
   143                     }
       
   144                 }
       
   145             }
       
   146 
       
   147             if (dataTransferred && socket->canTransferMore())
       
   148                 canTransferMore = true;
       
   149             else
       
   150                 pendingSockets.remove(socket);
       
   151         }
       
   152     } while (canTransferMore && (bytesToWrite > 0 || bytesToRead > 0) && !pendingSockets.isEmpty());
       
   153 
       
   154     if (canTransferMore || bytesToWrite == 0 || bytesToRead == 0)
       
   155         scheduleTransfer();
       
   156 }