Upstream tarball 20080510
[amule.git] / src / kademlia / routing / Contact.cpp
blobb64f04e15fba396425231ce5a101f928611f26e7
1 //
2 // This file is part of aMule Project
3 //
4 // Copyright (c) 2004-2008 Angel Vidal (Kry) ( kry@amule.org )
5 // Copyright (c) 2004-2008 aMule Project ( http://www.amule-project.net )
6 // Copyright (C)2003 Barry Dunne (http://www.emule-project.net)
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 // This work is based on the java implementation of the Kademlia protocol.
23 // Kademlia: Peer-to-peer routing based on the XOR metric
24 // Copyright (C) 2002 Petar Maymounkov [petar@post.harvard.edu]
25 // http://kademlia.scs.cs.nyu.edu
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 #include "Contact.h"
41 #include <common/Macros.h>
43 #include "../../Statistics.h"
45 ////////////////////////////////////////
46 using namespace Kademlia;
47 ////////////////////////////////////////
49 CContact::~CContact()
51 theStats::RemoveKadNode();
54 CContact::CContact(const CUInt128 &clientID, uint32_t ip, uint16_t udpPort, uint16_t tcpPort, uint8_t version, const CKadUDPKey& key, bool ipVerified, const CUInt128 &target)
55 : m_clientID(clientID),
56 m_distance(target ^ clientID),
57 m_ip(ip),
58 m_tcpPort(tcpPort),
59 m_udpPort(udpPort),
60 m_type(3),
61 m_lastTypeSet(time(NULL)),
62 m_expires(0),
63 m_created(m_lastTypeSet),
64 m_inUse(0),
65 m_version(version),
66 m_checkKad2(true),
67 m_ipVerified(ipVerified),
68 m_udpKey(key)
70 wxASSERT(udpPort);
71 theStats::AddKadNode();
74 void CContact::Copy(const CContact& from) throw()
76 m_clientID = from.m_clientID;
77 m_distance = from.m_distance;
78 m_ip = from.m_ip;
79 m_tcpPort = from.m_tcpPort;
80 m_udpPort = from.m_udpPort;
81 m_inUse = from.m_inUse;
82 m_lastTypeSet = from.m_lastTypeSet;
83 m_expires = from.m_expires;
84 m_created = from.m_created;
85 m_type = from.m_type;
86 m_version = from.m_version;
87 m_checkKad2 = from.m_checkKad2;
88 m_ipVerified = from.m_ipVerified;
89 m_udpKey = from.m_udpKey;
92 void CContact::CheckingType() throw()
94 time_t now = time(NULL);
96 if(now - m_lastTypeSet < 10 || m_type == 4) {
97 return;
100 m_lastTypeSet = now;
102 m_expires = now + MIN2S(2);
103 m_type++;
106 void CContact::UpdateType() throw()
108 time_t now = time(NULL);
109 uint32_t hours = (now - m_created) / HR2S(1);
110 switch (hours) {
111 case 0:
112 m_type = 2;
113 m_expires = now + HR2S(1);
114 break;
115 case 1:
116 m_type = 1;
117 m_expires = now + MIN2S(90); //HR2S(1.5)
118 break;
119 default:
120 m_type = 0;
121 m_expires = now + HR2S(2);
125 time_t CContact::GetLastSeen() const throw()
127 // calculating back from expire time, so we don't need an additional field.
128 // might result in wrong values if doing CheckingType() for example, so don't use for important timing stuff
129 if (m_expires != 0) {
130 switch (m_type) {
131 case 2: return m_expires - HR2S(1);
132 case 1: return m_expires - MIN2S(90) /*(unsigned)HR2S(1.5)*/;
133 case 0: return m_expires - HR2S(2);
136 return 0;
138 // File_checked_for_headers