libktorrent  2.2.0
connectionlimit.h
1 /***************************************************************************
2  * Copyright (C) 2012 by *
3  * Joris Guisson <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 
21 #ifndef BT_CONNECTIONLIMIT_H
22 #define BT_CONNECTIONLIMIT_H
23 
24 #include <QMap>
25 #include <QSharedPointer>
26 #include <util/constants.h>
27 #include <util/sha1hash.h>
28 #include <ktorrent_export.h>
29 
30 namespace bt
31 {
35  class KTORRENT_EXPORT ConnectionLimit
36  {
37  public:
39  virtual ~ConnectionLimit();
40 
42  bt::Uint32 totalConnections() const {return global_total;}
43 
49  void setLimits(bt::Uint32 global_limit, bt::Uint32 torrent_limit);
50 
55  class Token
56  {
57  public:
58  Token(ConnectionLimit & limit, const bt::SHA1Hash & hash);
59  ~Token();
60 
62  const bt::SHA1Hash & infoHash() const {return hash;}
63 
64  typedef QSharedPointer<Token> Ptr;
65 
66  private:
67  ConnectionLimit & limit;
68  bt::SHA1Hash hash;
69  };
70 
71 
77  Token::Ptr acquire(const SHA1Hash & hash);
78 
79  protected:
84  void release(const Token & token);
85 
86  private:
87  bt::Uint32 global_limit;
88  bt::Uint32 global_total;
89  bt::Uint32 torrent_limit;
90  QMap<SHA1Hash, bt::Uint32> torrent_totals;
91  };
92 
93 }
94 
95 #endif // BT_CONNECTIONLIMIT_H
bt::ConnectionLimit::Token::infoHash
const bt::SHA1Hash & infoHash() const
Get the info hash.
Definition: connectionlimit.h:62
bt::ConnectionLimit::totalConnections
bt::Uint32 totalConnections() const
Get the total number of connections currently in use.
Definition: connectionlimit.h:42
bt::ConnectionLimit::acquire
Token::Ptr acquire(const SHA1Hash &hash)
bt::ConnectionLimit::release
void release(const Token &token)
bt::ConnectionLimit
Definition: connectionlimit.h:36
bt::SHA1Hash
Stores a SHA1 hash.
Definition: sha1hash.h:41
bt::ConnectionLimit::Token
Definition: connectionlimit.h:56
bt::ConnectionLimit::setLimits
void setLimits(bt::Uint32 global_limit, bt::Uint32 torrent_limit)