Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d642b911ece2213d7a2941a7531c7d48297652ff (plain) (blame)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************
 * Copyright (c) 2006, 2007 Remy Suen, Composent Inc., and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Remy Suen <remy.suen@gmail.com> - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.internal.provider.bittorrent;

import java.io.IOException;

import org.eclipse.bittorrent.IPieceProgressListener;
import org.eclipse.bittorrent.ITorrentStateListener;
import org.eclipse.bittorrent.Torrent;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.filetransfer.IFileRangeSpecification;
import org.eclipse.ecf.filetransfer.IFileTransferListener;
import org.eclipse.ecf.filetransfer.IFileTransferPausable;
import org.eclipse.ecf.filetransfer.IFileTransferRateControl;
import org.eclipse.ecf.filetransfer.IIncomingFileTransfer;
import org.eclipse.ecf.filetransfer.IOutgoingFileTransfer;
import org.eclipse.ecf.filetransfer.UserCancelledException;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent;

final class TorrentFileTransfer implements IFileTransferPausable, IFileTransferRateControl, IIncomingFileTransfer, IOutgoingFileTransfer {

	private final ID id;

	private final IFileTransferListener listener;

	private final Torrent torrent;

	private final IPieceProgressListener pieceListener;

	private final ITorrentStateListener stateListener;

	private final double total;

	private Exception exception;

	private boolean paused = false;

	TorrentFileTransfer(ID id, IFileTransferListener listener, Torrent torrent) {
		this.id = id;
		this.listener = listener;
		this.torrent = torrent;
		total = torrent.getTorrentFile().getTotalLength();

		pieceListener = new IPieceProgressListener() {
			public void blockDownloaded(int piece, int index, int blockLength) {
				TorrentFileTransfer.this.listener.handleTransferEvent(new IIncomingFileTransferReceiveDataEvent() {

					private static final long serialVersionUID = -7666111308704272599L;

					public IIncomingFileTransfer getSource() {
						return TorrentFileTransfer.this;
					}

				});
			}
		};

		stateListener = new ITorrentStateListener() {
			public void stateChanged(int state) {
				if (state == ITorrentStateListener.FINISHED) {
					notifyCompletion(null);
				}
			}
		};

		torrent.addPieceProgressListener(pieceListener);
		torrent.addTorrentStateListener(stateListener);

		try {
			torrent.start();
		} catch (final IOException e) {
			notifyCompletion(e);
		}
	}

	private void notifyCompletion(Exception exception) {
		this.exception = exception;
		torrent.removePieceProgressListener(pieceListener);
		torrent.removeTorrentStateListener(stateListener);

		listener.handleTransferEvent(new IIncomingFileTransferReceiveDoneEvent() {

			private static final long serialVersionUID = -6685158329789351560L;

			public Exception getException() {
				return TorrentFileTransfer.this.exception;
			}

			public IIncomingFileTransfer getSource() {
				return TorrentFileTransfer.this;
			}

		});
	}

	public void cancel() {
		try {
			torrent.stop();
			notifyCompletion(new UserCancelledException());
		} catch (final IOException e) {
			notifyCompletion(e);
		}
	}

	public long getBytesSent() {
		return torrent.getUploaded();
	}

	public Exception getException() {
		return exception;
	}

	public double getPercentComplete() {
		return (total - torrent.getRemaining()) / total;
	}

	public Object getAdapter(Class adapter) {
		return adapter.isInstance(this) ? this : null;
	}

	public ID getID() {
		return id;
	}

	public long getBytesReceived() {
		return torrent.getDownloaded();
	}

	public boolean isDone() {
		return exception != null || getPercentComplete() == 1;
	}

	public boolean pause() {
		if (paused || isDone()) {
			return false;
		}

		try {
			torrent.stop();
			paused = true;
			return true;
		} catch (final IOException e) {
			notifyCompletion(e);
			return false;
		}
	}

	public boolean resume() {
		if (!paused || isDone()) {
			return false;
		}

		try {
			torrent.start();
			paused = false;
			return true;
		} catch (final IOException e) {
			notifyCompletion(e);
			return false;
		}
	}

	public void setMaxDownloadSpeed(long maxDownloadSpeed) {
		torrent.setMaxDownloadSpeed(maxDownloadSpeed);
	}

	public void setMaxUploadSpeed(long maxUploadSpeed) {
		torrent.setMaxUploadSpeed(maxUploadSpeed);
	}

	public boolean isPaused() {
		return paused;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.filetransfer.IIncomingFileTransfer#getListener()
	 */
	public IFileTransferListener getListener() {
		return listener;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.filetransfer.IIncomingFileTransfer#getFileRangeSpecification()
	 */
	public IFileRangeSpecification getFileRangeSpecification() {
		return null;
	}

	public long getFileLength() {
		return torrent.getTorrentFile().getTotalLength();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.filetransfer.IIncomingFileTransfer#getRemoteFileName()
	 */
	public String getRemoteFileName() {
		if (torrent == null)
			return null;
		if (!torrent.getTorrentFile().isMultiFile()) {
			return torrent.getTorrentFile().getFilenames()[0];
		}
		return null;
	}

}

Back to the top