Upstream tarball 20080510
[amule.git] / src / kademlia / routing / RoutingZone.h
blob6e0c39f12d3244bd6f573aec76fcb0252f35cbe6
1 // -*- C++ -*-
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 Angel Vidal (Kry) ( kry@amule.org )
5 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2003 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 ////////////////////////////////////////
46 namespace Kademlia {
47 ////////////////////////////////////////
49 class CRoutingBin;
50 class CContact;
51 class CKadUDPKey;
53 /**
54 * The *Zone* is just a node in a binary tree of *Zone*s.
55 * Each zone is either an internal node or a leaf node.
56 * Internal nodes have "bin == null" and "subZones[i] != null",
57 * leaf nodes have "subZones[i] == null" and "bin != null".
59 * All key pseudoaddresses are relative to the center (self), which
60 * is considered to be 000..000
62 class CRoutingZone
64 public:
66 CRoutingZone();
67 ~CRoutingZone();
69 bool OnBigTimer() const;
70 void OnSmallTimer();
71 uint32_t Consolidate();
73 bool Add(const CUInt128 &id, uint32_t ip, uint16_t port, uint16_t tport, uint8_t version, const CKadUDPKey& key, bool ipVerified, bool update);
74 bool AddUnfiltered(const CUInt128 &id, uint32_t ip, uint16_t port, uint16_t tport, uint8_t version, const CKadUDPKey& key, bool ipVerified, bool update);
75 bool Add(CContact *contact, bool update);
77 void ReadFile(const wxString& specialNodesdat = wxEmptyString);
79 CContact *GetContact(const CUInt128& id) const throw();
80 CContact *GetContact(uint32_t ip, uint16_t port, bool tcpPort) const throw();
81 CContact *GetRandomContact(uint32_t maxType, uint32_t minKadVersion) const throw();
82 uint32_t GetNumContacts() const throw();
84 // Returns a list of all contacts in all leafs of this zone.
85 void GetAllEntries(ContactList *result, bool emptyFirst = true) const;
87 // Returns the *maxRequired* tokens that are closest to the target within this zone's subtree.
88 void GetClosestTo(uint32_t maxType, const CUInt128& target, const CUInt128& distance, uint32_t maxRequired, ContactMap *result, bool emptyFirst = true, bool setInUse = false) const;
90 // Ideally: Returns all contacts that are in buckets of common range between us and the asker.
91 // In practice: returns the contacts from the top (2^{logBase+1}) buckets.
92 uint32_t GetBootstrapContacts(ContactList *results, uint32_t maxRequired) const;
94 uint32_t EstimateCount() const throw();
96 time_t m_nextBigTimer;
97 time_t m_nextSmallTimer;
99 private:
101 CRoutingZone(CRoutingZone *super_zone, int level, const CUInt128& zone_index) { Init(super_zone, level, zone_index); }
102 void Init(CRoutingZone *super_zone, int level, const CUInt128& zone_index);
104 void WriteFile();
106 bool IsLeaf() const throw() { return m_bin != NULL; }
107 bool CanSplit() const throw();
109 // Returns all contacts from this zone tree that are no deeper than *depth* from the current zone.
110 void TopDepth(int depth, ContactList *result, bool emptyFirst = true) const;
112 // Returns the maximum depth of the tree as the number of edges of the longest path to a leaf.
113 uint32_t GetMaxDepth() const throw();
115 void RandomBin(ContactList *result, bool emptyFirst = true) const;
117 void Split();
119 void StartTimer();
120 void StopTimer();
122 void RandomLookup() const;
125 * Generates a new TokenBin for this zone. Used when the current zone is becoming a leaf zone.
126 * Must be deleted by caller
128 CRoutingZone *GenSubZone(unsigned side);
131 * Zone pair is an array of two. Either both entries are null, which
132 * means that *this* is a leaf zone, or both are non-null which means
133 * that *this* has been split into equally sized finer zones.
134 * The zone with index 0 is the one closer to our *self* token.
136 CRoutingZone *m_subZones[2];
137 CRoutingZone *m_superZone;
139 static wxString m_filename;
140 static CUInt128 me;
143 * The level indicates what size chunk of the address space
144 * this zone is representing. Level 0 is the whole space,
145 * level 1 is 1/2 of the space, level 2 is 1/4, etc.
147 uint32_t m_level;
150 * This is the distance in number of zones from the zone at this level
151 * that contains the center of the system; distance is wrt the XOR metric.
153 CUInt128 m_zoneIndex;
155 /** List of contacts, if this zone is a leaf zone. */
156 CRoutingBin *m_bin;
159 } // End namespace
161 #endif // __ROUTING_ZONE__
162 // File_checked_for_headers