ClientList Class Reference

ClientList manages all clients currently in use. More...

#include <clientlist.h>

List of all members.


Public Member Functions

void addClient (IPV4Address addr)
 Add a client which is to be connected.
bool addSource (const Hash< ED2KHash > &file, IPV4Address caddr, IPV4Address saddr, bool doConn=true)
 Add a client that shall act as "source" for a file.
void init ()
 Initializes clientlist, setting up data structures and network connections as needed.
void exit ()
 Called on module exit; perform cleanup.

Static Public Member Functions

static ClientListinstance ()

Private Types

enum  ClientListEvt { EVT_REGEN_QUEUE }

Private Member Functions

 DECLARE_EVENT_TABLE (ClientList *, ClientListEvt)
 ClientList ()
 ~ClientList ()
 ClientList (const ClientList &)
ClientListoperator= (const ClientList &)
void onClientEvent (Client *c, ClientEvent evt)
 Event handler for client events.
void onServerEvent (ED2KServerSocket *s, SocketEvent evt)
 Event handler for socket server events.
void removeClient (Client *c)
 Erase a client from all known containers and delete the object.
void onIdChange (Client *c, uint32_t newId)
 Handles Client::changeId() signal; changes the client's Id, and updates our internal data structures to reflect the change.
void onClientListEvent (ClientList *, ClientListEvt evt)
 Event handler for our own events.
void updateQueue ()
 Re-generates m_queue vector (from m_queued set), ordered by the client's scores.
void startNextUpload ()
 Selects the next valid client from the queue (highest-ranking), and starts new upload on the client.
void checkOpenMoreSlots ()
 Checks if we are uploading ineffectivly (e.g.
ClientfindClient (IPV4Address addr)
 Locates a specific client.
ClientfindClientByUdp (IPV4Address addr)
 Locate a specific client, by ID and UDP port.
void onUdpData (ED2KUDPSocket *src, SocketEvent evt)
 Event handler for ClientUDP socket events.

Private Attributes

boost::scoped_ptr< Detail::CListm_clients
 List of all clients we have alive.
std::list< Client * > m_queue
 This is the queued clients list.
std::set< Client * > m_queued
 This set contains all clients which have m_uploadClient member pointer and are not currently uploading.
std::set< Client * > m_uploading
 This set contains all clients that are currently in uploading state, e.g.
ED2KServerSocketm_listener
 Main ED2K listener socket.
boost::scoped_array< char > m_udpBuffer
 Input buffer for UDP data; the size is defined by UDP_BUFSIZE.

Detailed Description

ClientList manages all clients currently in use.

A client is considered "in use" when it has DownloadClient and/or UploadClient members alive. This class owns the Client objects and is responsible for deleting the clients, when the client submits EVT_DESTROY event.

Clients can be found in this list by searching with the Client IP. Multiple clients from same IP are allowed, in which case the differenciating can be made based on the client's ports.

Definition at line 42 of file clientlist.h.


Member Enumeration Documentation

enum ClientList::ClientListEvt [private]
 

Enumeration values:
EVT_REGEN_QUEUE  Indicates ClientList to regen queue.

Definition at line 43 of file clientlist.h.


Constructor & Destructor Documentation

ClientList::ClientList  )  [private]
 

Definition at line 108 of file clientlist.cpp.

References EVT_REGEN_QUEUE, onClientEvent(), onClientListEvent(), QUEUE_UPDATE_TIME, and UDP_BUFSIZE.

ClientList::~ClientList  )  [private]
 

Definition at line 123 of file clientlist.cpp.

ClientList::ClientList const ClientList  )  [private]
 


Member Function Documentation

ClientList::DECLARE_EVENT_TABLE ClientList ,
ClientListEvt 
[private]
 

static ClientList& ClientList::instance  )  [inline, static]
 

Definition at line 48 of file clientlist.h.

Referenced by ED2K::onExit(), ED2K::onInit(), and ServerList::onPacket().

void ClientList::addClient IPV4Address  addr  ) 
 

Add a client which is to be connected.

This method is generally used by ServerList to notify us about callback requests.

Parameters:
addr Address the client is located at

Definition at line 629 of file clientlist.cpp.

References CONNECT_TIMEOUT, Client::establishConnection(), findClient(), and m_clients.

Referenced by ServerList::onPacket().

bool ClientList::addSource const Hash< ED2KHash > &  file,
IPV4Address  caddr,
IPV4Address  saddr,
bool  doConn = true
 

