ServerList Class Reference

ServerList class encapsulates the list of known servers. More...

#include <serverlist.h>

List of all members.


Public Types

enum  Status { ST_CONNECTED = 0x01, ST_CONNECTING = 0x02 }
 Statuses. More...

Public Member Functions

void load (const std::string &file)
 Load the list from file, adding all found items to the list.
void save (const std::string &file) const
 Save the list to the specified file.
void init ()
 Initialize ServerList networking.
void exit ()
 Exit ServerList.
void connect ()
 Connect to a random server.
void connect (Detail::Server *s)
 Connect to a specific server, possibly disconnecting from current server, if connected.
IPV4Address getCurServerAddr () const
uint8_t getStatus () const
 Get current connection status.
void reqCallback (uint32_t id)
 Request callback from a client via server.
void addServer (IPV4Address addr)
 Add a server by address.
Packet handlers
void onPacket (const ED2KPacket::ServerMessage &p)
void onPacket (const ED2KPacket::IdChange &p)
void onPacket (const ED2KPacket::ServerStatus &p)
void onPacket (const ED2KPacket::ServerIdent &p)
void onPacket (const ED2KPacket::ServerList &p)
void onPacket (const ED2KPacket::SearchResult &p)
void onPacket (const ED2KPacket::CallbackReq &p)
void onPacket (const ED2KPacket::FoundSources &p)

Static Public Member Functions

static ServerListinstance ()
 Return the only instance of this class.

Private Types

enum  ServerListEvent {
  EVT_PINGSERVER, EVT_REQSOURCES, EVT_LOGINTIMEOUT, EVT_QUERYSERVER,
  EVT_CONNECT
}
 Events emitted from ServerList class (used internally only). More...
enum  ServerEvent { EVT_PINGTIMEOUT }
 Events emitted from Server class (used internally only). More...

Private Member Functions

 DECLARE_EVENT_TABLE (ServerList *, ServerListEvent)
 ServerList ()
 Singleton - private.
 ~ServerList ()
 Singleton - private.
void onServerListEvent (ServerList *sl, ServerListEvent evt)
 Event handler for our own events, used for timed events.
void onServerSocketEvent (ED2KClientSocket *c, SocketEvent evt)
 Event handler for currentserver events.
void onServerEvent (Detail::Server *serc, int event)
 Event handler for internal Server events.
void sendLoginRequest ()
 Sends login request.
void publishFiles (bool useZlib)
 Sends our current shared files list to server (all of them).
void publishFile (SharedFile *sf)
 Publish a single file to server.
void performSearch (SearchPtr search)
 Perform a search on eDonkey2000 network, based on criteria described in argument.
void addDefaultServers ()
 Adds a set of hardcoded servers to the server list.
void onSharedFileEvent (SharedFile *sf, int evt)
 Event handler for events emitted from SharedFile.
