Peer.hGo 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 _PEER_H 00024 #define _PEER_H 00025 00026 #include <string> 00027 #include <socket_include.h> 00028 #include <list> 00029 #include "bitmap_t.h" 00030 #include "Request.h" 00031 00032 00033 class pSocket; 00034 00035 typedef std::list<Request *> request_v; 00036 00037 00039 00040 class Peer 00041 { 00042 public: 00043 Peer(ISocketHandler&,const std::string& hash,const std::string& ip,const std::string& id,port_t port); 00044 ~Peer(); 00045 00046 const std::string& GetIP() { return m_ip; } 00047 const std::string& GetID() { return m_id; } 00048 port_t GetPort() { return m_port; } 00049 void SetPort(port_t x) { m_port = x; } 00050 00051 ISocketHandler& Handler() const { return m_handler; } 00052 unsigned char *GetBitmap() { return m_bitmap ? m_bitmap -> GetBitmap() : NULL; } 00053 size_t GetBitmapSize() { return m_bitmap_size; } 00054 00056 bool IsChoked() { return m_choke; } 00057 void SetChoked(bool x = true) { m_choke = x; if (x) m_t_choke = time(NULL); } 00059 bool IsInterested() { return m_interested; } 00060 void SetInterested(bool x = true) { m_interested = x; } 00061 time_t GetChokeTime() { return m_t_choke; } 00062 00063 const std::string& GetHash() { return m_hash; } 00064 00065 bool Connected(); 00066 pSocket *PeerSocket(); 00067 00068 bool IsSet(size_t nr) { return m_bitmap ? m_bitmap -> IsSet(nr) : false; } 00069 void set(size_t nr); 00070 00071 request_v& Requests() { return m_requests; } 00072 size_t GotSlice(size_t piece,size_t offset); 00073 00074 // reset age of all request on incoming piece 00075 void RefreshRequests(); 00076 00077 bool Tried() { return m_tried; } 00078 void SetTried(bool x = true) { m_tried = x; } 00079 bool Failed() { return m_failed; } 00080 void SetFailed(bool x = true) { m_failed = x; } 00081 00083 size_t GetSet() { return m_bitmap ? m_bitmap -> GetSet() : 0; } 00084 00091 size_t Score(bitmap_t *); 00092 00093 // new 00094 void RequestAvailable(); 00095 void RemoveRequests(); 00096 00097 private: 00098 Peer(const Peer& s) : m_handler(s.Handler()) {} // copy constructor 00099 Peer& operator=(const Peer& ) { return *this; } // assignment operator 00100 00101 ISocketHandler& m_handler; 00102 std::string m_hash; 00103 std::string m_ip; 00104 std::string m_id; 00105 port_t m_port; 00106 bitmap_t *m_bitmap; 00107 size_t m_bitmap_size; 00108 bool m_choke; 00109 bool m_interested; 00110 time_t m_t_choke; 00111 request_v m_requests; 00112 bool m_tried; 00113 bool m_failed; 00114 }; 00115 00116 00117 00118 00119 #endif // _PEER_H |