Skip to main content
summaryrefslogtreecommitdiffstats
blob: f9005ee9d7cf8884181fb0f47af459cc809a6053 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/****************************************************************************
 * Copyright (c) 2004 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:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/
package org.eclipse.ecf.internal.provider.xmpp.filetransfer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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.filetransfer.FileTransferJob;
import org.eclipse.ecf.filetransfer.IFileTransferInfo;
import org.eclipse.ecf.filetransfer.IFileTransferListener;
import org.eclipse.ecf.filetransfer.IOutgoingFileTransfer;
import org.eclipse.ecf.filetransfer.events.IFileTransferEvent;
import org.eclipse.ecf.filetransfer.events.IOutgoingFileTransferResponseEvent;
import org.eclipse.ecf.filetransfer.events.IOutgoingFileTransferSendDataEvent;
import org.eclipse.ecf.filetransfer.events.IOutgoingFileTransferSendDoneEvent;
import org.eclipse.ecf.provider.xmpp.identity.XMPPID;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.filetransfer.FileTransfer;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
import org.jivesoftware.smackx.filetransfer.FileTransfer.Status;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer.NegotiationProgress;

public class XMPPOutgoingFileTransfer implements IOutgoingFileTransfer {

	private static final int BUFFER_SIZE = 4096;

	ID sessionID;
	XMPPID remoteTarget;
	IFileTransferInfo transferInfo;
	IFileTransferListener listener;
	FileTransferManager manager;

	File localFile;

	long fileSize;

	OutgoingFileTransfer outgoingFileTransfer;

	long amountWritten = 0;

	Status status;

	Exception exception;

	public XMPPOutgoingFileTransfer(FileTransferManager manager, XMPPID remoteTarget, IFileTransferInfo fileTransferInfo, IFileTransferListener listener) {
		this.manager = manager;
		this.remoteTarget = remoteTarget;
		this.transferInfo = fileTransferInfo;
		this.listener = listener;
		this.sessionID = createSessionID();
		final String fullyQualifiedName = remoteTarget.getFQName();
		outgoingFileTransfer = manager.createOutgoingFileTransfer(fullyQualifiedName);
	}

	private ID createSessionID() {
		try {
			return IDFactory.getDefault().createGUID();
		} catch (final IDCreateException e) {
			throw new NullPointerException("cannot create id for XMPPOutgoingFileTransfer");
		}
	}

	public synchronized ID getRemoteTargetID() {
		return remoteTarget;
	}

	public ID getID() {
		return sessionID;
	}

	private void fireTransferListenerEvent(IFileTransferEvent event) {
		listener.handleTransferEvent(event);
	}

	private void setStatus(Status s) {
		this.status = s;
	}

	private void setException(Exception e) {
		this.exception = e;
	}

	private Status getStatus() {
		return this.status;
	}

	NegotiationProgress progress = new NegotiationProgress();

	public synchronized void startSend(File localFile, String description) throws XMPPException {
		this.localFile = localFile;
		this.fileSize = localFile.length();
		setStatus(Status.INITIAL);
		// outgoingFileTransfer.sendFile(localFile, description);
		outgoingFileTransfer.sendFile(localFile.getAbsolutePath(), this.fileSize, description, progress);

		final Thread transferThread = new Thread(new Runnable() {
			public void run() {
				setStatus(outgoingFileTransfer.getStatus());
				boolean negotiation = true;
				while (negotiation) {
					// check the state of the progress
					try {
						Thread.sleep(300);
					} catch (final InterruptedException e) {
						return;
					}
					final Status s = progress.getStatus();
					setStatus(s);
					final boolean negotiated = getStatus().equals(Status.NEGOTIATED);
					if (s.equals(Status.NEGOTIATED) || s.equals(Status.CANCLED) || s.equals(Status.COMPLETE) || s.equals(Status.ERROR) || s.equals(Status.REFUSED)) {
						fireTransferListenerEvent(new IOutgoingFileTransferResponseEvent() {
							private static final long serialVersionUID = -5940612388464073240L;

							public boolean requestAccepted() {
								return negotiated;
							}

							public IOutgoingFileTransfer getSource() {
								return XMPPOutgoingFileTransfer.this;
							}

							public String toString() {
								final StringBuffer buf = new StringBuffer("OutgoingFileTransferResponseEvent[");
								buf.append("requestAccepted=").append(requestAccepted()).append("]");
								return buf.toString();
							}

							public void setFileTransferJob(FileTransferJob job) {
								// does nothing with this implementation
							}
						});
						// And negotiation is over
						negotiation = false;
					}
				}

				final OutputStream outs = progress.getOutputStream();

				if (outs == null)
					return;

				InputStream inputStream = null;

				try {
					inputStream = new FileInputStream(XMPPOutgoingFileTransfer.this.localFile);
					writeToStream(inputStream, outs);
				} catch (final FileNotFoundException e) {
					setStatus(FileTransfer.Status.ERROR);
					setException(e);
				} catch (final XMPPException e) {
					setStatus(FileTransfer.Status.ERROR);
					setException(e);
				} finally {
					setStatus(Status.COMPLETE);
					try {
						if (inputStream != null) {
							inputStream.close();
						}
					} catch (final IOException e) {
						/* Do Nothing */
					}
					try {
						outs.flush();
						outs.close();
					} catch (final IOException e) {
						/* Do Nothing */
					}
					// Then notify that the sending is done
					fireTransferListenerEvent(new IOutgoingFileTransferSendDoneEvent() {
						private static final long serialVersionUID = -6315336868737148845L;

						public IOutgoingFileTransfer getSource() {
							return XMPPOutgoingFileTransfer.this;
						}

						public String toString() {
							final StringBuffer buf = new StringBuffer("IOutgoingFileTransferSendDoneEvent[");
							buf.append("isDone=" + getSource().isDone());
							buf.append(";bytesSent=").append(getSource().getBytesSent()).append("]");
							return buf.toString();
						}

					});
				}
			}
		}, "ECF XMPP file send");

		transferThread.start();
	}

	public synchronized void cancel() {
		setStatus(Status.CANCLED);
	}

	public synchronized File getLocalFile() {
		return localFile;
	}

	public Object getAdapter(Class adapter) {
		return null;
	}

	public long getBytesSent() {
		return amountWritten;
	}

	public Exception getException() {
		return exception;
	}

	public double getPercentComplete() {
		return (fileSize <= 0) ? 1.0 : (((double) amountWritten) / ((double) fileSize));
	}

	public boolean isDone() {
		return status == Status.CANCLED || status == Status.ERROR || status == Status.COMPLETE;
	}

	public ID getSessionID() {
		return sessionID;
	}

	protected void writeToStream(final InputStream in, final OutputStream out) throws XMPPException {
		final byte[] b = new byte[BUFFER_SIZE];
		int count = 0;
		amountWritten = 0;

		do {
			try {
				out.write(b, 0, count);
			} catch (final IOException e) {
				throw new XMPPException("error writing to output stream", e);
			}

			amountWritten += count;

			if (count > 0) {
				fireTransferListenerEvent(new IOutgoingFileTransferSendDataEvent() {
					private static final long serialVersionUID = 2327297070577249812L;

					public IOutgoingFileTransfer getSource() {
						return XMPPOutgoingFileTransfer.this;
					}

					public String toString() {
						final StringBuffer buf = new StringBuffer("IOutgoingFileTransferSendDataEvent[");
						buf.append("bytesSent=").append(getSource().getBytesSent());
						buf.append(";percentComplete=").append(getSource().getPercentComplete()).append("]");
						return buf.toString();
					}

				});
			}
			// read more bytes from the input stream
			try {
				count = in.read(b);
			} catch (final IOException e) {
				throw new XMPPException("error reading from input stream", e);
			}
		} while (count != -1 && !getStatus().equals(Status.CANCLED));

		// the connection was likely terminated abrubtly if these are not equal
		if (!getStatus().equals(Status.CANCLED) && amountWritten != fileSize) {
			setStatus(Status.ERROR);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.filetransfer.IFileTransfer#getFileLength()
	 */
	public long getFileLength() {
		return fileSize;
	}

}

Back to the top