Logo
~Apps~
~Projects~
~Contact~


PeerHandler Class Reference

Custom sockethandler for bittorrent client. More...

#include <PeerHandler.h>

Collaboration diagram for PeerHandler:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 PeerHandler ()
 PeerHandler (StdLog *)
 ~PeerHandler ()
void RegSession (Session *)
bool SessionExists (const std::string &hash)
SessionGetSession (const std::string &hash)
const std::string & GetTorrentDirectory ()
void SetTorrentDirectory (const std::string &x)
void mkpath (const std::string &)
void Tick (time_t)
port_t GetListenPort ()
void SetListenPort (port_t x)
const std::string & GetExternIP ()
void SetExternIP (const std::string &x)
void dprintf (int id, char *format,...)
int GetChokeTimer ()
void SetChokeTimer (int x)
size_t GetMinPeers ()
void SetMinPeers (size_t x)
size_t GetMaxPeers ()
void SetMaxPeers (size_t x)
int LocalChokeTime ()
void SetLocalChokeTime (int x)
size_t PeerCount (const std::string &hash)
bool Connected (Peer *)
pSocketPeerSocket (Peer *)
void SendHave (const std::string &have, size_t piece)
bool IgnoreChoke ()
void SetIgnoreChoke (bool x=true)
int MaxRequestAge ()
void SetMaxRequestAge (int x)
void Save ()
void Show ()
void Show (StatusSocket *)
bool Quit ()
void SetQuit (bool x=true)
void SetDebug (int x)
int GetDebug ()
size_t GetDownloaders ()
void SetDownloaders (size_t x)
size_t GetOptimistic ()
void SetOptimistic (size_t x)
void CheckDownloadRate ()
std::string GetTime ()

Private Types

typedef std::map< std::string,
Session * > 
session_m

Private Member Functions

 PeerHandler (const PeerHandler &)
PeerHandleroperator= (const PeerHandler &)

Private Attributes

session_m m_sessions
std::string m_torrent_dir
port_t m_listen_port
std::string m_extern_ip
int m_choke_timer
size_t m_min_peers
size_t m_max_peers
int m_local_choke_time
bool m_ignore_choke
int m_max_request_age
bool m_quit
int m_debug
size_t m_downloaders
size_t m_optimistic

Detailed Description

Custom sockethandler for bittorrent client.

Definition at line 40 of file PeerHandler.h.


Member Typedef Documentation

typedef std::map<std::string,Session *> PeerHandler::session_m [private]

Definition at line 42 of file PeerHandler.h.


Constructor & Destructor Documentation

PeerHandler::PeerHandler (  ) 

Definition at line 47 of file PeerHandler.cpp.

00048 :SocketHandler()
00049 ,m_listen_port(0)
00050 ,m_choke_timer(300)
00051 ,m_min_peers(10)
00052 ,m_max_peers(20)
00053 ,m_local_choke_time(30)
00054 ,m_ignore_choke(false)
00055 ,m_max_request_age(30)
00056 ,m_quit(false)
00057 ,m_debug(0)
00058 ,m_downloaders(4)
00059 ,m_optimistic(1)
00060 {
00061         srand(time(NULL) * (time_t)this);
00062 }

PeerHandler::PeerHandler ( StdLog *   ) 

Definition at line 65 of file PeerHandler.cpp.

00066 :SocketHandler(p)
00067 ,m_listen_port(0)
00068 ,m_choke_timer(300)
00069 ,m_min_peers(10)
00070 ,m_max_peers(20)
00071 ,m_local_choke_time(30)
00072 ,m_ignore_choke(false)
00073 ,m_max_request_age(30)
00074 ,m_quit(false)
00075 ,m_debug(0)
00076 ,m_downloaders(4)
00077 ,m_optimistic(1)
00078 {
00079 }

PeerHandler::~PeerHandler (  ) 

Definition at line 82 of file PeerHandler.cpp.

References m_sessions.

00083 {
00084         while (m_sessions.size())
00085         {
00086                 session_m::iterator it = m_sessions.begin();
00087                 Session *p = (*it).second;
00088                 delete p;
00089                 m_sessions.erase(it);
00090         }
00091 }

