Protocols::BitTorrent::Transfers::Choker Class Reference

#include <Choker.h> [code]

Inherits Protocols::BitTorrent::Transfers::PacketProcessor.

Inheritance diagram for Protocols::BitTorrent::Transfers::Choker:

Inheritance graph
[legend]
Collaboration diagram for Protocols::BitTorrent::Transfers::Choker:

Collaboration graph
[legend]
List of all members.

Detailed Description

Implements a choking algorithm, chokes and unchokes peer connections.

Implements the standard choking algorithm presented in the official BitTorrent specs: http://www.bittorrent.org/protocol.html

Supports:

Todo:
UploadSlotsCount should be configurable/dynamic. Maybe use an interface to something like a SlotsManager. It could decide based on the currently available traffic or static user configuration how many upload slots can be used. It might also take care of splitting the Calitko-wide number of slots to the individual protocols.

Definition at line 78 of file Choker.h.

Public Member Functions

 Choker (Timer *, RandomNumberGenerator *, TransferManagerState *)
 Constructs a Choker object.
 ~Choker ()
 Destructor.
void start ()
 Start the Choker - it will choke/unchoke sessions in a regular interval.
void stop ()
 Stop the Choker - it will no longer choke/unchoke sessions.
void chokingTime ()
 The handler that gets called for each choking/unchoking round.
void openedSession (TransferSession *)
 Add a new session to the Choker.
void incomingPacket (TransferSession *, const Packet &)
 We ignore all incoming packets.
void outgoingPacket (TransferSession *, const Packet &)
 We ignore all outgoing packets.
void closedSession (TransferSession *)
 When a connection is closed just remove if from the internal lists.

Private Types

typedef QList< TransferSession * > Sessions

Private Member Functions

CALITKO_TESTABLE Choker (const Choker &)
Chokeroperator= (const Choker &)
void doOptimisticUnchokingIfTimeHasCome ()
 Helper that does optimistic unchoking only if we have reached this round.
Choker::Sessions choseOptimisticlyUnchokedSessions ()
 Helper that selects a session to be our optimistic unchoke.
void sortSessionsForSelection ()
 Helper that sorts the session by putting the best peers first.
void reallocateUploadSlotsAndChokeUnchokeSessions ()
 Helper that unchokes best uploading peers and chokes the others.
int numberOfUploadSlots () const
 Helper that returns the number of upload slots we have available.

Static Private Member Functions

static void chokeSessionIfNeeded (TransferSession *)
 Helper that send a Choke packet only if peer is currently unchoked.
static void unchokeSessionIfNeeded (TransferSession *)
 Helper that send an Unchoke packet only if peer is currently choked.
static bool moreDownloadedAndMoreInterested (const TransferSession *, const TransferSession *)
 MoreThan comparison helper.
static bool moreUploadedAndMoreInterested (const TransferSession *, const TransferSession *)
 MoreThan comparison helper.

Private Attributes

Timertimer
RandomNumberGeneratorrandomNumberGenerator
TransferManagerStatetransferManagerState
Sessions sessions
Sessions newSessions
Sessions optimisticlyUnchokedSessions
int optimisticChokingCounter

Static Private Attributes

static const int UploadSlotsCount = 4
static const int OptimisticChokingRate = 3
static const int ChokingTimeout = 10000
static const Choke choke
static const Unchoke unchoke


Member Typedef Documentation

typedef QList<TransferSession *> Protocols::BitTorrent::Transfers::Choker::Sessions [private]
 

Definition at line 101 of file Choker.h.


Constructor & Destructor Documentation

CALITKO_TESTABLE Protocols::BitTorrent::Transfers::Choker::Choker const Choker  )  [private]
 

Choker::Choker Timer timer,
RandomNumberGenerator randomNumberGenerator,
TransferManagerState transferManagerState
 

Constructs a Choker object.

Parameters:
timer is the Timer object used to have our chokingTime() handler called for each choking/unchoking round.

Definition at line 33 of file Choker.cpp.

Choker::~Choker  ) 
 

Destructor.

Definition at line 44 of file Choker.cpp.


Member Function Documentation

void Choker::chokeSessionIfNeeded TransferSession  )  [static, private]
 

Helper that send a Choke packet only if peer is currently unchoked.

Definition at line 244 of file Choker.cpp.

void Choker::chokingTime  ) 
 

The handler that gets called for each choking/unchoking round.

Definition at line 94 of file Choker.cpp.

Choker::Sessions Choker::choseOptimisticlyUnchokedSessions  )  [private]
 

Helper that selects a session to be our optimistic unchoke.

Randomly selects one of the choked sessions to become the next optimistic unchoke. New connections (since the last optimistic choke) are given three times bigger chance to get elected.

Note:
We do not consider seeders as optimistic unchoking candidates as they need to always be choked because they would never request a piece from us.

We only do optimistic unchokes for a snubbed session if we are in downloading mode. We would want to find better sessions which could reciprocate, so that we could complete our downloads faster. Once in seeding mode we will ignore being snubbed because there is actually nothing that the remote peers would be able to provide us.

Definition at line 141 of file Choker.cpp.

void Choker::closedSession TransferSession session  )  [virtual]
 

When a connection is closed just remove if from the internal lists.

Note:
The closed session could have occupied an upload slot which will not be reallocated to another session until chokingTime() is called by the Timer in no more than ChokingTimeout miliseconds.

Implements Protocols::BitTorrent::Transfers::PacketProcessor.

Definition at line 87 of file Choker.cpp.

