Upstream tarball 9440
[amule.git] / src / kademlia / routing / RoutingZone.h
blob3565344f6f121244ee1f62330050ce500e792359
1 // -*- C++ -*-
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2003-2008 Barry Dunne (http://www.emule-project.net)
7 //
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 // Note To Mods //
29 Please do not change anything here and release it..
30 There is going to be a new forum created just for the Kademlia side of the client..
31 If you feel there is an error or a way to improve something, please
32 post it in the forum first and let us look at it.. If it is a real improvement,
33 it will be added to the offical client.. Changing something without knowing
34 what all it does can cause great harm to the network if released in mass form..
35 Any mod that changes anything within the Kademlia side will not be allowed to advertise
36 there client on the eMule forum..
39 #ifndef __ROUTING_ZONE__
40 #define __ROUTING_ZONE__
42 #include "Maps.h"
43 #include "../utils/UInt128.h"
45 class CFileDataIO;
47 ////////////////////////////////////////
48 namespace Kademlia {
49 ////////////////////////////////////////
51 class CRoutingBin;
52 class CContact;
53 class CKadUDPKey;
55 /**
56 * The *Zone* is just a node in a binary tree of *Zone*s.
57 * Each zone is either an internal node or a leaf node.
58 * Internal nodes have "bin == null" and "subZones[i] != null",
59 * leaf nodes have "subZones[i] == null" and "bin != null".
61 * All key pseudoaddresses are relative to the center (self), which
62 * is considered to be 000..000
64 class CRoutingZone
66 public:
68 CRoutingZone();
69 ~CRoutingZone();
71 bool OnBigTimer() const;
72 void OnSmallTimer();
73 uint32_t Consolidate();
75 bool Add(const CUInt128 &id, uint32_t ip, uint16_t port, uint16_t tport, uint8_t version, const CKadUDPKey& key, bool& ipVerified, bool update, bool fromNodesDat, bool fromHello);
76 bool AddUnfiltered(const CUInt128 &id, uint32_t ip, uint16_t port, uint16_t tport, uint8_t version, const CKadUDPKey& key, bool& ipVerified, bool update, bool fromNodesDat, bool fromHello);
77 bool Add(CContact *contact, bool& update, bool& outIpVerified);
79 void ReadFile(const wxString& specialNodesdat = wxEmptyString);
81 bool VerifyContact(const CUInt128& id, uint32_t ip);
82 CContact *GetContact(const CUInt128& id) const throw();
83 CContact *GetContact(uint32_t ip, uint16_t port, bool tcpPort) const throw();
84 CContact *GetRandomContact(uint32_t maxType, uint32_t minKadVersion) const throw();
85 uint32_t GetNumContacts() const throw();
86 void GetNumContacts(uint32_t& nInOutContacts, uint32_t& nInOutFilteredContacts, uint8_t minVersion) const;
88 // Returns a list of all contacts in all leafs of this zone.
89 void GetAllEntries(ContactList *result, bool emptyFirst = true) const;
91 // Returns the *maxRequired* tokens that are closest to the target within this zone's subtree.
92 void GetClosestTo(uint32_t maxType, const CUInt128& target, const CUInt128& distance, uint32_t maxRequired, ContactMap *result, bool emptyFirst = true, bool setInUse = false) const;
94 // Ideally: Returns all contacts that are in buckets of common range between us and the asker.
95 // In practice: returns the contacts from the top (2^{logBase+1}) buckets.
96 uint32_t GetBootstrapContacts(ContactList *results, uint32_t maxRequired) const;
98 uint32_t EstimateCount() const throw();
100 time_t m_nextBigTimer;
101 time_t m_nextSmallTimer;
103 private:
105 CRoutingZone(CRoutingZone *super_zone, int level, const CUInt128& zone_index) { Init(super_zone, level, zone_index); }
106 void Init(CRoutingZone *super_zone, int level, const CUInt128& zone_index);
107 void ReadBootstrapNodesDat(CFileDataIO& file);
108 #if 0
109 void WriteBootstrapFile();
110 #endif
112 void WriteFile();
114 bool IsLeaf() const throw() { return m_bin != NULL; }
115 bool CanSplit() const throw();
117 // Returns all contacts from this zone tree that are no deeper than *depth* from the current zone.
118 void TopDepth(int depth, ContactList *result, bool emptyFirst = true) const;
120 // Returns the maximum depth of the tree as the number of edges of the longest path to a leaf.
121 uint32_t GetMaxDepth() const throw();
123 void RandomBin(ContactList *result, bool emptyFirst = true) const;
125 void Split();
127 void StartTimer();
128 void StopTimer();
130 void RandomLookup() const;
132 void SetAllContactsVerified();
135 * Generates a new TokenBin for this zone. Used when the current zone is becoming a leaf zone.
136 * Must be deleted by caller
138 CRoutingZone *GenSubZone(unsigned side);
141 * Zone pair is an array of two. Either both entries are null, which
142 * means that *this* is a leaf zone, or both are non-null which means
143 * that *this* has been split into equally sized finer zones.
144 * The zone with index 0 is the one closer to our *self* token.
146 CRoutingZone *m_subZones[2];
147 CRoutingZone *m_superZone;
149 static wxString m_filename;
150 static CUInt128 me;
153 * The level indicates what size chunk of the address space
154 * this zone is representing. Level 0 is the whole space,
155 * level 1 is 1/2 of the space, level 2 is 1/4, etc.
157 uint32_t m_level;
160 * This is the distance in number of zones from the zone at this level
161 * that contains the center of the system; distance is wrt the XOR metric.
163 CUInt128 m_zoneIndex;
165 /** List of contacts, if this zone is a leaf zone. */
166 CRoutingBin *m_bin;
169 } // End namespace
171 #endif // __ROUTING_ZONE__
172 // File_checked_for_headers