PeerHandler::PeerHandler ( const PeerHandler  )  [inline, private]

Definition at line 117 of file PeerHandler.h.

00117 {} // copy constructor


Member Function Documentation

void PeerHandler::RegSession ( Session  ) 

Definition at line 94 of file PeerHandler.cpp.

References m_sessions.

Referenced by MetainfoSocket::InitSession().

00095 {
00096         m_sessions[p -> GetInfoHash()] = p;
00097 }

bool PeerHandler::SessionExists ( const std::string &  hash  ) 

Definition at line 100 of file PeerHandler.cpp.

References m_sessions.

Referenced by MetainfoSocket::InitSession().

00101 {
00102         for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++)
00103         {
00104                 if ((*it).first == hash && dynamic_cast<Session *>((*it).second))
00105                         return true;
00106         }
00107         return false;
00108 }

Session * PeerHandler::GetSession ( const std::string &  hash  ) 

Definition at line 127 of file PeerHandler.cpp.

References m_sessions.

Referenced by FileManager::FileManager(), FileManager::GetFiles(), FileManager::ReadPiece(), FileManager::Verify(), and FileManager::Write().

00128 {
00129         for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++)
00130         {
00131                 if ((*it).first == hash && dynamic_cast<Session *>((*it).second))
00132                         return (*it).second;
00133         }
00134         return NULL;
00135 }

const std::string& PeerHandler::GetTorrentDirectory (  )  [inline]

Definition at line 52 of file PeerHandler.h.

References m_torrent_dir.

Referenced by FileManager::FileManager(), FileManager::GetFiles(), and MetainfoSocket::InitSession().

00052 { return m_torrent_dir; }

void PeerHandler::SetTorrentDirectory ( const std::string &  x  )  [inline]

Definition at line 53 of file PeerHandler.h.

References m_torrent_dir.

Referenced by main().

00053 { m_torrent_dir = x; }

void PeerHandler::mkpath ( const std::string &   ) 

Definition at line 111 of file PeerHandler.cpp.

Referenced by MetainfoSocket::InitSession(), and FileManager::OpenFiles().

00112 {
00113         for (size_t i = 0; i < path.size(); i++)
00114         {
00115                 if (path[i] == '/')
00116                 {
00117 #ifdef _WIN32
00118                         _mkdir(path.substr(0, i).c_str());
00119 #else
00120                         mkdir(path.substr(0, i).c_str(), 0750);
00121 #endif
00122                 }
00123         }
00124 }

void PeerHandler::Tick ( time_t   ) 

Definition at line 138 of file PeerHandler.cpp.

References m_sessions.

Referenced by main().

00139 {
00140         for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++)
00141         {
00142                 Session *sess = (*it).second;
00143                 if (sess)
00144                 {
00145                         if (now > sess -> GetTimeTracker() + sess -> GetInterval())
00146                         {
00147                                 tSocket *p = new tSocket(*this, sess -> GetInfoHash());
00148                                 p -> SetDeleteByHandler();
00149                                 Add(p);
00150                                 sess -> SetTimeTracker();
00151                         }
00152                         sess -> Update();
00153                 }
00154         }
00155 }

port_t PeerHandler::GetListenPort (  )  [inline]

Definition at line 58 of file PeerHandler.h.

References m_listen_port.

00058 { return m_listen_port; }

void PeerHandler::SetListenPort ( port_t  x  )  [inline]

Definition at line 59 of file PeerHandler.h.

References m_listen_port.

Referenced by main().

00059 { m_listen_port = x; }

const std::string& PeerHandler::GetExternIP (  )  [inline]

Definition at line 60 of file PeerHandler.h.

References m_extern_ip.

00060 { return m_extern_ip; }

void PeerHandler::SetExternIP ( const std::string &  x  )  [inline]

Definition at line 61 of file PeerHandler.h.

References m_extern_ip.

Referenced by main().

00061 { m_extern_ip = x; }

void PeerHandler::dprintf ( int  id,
char *  format,
  ... 
)

Definition at line 158 of file PeerHandler.cpp.

References cstring::c_str().

Referenced by MyLog::error().

