Protocols::BitTorrent::Transfers::TransferSessionImpl Class Reference

#include <TransferSessionImpl.h> [code]

Inherits Protocols::BitTorrent::Transfers::TransferSession, and Protocols::BitTorrent::Transfers::SessionStatus.

Inheritance diagram for Protocols::BitTorrent::Transfers::TransferSessionImpl:

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

Collaboration graph
[legend]
List of all members.

Detailed Description

A standard TransferSession implementation.

This implementatio of TransferSession keeps track of all incoming and outgoing packets and updates the internal state accordingly.

Note:
The Session object used should be configured to use prioritized DataQueue (e.g. PiecesAreLowPriorityQueue). The reason is that Piece packets are much bigger compared to the other type of packets and they better be have lower priority in order for control packets to pass through faster.
Todo:
Rename to StandardTransferSession??

Definition at line 92 of file TransferSessionImpl.h.

Public Member Functions

 TransferSessionImpl (Session *, const Handshake &, IntegerResetStopwatch *downloadStopwatch, IntegerResetStopwatch *uploadStopwatch, TransferSessionStatus *)
 Constructs a TransferSessionImpl object.
void sendPacket (const Packet &)
 Queues up and sends packet at first possible occasion.
void close ()
 Closes this session.
bool areWeChoked () const
 Tells whether the remote peer has choked us.
bool areWeInterested () const
 Tells whether we have registered interest in the other peer's pieces.
bool areWeSnubbed () const
 Tells whether the remote peer has not uploaded anything to us recently.
bool areWeSeeder () const
 Tells whether are are a seeder (we have reported to have all pieces).
bool isPeerChoked () const
 Tells whether we have choked the remote peer.
bool isPeerInterested () const
 Tells whether the remote peer has registered interest in our pieces.
bool isPeerSnubbed () const
 Tells whether we have not uploaded anything to the peer recently.
bool isPeerSeeder () const
 Tells whether the remote peer has reported is is a seeder (has all pieces).
qint64 totalDownloaded () const
 The total number of bytes (raw Piece bytes only) downloaded from the peer.
qint64 totalUploaded () const
 The total number of bytes (raw Piece bytes only) uploaded to the peer.
qint64 currentDownloadSpeed () const
 Average download speed (bytes/second) calculated over a sliding time window.
qint64 currentUploadSpeed () const
 Average upload speed (bytes/second) calculated over a sliding time window.
QBitArray peerPiecesAvailable () const
 Returns a bit array indicating all pieces the remote peer reported to have.
QBitArray wePiecesAvailable () const
 Returns a bit array indicating all pieces we reported to have.
void sessionReceivedData (Session *, const Data &)
 Handler for the SessionState notification.
void sessionSendingData (Session *, const Data &)
 Handler for the SessionState notification.
void sessionClosing (Session *)
 Handler for the SessionState notification.
void sessionClosed (Session *)
 Handler for the SessionState notification.

Private Types

typedef ObjectDispatcher<
PacketBase
PacketDispatcher

Private Member Functions

CALITKO_TESTABLE TransferSessionImpl (const TransferSessionImpl &)
TransferSessionImploperator= (const TransferSessionImpl &)
void receivedChoke (const Choke &)
 Handler for Choke packets - sets our 'choked' status.
void receivedUnchoke (const Unchoke &)
 Handler for Unchoke packets - clears our 'choked' status.
void receivedInterested (const Interested &)
 Handler for Interested packets - sets peer's 'interested' status.
void receivedNotInterested (const NotInterested &)
 Handler for NotInterested packets - clears peer's 'interested' status.
void receivedPiece (const Piece &)
 Handler for Piece packets - keeps track of total download volume and speed.
void receivedBitField (const BitField &)
 Handler for BitField packet - keeps track of peer's available pieces.
void receivedHaveAll (const HaveAll &)
 Handler for HaveAll packet - marks all pieces are available at the peer.
void receivedHaveNone (const HaveNone &)
 Handler for HaveNone packet - marks all pieces as not available at the peer.
void receivedHave (const Have &)
 Handler for Have packets - marks the corresponding piece as available.
void receivedUnhandled (const PacketBase &)
 Handler for an unhandled packet types.
void sentChoke (const Choke &)
 Handler for Choke packets - sets the peer's 'choked' status.
