JingleInbandTransport.java

  1package eu.siacs.conversations.xmpp.jingle;
  2
  3import java.io.IOException;
  4import java.io.InputStream;
  5import java.io.OutputStream;
  6import java.security.MessageDigest;
  7import java.security.NoSuchAlgorithmException;
  8import java.util.Arrays;
  9
 10import android.util.Base64;
 11import android.util.Log;
 12
 13import eu.siacs.conversations.Config;
 14import eu.siacs.conversations.entities.Account;
 15import eu.siacs.conversations.entities.DownloadableFile;
 16import eu.siacs.conversations.persistance.FileBackend;
 17import eu.siacs.conversations.utils.CryptoHelper;
 18import eu.siacs.conversations.xml.Element;
 19import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 20import eu.siacs.conversations.xmpp.jid.Jid;
 21import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 22
 23public class JingleInbandTransport extends JingleTransport {
 24
 25	private Account account;
 26	private Jid counterpart;
 27	private int blockSize;
 28	private int bufferSize;
 29	private int seq = 0;
 30	private String sessionId;
 31
 32	private boolean established = false;
 33
 34	private boolean connected = true;
 35
 36	private DownloadableFile file;
 37	private JingleConnection connection;
 38
 39	private InputStream fileInputStream = null;
 40	private OutputStream fileOutputStream = null;
 41	private long remainingSize = 0;
 42	private long fileSize = 0;
 43	private MessageDigest digest;
 44
 45	private OnFileTransmissionStatusChanged onFileTransmissionStatusChanged;
 46
 47	private OnIqPacketReceived onAckReceived = new OnIqPacketReceived() {
 48		@Override
 49		public void onIqPacketReceived(Account account, IqPacket packet) {
 50			if (connected && packet.getType() == IqPacket.TYPE.RESULT) {
 51				sendNextBlock();
 52			}
 53		}
 54	};
 55
 56	public JingleInbandTransport(final JingleConnection connection, final String sid, final int blocksize) {
 57		this.connection = connection;
 58		this.account = connection.getAccount();
 59		this.counterpart = connection.getCounterPart();
 60		this.blockSize = blocksize;
 61		this.bufferSize = blocksize / 4;
 62		this.sessionId = sid;
 63	}
 64
 65	public void connect(final OnTransportConnected callback) {
 66		IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
 67		iq.setTo(this.counterpart);
 68		Element open = iq.addChild("open", "http://jabber.org/protocol/ibb");
 69		open.setAttribute("sid", this.sessionId);
 70		open.setAttribute("stanza", "iq");
 71		open.setAttribute("block-size", Integer.toString(this.blockSize));
 72		this.connected = true;
 73		this.account.getXmppConnection().sendIqPacket(iq,
 74				new OnIqPacketReceived() {
 75
 76					@Override
 77					public void onIqPacketReceived(Account account,
 78							IqPacket packet) {
 79						if (packet.getType() == IqPacket.TYPE.ERROR) {
 80							callback.failed();
 81						} else {
 82							callback.established();
 83						}
 84					}
 85				});
 86	}
 87
 88	@Override
 89	public void receive(DownloadableFile file,
 90			OnFileTransmissionStatusChanged callback) {
 91		this.onFileTransmissionStatusChanged = callback;
 92		this.file = file;
 93		try {
 94			this.digest = MessageDigest.getInstance("SHA-1");
 95			digest.reset();
 96			file.getParentFile().mkdirs();
 97			file.createNewFile();
 98			this.fileOutputStream = file.createOutputStream();
 99			if (this.fileOutputStream == null) {
100				Log.d(Config.LOGTAG,account.getJid().toBareJid()+": could not create output stream");
101				callback.onFileTransferAborted();
102				return;
103			}
104			this.remainingSize = this.fileSize = file.getExpectedSize();
105		} catch (final NoSuchAlgorithmException | IOException e) {
106			Log.d(Config.LOGTAG,account.getJid().toBareJid()+" "+e.getMessage());
107			callback.onFileTransferAborted();
108		}
109    }
110
111	@Override
112	public void send(DownloadableFile file,
113			OnFileTransmissionStatusChanged callback) {
114		this.onFileTransmissionStatusChanged = callback;
115		this.file = file;
116		try {
117			if (this.file.getKey() != null) {
118				this.remainingSize = (this.file.getSize() / 16 + 1) * 16;
119			} else {
120				this.remainingSize = this.file.getSize();
121			}
122			this.fileSize = this.remainingSize;
123			this.digest = MessageDigest.getInstance("SHA-1");
124			this.digest.reset();
125			fileInputStream = this.file.createInputStream();
126			if (fileInputStream == null) {
127				Log.d(Config.LOGTAG,account.getJid().toBareJid()+": could no create input stream");
128				callback.onFileTransferAborted();
129				return;
130			}
131			if (this.connected) {
132				this.sendNextBlock();
133			}
134		} catch (NoSuchAlgorithmException e) {
135			callback.onFileTransferAborted();
136			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": "+e.getMessage());
137		}
138	}
139
140	@Override
141	public void disconnect() {
142		this.connected = false;
143		if (this.fileOutputStream != null) {
144			try {
145				this.fileOutputStream.close();
146			} catch (IOException e) {
147
148			}
149		}
150		if (this.fileInputStream != null) {
151			try {
152				this.fileInputStream.close();
153			} catch (IOException e) {
154
155			}
156		}
157	}
158
159	private void sendNextBlock() {
160		byte[] buffer = new byte[this.bufferSize];
161		try {
162			int count = fileInputStream.read(buffer);
163			this.remainingSize -= count;
164			if (count != buffer.length && count != -1) {
165				int rem = fileInputStream.read(buffer,count,buffer.length-count);
166				if (rem > 0) {
167					count += rem;
168				}
169			}
170			this.digest.update(buffer,0,count);
171			String base64 = Base64.encodeToString(buffer,0,count, Base64.NO_WRAP);
172			IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
173			iq.setTo(this.counterpart);
174			Element data = iq.addChild("data", "http://jabber.org/protocol/ibb");
175			data.setAttribute("seq", Integer.toString(this.seq));
176			data.setAttribute("block-size", Integer.toString(this.blockSize));
177			data.setAttribute("sid", this.sessionId);
178			data.setContent(base64);
179			this.account.getXmppConnection().sendIqPacket(iq, this.onAckReceived);
180			this.seq++;
181			if (this.remainingSize > 0) {
182				connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100));
183			} else {
184				file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
185				this.onFileTransmissionStatusChanged.onFileTransmitted(file);
186				fileInputStream.close();
187			}
188		} catch (IOException e) {
189			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": "+e.getMessage());
190			FileBackend.close(fileInputStream);
191			this.onFileTransmissionStatusChanged.onFileTransferAborted();
192		}
193	}
194
195	private void receiveNextBlock(String data) {
196		try {
197			byte[] buffer = Base64.decode(data, Base64.NO_WRAP);
198			if (this.remainingSize < buffer.length) {
199				buffer = Arrays.copyOfRange(buffer, 0, (int) this.remainingSize);
200			}
201			this.remainingSize -= buffer.length;
202			this.fileOutputStream.write(buffer);
203			this.digest.update(buffer);
204			if (this.remainingSize <= 0) {
205				file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
206				fileOutputStream.flush();
207				fileOutputStream.close();
208				this.onFileTransmissionStatusChanged.onFileTransmitted(file);
209			} else {
210				connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100));
211			}
212		} catch (IOException e) {
213			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": "+e.getMessage());
214			FileBackend.close(fileOutputStream);
215			this.onFileTransmissionStatusChanged.onFileTransferAborted();
216		}
217	}
218
219	public void deliverPayload(IqPacket packet, Element payload) {
220		if (payload.getName().equals("open")) {
221			if (!established) {
222				established = true;
223				connected = true;
224				this.receiveNextBlock("");
225				this.account.getXmppConnection().sendIqPacket(
226						packet.generateResponse(IqPacket.TYPE.RESULT), null);
227			} else {
228				this.account.getXmppConnection().sendIqPacket(
229						packet.generateResponse(IqPacket.TYPE.ERROR), null);
230			}
231		} else if (connected && payload.getName().equals("data")) {
232			this.receiveNextBlock(payload.getContent());
233			this.account.getXmppConnection().sendIqPacket(
234					packet.generateResponse(IqPacket.TYPE.RESULT), null);
235		} else {
236			// TODO some sort of exception
237		}
238	}
239}