00159 {
00160 //      char *colors = "rgybmcwlRGYBMCWL";
00161         char *colors = "rgymcRGYBMCWL";
00162         va_list ap;
00163         va_start(ap, format);
00164         char tmp[5000];
00165 #ifdef _WIN32
00166         vsprintf(tmp, format, ap);
00167         va_end(ap);
00168         printf("%s\n", tmp);
00169 #else
00170         vsnprintf(tmp, 5000, format, ap);
00171         va_end(ap);
00172         cstring str;
00173         char slask[10];
00174         sprintf(slask, "&%c", colors[id % strlen(colors)]);
00175         str = slask;
00176         str += tmp;
00177         str += "&n";
00178         printf("%s\n", str.c_str());
00179         if (0)
00180         {
00181                 FILE *fil = fopen("btlog","at");
00182                 if (!fil)
00183                         fil = fopen("btlog","wt");
00184                 fprintf(fil,"%s\n", str.c_str());
00185                 fclose(fil);
00186         }
00187 #endif
00188 }

int PeerHandler::GetChokeTimer (  )  [inline]

Definition at line 65 of file PeerHandler.h.

References m_choke_timer.

Referenced by Session::AddConnect().

00065 { return m_choke_timer; }

void PeerHandler::SetChokeTimer ( int  x  )  [inline]

Definition at line 66 of file PeerHandler.h.

References m_choke_timer.

Referenced by main().

00066 { m_choke_timer = x; }

size_t PeerHandler::GetMinPeers (  )  [inline]

outgoing connections stop at this limit

Definition at line 68 of file PeerHandler.h.

References m_min_peers.

Referenced by Session::Update().

00068 { return m_min_peers; }

void PeerHandler::SetMinPeers ( size_t  x  )  [inline]

Definition at line 69 of file PeerHandler.h.

References m_min_peers.

Referenced by main().

00069 { m_min_peers = x; }

size_t PeerHandler::GetMaxPeers (  )  [inline]

no more incoming connections will be accepted above this limit

Definition at line 71 of file PeerHandler.h.

References m_max_peers.

00071 { return m_max_peers; }

void PeerHandler::SetMaxPeers ( size_t  x  )  [inline]

Definition at line 72 of file PeerHandler.h.

References m_max_peers.

Referenced by main().

00072 { m_max_peers = x; }

int PeerHandler::LocalChokeTime (  )  [inline]

Definition at line 74 of file PeerHandler.h.

References m_local_choke_time.

00074 { return m_local_choke_time; }

void PeerHandler::SetLocalChokeTime ( int  x  )  [inline]

Definition at line 75 of file PeerHandler.h.

References m_local_choke_time.

00075 { m_local_choke_time = x; }

size_t PeerHandler::PeerCount ( const std::string &  hash  ) 

Definition at line 191 of file PeerHandler.cpp.

Referenced by Session::Update().

00192 {
00193         size_t q = 0;
00194         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00195         {
00196                 pSocket *p = dynamic_cast<pSocket *>((*it).second);
00197                 if (p && p -> GetHash() == hash)
00198                 {
00199                         q++;
00200                 }
00201         }
00202         return q;
00203 }

bool PeerHandler::Connected ( Peer  ) 

Definition at line 206 of file PeerHandler.cpp.

00207 {
00208         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00209         {
00210                 pSocket *p = dynamic_cast<pSocket *>((*it).second);
00211                 if (p && p -> GetHash() == peer -> GetHash() && p -> GetRemoteAddress() == peer -> GetIP())
00212                 {
00213                         return true;
00214                 }
00215         }
00216         return false;
00217 }

pSocket * PeerHandler::PeerSocket ( Peer  ) 

Definition at line 220 of file PeerHandler.cpp.

00221 {
00222         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00223         {
00224                 pSocket *p = dynamic_cast<pSocket *>((*it).second);
00225                 if (p && p -> GetHash() == peer -> GetHash() && p -> GetRemoteAddress() == peer -> GetIP())
00226                 {
00227                         return p;
00228                 }
00229         }
00230         return NULL;
00231 }

void PeerHandler::SendHave ( const std::string &  have,
size_t  piece 
)

