Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b6ac8f895856d092890d541740b945ecfde2ea7 (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
/*******************************************************************************
 * Copyright (c) 2006, 2009 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.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.ecf.core.ContainerConnectException;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.IContainerListener;
import org.eclipse.ecf.core.events.ContainerDisposeEvent;
import org.eclipse.ecf.core.events.IContainerEvent;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDCreateException;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.core.util.Proxy;
import org.eclipse.ecf.filetransfer.FileTransferJob;
import org.eclipse.ecf.filetransfer.IFileRangeSpecification;
import org.eclipse.ecf.filetransfer.IFileTransferListener;
import org.eclipse.ecf.filetransfer.IIncomingFileTransfer;
import org.eclipse.ecf.filetransfer.IncomingFileTransferException;
import org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException;
import org.eclipse.ecf.filetransfer.UserCancelledException;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent;
import org.eclipse.ecf.filetransfer.identity.IFileID;
import org.eclipse.ecf.filetransfer.service.IRetrieveFileTransfer;
import org.eclipse.ecf.protocol.bittorrent.Torrent;
import org.eclipse.ecf.protocol.bittorrent.TorrentFactory;
import org.eclipse.ecf.protocol.bittorrent.TorrentFile;

public final class BitTorrentContainer implements IContainer, IRetrieveFileTransfer {

	private final List containerListeners;

	private final ID id;

	public BitTorrentContainer() throws IDCreateException {
		id = IDFactory.getDefault().createGUID();
		containerListeners = new ArrayList();
		BitTorrentProviderPlugin.getDefault().setConfigurationPath();
	}

	public void connect(ID targetID, IConnectContext connectContext) throws ContainerConnectException {
		// do nothing since BitTorrent does not have a central network
	}

	public void disconnect() {
		// do nothing since BitTorrent does not have a central network
	}

	public void dispose() {
		fireContainerEvent(new ContainerDisposeEvent(id));
	}

	private void fireContainerEvent(IContainerEvent event) {
		synchronized (containerListeners) {
			for (int i = 0; i < containerListeners.size(); i++) {
				final IContainerListener icl = (IContainerListener) containerListeners.get(i);
				icl.handleEvent(event);
			}
		}
	}

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

	public Namespace getConnectNamespace() {
		return null;
	}

	public ID getConnectedID() {
		return null;
	}

	public ID getID() {
		return id;
	}

	public void addListener(IContainerListener listener) {
		synchronized (containerListeners) {
			if (!containerListeners.contains(listener)) {
				containerListeners.add(listener);
			}
		}
	}

	public void removeListener(IContainerListener listener) {
		synchronized (containerListeners) {
			containerListeners.remove(listener);
		}
	}

	public void sendRetrieveRequest(final IFileID remoteFileReference, final IFileTransferListener transferListener, Map options) throws IncomingFileTransferException {
		Assert.isNotNull(remoteFileReference, "remoteFileReference cannot be null"); //$NON-NLS-1$
		Assert.isLegal(remoteFileReference instanceof TorrentID, "remoteFileReference must be instanceof TorrentID"); //$NON-NLS-1$
		Assert.isNotNull(transferListener, "transferListener cannot be null"); //$NON-NLS-1$

		transferListener.handleTransferEvent(new IIncomingFileTransferReceiveStartEvent() {

			private IIncomingFileTransfer transfer;

			private boolean cancelled = false;

			public IIncomingFileTransfer receive(File localFileToSave) throws IOException {
				if (cancelled) {
					throw new RuntimeException(new UserCancelledException());
				}

				Assert.isNotNull(localFileToSave, "localFileToSave cannot be null"); //$NON-NLS-1$

				if (localFileToSave.exists() && !localFileToSave.canWrite()) {
					throw new IOException("file="+localFileToSave.getAbsolutePath()+" does not exist or cannot be written to"); //$NON-NLS-1$ //$NON-NLS-2$
				}

				final TorrentFile file = new TorrentFile(((TorrentID) remoteFileReference).getFile());
				file.setTargetFile(localFileToSave);
				final Torrent torrent = TorrentFactory.createTorrent(file);
				transfer = new TorrentFileTransfer(remoteFileReference, transferListener, torrent);
				return transfer;
			}

			public IIncomingFileTransfer receive(File localFileToSave, FileTransferJob fileTransferJob) throws IOException {
				if (cancelled) {
					throw new RuntimeException(new UserCancelledException());
				}

				Assert.isNotNull(localFileToSave, "localFileToSave must not be null"); //$NON-NLS-1$

				if (localFileToSave.exists() && !localFileToSave.canWrite()) {
					throw new IOException("file="+localFileToSave.getAbsolutePath()+" does not exist or cannot be written to"); //$NON-NLS-1$ //$NON-NLS-2$
				}

				final TorrentFile file = new TorrentFile(((TorrentID) remoteFileReference).getFile());
				file.setTargetFile(localFileToSave);
				final Torrent torrent = TorrentFactory.createTorrent(file);
				transfer = new TorrentFileTransfer(remoteFileReference, transferListener, torrent);
				return transfer;
			}

			public void cancel() {
				if (transfer != null) {
					transfer.cancel();
					cancelled = true;
				}
			}

			public IFileID getFileID() {
				return remoteFileReference;
			}

			public IIncomingFileTransfer receive(OutputStream streamToStore) throws IOException {
				throw new UnsupportedOperationException("receive(OutputStream) not supported by this provider");
			}

			public IIncomingFileTransfer receive(OutputStream streamToStore, FileTransferJob fileTransferJob) throws IOException {
				throw new UnsupportedOperationException("receive(OutputStream,FileTransferJob) not supported by this provider");
			}

			public IIncomingFileTransfer getSource() {
				return transfer;
			}

			public Map getResponseHeaders() {
				return null;
			}

		});
	}

	public Namespace getRetrieveNamespace() {
		return IDFactory.getDefault().getNamespaceByName(BitTorrentProviderPlugin.NAMESPACE_ID);
	}

	public void setConnectContextForAuthentication(IConnectContext connectContext) {
	}

	public void setProxy(Proxy proxy) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.filetransfer.IRetrieveFileTransferContainerAdapter#sendRetrieveRequest(org.eclipse.ecf.filetransfer.identity.IFileID, org.eclipse.ecf.filetransfer.IFileRangeSpecification, org.eclipse.ecf.filetransfer.IFileTransferListener, java.util.Map)
	 */
	public void sendRetrieveRequest(IFileID remoteFileID, IFileRangeSpecification rangeSpecification, IFileTransferListener transferListener, Map options) throws IncomingFileTransferException {
		// TODO Auto-generated method stub
		throw new InvalidFileRangeSpecificationException("not supported", rangeSpecification);
	}
}

Back to the top