void reqSources ()
 Request sources for all temporary files which have the neccesery information (e.g.
void reqSources (Download &d)
 Requests sources for download from current server (if connected).
void queryNextServer ()
 Chooses next server to perform UDP queries with.
void pingServer (Detail::QTIter &it)
 Ping the server pointed to by iterator.
void udpGetSources (Detail::QTIter &it)
 Get sources via UDP from server pointed to by iterator.
void onUdpData (ED2KUDPSocket *sock, SocketEvent evt)
 Event handler for server udp listener.
void handleGlobSources (std::istringstream &i, IPV4Address from)
 Handles GlobFoundSources data from UDP servers.
void handleGlobStatRes (std::istringstream &i, IPV4Address from)
 Handles GlobStatRes data from UDP servers.
virtual uint8_t getOperCount () const
virtual Object::Operation getOper (uint8_t n) const
virtual void doOper (const Object::Operation &op)

Private Attributes

ED2KClientSocketm_serverSocket
 Socket to current server.
Detail::Serverm_currentServer
 Currently connected server.
SearchPtr m_curSearch
 Current search in progress.
ED2KUDPSocketm_udpSocket
 Server udp listener.
uint64_t m_lastSourceRequest
 Time of last src req.
boost::shared_ptr< ED2KParser<
ServerList > > 
m_parser
 Server stream parser.
uint8_t m_status
 Keeps track of currect connection status.
boost::scoped_ptr< Detail::MIServerListm_list
 List of servers, sorted by IP and Name.

Detailed Description

ServerList class encapsulates the list of known servers.

The list is loaded from config/ed2k/server.met on startup, and saved to same file on shutdown. The file format conforms to eDonkey2000 protocol specification. This class also has the added responsibility of handling server connections, as well as communication with currently connected server.

ServerList class is a singleton with lazy initialization. The only instance of this class may be retrieved via static instance() member function.

Definition at line 47 of file serverlist.h.


Member Enumeration Documentation

enum ServerList::Status
 

Statuses.

Enumeration values:
ST_CONNECTED  Connected.
ST_CONNECTING  Not connected.

Definition at line 121 of file serverlist.h.

enum ServerList::ServerListEvent [private]
 

Events emitted from ServerList class (used internally only).

Enumeration values:
EVT_PINGSERVER  Ping server with empty OfferFiles packet.
EVT_REQSOURCES  Request sources from server.
EVT_LOGINTIMEOUT  Login attempt timeouts.
EVT_QUERYSERVER  UDP GetSources and Ping request time.
EVT_CONNECT  Attempt to connect to next server.

Definition at line 146 of file serverlist.h.

enum ServerList::ServerEvent [private]
 

Events emitted from Server class (used internally only).

Enumeration values:
EVT_PINGTIMEOUT  Indicates UDP query timed out.

Definition at line 155 of file serverlist.h.


Constructor & Destructor Documentation

ServerList::ServerList  )  [private]
 

Singleton - private.

Definition at line 113 of file serverlist.cpp.

References onServerEvent(), onServerListEvent(), onSharedFileEvent(), and performSearch().

Referenced by instance().

ServerList::~ServerList  )  [private]
 

Singleton - private.

Definition at line 166 of file serverlist.cpp.


Member Function Documentation

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

Return the only instance of this class.

Definition at line 50 of file serverlist.h.

References ServerList().

Referenced by Client::establishConnection(), ED2K::onExit(), and ED2K::onInit().

void ServerList::load const std::string &  file  ) 
 

Load the list from file, adding all found items to the list.

Definition at line 168 of file serverlist.cpp.

References addDefaultServers(), DownloadList::find(), Detail::Server::getAddr(), m_list, ST_METHEADER, ST_METVERSION, and TRACE.

Referenced by ED2K::onInit().

void ServerList::save const std::string &  file  )  const
 

Save the list to the specified file.

Definition at line 225 of file serverlist.cpp.

References m_list, and ST_METVERSION.

Referenced by ED2K::onExit().

void ServerList::init  ) 
 

Initialize ServerList networking.

Definition at line 132 of file serverlist.cpp.

References addServer(), connect(), DownloadList::instance(), m_udpSocket, DownloadList::onAdded, onUdpData(), queryNextServer(), and reqSources().

Referenced by ED2K::onInit().

void ServerList::exit  ) 
 

Exit ServerList.

Definition at line 156 of file serverlist.cpp.

References m_list, m_serverSocket, and m_udpSocket.

Referenced by ED2K::onExit().

void ServerList::connect  ) 
 

Connect to a random server.

If the connections fail, the connecting attempt to continue until the connection has been established, or the list is exhausted.

Definition at line 265 of file serverlist.cpp.

References addDefaultServers(), m_currentServer, m_list, m_serverSocket, m_status, ST_CONNECTING, and TRACE.

Referenced by doOper(), Client::establishConnection(), init(), onPacket(), onServerListEvent(), and onServerSocketEvent().

void ServerList::connect Detail::Server s  ) 
 

