libktorrent  2.2.0
database.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 DHTDATABASE_H
21 #define DHTDATABASE_H
22 
23 #include <qmap.h>
24 #include <qlist.h>
25 #include <net/address.h>
26 #include <util/ptrmap.h>
27 #include <util/constants.h>
28 #include <util/array.h>
29 #include "key.h"
30 
31 
32 
33 namespace dht
34 {
36  const bt::Uint32 MAX_ITEM_AGE = 30 * 60 * 1000;
37 
44  class DBItem
45  {
46  public:
47  DBItem();
48  DBItem(const net::Address & addr);
49  DBItem(const DBItem & item);
50  virtual ~DBItem();
51 
53  bool expired(bt::TimeStamp now) const;
54 
56  const net::Address & getAddress() const {return addr;}
57 
63  bt::Uint32 pack(bt::Uint8* buf) const;
64 
65  DBItem & operator = (const DBItem & item);
66  private:
67  net::Address addr;
68  bt::TimeStamp time_stamp;
69  };
70 
71  typedef QList<DBItem> DBItemList;
72 
78  class Database
79  {
80  public:
81  Database();
82  virtual ~Database();
83 
89  void store(const dht::Key & key, const DBItem & dbi);
90 
102  void sample(const dht::Key & key, DBItemList & dbl, bt::Uint32 max_entries, bt::Uint32 ip_version);
103 
109  void expire(bt::TimeStamp now);
110 
117  QByteArray genToken(const net::Address & addr);
118 
125  bool checkToken(const QByteArray & token, const net::Address & addr);
126 
128  bool contains(const dht::Key & key) const;
129 
131  void insert(const dht::Key & key);
132 
133  private:
135  QMap<QByteArray, bt::TimeStamp> tokens;
136  };
137 
138 }
139 
140 #endif
dht::Database::sample
void sample(const dht::Key &key, DBItemList &dbl, bt::Uint32 max_entries, bt::Uint32 ip_version)
net::Address
Definition: address.h:41
bt::PtrMap< dht::Key, DBItemList >
dht::Database::insert
void insert(const dht::Key &key)
Insert an empty item (only if it isn't already in the DB)
dht::Key
Key in the distributed hash table.
Definition: key.h:39
dht::Database
Definition: database.h:79
dht::Database::genToken
QByteArray genToken(const net::Address &addr)
dht::DBItem
Definition: database.h:45
dht::DBItem::expired
bool expired(bt::TimeStamp now) const
See if the item is expired.
dht::Database::expire
void expire(bt::TimeStamp now)
dht::DBItem::pack
bt::Uint32 pack(bt::Uint8 *buf) const
dht::Database::contains
bool contains(const dht::Key &key) const
Test whether or not the DB contains a key.
dht::DBItem::getAddress
const net::Address & getAddress() const
Get the address of an item.
Definition: database.h:56
dht::Database::checkToken
bool checkToken(const QByteArray &token, const net::Address &addr)
dht::Database::store
void store(const dht::Key &key, const DBItem &dbi)