View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.azureus;
21  
22  import java.awt.event.ActionEvent;
23  import java.io.File;
24  
25  import javax.swing.Action;
26  
27  import org.apache.log4j.Logger;
28  import org.gudy.azureus2.core3.disk.DiskManager;
29  import org.gudy.azureus2.core3.disk.DiskManagerFileInfo;
30  import org.gudy.azureus2.core3.download.DownloadManager;
31  import org.gudy.azureus2.core3.download.DownloadManagerListener;
32  import org.gudy.azureus2.core3.download.DownloadManagerPeerListener;
33  import org.gudy.azureus2.core3.download.DownloadManagerTrackerListener;
34  import org.gudy.azureus2.core3.peer.PEPeer;
35  import org.gudy.azureus2.core3.peer.PEPeerManager;
36  import org.gudy.azureus2.core3.peer.PEPiece;
37  import org.gudy.azureus2.core3.torrent.TOTorrent;
38  import org.gudy.azureus2.core3.tracker.client.TRTrackerResponse;
39  import org.gudy.azureus2.core3.tracker.client.TRTrackerScraperResponse;
40  import org.xnap.XNap;
41  import org.xnap.peer.Peer;
42  import org.xnap.plugin.Plugin;
43  import org.xnap.transfer.AbstractDownload;
44  import org.xnap.transfer.DefaultSegment;
45  import org.xnap.transfer.Segment;
46  import org.xnap.transfer.Transfer;
47  import org.xnap.transfer.action.AbstractStartAction;
48  import org.xnap.transfer.action.AbstractStopAction;
49  
50  /***
51   * Manages {@link AzureusDownload} objects.
52   */
53  public class AzureusDownloadContainer extends AbstractDownload 
54  	implements DownloadManagerListener, DownloadManagerPeerListener, 
55  			   DownloadManagerTrackerListener {
56  
57      //--- Constant(s) ---
58  
59      //--- Data field(s) ---
60  
61      private static Logger logger 
62  		= Logger.getLogger(AzureusDownloadContainer.class);
63  
64      private DownloadManager manager;
65  	private DefaultSegment[] segments;
66  	private long offset = 0;
67  
68  	private boolean childrenAdded;
69  	
70      //--- Constructor(s) ---
71  
72      /***
73  	 *
74       */
75      public AzureusDownloadContainer(DownloadManager manager) 
76      {
77  		this.manager = manager;
78  		
79  		manager.addListener(this);
80  		manager.addPeerListener(this);
81  		manager.addTrackerListener(this);
82      }
83  
84      //--- Method(s) ---
85  
86      public Action[] getActions()
87      {
88  		return new Action[] { new StartAction(), new StopAction() };
89      }
90  
91      public long getBytesTransferred() 
92      {
93  		return getTotalBytesTransferred() - offset;
94      }
95  
96      public String getDescription()
97  	{
98      	StringBuffer sb = new StringBuffer();
99      	TOTorrent torrent = manager.getTorrent();
100     	if (torrent != null) {
101     		sb.append("URL: " + torrent.getAnnounceURL() + "<br>");
102     	}
103     	return sb.toString();
104     }
105     
106     public File getFile()
107     {
108 		return new File(manager.getFullName());
109     }
110 
111     public String getFilename()
112     {
113 		return getFile().getName();
114     }
115 
116     public long getFilesize()
117     {
118 		return manager.getSize();
119     }
120 
121     public Peer getPeer()
122     {
123 		return null;
124     }
125 
126     public Plugin getPlugin()
127     {
128 		return AzureusPlugin.getInstance();
129     }
130 
131     public Segment[] getSegments()
132     {
133     	if (segments != null) {
134     		boolean[] status = manager.getPiecesStatus();
135     		if (status != null) {
136 	    		for (int i = 0; i < segments.length && i < status.length; i++) {
137 	    			segments[i].setTransferred((status[i]) 
138 						? segments[i].getEnd() - segments[i].getStart()
139 						: 0);
140 	    		}
141 	    	}
142     	}
143 		return segments;
144     }
145 
146     public String getStatus()
147     {   	
148 		switch (manager.getState()) {
149 		case DownloadManager.STATE_WAITING :
150 			return XNap.tr("Waiting");
151     	case DownloadManager.STATE_INITIALIZING:
152     		return XNap.tr("Initializing");
153     	case DownloadManager.STATE_INITIALIZED:
154     		return XNap.tr("Initialized");
155 		case DownloadManager.STATE_ALLOCATING:
156 			return XNap.tr("Allocating");
157 		case DownloadManager.STATE_CHECKING:
158 			return XNap.tr("Checking");
159 		case DownloadManager.STATE_READY:
160 			return XNap.tr("Ready");
161 		case DownloadManager.STATE_DOWNLOADING:
162 			return XNap.tr("Downloading");
163 		case DownloadManager.STATE_SEEDING:
164 			return XNap.tr("Seeding");
165 		case DownloadManager.STATE_STOPPING:
166 			return XNap.tr("Stopping");
167 		case DownloadManager.STATE_STOPPED:
168 			return XNap.tr("Stopped");
169 		case DownloadManager.STATE_QUEUED:
170 			return XNap.tr("Queued");
171 		case DownloadManager.STATE_ERROR:
172 			return XNap.tr("Error {0}", manager.getErrorDetails());
173 		default:
174 			return XNap.tr("Unknown State {0}", manager.getState());
175 		}
176     }
177 
178     public long getTotalBytesTransferred()
179     {
180     	DiskManager dm = manager.getDiskManager();
181 		return (dm != null) ? dm.getTotalLength() - dm.getRemaining() : 0;
182     }
183 
184     public boolean isDone()
185     {
186 		int s = manager.getState();
187 		return s == DownloadManager.STATE_SEEDING 
188 			|| s == DownloadManager.STATE_ERROR;
189     }
190 
191     public boolean isRunning()
192     {
193 		int s = manager.getState();
194 		return s != DownloadManager.STATE_STOPPED;		
195     }
196 
197     /***
198      * Returns -1;
199      */
200     public int getQueuePosition()
201     {
202 		return -1;
203     }
204 
205 	/***
206 	 *  @see org.gudy.azureus2.core3.download.DownloadManagerListener#stateChanged(org.gudy.azureus2.core3.download.DownloadManager, int)
207 	 */
208 	public void stateChanged(DownloadManager manager, int state) 
209 	{
210 		if (state == DownloadManager.STATE_CHECKING) {	
211 			initializeChildren();
212 		}
213 		else if (state == DownloadManager.STATE_READY) {
214 			initializeChildren();
215 			
216 			this.offset = getTotalBytesTransferred();
217 			manager.startDownload();
218 		}
219 		else if (state == DownloadManager.STATE_DOWNLOADING) {
220 			transferStarted();
221 			Transfer[] children = getChildren(); 
222 			for (int i = 0 ; i < children.length; i++) {
223 				((AzureusDownload)children[i]).transferStarted();				
224 			}
225 		}
226 		else {
227 			transferStopped();
228 			Transfer[] children = getChildren(); 
229 			for (int i = 0 ; i < children.length; i++) {
230 				((AzureusDownload)children[i]).transferStopped();				
231 			}
232 		}
233 		
234 		stateChanged();
235 	}
236 
237 	/***
238 	 * 
239 	 */
240 	private void initializeChildren()
241 	{
242 		if (!childrenAdded) {
243 			childrenAdded = true;
244 			
245 			DefaultSegment[] segments = new DefaultSegment[manager.getNbPieces()];
246 			for (int i = 0; i < segments.length; i++) {
247 				long start = i * manager.getDiskManager().getPieceLength();
248 				segments[i] = new DefaultSegment(getFilesize(), start, start + manager.getDiskManager().getPieceLength(), 0, 0);
249 			}
250 
251 			// atomic copy?
252 			this.segments = segments;  
253 			
254 			childrenAdded = true;
255 			DiskManagerFileInfo[] files = manager.getDiskManager().getFiles();
256 			if (files != null) {
257 				for (int i = 0; i < files.length; i++) {
258 					add(new AzureusDownload(this, files[i]));
259 				}
260 			}
261 		}
262 	}
263 
264 	public void updateAvailability()
265 	{
266 		if (segments == null) {
267 			return;
268 		}
269 		
270 		PEPeerManager pm = manager.getPeerManager();
271 		if (pm != null) {
272 			int[] availability = pm.getAvailability();
273 			if (availability != null) {
274 				for (int i = 0; i < segments.length && i < availability.length; i++) {
275 					if (availability[i] > 0) {
276 						segments[i].setAvailability(64 + 5 * availability[i]);
277 					}
278 					else {
279 						segments[i].setAvailability(0);
280 					}
281 				}
282 			}
283 		}
284 	}
285 	
286 	/***
287 	 *  @see org.gudy.azureus2.core3.download.DownloadManagerListener#downloadComplete(org.gudy.azureus2.core3.download.DownloadManager)
288 	 */
289 	public void downloadComplete(DownloadManager manager) 
290 	{
291 		stateChanged();
292 	}
293 
294 	/***
295 	 *  @see org.gudy.azureus2.core3.download.DownloadManagerListener#completionChanged(org.gudy.azureus2.core3.download.DownloadManager, boolean)
296 	 */
297 	public void completionChanged(DownloadManager manager, boolean bCompleted) 
298 	{
299 		stateChanged();
300 	}
301 
302 	/***
303 	 *  @see org.gudy.azureus2.core3.download.DownloadManagerPeerListener#peerAdded(org.gudy.azureus2.core3.peer.PEPeer)
304 	 */
305 	public void peerAdded(PEPeer peer) 
306 	{
307 		updateAvailability();
308 	}
309 
310 	/***
311 	 *  @see org.gudy.azureus2.core3.download.DownloadManagerPeerListener#peerRemoved(org.gudy.azureus2.core3.peer.PEPeer)
312 	 */
313 	public void peerRemoved(PEPeer peer) 
314 	{
315 		updateAvailability();
316 	}
317 
318 	/***
319 	 *  @see org.gudy.azureus2.core3.download.DownloadManagerPeerListener#pieceAdded(org.gudy.azureus2.core3.peer.PEPiece)
320 	 */
321 	public void pieceAdded(PEPiece piece) 
322 	{
323 	}
324 
325 	/***
326 	 *  @see org.gudy.azureus2.core3.download.DownloadManagerPeerListener#pieceRemoved(org.gudy.azureus2.core3.peer.PEPiece)
327 	 */
328 	public void pieceRemoved(PEPiece piece) 
329 	{
330 	}
331 
332 	/***
333 	 *  @see org.gudy.azureus2.core3.download.DownloadManagerTrackerListener#scrapeResult(org.gudy.azureus2.core3.tracker.client.TRTrackerScraperResponse)
334 	 */
335 	public void scrapeResult(TRTrackerScraperResponse response) 
336 	{
337 	}
338 
339 	/***
340 	 *  @see org.gudy.azureus2.core3.download.DownloadManagerTrackerListener#announceResult(org.gudy.azureus2.core3.tracker.client.TRTrackerResponse)
341 	 */
342 	public void announceResult(TRTrackerResponse response) 
343 	{
344 	}
345 
346     //--- Inner Class(es) ---
347 
348 	private class StartAction extends AbstractStartAction {
349 
350 		public void actionPerformed(ActionEvent e) 
351 		{
352 			manager.initialize();
353 		}
354 
355 	}
356 
357 	private class StopAction extends AbstractStopAction {
358 
359 		public void actionPerformed(ActionEvent event) 
360 		{
361 			manager.stopIt();
362 		}
363 
364 	}
365 
366 	/***
367 	 * @return
368 	 */
369 	public DownloadManager getManager()
370 	{
371 		return manager;
372 	}
373 
374 }
375