Connect to a specific server, possibly disconnecting from current server, if connected.

Parameters:
s Server to connect to

Definition at line 302 of file serverlist.cpp.

References Detail::Server::getAddr(), m_currentServer, m_serverSocket, m_status, onServerSocketEvent(), and ST_CONNECTING.

IPV4Address ServerList::getCurServerAddr  )  const
 

Returns:
Address of currently connected server
Exceptions:
std::runtime_error if not connected

Definition at line 1001 of file serverlist.cpp.

References Detail::Server::getAddr(), and m_currentServer.

Referenced by Client::establishConnection().

uint8_t ServerList::getStatus  )  const [inline]
 

Get current connection status.

Returns:
Bitfield containing one or more of Status values.

Definition at line 98 of file serverlist.h.

References m_status.

void ServerList::reqCallback uint32_t  id  ) 
 

Request callback from a client via server.

Parameters:
id ID if the client we wish to contact us

Definition at line 584 of file serverlist.cpp.

References m_serverSocket.

Referenced by Client::establishConnection().

void ServerList::addServer IPV4Address  addr  ) 
 

Add a server by address.

Parameters:
addr Address of the server

Definition at line 1008 of file serverlist.cpp.

References m_list.

Referenced by init().

ServerList::DECLARE_EVENT_TABLE ServerList ,
ServerListEvent 
[private]
 

void ServerList::onServerListEvent ServerList sl,
ServerListEvent  evt
[private]
 

Event handler for our own events, used for timed events.

Definition at line 678 of file serverlist.cpp.

References Detail::Server::addFailedCount(), connect(), EVT_CONNECT, EVT_LOGINTIMEOUT, EVT_PINGSERVER, EVT_QUERYSERVER, EVT_REQSOURCES, m_currentServer, m_serverSocket, m_status, queryNextServer(), reqSources(), ST_CONNECTED, and TRACE.

Referenced by ServerList().

void ServerList::onServerSocketEvent ED2KClientSocket c,
SocketEvent  evt
[private]
 

Event handler for currentserver events.

Definition at line 316 of file serverlist.cpp.

References Detail::Server::addFailedCount(), connect(), EVT_CONNECT, m_currentServer, m_parser, m_serverSocket, sendLoginRequest(), and TRACE.

Referenced by connect().

void ServerList::onServerEvent Detail::Server serc,
int  event
[private]
 

Event handler for internal Server events.

Definition at line 969 of file serverlist.cpp.

References TRACE.

Referenced by ServerList().

void ServerList::sendLoginRequest  )  [private]
 

Sends login request.

Definition at line 369 of file serverlist.cpp.

References EVT_LOGINTIMEOUT, m_currentServer, m_serverSocket, SERVERTIMEOUT, and Detail::Server::setFailedCount().

Referenced by onServerSocketEvent().

void ServerList::publishFiles bool  useZlib  )  [private]
 

Sends our current shared files list to server (all of them).

Parameters:
useZlib Whether to use Zlib compression (and the related 'special' id's during sending.

Definition at line 383 of file serverlist.cpp.

References m_serverSocket, makeED2KFile(), PR_ED2K, PR_ZLIB, and ED2KPacket::OfferFiles::push().

Referenced by onPacket().

void ServerList::publishFile SharedFile *  sf  )  [private]
 

Publish a single file to server.

Parameters:
sf File to publish

Definition at line 624 of file serverlist.cpp.

References FL_ZLIB, Detail::Server::getTcpFlags(), m_currentServer, m_serverSocket, makeED2KFile(), PR_ED2K, PR_ZLIB, and TRACE.

Referenced by onSharedFileEvent().

void ServerList::performSearch SearchPtr  search  )  [private]
 

Perform a search on eDonkey2000 network, based on criteria described in argument.

Parameters:
search Pointer to search object describing the search criteria.

Definition at line 428 of file serverlist.cpp.

References m_curSearch, m_serverSocket, m_status, ST_CONNECTED, and TRACE.

