summaryrefslogtreecommitdiffstats
path: root/libktorrent/torrent/peersourcemanager.h
blob: e7de4f80d7143a64f7e151af01369bccaad0f9c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/
#ifndef BTPEERSOURCEMANAGER_H
#define BTPEERSOURCEMANAGER_H

#include <tqtimer.h>
#include <tqdatetime.h>
#include <tqptrlist.h>
#include <util/ptrmap.h>
#include <interfaces/trackerslist.h>

namespace kt
{
	class PeerSource;
}

namespace dht
{
	class DHTTrackerBackend;
}

namespace bt
{
	class Tracker;
	class PeerManager;
	class Torrent;
	class TorrentControl;

	/**
	 * @author Joris Guisson <joris.guisson@gmail.com>
	 * 
	 * This class manages all PeerSources.
	*/
	class PeerSourceManager : public TQObject, public kt::TrackersList
	{
		TQ_OBJECT
  
				
		TorrentControl* tor;
		PeerManager* pman;
		PtrMap<KURL,Tracker> trackers;
		TQPtrList<kt::PeerSource> additional;
		Tracker* curr;
		dht::DHTTrackerBackend* m_dht;
		bool started;
		bool pending;
		KURL::List custom_trackers;
		TQDateTime request_time;
		TQTimer timer;
		Uint32 failures;
		bool no_save_custom_trackers;
	public:
		PeerSourceManager(TorrentControl* tor,PeerManager* pman);
		virtual ~PeerSourceManager();
	
				
		/**
		 * Add a PeerSource, the difference between PeerSource and Tracker
		 * is that only one Tracker can be used at the same time, 
		 * PeerSource can always be used.
		 * @param ps The PeerSource
		 */
		void addPeerSource(kt::PeerSource* ps);

		/**
		 * See if the PeerSourceManager has been started 
		 */
		bool isStarted() const {return started;}
		
		/**
		 * Start gathering peers
		 */
		void start();
		
		/**
		 * Stop gathering peers
		 * @param wjob WaitJob to wait at exit for the completion of stopped events to the trackers
		 */
		void stop(WaitJob* wjob = 0);
		
		/**
		 * Notify peersources and trackrs that the download is complete.
		 */
		void completed();
		
		/**
		 * Do a manual update on all peer sources and trackers.
		 */
		void manualUpdate();
		
		/**
		 * Remove a Tracker or PeerSource.
		 * @param ps 
		 */
		void removePeerSource(kt::PeerSource* ps);
		
		virtual KURL getTrackerURL() const;
		virtual KURL::List getTrackerURLs();
		virtual void addTracker(KURL url, bool custom = true,int tier = 1);
		virtual bool removeTracker(KURL url);
		virtual void setTracker(KURL url);
		virtual void restoreDefault();
		
		/**
		 * Get the time to the next tracker update.
		 * @return The time in seconds
		 */
		Uint32 getTimeToNextUpdate() const;
		
		/// Get the number of potential seeders
		Uint32 getNumSeeders() const;
		
		/// Get the number of potential leechers
		Uint32 getNumLeechers() const;
		
		/// Get the number of failures
		Uint32 getNumFailures() const {return failures;}
		
		///Adds DHT as PeerSource for this torrent
		void addDHT();
		///Removes DHT from PeerSourceManager for this torrent.
		void removeDHT();
		///Checks if DHT is enabled
		bool dhtStarted();
		
	private slots:
		/**
		 * The an error happened contacting the tracker.
		 * @param err The error
		 */
		void onTrackerError(const TQString & err);
		
		/**
		 * Tracker update was OK.
		 * @param 
		 */
		void onTrackerOK();
		
		/**
		 * Tracker is doing a request.  
		 */
		void onTrackerRequestPending();
		
		/**
		 * Update the current tracker manually
		 */
		void updateCurrentManually();
		
	signals:
		/**
		 * Status has changed of the tracker.
		 * @param ns The new status
		 */
		void statusChanged(const TQString & ns);
		
	private:
		void saveCustomURLs();
		void loadCustomURLs();
		void addTracker(Tracker* trk);
		void switchTracker(Tracker* trk);
		Tracker* selectTracker();
	};

}

#endif