void sentUnchoke (const Unchoke &)
 Handler for Unchoke packets - clars the peer's 'choked' status.
void sentInterested (const Interested &)
 Handler for Interested packets - sets our 'interested' status.
void sentNotInterested (const NotInterested &)
 Handler for Interested packets - clears our 'interested' status.
void sentPiece (const Piece &)
 Handler for Piece packets - keeps track of total upload volume and speed.
void sentBitField (const BitField &)
 Handler for BitField packet - keeps track of our available pieces.
void sentHaveAll (const HaveAll &)
 Handler for HaveAll packet - marks all pieces as available at our side.
void sentHaveNone (const HaveNone &)
 Handler for HaveNone packet - marks all pieces as not available at our side.
void sentHave (const Have &)
 Handler for Have packets - marks the corresponding piece as available.
void sentUnhandled (const PacketBase &)
 Handler for unhandled packet types.
void updateDownloadSpeed (int pieceLength) const
 Helper to add a number of bytes to the download speed computation window.
void updateUploadSpeed (int pieceLength) const
 Helper to add a number of bytes to the upload speed computation window.
void unregisterBitFieldHaveAllHaveNoneReceiveHandlers ()
 Helper to unregister the handlers for BitField, HaveAll, HaveNone packets.
void unregisterBitFieldHaveAllHaveNoneSendHandlers ()
 Helper to unregister the handlers for BitField, HaveAll, HaveNone packets.

Static Private Member Functions

static void shiftList (QList< qint64 > &list, qint64 count)
 Helper that shift a list of integers to the right adding 0s on the left.
static qint64 calculateAverage (const QList< qint64 > &list)
 Calculatex the sum of all elements in list and devide by its size.
static void setPiecesAvailable (QBitArray &piecesAvailable, const QByteArray &bitField)
 Sets piecesAvailable from the raw bytes in bitField.

Private Attributes

Sessionsession_
Handshake handshake_
TransferSessionStatusstatus_
PacketDispatcher inPacketDispatcher_
PacketDispatcher outPacketDispatcher_
bool areWeChoked_
bool areWeInterested_
bool areWeSeeder_
bool isPeerChoked_
bool isPeerInterested_
bool isPeerSeeder_
qint64 totalDownloaded_
qint64 totalUploaded_
IntegerResetStopwatchdownloadStopwatch_
IntegerResetStopwatchuploadStopwatch_
QList< qint64 > downloadHistory_
QList< qint64 > uploadHistory_
QBitArray peerPiecesAvailable_
QBitArray wePiecesAvailable_

Static Private Attributes

static const int SpeedMovingAverageDuration = 20
 Time window (seconds) used to calculate average upload/download speeds.
static const int SnubbedTimeout = 20
 Time (seconds) no Piece is received to consider the session snubbed.


Member Typedef Documentation

typedef ObjectDispatcher<PacketBase> Protocols::BitTorrent::Transfers::TransferSessionImpl::PacketDispatcher [private]
 

Definition at line 163 of file TransferSessionImpl.h.


Constructor & Destructor Documentation

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

TransferSessionImpl::TransferSessionImpl Session session,
const Handshake handshake,
IntegerResetStopwatch downloadStopwatch,
IntegerResetStopwatch uploadStopwatch,
TransferSessionStatus status
 

Constructs a TransferSessionImpl object.

Parameters:
session is the underlying Session object to use.
handshake contains the already negotiated connection parameters.
downloadStopwatch is a IntegerResetStopwatch object.
uploadStopwatch is a IntegerResetStopwatch object.
status is the object to receive our status notifications.
Todo:
Register handlers fro the Fast Extension packets only if Fast Extension was negotiated. Done for HaveAll and HaveNone - register and implement also SuggestPiece and CancelReqeust.

Definition at line 39 of file TransferSessionImpl.cpp.


Member Function Documentation

bool TransferSessionImpl::areWeChoked  )  const [virtual]
 

Tells whether the remote peer has choked us.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 145 of file TransferSessionImpl.cpp.

bool TransferSessionImpl::areWeInterested  )  const [virtual]
 

Tells whether we have registered interest in the other peer's pieces.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 151 of file TransferSessionImpl.cpp.

bool TransferSessionImpl::areWeSeeder  )  const [virtual]
 