Referenced by ServerList().

void ServerList::addDefaultServers  )  [private]
 

Adds a set of hardcoded servers to the server list.

Definition at line 251 of file serverlist.cpp.

References m_list.

Referenced by connect(), and load().

void ServerList::onSharedFileEvent SharedFile *  sf,
int  evt
[private]
 

Event handler for events emitted from SharedFile.

This function is called from event table and should never be called directly.

Definition at line 595 of file serverlist.cpp.

References m_serverSocket, and publishFile().

Referenced by ServerList().

void ServerList::reqSources  )  [private]
 

Request sources for all temporary files which have the neccesery information (e.g.

ED2KHash).

Definition at line 660 of file serverlist.cpp.

References DownloadList::begin(), DownloadList::end(), EVT_REQSOURCES, DownloadList::instance(), m_lastSourceRequest, m_serverSocket, SOURCEREASKTIME, and TRACE.

Referenced by init(), onPacket(), and onServerListEvent().

void ServerList::reqSources Download d  )  [private]
 

Requests sources for download from current server (if connected).

Does nothing if we are currently not connected to a server.

Parameters:
d Download to request sources for

Definition at line 646 of file serverlist.cpp.

References m_serverSocket, and TRACE.

void ServerList::queryNextServer  )  [private]
 

Chooses next server to perform UDP queries with.

Definition at line 847 of file serverlist.cpp.

References SOURCEREASKTIME, and TRACE.

Referenced by init(), and onServerListEvent().

void ServerList::pingServer Detail::QTIter it  )  [private]
 

Ping the server pointed to by iterator.

Definition at line 898 of file serverlist.cpp.

References EVT_PINGTIMEOUT, ED2KPacket::GlobStatReq::getChallenge(), m_list, m_udpSocket, and TRACE.

void ServerList::udpGetSources Detail::QTIter it  )  [private]
 

Get sources via UDP from server pointed to by iterator.

Definition at line 920 of file serverlist.cpp.

References ED2KPacket::GlobGetSources::addHash(), Download::getHash(), DownloadList::getNextForUdpQuery(), Download::getSize(), DownloadList::instance(), and TRACE.

void ServerList::onUdpData ED2KUDPSocket sock,
SocketEvent  evt
[private]
 

Event handler for server udp listener.

Definition at line 707 of file serverlist.cpp.

References OP_GLOBFOUNDSOURCES, OP_GLOBSTATRES, and PR_ED2K.

Referenced by init().

void ServerList::handleGlobSources std::istringstream &  i,
IPV4Address  from
[private]
 

Handles GlobFoundSources data from UDP servers.

Parameters:
i Input stream to read packet(s) from
from The server that sent this data

Definition at line 740 of file serverlist.cpp.

References ED2KPacket::GlobFoundSources::begin(), ED2KPacket::GlobFoundSources::end(), Detail::foundSource, ED2KPacket::GlobFoundSources::getHash(), OP_GLOBFOUNDSOURCES, PR_ED2K, ED2KPacket::GlobFoundSources::size(), and TRACE.

void ServerList::handleGlobStatRes std::istringstream &  i,
IPV4Address  from
[private]
 

Handles GlobStatRes data from UDP servers.

Parameters:
i Input stream to read packet from
from The server that sent this data

Definition at line 787 of file serverlist.cpp.

References ED2KPacket::GlobStatRes::getChallenge(), ED2KPacket::GlobStatRes::getFiles(), ED2KPacket::GlobStatRes::getHardLimit(), ED2KPacket::GlobStatRes::getLowIdUsers(), ED2KPacket::GlobStatRes::getMaxUsers(), ED2KPacket::GlobStatRes::getSoftLimit(), ED2KPacket::GlobStatRes::getUdpFlags(), ED2KPacket::GlobStatRes::getUsers(), and TRACE.

void ServerList::onPacket const ED2KPacket::ServerMessage p  ) 
 

Definition at line 443 of file serverlist.cpp.