Add a client that shall act as "source" for a file.

Parameters:
file File offered by the client
caddr Address of the client
saddr Address of the server the client is on
doConn Whether to establish connection with the client
Returns:
True if source was added, false otherwise
Note:
This method returns false also when the hash was added to an already existing source.

Definition at line 564 of file clientlist.cpp.

References Client::addOffered(), Client::establishConnection(), EVT_REASKFILEPING, DownloadList::find(), DownloadList::instance(), and Client::setServerAddr().

Referenced by init().

void ClientList::init  ) 
 

Initializes clientlist, setting up data structures and network connections as needed.

This function should be called when the module is initially loaded, but can also be called at any later time on runtime to update/reset/restart the ClientList's internals. On reason why you might want to call this is when you modify the "TCP Port" value in ED2KConfig, in which case the main listener must be restarted (done in this function).

Definition at line 141 of file clientlist.cpp.

References addSource(), Client::getUdpSocket(), m_listener, onIdChange(), onServerEvent(), and onUdpData().

Referenced by ED2K::onInit().

void ClientList::exit  ) 
 

Called on module exit; perform cleanup.

Definition at line 125 of file clientlist.cpp.

References Client::getUdpSocket(), m_clients, m_listener, Detail::TRACE_CLIENT, TRACE_CLIST, and TRACE_SECIDENT.

Referenced by ED2K::onExit().

ClientList& ClientList::operator= const ClientList  )  [private]
 

void ClientList::onClientEvent Client c,
ClientEvent  evt
[private]
 

Event handler for client events.

Definition at line 292 of file clientlist.cpp.

References EVT_CALLBACK_T, EVT_CANCEL_UPLOADREQ, EVT_DESTROY, EVT_REASKFILEPING, EVT_REASKTIMEOUT, EVT_UPLOADREQ, m_queue, m_queued, m_uploading, removeClient(), startNextUpload(), Detail::TRACE_CLIENT, and UDP_REASK_INTERVAL.

Referenced by ClientList().

void ClientList::onServerEvent ED2KServerSocket s,
SocketEvent  evt
[private]
 

Event handler for socket server events.

Definition at line 210 of file clientlist.cpp.

References m_clients, m_listener, and TRACE_CLIST.

Referenced by init().

void ClientList::removeClient Client c  )  [private]
 

Erase a client from all known containers and delete the object.

Parameters:
c Client to be destroyed.

Definition at line 279 of file clientlist.cpp.

References m_clients, m_queued, m_uploading, startNextUpload(), and TRACE_CLIST.

Referenced by onClientEvent().

void ClientList::onIdChange Client c,
uint32_t  newId
[private]
 

Handles Client::changeId() signal; changes the client's Id, and updates our internal data structures to reflect the change.

Parameters:
c Client that changes it's ID
newId New ID of the client

Definition at line 229 of file clientlist.cpp.

References Detail::ID_Client, Detail::ID_Id, m_clients, m_uploading, and TRACE_CLIST.

Referenced by init().

void ClientList::onClientListEvent ClientList ,
ClientListEvt  evt
[private]
 

Event handler for our own events.

This is actually used for queue updating timed callback events (however, the events emitted from this class may also be intercepted by external watchers if interested). This function is called from event loop and should never be called directly.

Parameters:
evt Event that happened

Definition at line 421 of file clientlist.cpp.

References EVT_REGEN_QUEUE, and updateQueue().

Referenced by ClientList().

void ClientList::updateQueue  )  [private]
 

Re-generates m_queue vector (from m_queued set), ordered by the client's scores.

Sets each client's m_uploadClient member's queue ranking based on their position in the resulting m_queue vector.

This function is called once per every X seconds, where X is the queue update interval. The purpose of this is to be able to send queue rankings to remote clients (thus each client's member m_qr value will indicate it's current position in the queue), as well as for picking up highest-ranking clients from the queue when we want to start sending data to them.

Definition at line 448 of file clientlist.cpp.

References checkOpenMoreSlots(), EVT_REGEN_QUEUE, m_queue, m_queued, Client::m_queueInfo, QUEUE_DROPTIME, QUEUE_UPDATE_TIME, Client::removeFromQueue(), scoreComp(), and TRACE_CLIST.

Referenced by onClientListEvent().

void ClientList::startNextUpload  )  [private]
 