Definition at line 234 of file PeerHandler.cpp.

Referenced by Session::Update().

00235 {
00236         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00237         {
00238                 pSocket *p = dynamic_cast<pSocket *>((*it).second);
00239                 if (p && p -> GetHash() == hash && p -> CTS() )
00240                 {
00241                         p -> SendHave(piece);
00242                 }
00243         }
00244 }

bool PeerHandler::IgnoreChoke (  )  [inline]

Definition at line 82 of file PeerHandler.h.

References m_ignore_choke.

Referenced by Session::Update().

00082 { return m_ignore_choke; }

void PeerHandler::SetIgnoreChoke ( bool  x = true  )  [inline]

Definition at line 83 of file PeerHandler.h.

References m_ignore_choke.

Referenced by main().

00083 { m_ignore_choke = x; }

int PeerHandler::MaxRequestAge (  )  [inline]

Definition at line 85 of file PeerHandler.h.

References m_max_request_age.

00085 { return m_max_request_age; }

void PeerHandler::SetMaxRequestAge ( int  x  )  [inline]

Definition at line 86 of file PeerHandler.h.

References m_max_request_age.

Referenced by main().

00086 { m_max_request_age = x; }

void PeerHandler::Save (  ) 

Definition at line 247 of file PeerHandler.cpp.

References m_sessions.

Referenced by main().

00248 {
00249         for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++)
00250         {
00251                 Session *sess = (*it).second;
00252                 sess -> Save();
00253         }
00254 }

void PeerHandler::Show (  )  [inline]

Definition at line 90 of file PeerHandler.h.

References m_sessions.

Referenced by main().

00090                     {
00091                 for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++)
00092                 {
00093                         Session *sess = (*it).second;
00094                         if (sess)
00095                         {
00096                                 sess -> PeerStatus();
00097                         }
00098                 }
00099         }

void PeerHandler::Show ( StatusSocket  ) 

Definition at line 257 of file PeerHandler.cpp.

References m_sessions.

00258 {
00259         for (session_m::iterator it = m_sessions.begin(); it != m_sessions.end(); it++)
00260         {
00261                 Session *sess = (*it).second;
00262                 sess -> Status(sock);
00263         }
00264 }

bool PeerHandler::Quit (  )  [inline]

Definition at line 103 of file PeerHandler.h.

References m_quit.

Referenced by main().

00103 { return m_quit; }

void PeerHandler::SetQuit ( bool  x = true  )  [inline]

Definition at line 104 of file PeerHandler.h.

References m_quit.

00104 { m_quit = x; }

void PeerHandler::SetDebug ( int  x  )  [inline]

Definition at line 106 of file PeerHandler.h.

References m_debug.

00106 { m_debug = x; }

int PeerHandler::GetDebug (  )  [inline]

Definition at line 107 of file PeerHandler.h.

References m_debug.

Referenced by MyLog::error(), and Session::Update().

00107 { return m_debug; }

size_t PeerHandler::GetDownloaders (  )  [inline]

Definition at line 108 of file PeerHandler.h.

References m_downloaders.

Referenced by CheckDownloadRate(), and Session::Update().

00108 { return m_downloaders; }

void PeerHandler::SetDownloaders ( size_t  x  )  [inline]

Definition at line 109 of file PeerHandler.h.

References m_downloaders.

Referenced by main().

00109 { m_downloaders = x; }

size_t PeerHandler::GetOptimistic (  )  [inline]

Definition at line 110 of file PeerHandler.h.

References m_optimistic.

Referenced by CheckDownloadRate(), and Session::Update().

00110 { return m_optimistic; }

void PeerHandler::SetOptimistic ( size_t  x  )  [inline]

Definition at line 111 of file PeerHandler.h.

References m_optimistic.

Referenced by main().

00111 { m_optimistic = x; }

void PeerHandler::CheckDownloadRate (  ) 

Definition at line 267 of file PeerHandler.cpp.

References DEB, GetDownloaders(), and GetOptimistic().

Referenced by Session::Update().