Tells whether are are a seeder (we have reported to have all pieces).

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 164 of file TransferSessionImpl.cpp.

bool TransferSessionImpl::areWeSnubbed  )  const [virtual]
 

Tells whether the remote peer has not uploaded anything to us recently.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 157 of file TransferSessionImpl.cpp.

qint64 TransferSessionImpl::calculateAverage const QList< qint64 > &  list  )  [static, private]
 

Calculatex the sum of all elements in list and devide by its size.

Definition at line 469 of file TransferSessionImpl.cpp.

void TransferSessionImpl::close  )  [virtual]
 

Closes this session.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 139 of file TransferSessionImpl.cpp.

qint64 TransferSessionImpl::currentDownloadSpeed  )  const [virtual]
 

Average download speed (bytes/second) calculated over a sliding time window.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 207 of file TransferSessionImpl.cpp.

qint64 TransferSessionImpl::currentUploadSpeed  )  const [virtual]
 

Average upload speed (bytes/second) calculated over a sliding time window.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 214 of file TransferSessionImpl.cpp.

bool TransferSessionImpl::isPeerChoked  )  const [virtual]
 

Tells whether we have choked the remote peer.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 170 of file TransferSessionImpl.cpp.

bool TransferSessionImpl::isPeerInterested  )  const [virtual]
 

Tells whether the remote peer has registered interest in our pieces.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 176 of file TransferSessionImpl.cpp.

bool TransferSessionImpl::isPeerSeeder  )  const [virtual]
 

Tells whether the remote peer has reported is is a seeder (has all pieces).

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 189 of file TransferSessionImpl.cpp.

bool TransferSessionImpl::isPeerSnubbed  )  const [virtual]
 

Tells whether we have not uploaded anything to the peer recently.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 182 of file TransferSessionImpl.cpp.

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

QBitArray TransferSessionImpl::peerPiecesAvailable  )  const [virtual]
 

Returns a bit array indicating all pieces the remote peer reported to have.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 221 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedBitField const BitField  )  [private]
 

Handler for BitField packet - keeps track of peer's available pieces.

Definition at line 309 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedChoke const Choke  )  [private]
 

Handler for Choke packets - sets our 'choked' status.

Definition at line 277 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedHave const Have  )  [private]
 

Handler for Have packets - marks the corresponding piece as available.

Definition at line 332 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedHaveAll const HaveAll  )  [private]
 

Handler for HaveAll packet - marks all pieces are available at the peer.

Definition at line 316 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedHaveNone const HaveNone  )  [private]
 

Handler for HaveNone packet - marks all pieces as not available at the peer.

Definition at line 324 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedInterested const Interested  )  [private]
 

Handler for Interested packets - sets peer's 'interested' status.

Definition at line 289 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedNotInterested const NotInterested  )  [private]
 

Handler for NotInterested packets - clears peer's 'interested' status.

Definition at line 295 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedPiece const Piece  )  [private]
 

Handler for Piece packets - keeps track of total download volume and speed.

Definition at line 301 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedUnchoke const Unchoke  )  [private]
 

Handler for Unchoke packets - clears our 'choked' status.

Definition at line 283 of file TransferSessionImpl.cpp.

void TransferSessionImpl::receivedUnhandled const PacketBase  )  [private]
 

Handler for an unhandled packet types.

A packet handler should be installed for each allowed PacketBase derived packet type. If no handler is registered this means that the remote peer is not allowed to send us packets of this type. If the peer does so anyway we should close the session.

Definition at line 346 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sendPacket const Packet packet  )  [virtual]
 

Queues up and sends packet at first possible occasion.

Parameters:
packet is the Packet to queue and send.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 131 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentBitField const BitField  )  [private]
 

Handler for BitField packet - keeps track of our available pieces.

Definition at line 384 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentChoke const Choke  )  [private]
 

Handler for Choke packets - sets the peer's 'choked' status.

Definition at line 352 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentHave const Have  )  [private]
 

Handler for Have packets - marks the corresponding piece as available.

Definition at line 407 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentHaveAll const HaveAll  )  [private]
 

Handler for HaveAll packet - marks all pieces as available at our side.

Definition at line 391 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentHaveNone const HaveNone  )  [private]
 

Handler for HaveNone packet - marks all pieces as not available at our side.