Selects the next valid client from the queue (highest-ranking), and starts new upload on the client.

Definition at line 395 of file clientlist.cpp.

References m_queue, m_queued, m_uploading, Client::startUpload(), and TRACE_CLIST.

Referenced by checkOpenMoreSlots(), onClientEvent(), and removeClient().

void ClientList::checkOpenMoreSlots  )  [private]
 

Checks if we are uploading ineffectivly (e.g.

current global upload rate is significently below what is the limit), and then attempts to open up another upload slot.

Definition at line 495 of file clientlist.cpp.

References Detail::SourceInfo::count(), ED2KPacket::getOverheadDn(), ED2KPacket::getOverheadUp(), m_queue, and startNextUpload().

Referenced by updateQueue().

Client * ClientList::findClient IPV4Address  addr  )  [private]
 

Locates a specific client.

by ID and TCP port

Parameters:
addr ClientID and TCP port of the searched client
Returns:
Pointer to the client, or 0 if not found

Definition at line 603 of file clientlist.cpp.

References Detail::ID_Id.

Referenced by addClient().

Client * ClientList::findClientByUdp IPV4Address  addr  )  [private]
 

Locate a specific client, by ID and UDP port.

Parameters:
addr ClientID and UDP port of the searched client
Returns:
Pointer to the client, or 0 if not found

Definition at line 616 of file clientlist.cpp.

References Detail::ID_Id, and m_clients.

void ClientList::onUdpData ED2KUDPSocket src,
SocketEvent  evt
[private]
 

Event handler for ClientUDP socket events.

Definition at line 652 of file clientlist.cpp.

References Client::onPacket(), OP_FILENOTFOUND, OP_QUEUEFULL, OP_REASKACK, OP_REASKFILEPING, PR_EMULE, and UDP_BUFSIZE.

Referenced by init().


Member Data Documentation

boost::scoped_ptr<Detail::CList> ClientList::m_clients [private]
 

List of all clients we have alive.

Definition at line 184 of file clientlist.h.

Referenced by addClient(), exit(), findClientByUdp(), onIdChange(), onServerEvent(), and removeClient().

std::list<Client*> ClientList::m_queue [private]
 

This is the queued clients list.

The contents of this list are ordered by the clients score, generated every X seconds. Note that every X seconds, this container is cleared and re-filled from m_queuedClients set. Also note that there may be dangling pointers existing in this list, in worst-case scenario for up to X seconds (e.g. until after next full regen). As such, if you access this container, you need to make sure the pointer you just got is still valid by looking it up from m_queuedClients set. If it is not found there, the pointer is invalid and should be discarded.

Definition at line 197 of file clientlist.h.

Referenced by checkOpenMoreSlots(), onClientEvent(), startNextUpload(), and updateQueue().

std::set<Client*> ClientList::m_queued [private]
 

This set contains all clients which have m_uploadClient member pointer and are not currently uploading.

This set is used as base for building up the actual upload queue into m_queue list every X seconds. The purpose of this set is to have a reference container in which we can actually erase elements using fast integer-based lookups (which we couldn't do in m_queue list since that one is ordered by score).

Note that we could achive the same purpose by using m_clients container, however, m_clients also contains downloadclients, so it would cause many more loops in there to find the queued clients than it is to find them here. Also, this set is a more static entity than m_queue member (which is completely regenerated often).

Definition at line 214 of file clientlist.h.

Referenced by onClientEvent(), removeClient(), startNextUpload(), and updateQueue().

std::set<Client*> ClientList::m_uploading [private]
 

This set contains all clients that are currently in uploading state, e.g.

sending data. The number of elements allowed in this list depends on various factors, however it should never go below 1.

Definition at line 221 of file clientlist.h.

Referenced by onClientEvent(), onIdChange(), removeClient(), and startNextUpload().

ED2KServerSocket* ClientList::m_listener [private]
 

Main ED2K listener socket.

This is where all incoming TCP clients connect to. By default it is set to listen on port 4662, however it may be changed in configuration, under key "TCP Port".

Note that if you modify the listener address, you need to also call ClientList::init() to restart the listener.

Definition at line 231 of file clientlist.h.

Referenced by exit(), init(), and onServerEvent().

boost::scoped_array<char> ClientList::m_udpBuffer [private]
 

Input buffer for UDP data; the size is defined by UDP_BUFSIZE.

Definition at line 236 of file clientlist.h.


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