libktorrent  2.2.0
peerinterface.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef BTPEERINTERFACE_H
21 #define BTPEERINTERFACE_H
22 
23 #include <ktorrent_export.h>
24 #include <util/constants.h>
25 #include <util/bitset.h>
26 #include <peer/peerid.h>
27 
28 
29 namespace bt
30 {
31 
39  class KTORRENT_EXPORT PeerInterface
40  {
41  public:
47  PeerInterface(const PeerID & peer_id, Uint32 num_chunks);
48  virtual ~PeerInterface();
49 
50  struct Stats
51  {
53  QString ip_address;
55  QString hostname;
57  QString client;
59  bt::Uint32 download_rate;
61  bt::Uint32 upload_rate;
63  bool choked;
65  bool snubbed;
67  float perc_of_file;
71  bt::Uint64 bytes_uploaded;
73  bt::Uint64 bytes_downloaded;
75  double aca_score;
79  bool interested;
83  bool encrypted;
85  bt::Uint32 num_up_requests;
87  bt::Uint32 num_down_requests;
91  bool local;
95  bt::Uint32 max_request_queue;
97  TimeStamp time_choked;
99  TimeStamp time_unchoked;
101  bt::TransportProtocol transport_protocol;
104 
106  QString address() const {return hostname.isEmpty() ? ip_address : hostname;}
107  };
108 
110  const Stats & getStats() const {return stats;}
111 
115  virtual void kill() = 0;
116 
118  bool isKilled() const {return killed;}
119 
123  virtual bt::Uint32 averageDownloadSpeed() const = 0;
124 
126  const BitSet & getBitSet() const {return pieces;}
127 
129  const PeerID & getPeerID() const {return peer_id;}
130 
132  bool isChoked() const {return stats.choked;}
133 
135  bool isInterested() const {return stats.interested;}
136 
138  bool areWeInterested() const {return stats.am_interested;}
139 
141  bool areWeChoked() const {return !stats.has_upload_slot || paused;}
142 
144  bool isDHTSupported() const {return stats.dht_support;}
145 
147  TimeStamp getChokeTime() const {return stats.time_choked;}
148 
150  TimeStamp getUnchokeTime() const {return stats.time_unchoked;}
151 
153  bool isSeeder() const {return pieces.allOn();}
154 
156  virtual void chunkAllowed(bt::Uint32 chunk) = 0;
157 
159  virtual void handlePacket(const bt::Uint8* packet, bt::Uint32 size) = 0;
160 
161 
162  protected:
163  mutable PeerInterface::Stats stats;
164  bool paused;
165  bool killed;
166  PeerID peer_id;
167  BitSet pieces;
168  };
169 
170 }
171 
172 #endif
bt::PeerInterface
Interface for a Peer.
Definition: peerinterface.h:40
bt::BitSet
Simple implementation of a BitSet.
Definition: bitset.h:37
bt::PeerInterface::Stats::bytes_downloaded
bt::Uint64 bytes_downloaded
Amount of data downloaded.
Definition: peerinterface.h:73
bt::PeerInterface::Stats
Definition: peerinterface.h:51
bt::PeerInterface::isInterested
bool isInterested() const
Is the Peer interested.
Definition: peerinterface.h:135
bt::PeerInterface::Stats::transport_protocol
bt::TransportProtocol transport_protocol
The transport protocol used by the peer.
Definition: peerinterface.h:101
bt::PeerInterface::Stats::address
QString address() const
Get the address of the peer (hostname if it is valid, IP otherwise)
Definition: peerinterface.h:106
bt::PeerInterface::Stats::extension_protocol
bool extension_protocol
Whether or not the peer supports the extension protocol.
Definition: peerinterface.h:93
bt::PeerInterface::Stats::max_request_queue
bt::Uint32 max_request_queue
Max number of outstanding requests (reqq in extended protocol handshake)
Definition: peerinterface.h:95
bt::PeerInterface::Stats::local
bool local
Is this a peer on the local network.
Definition: peerinterface.h:91
bt::PeerInterface::areWeInterested
bool areWeInterested() const
Are we interested in the Peer.
Definition: peerinterface.h:138
bt::PeerInterface::Stats::perc_of_file
float perc_of_file
Percentage of file which the peer has.
Definition: peerinterface.h:67
bt::PeerInterface::Stats::am_interested
bool am_interested
Am I interested in the peer.
Definition: peerinterface.h:81
bt::PeerInterface::getBitSet
const BitSet & getBitSet() const
Get the Peer's BitSet.
Definition: peerinterface.h:126
bt::PeerInterface::Stats::download_rate
bt::Uint32 download_rate
Download rate (bytes/s)
Definition: peerinterface.h:59
bt::PeerInterface::Stats::encrypted
bool encrypted
Whether or not this connection is encrypted.
Definition: peerinterface.h:83
bt::PeerInterface::Stats::upload_rate
bt::Uint32 upload_rate
Upload rate (bytes/s)
Definition: peerinterface.h:61
bt::PeerInterface::Stats::aca_score
double aca_score
Advanced choke algorithm score.
Definition: peerinterface.h:75
bt::PeerInterface::Stats::snubbed
bool snubbed
Snubbed or not (i.e. we haven't received a piece for a minute)
Definition: peerinterface.h:65
bt::PeerInterface::isDHTSupported
bool isDHTSupported() const
See if the peer supports DHT.
Definition: peerinterface.h:144
bt::PeerInterface::Stats::time_unchoked
TimeStamp time_unchoked
Time the peer unchoked us.
Definition: peerinterface.h:99
bt::PeerInterface::Stats::dht_support
bool dht_support
Does this peer support DHT.
Definition: peerinterface.h:69
bt::PeerInterface::getUnchokeTime
TimeStamp getUnchokeTime() const
Get the time when this Peer unchoked us.
Definition: peerinterface.h:150
bt::PeerInterface::PeerInterface
PeerInterface(const PeerID &peer_id, Uint32 num_chunks)
bt::PeerInterface::Stats::client
QString client
The client (Azureus, BitComet, ...)
Definition: peerinterface.h:57
bt::PeerInterface::Stats::interested
bool interested
Is the peer interested.
Definition: peerinterface.h:79
bt::PeerInterface::isKilled
bool isKilled() const
See if the peer has been killed.
Definition: peerinterface.h:118
bt::PeerInterface::Stats::partial_seed
bool partial_seed
Is this a partial seed.
Definition: peerinterface.h:103
bt::PeerInterface::kill
virtual void kill()=0
bt::PeerInterface::Stats::num_down_requests
bt::Uint32 num_down_requests
Number of outstanding download requests queued.
Definition: peerinterface.h:87
bt::PeerInterface::Stats::fast_extensions
bool fast_extensions
Supports the fast extensions.
Definition: peerinterface.h:89
bt::PeerInterface::Stats::bytes_uploaded
bt::Uint64 bytes_uploaded
Amount of data uploaded.
Definition: peerinterface.h:71
bt::PeerInterface::getChokeTime
TimeStamp getChokeTime() const
Get the time when this Peer choked us.
Definition: peerinterface.h:147
bt::PeerInterface::chunkAllowed
virtual void chunkAllowed(bt::Uint32 chunk)=0
Peer is allowed to download chunk (used for superseeding)
bt::PeerInterface::Stats::has_upload_slot
bool has_upload_slot
Flag to indicate if this peer has an upload slot.
Definition: peerinterface.h:77
bt::PeerInterface::getPeerID
const PeerID & getPeerID() const
Get the Peer's ID.
Definition: peerinterface.h:129
bt::PeerInterface::getStats
const Stats & getStats() const
Get the Peer's statistics.
Definition: peerinterface.h:110
bt::PeerInterface::isSeeder
bool isSeeder() const
See if the peer is a seeder.
Definition: peerinterface.h:153
bt::PeerInterface::Stats::ip_address
QString ip_address
IP address of peer (dotted notation)
Definition: peerinterface.h:53
bt::PeerInterface::Stats::time_choked
TimeStamp time_choked
Time the peer choked us.
Definition: peerinterface.h:97
bt::PeerID
Definition: peerid.h:33
bt::PeerInterface::areWeChoked
bool areWeChoked() const
Are we choked for the Peer.
Definition: peerinterface.h:141
bt::PeerInterface::Stats::hostname
QString hostname
Host name of the peer.
Definition: peerinterface.h:55
bt::PeerInterface::isChoked
bool isChoked() const
Is the Peer choked.
Definition: peerinterface.h:132
bt::PeerInterface::averageDownloadSpeed
virtual bt::Uint32 averageDownloadSpeed() const =0
bt::PeerInterface::Stats::num_up_requests
bt::Uint32 num_up_requests
Number of upload requests queued.
Definition: peerinterface.h:85
bt::PeerInterface::Stats::choked
bool choked
Choked or not.
Definition: peerinterface.h:63
bt::PeerInterface::handlePacket
virtual void handlePacket(const bt::Uint8 *packet, bt::Uint32 size)=0
Handle a received packet.