Definition at line 399 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentInterested const Interested  )  [private]
 

Handler for Interested packets - sets our 'interested' status.

Definition at line 364 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentNotInterested const NotInterested  )  [private]
 

Handler for Interested packets - clears our 'interested' status.

Definition at line 370 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentPiece const Piece  )  [private]
 

Handler for Piece packets - keeps track of total upload volume and speed.

Definition at line 376 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentUnchoke const Unchoke  )  [private]
 

Handler for Unchoke packets - clars the peer's 'choked' status.

Definition at line 358 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sentUnhandled const PacketBase  )  [private]
 

Handler for unhandled packet types.

Control reaches here if a handler for this certain PacketBase derived class is not installed. If no handler is installed this means that sending this type of packets is not allowed. We will assert because this is a programmer error.

Definition at line 421 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sessionClosed Session  ) 
 

Handler for the SessionState notification.

Forwards the notification over our TransferSessionStatus interface.

Definition at line 271 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sessionClosing Session  ) 
 

Handler for the SessionState notification.

Forwards the notification over our TransferSessionStatus interface.

Definition at line 262 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sessionReceivedData Session ,
const Data data
 

Handler for the SessionState notification.

Internally dispatches the packet to update the TransferSessionImpl object's internal state.

Unless the received message causes the session to be closed, the received data is converted to a Packet and a TransferSessionStatus notification is sent.

Definition at line 241 of file TransferSessionImpl.cpp.

void TransferSessionImpl::sessionSendingData Session ,
const Data
 

Handler for the SessionState notification.

Currently ignored. Our corresponding notification transferSessionSendingPacket() is sent from sendPacket().

Definition at line 253 of file TransferSessionImpl.cpp.

void TransferSessionImpl::setPiecesAvailable QBitArray &  piecesAvailable,
const QByteArray &  bitField
[static, private]
 

Sets piecesAvailable from the raw bytes in bitField.

Definition at line 477 of file TransferSessionImpl.cpp.

void TransferSessionImpl::shiftList QList< qint64 > &  list,
qint64  count
[static, private]
 

Helper that shift a list of integers to the right adding 0s on the left.

Definition at line 459 of file TransferSessionImpl.cpp.

qint64 TransferSessionImpl::totalDownloaded  )  const [virtual]
 

The total number of bytes (raw Piece bytes only) downloaded from the peer.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 195 of file TransferSessionImpl.cpp.

qint64 TransferSessionImpl::totalUploaded  )  const [virtual]
 

The total number of bytes (raw Piece bytes only) uploaded to the peer.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 201 of file TransferSessionImpl.cpp.

void TransferSessionImpl::unregisterBitFieldHaveAllHaveNoneReceiveHandlers  )  [private]
 

Helper to unregister the handlers for BitField, HaveAll, HaveNone packets.

By unregistering these handlers we effectively disable the reception of these packet types. If a peer sends us one of these nevertheless, then we will automatically close the session.

These three packet types are exclusive - only one is allowed to be sent and this type should be sent only once right after the session is established.

Note:
We do not enforce that HaveAll, HaveNone or BitField is the first of the packets ever received.

Definition at line 500 of file TransferSessionImpl.cpp.

void TransferSessionImpl::unregisterBitFieldHaveAllHaveNoneSendHandlers  )  [private]
 

Helper to unregister the handlers for BitField, HaveAll, HaveNone packets.

By unregistering these handlers we effectively disable the sending of these packet types. If we send one of these nevertheless, then we will assert.

Definition at line 519 of file TransferSessionImpl.cpp.

void TransferSessionImpl::updateDownloadSpeed int  pieceLength  )  const [private]
 

Helper to add a number of bytes to the download speed computation window.

The function checks how much time elapsed since last piece was received. If more than one second elapsed, the window is slided accordingly. The new bytes are counted in any case.

Todo:
When do we restart the stopwatch? Be carefull with the integers!

Definition at line 434 of file TransferSessionImpl.cpp.

void TransferSessionImpl::updateUploadSpeed int  pieceLength  )  const [private]
 

Helper to add a number of bytes to the upload speed computation window.

The function checks how much time elapsed since last piece was sent. If more than one second elapsed, the window is slided accordingly. The new bytes are counted in any case.