References connect().

void ServerList::onPacket const ED2KPacket::IdChange p  ) 
 

Definition at line 459 of file serverlist.cpp.

References EVT_PINGSERVER, FL_ZLIB, Detail::Server::getAddr(), Detail::Server::getName(), Detail::Server::getTcpFlags(), m_currentServer, m_lastSourceRequest, m_serverSocket, m_status, publishFiles(), reqSources(), SERVERPINGTIME, Detail::Server::setTcpFlags(), and ST_CONNECTED.

void ServerList::onPacket const ED2KPacket::ServerStatus p  ) 
 

Definition at line 490 of file serverlist.cpp.

References m_currentServer, Detail::Server::setFiles(), and Detail::Server::setUsers().

void ServerList::onPacket const ED2KPacket::ServerIdent p  ) 
 

Definition at line 502 of file serverlist.cpp.

References m_currentServer, and m_list.

void ServerList::onPacket const ED2KPacket::ServerList p  ) 
 

Definition at line 517 of file serverlist.cpp.

References m_list.

void ServerList::onPacket const ED2KPacket::SearchResult p  ) 
 

Definition at line 535 of file serverlist.cpp.

References m_curSearch.

void ServerList::onPacket const ED2KPacket::CallbackReq p  ) 
 

Definition at line 551 of file serverlist.cpp.

References ClientList::addClient(), ClientList::instance(), and TRACE.

void ServerList::onPacket const ED2KPacket::FoundSources p  ) 
 

Definition at line 558 of file serverlist.cpp.

References Detail::foundSource, Detail::Server::getAddr(), and m_currentServer.

uint8_t ServerList::getOperCount  )  const [private, virtual]
 

Definition at line 1016 of file serverlist.cpp.

Object::Operation ServerList::getOper uint8_t  n  )  const [private, virtual]
 

Definition at line 1018 of file serverlist.cpp.

void ServerList::doOper const Object::Operation &  op  )  [private, virtual]
 

Definition at line 1027 of file serverlist.cpp.

References connect(), and m_list.


Member Data Documentation

ED2KClientSocket* ServerList::m_serverSocket [private]
 

Socket to current server.

Definition at line 131 of file serverlist.h.

Referenced by connect(), exit(), onPacket(), onServerListEvent(), onServerSocketEvent(), onSharedFileEvent(), performSearch(), publishFile(), publishFiles(), reqCallback(), reqSources(), and sendLoginRequest().

Detail::Server* ServerList::m_currentServer [private]
 

Currently connected server.

Definition at line 132 of file serverlist.h.

Referenced by connect(), getCurServerAddr(), onPacket(), onServerListEvent(), onServerSocketEvent(), publishFile(), and sendLoginRequest().

SearchPtr ServerList::m_curSearch [private]
 

Current search in progress.

Definition at line 133 of file serverlist.h.

Referenced by onPacket(), and performSearch().

ED2KUDPSocket* ServerList::m_udpSocket [private]
 

Server udp listener.

Definition at line 134 of file serverlist.h.

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

uint64_t ServerList::m_lastSourceRequest [private]
 

Time of last src req.

Definition at line 135 of file serverlist.h.

Referenced by onPacket(), and reqSources().

boost::shared_ptr<ED2KParser<ServerList> > ServerList::m_parser [private]
 

Server stream parser.

Definition at line 138 of file serverlist.h.

Referenced by onServerSocketEvent().

uint8_t ServerList::m_status [private]
 

Keeps track of currect connection status.

Definition at line 140 of file serverlist.h.

Referenced by connect(), getStatus(), onPacket(), onServerListEvent(), and performSearch().

boost::scoped_ptr<Detail::MIServerList> ServerList::m_list [private]
 

List of servers, sorted by IP and Name.

Definition at line 143 of file serverlist.h.

Referenced by addDefaultServers(), addServer(), connect(), doOper(), exit(), load(), onPacket(), pingServer(), and save().


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