Upstream tarball 20080304
[amule.git] / src / kademlia / routing / RoutingZone.h
blobf069d6c3a4fc929907b7a21e470d9fb981e0a842
1 //
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 CPing;
51 class CContact;
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 //friend CRoutingZone;
65 public:
67 CRoutingZone();
68 ~CRoutingZone();
70 time_t m_nextBigTimer;
71 time_t m_nextSmallTimer;
72 bool OnBigTimer(void);
73 void OnSmallTimer(void);
75 bool Add(const CUInt128 &id, uint32 ip, uint16 port, uint16 tport, byte type);
76 void Remove(const CUInt128 &id);
77 void SetAlive(uint32 ip, uint16 port);
79 CContact *GetContact(const CUInt128 &id) const;
80 uint32 GetNumContacts(void) const;
82 // Returns a list of all contacts in all leafs of this zone.
83 void GetAllEntries(ContactList *result, bool emptyFirst = true);
85 // Returns the *maxRequired* tokens that are closest to the target within this zone's subtree.
86 void GetClosestTo(uint32 maxType, const CUInt128 &target, const CUInt128 &distance, uint32 maxRequired, ContactMap *result, bool emptyFirst = true, bool setInUse = false) const;
88 // Ideally: Returns all contacts that are in buckets of common range between us and the asker.
89 // In practice: returns the contacts from the top (2^{logBase+1}) buckets.
90 uint32 GetBootstrapContacts(ContactList *results, uint32 maxRequired);
92 /** Debugging. */
93 // void dumpContents(LPCTSTR prefix = NULL) const;
94 // void selfTest(void);
96 // uint64 getApproximateNodeCount(uint32 ourLevel) const;
98 bool IsDirty() const { return dirty; }
100 uint32 EstimateCount();
102 void Merge(void);
104 private:
106 CRoutingZone(CRoutingZone *super_zone, int level, const CUInt128 &zone_index);
107 void Init(CRoutingZone *super_zone, int level, const CUInt128 &zone_index);
109 bool AddByDistance(const CUInt128 &distance, const CUInt128 &id, uint32 ip, uint16 port, uint16 tport, byte type);
112 * Zone pair is an array of two. Either both entries are null, which
113 * means that *this* is a leaf zone, or both are non-null which means
114 * that *this* has been split into equally sized finer zones.
115 * The zone with index 0 is the one closer to our *self* token.
117 CRoutingZone *m_subZones[2];
118 CRoutingZone *m_superZone;
120 static wxString m_filename;
121 static CUInt128 me;
123 void ReadFile(void);
124 void WriteFile(void);
126 bool IsLeaf(void) const;
127 bool CanSplit(void) const;
129 // Returns all contacts from this zone tree that are no deeper than *depth* from the current zone.
130 void TopDepth(int depth, ContactList *result, bool emptyFirst = true);
132 // Returns the maximum depth of the tree as the number of edges of the longest path to a leaf.
133 uint32 GetMaxDepth(void) const;
135 void RandomBin(ContactList *result, bool emptyFirst = true);
138 * The level indicates what size chunk of the address space
139 * this zone is representing. Level 0 is the whole space,
140 * level 1 is 1/2 of the space, level 2 is 1/4, etc.
142 uint32 m_level;
145 * This is the distance in number of zones from the zone at this level
146 * that contains the center of the system; distance is wrt the XOR metric.
148 CUInt128 m_zoneIndex;
150 /** List of contacts, if this zone is a leaf zone. */
151 CRoutingBin *m_bin;
154 * Generates a new TokenBin for this zone. Used when the current zone is becoming a leaf zone.
155 * Must be deleted by caller
157 CRoutingZone *GenSubZone(int side);
159 void Split(void);
161 void StartTimer(void);
162 void StopTimer(void);
164 void RandomLookup(void);
166 bool dirty;
169 } // End namespace
171 #endif // __ROUTING_ZONE__
172 // File_checked_for_headers