00268 {
00269         // TODO: disabled
00270         return;
00271 
00272         std::map<pSocket *,size_t> mmap;
00273         size_t max = 0;
00274 DEB(printf("CheckDownloadRate()\n");)
00275         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00276         {
00277                 pSocket *p = dynamic_cast<pSocket *>((*it).second);
00278                 if (p)
00279                 {
00280                         Peer *peer = p -> GetPeer();
00281                         if (p && peer && p -> CTS() && !peer -> IsChoked())
00282                         {
00283                                 size_t sz;
00284                                 if (p -> GetDownloadRate(sz))
00285                                 {
00286                                         mmap[p] = sz;
00287                                         max = MAX(max,sz);
00288                                 }
00289                         }
00290                 }
00291         }
00292         if (mmap.size() == GetDownloaders() + GetOptimistic() )
00293         {
00294                 pSocket *least = NULL;
00295                 for (std::map<pSocket *,size_t>::iterator it = mmap.begin(); it != mmap.end(); it++)
00296                 {
00297                         pSocket *sock = (*it).first;
00298                         size_t sz = (*it).second;
00299                         if (sz < max)
00300                         {
00301                                 least = sock;
00302                                 max = sz;
00303                         }
00304                 }
00305                 if (least)
00306                 {
00307 //DEB(printf("Kicking pSocket with download rate %d bytes/sec\n", max);)
00308                         least -> SendChoke(true);
00309                 }
00310         }
00311 }

std::string PeerHandler::GetTime (  ) 

Definition at line 314 of file PeerHandler.cpp.

00315 {
00316         char slask[100];
00317 #ifdef _WIN32
00318         SYSTEMTIME time;
00319         ::GetLocalTime(&time);
00320         sprintf(slask, "%ld.%03ld", time.wHour * 3600 + 
00321                 time.wMinute + 60 + time.wSecond, time.wMilliseconds);
00322 #else
00323         struct timeval tv;
00324         struct timezone tz;
00325         gettimeofday(&tv, &tz);
00326         sprintf(slask, "%ld.%06ld", tv.tv_sec, tv.tv_usec);
00327 #endif
00328         return slask;
00329 }

PeerHandler& PeerHandler::operator= ( const PeerHandler  )  [inline, private]

Definition at line 118 of file PeerHandler.h.

00118 { return *this; } // assignment operator


Member Data Documentation

std::string PeerHandler::m_torrent_dir [private]

Definition at line 121 of file PeerHandler.h.

Referenced by GetTorrentDirectory(), and SetTorrentDirectory().

port_t PeerHandler::m_listen_port [private]

Definition at line 122 of file PeerHandler.h.

Referenced by GetListenPort(), and SetListenPort().

std::string PeerHandler::m_extern_ip [private]

Definition at line 123 of file PeerHandler.h.

Referenced by GetExternIP(), and SetExternIP().

Definition at line 124 of file PeerHandler.h.

Referenced by GetChokeTimer(), and SetChokeTimer().

size_t PeerHandler::m_min_peers [private]

Definition at line 125 of file PeerHandler.h.

Referenced by GetMinPeers(), and SetMinPeers().

size_t PeerHandler::m_max_peers [private]

Definition at line 126 of file PeerHandler.h.

Referenced by GetMaxPeers(), and SetMaxPeers().

Definition at line 127 of file PeerHandler.h.

Referenced by LocalChokeTime(), and SetLocalChokeTime().

Definition at line 128 of file PeerHandler.h.

Referenced by IgnoreChoke(), and SetIgnoreChoke().

Definition at line 129 of file PeerHandler.h.

Referenced by MaxRequestAge(), and SetMaxRequestAge().

bool PeerHandler::m_quit [private]

Definition at line 130 of file PeerHandler.h.

Referenced by Quit(), and SetQuit().

int PeerHandler::m_debug [private]

Definition at line 131 of file PeerHandler.h.

Referenced by GetDebug(), and SetDebug().

size_t PeerHandler::m_downloaders [private]

Definition at line 132 of file PeerHandler.h.

Referenced by GetDownloaders(), and SetDownloaders().

size_t PeerHandler::m_optimistic [private]

Definition at line 133 of file PeerHandler.h.

Referenced by GetOptimistic(), and SetOptimistic().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4