void Choker::doOptimisticUnchokingIfTimeHasCome  )  [private]
 

Helper that does optimistic unchoking only if we have reached this round.

Todo:
What if we have UploadSlotsCount changing dynamically at runtime and the new number of slots is less than before. Additionally, we have a number of snubbed connections which for each of which make an additional optimistic unchoke. Then we could end up with more optimistic unchokes than number of slots. The ones that are too much should not be optimisticly unchoked.

Definition at line 110 of file Choker.cpp.

void Choker::incomingPacket TransferSession ,
const Packet
[virtual]
 

We ignore all incoming packets.

Implements Protocols::BitTorrent::Transfers::PacketProcessor.

Definition at line 70 of file Choker.cpp.

bool Choker::moreDownloadedAndMoreInterested const TransferSession a,
const TransferSession b
[static, private]
 

MoreThan comparison helper.

Parameters:
a first session.
b second session.
Returns:
true if the download speed of a is smaller from the download speed fo b. If the speeds are equal, returns true if is more interested than b (i.e. a is interested and b is not).

false otherwise.

Definition at line 267 of file Choker.cpp.

bool Choker::moreUploadedAndMoreInterested const TransferSession a,
const TransferSession b
[static, private]
 

MoreThan comparison helper.

Parameters:
a first session.
b second session.
Returns:
true if the upload speed of a is smaller from the upload speed fo b. If the speeds are equal, returns true if is more interested than b (i.e. a is interested and b is not).

false otherwise.

Definition at line 291 of file Choker.cpp.

int Choker::numberOfUploadSlots  )  const [private]
 

Helper that returns the number of upload slots we have available.

Note:
The optimisticly choked sessions that are interested are counted towards the upload slots limit.

Definition at line 232 of file Choker.cpp.

void Choker::openedSession TransferSession  )  [virtual]
 

Add a new session to the Choker.

Implements Protocols::BitTorrent::Transfers::PacketProcessor.

Definition at line 63 of file Choker.cpp.

Choker& Protocols::BitTorrent::Transfers::Choker::operator= const Choker  )  [private]
 

void Choker::outgoingPacket TransferSession ,
const Packet
[virtual]
 

We ignore all outgoing packets.

Implements Protocols::BitTorrent::Transfers::PacketProcessor.

Definition at line 76 of file Choker.cpp.

void Choker::reallocateUploadSlotsAndChokeUnchokeSessions  )  [private]
 

Helper that unchokes best uploading peers and chokes the others.

Precondition:
sessions is already sorted with the "best" peers at front and the "worst" peers at the back of the list (see sortSessionsForSelection()).
Note:
Neither chokes nor unchokes optimisticly choked sessions.
Todo:
If the peer is a seeder it does not need to download anything from us. Should we always keep it choked, or we can treat it like any other peer? I makes sense that we always choke it otherwise we might assign it an uploading slot or select it to be an optimistic choke which makes no sense.

Definition at line 207 of file Choker.cpp.

void Choker::sortSessionsForSelection  )  [private]
 

Helper that sorts the session by putting the best peers first.

Best peers are the peers that we download most from. If two peers upload the same ammount to us then a better one is the one that is interested in downloading from us.

Todo:
If we are uploading only, then sessions should be sorted by upload rather then download speed.

Definition at line 185 of file Choker.cpp.

void Choker::start  ) 
 

Start the Choker - it will choke/unchoke sessions in a regular interval.

Definition at line 49 of file Choker.cpp.

void Choker::stop  ) 
 

Stop the Choker - it will no longer choke/unchoke sessions.

Definition at line 57 of file Choker.cpp.

void Choker::unchokeSessionIfNeeded TransferSession  )  [static, private]
 

Helper that send an Unchoke packet only if peer is currently choked.

Definition at line 251 of file Choker.cpp.


Member Data Documentation

const Choke Choker::choke [static, private]
 

Definition at line 120 of file Choker.h.

const int Protocols::BitTorrent::Transfers::Choker::ChokingTimeout = 10000 [static, private]
 

Definition at line 119 of file Choker.h.

Sessions Protocols::BitTorrent::Transfers::Choker::newSessions [private]
 

Definition at line 127 of file Choker.h.

int Protocols::BitTorrent::Transfers::Choker::optimisticChokingCounter [private]
 

Definition at line 129 of file Choker.h.

const int Protocols::BitTorrent::Transfers::Choker::OptimisticChokingRate = 3 [static, private]
 

Definition at line 118 of file Choker.h.

Sessions Protocols::BitTorrent::Transfers::Choker::optimisticlyUnchokedSessions [private]
 

Definition at line 128 of file Choker.h.

RandomNumberGenerator* Protocols::BitTorrent::Transfers::Choker::randomNumberGenerator [private]
 

Definition at line 124 of file Choker.h.

Sessions Protocols::BitTorrent::Transfers::Choker::sessions [private]
 

Definition at line 126 of file Choker.h.

Timer* Protocols::BitTorrent::Transfers::Choker::timer [private]
 

Definition at line 123 of file Choker.h.

TransferManagerState* Protocols::BitTorrent::Transfers::Choker::transferManagerState [private]
 

Definition at line 125 of file Choker.h.

const Unchoke Choker::unchoke [static, private]
 

Definition at line 121 of file Choker.h.

const int Protocols::BitTorrent::Transfers::Choker::UploadSlotsCount = 4 [static, private]
 

Definition at line 117 of file Choker.h.


The documentation for this class was generated from the following files: