1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2005-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
10 //
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
24 //
25 
26 #ifndef DEADSOURCELIST_H
27 #define DEADSOURCELIST_H
28 
29 
30 #include <map>
31 
32 #include "Types.h"
33 
34 
35 class CUpDownClient;
36 
37 
38 /**
39  * This class keeps track of "invalid" sources.
40  *
41  * A dead source is a source that has been evaluated as being useles
42  * which can be due to serveral reasons, such as not responding to
43  * queries. This list then allows for those sources to be ignored
44  * for an set ammount of time in order to avoid the overhead of
45  * trying to connect to them.
46  *
47  * This is important, since these sources would be removed and readded
48  * repeatedly, causing extra overhead with no gain.
49  */
50 class CDeadSourceList
51 {
52 public:
53 	/**
54 	 * Constructor.
55 	 *
56 	 * @param isGlobal Specifies if the list is global or not, used for debugging.
57 	 */
58 	CDeadSourceList(bool isGlobal = false);
59 
60 
61 	/**
62 	 * Adds a client to the list of dead sources.
63 	 */
64 	void		AddDeadSource(const CUpDownClient* client);
65 
66 	/**
67 	 * Returns true if the client object is a dead source.
68 	 */
69 	bool		IsDeadSource(const CUpDownClient* client);
70 
71 
72 	/**
73 	 * Returns the number of sources.
74 	 */
75 	uint32		GetDeadSourcesCount() const;
76 
77 private:
78 	/**
79 	 * Removes too old entries from the list.
80 	 */
81 	void		CleanUp();
82 
83 
84 	/**
85 	 * Record of dead source.
86 	 */
87 	class CDeadSource
88 	{
89 	public:
90 		/**
91 		 * Constructor.
92 		 *
93 		 * @param ID The IP/ID of the recorded client.
94 		 * @param Port The TCP port of the recorded client.
95 		 * @param ServerIP The ip of the connected server.
96 		 * @param KadPort The Kad port used by the client.
97 		 *
98 		 * Notes:
99 		 *  * ID must be specified.
100 		 *  * Either KadPort or Port must be specified.
101 		 *  * For lowid sources, ServerIP must be specified.
102 		 *
103 		 */
104 		CDeadSource(uint32 ID, uint16 Port, uint32 ServerIP, uint16 KadPort);
105 
106 
107 		/**
108 		 * Equality operator.
109 		 */
110 		bool operator==(const CDeadSource& other) const;
111 
112 
113 		/**
114 		 * Sets the timestamp for the time where this entry will expire.
115 		 */
116 		void	SetTimeout( uint32 t );
117 
118 		/**
119 		 * Returns the timestamp of this entry.
120 		 */
121 		uint32	GetTimeout() const;
122 
123 	private:
124 		//! The ID/IP of the client.
125 		uint32			m_ID;
126 		//! The TCP port of the client
127 		uint16			m_Port;
128 		//! The Kad port of the client.
129 		uint16			m_KadPort;
130 		//! The IP of the server the client is connected to.
131 		uint32			m_ServerIP;
132 		//! The timestamp of DOOM!
133 		uint32			m_TimeStamp;
134 	};
135 
136 
137 	typedef std::multimap< uint32, CDeadSource > DeadSourceMap;
138 	typedef DeadSourceMap::iterator DeadSourceIterator;
139 	typedef std::pair<DeadSourceIterator, DeadSourceIterator> DeadSourcePair;
140 	//! List of currently dead sources.
141 	DeadSourceMap m_sources;
142 
143 
144 	//! The timestamp of when the last cleanup was performed.
145 	uint32	m_dwLastCleanUp;
146 	//! Specifies if the list is global or not.
147 	bool	m_bGlobalList;
148 };
149 
150 #endif
151 // File_checked_for_headers
152