Todo:
When do we restart the stopwatch? Be carefull with the integers!

Definition at line 450 of file TransferSessionImpl.cpp.

QBitArray TransferSessionImpl::wePiecesAvailable  )  const [virtual]
 

Returns a bit array indicating all pieces we reported to have.

Implements Protocols::BitTorrent::Transfers::TransferSession.

Definition at line 227 of file TransferSessionImpl.cpp.


Member Data Documentation

bool Protocols::BitTorrent::Transfers::TransferSessionImpl::areWeChoked_ [private]
 

Definition at line 176 of file TransferSessionImpl.h.

bool Protocols::BitTorrent::Transfers::TransferSessionImpl::areWeInterested_ [private]
 

Definition at line 177 of file TransferSessionImpl.h.

bool Protocols::BitTorrent::Transfers::TransferSessionImpl::areWeSeeder_ [private]
 

Definition at line 178 of file TransferSessionImpl.h.

QList<qint64> Protocols::BitTorrent::Transfers::TransferSessionImpl::downloadHistory_ [mutable, private]
 

Definition at line 187 of file TransferSessionImpl.h.

IntegerResetStopwatch* Protocols::BitTorrent::Transfers::TransferSessionImpl::downloadStopwatch_ [private]
 

Definition at line 185 of file TransferSessionImpl.h.

Handshake Protocols::BitTorrent::Transfers::TransferSessionImpl::handshake_ [private]
 

Definition at line 171 of file TransferSessionImpl.h.

PacketDispatcher Protocols::BitTorrent::Transfers::TransferSessionImpl::inPacketDispatcher_ [private]
 

Definition at line 173 of file TransferSessionImpl.h.

bool Protocols::BitTorrent::Transfers::TransferSessionImpl::isPeerChoked_ [private]
 

Definition at line 179 of file TransferSessionImpl.h.

bool Protocols::BitTorrent::Transfers::TransferSessionImpl::isPeerInterested_ [private]
 

Definition at line 180 of file TransferSessionImpl.h.

bool Protocols::BitTorrent::Transfers::TransferSessionImpl::isPeerSeeder_ [private]
 

Definition at line 181 of file TransferSessionImpl.h.

PacketDispatcher Protocols::BitTorrent::Transfers::TransferSessionImpl::outPacketDispatcher_ [private]
 

Definition at line 174 of file TransferSessionImpl.h.

QBitArray Protocols::BitTorrent::Transfers::TransferSessionImpl::peerPiecesAvailable_ [private]
 

Definition at line 190 of file TransferSessionImpl.h.

Session* Protocols::BitTorrent::Transfers::TransferSessionImpl::session_ [private]
 

Definition at line 170 of file TransferSessionImpl.h.

const int Protocols::BitTorrent::Transfers::TransferSessionImpl::SnubbedTimeout = 20 [static, private]
 

Time (seconds) no Piece is received to consider the session snubbed.

Definition at line 168 of file TransferSessionImpl.h.

const int Protocols::BitTorrent::Transfers::TransferSessionImpl::SpeedMovingAverageDuration = 20 [static, private]
 

Time window (seconds) used to calculate average upload/download speeds.

Definition at line 166 of file TransferSessionImpl.h.

TransferSessionStatus* Protocols::BitTorrent::Transfers::TransferSessionImpl::status_ [private]
 

Definition at line 172 of file TransferSessionImpl.h.

qint64 Protocols::BitTorrent::Transfers::TransferSessionImpl::totalDownloaded_ [private]
 

Definition at line 183 of file TransferSessionImpl.h.

qint64 Protocols::BitTorrent::Transfers::TransferSessionImpl::totalUploaded_ [private]
 

Definition at line 184 of file TransferSessionImpl.h.

QList<qint64> Protocols::BitTorrent::Transfers::TransferSessionImpl::uploadHistory_ [mutable, private]
 

Definition at line 188 of file TransferSessionImpl.h.

IntegerResetStopwatch* Protocols::BitTorrent::Transfers::TransferSessionImpl::uploadStopwatch_ [private]
 

Definition at line 186 of file TransferSessionImpl.h.

QBitArray Protocols::BitTorrent::Transfers::TransferSessionImpl::wePiecesAvailable_ [private]
 

Definition at line 191 of file TransferSessionImpl.h.


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