Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorslewis2007-03-21 21:08:23 +0000
committerslewis2007-03-21 21:08:23 +0000
commit00274f44a5e02d6213c0f485f2a8fc79b8a1b047 (patch)
tree8f43147474e35ea0b640487855f69d49c72f6d4a /providers/bundles/org.eclipse.ecf.provider.bittorrent
parent45a83a4a274669d7a6d9fba09d866d59bd309f36 (diff)
downloadorg.eclipse.ecf-00274f44a5e02d6213c0f485f2a8fc79b8a1b047.tar.gz
org.eclipse.ecf-00274f44a5e02d6213c0f485f2a8fc79b8a1b047.tar.xz
org.eclipse.ecf-00274f44a5e02d6213c0f485f2a8fc79b8a1b047.zip
Added using jobs api for outgoing file transfer handling. Added IIncomingFileTransfer.accept(OutputStream, IFileTransferListener) to api and provided implementation code. Added test code in org.eclipse.ecf.tests.filetransfer.OutgoingFileTransferTest.
Diffstat (limited to 'providers/bundles/org.eclipse.ecf.provider.bittorrent')
-rw-r--r--providers/bundles/org.eclipse.ecf.provider.bittorrent/src/org/eclipse/ecf/internal/provider/bittorrent/TorrentFileTransfer.java379
1 files changed, 193 insertions, 186 deletions
diff --git a/providers/bundles/org.eclipse.ecf.provider.bittorrent/src/org/eclipse/ecf/internal/provider/bittorrent/TorrentFileTransfer.java b/providers/bundles/org.eclipse.ecf.provider.bittorrent/src/org/eclipse/ecf/internal/provider/bittorrent/TorrentFileTransfer.java
index d90a4e968..3174b27c5 100644
--- a/providers/bundles/org.eclipse.ecf.provider.bittorrent/src/org/eclipse/ecf/internal/provider/bittorrent/TorrentFileTransfer.java
+++ b/providers/bundles/org.eclipse.ecf.provider.bittorrent/src/org/eclipse/ecf/internal/provider/bittorrent/TorrentFileTransfer.java
@@ -1,186 +1,193 @@
-/*******************************************************************************
- * 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.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 (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 (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 (IOException e) {
- notifyCompletion(e);
- return false;
- }
- }
-
- public boolean resume() {
- if (!paused || isDone()) {
- return false;
- }
-
- try {
- torrent.start();
- paused = false;
- return true;
- } catch (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;
- }
-
-}
+/*******************************************************************************
+ * 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.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 (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 (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 (IOException e) {
+ notifyCompletion(e);
+ return false;
+ }
+ }
+
+ public boolean resume() {
+ if (!paused || isDone()) {
+ return false;
+ }
+
+ try {
+ torrent.start();
+ paused = false;
+ return true;
+ } catch (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;
+ }
+
+}

Back to the top