Logo
~Apps~
~Projects~
~Contact~


PeerHandler.h

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2005  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifndef _PEERHANDLER_H
00024 #define _PEERHANDLER_H
00025 
00026 #include <SocketHandler.h>
00027 #include <StdLog.h>
00028 #include "Session.h"
00029 #ifdef _WIN32
00030 #define MAX(a,b) (a>b)?a:b
00031 #endif
00032 
00033 class Peer;
00034 class pSocket;
00035 class StatusSocket;
00036 
00037 
00039 
00040 class PeerHandler : public SocketHandler
00041 {
00042         typedef std::map<std::string,Session *> session_m;
00043 public:
00044         PeerHandler();
00045         PeerHandler(StdLog *);
00046         ~PeerHandler();
00047 
00048         void RegSession(Session *);
00049         bool SessionExists(const std::string& hash);
00050         Session *GetSession(const std::string& hash);
00051 
00052         const std::string& GetTorrentDirectory() { return m_torrent_dir; }
00053         void SetTorrentDirectory(const std::string& x) { m_torrent_dir = x; }
00054 
00055         void mkpath(const std::string& );
00056 
00057         void Tick(time_t);
00058         port_t GetListenPort() { return m_listen_port; }
00059         void SetListenPort(port_t x) { m_listen_port = x; }
00060         const std::string& GetExternIP() { return m_extern_ip; }
00061         void SetExternIP(const std::string& x) { m_extern_ip = x; }
00062 
00063         void dprintf(int id,char *format,...);
00064 
00065         int GetChokeTimer() { return m_choke_timer; }
00066         void SetChokeTimer(int x) { m_choke_timer = x; }
00068         size_t GetMinPeers() { return m_min_peers; }
00069         void SetMinPeers(size_t x) { m_min_peers = x; }
00071         size_t GetMaxPeers() { return m_max_peers; }
00072         void SetMaxPeers(size_t x) { m_max_peers = x; }
00073 
00074         int LocalChokeTime() { return m_local_choke_time; }
00075         void SetLocalChokeTime(int x) { m_local_choke_time = x; }
00076 
00077         size_t PeerCount(const std::string& hash);
00078         bool Connected(Peer *);
00079         pSocket *PeerSocket(Peer *);
00080         void SendHave(const std::string& have,size_t piece);
00081 
00082         bool IgnoreChoke() { return m_ignore_choke; }
00083         void SetIgnoreChoke(bool x = true) { m_ignore_choke = x; }
00084 
00085         int MaxRequestAge() { return m_max_request_age; }
00086         void SetMaxRequestAge(int x) { m_max_request_age = x; }
00087 
00088         void Save();
00089 
00090         void Show() {
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         }
00100 
00101         void Show(StatusSocket *);
00102 
00103         bool Quit() { return m_quit; }
00104         void SetQuit(bool x = true) { m_quit = x; }
00105 
00106         void SetDebug(int x) { m_debug = x; }
00107         int GetDebug() { return m_debug; }
00108         size_t GetDownloaders() { return m_downloaders; }
00109         void SetDownloaders(size_t x) { m_downloaders = x; }
00110         size_t GetOptimistic() { return m_optimistic; }
00111         void SetOptimistic(size_t x) { m_optimistic = x; }
00112 
00113         void CheckDownloadRate();
00114         std::string GetTime();
00115 
00116 private:
00117         PeerHandler(const PeerHandler& ) {} // copy constructor
00118         PeerHandler& operator=(const PeerHandler& ) { return *this; } // assignment operator
00119 
00120         session_m m_sessions;
00121         std::string m_torrent_dir;
00122         port_t m_listen_port;
00123         std::string m_extern_ip;
00124         int m_choke_timer;
00125         size_t m_min_peers;
00126         size_t m_max_peers;
00127         int m_local_choke_time;
00128         bool m_ignore_choke;
00129         int m_max_request_age;
00130         bool m_quit;
00131         int m_debug;
00132         size_t m_downloaders;
00133         size_t m_optimistic;
00134 };
00135 
00136 
00137 
00138 
00139 #endif // _PEERHANDLER_H
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