diff options
author | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-04-13 21:10:36 +0200 |
---|---|---|
committer | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-04-13 21:10:36 +0200 |
commit | 084ab51b1d7625cdf1cca4cc6a31715acffe5ce7 (patch) | |
tree | bd1ab4b6d48aaafd4e410b50d7f5f3d7fd8e5f14 /src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java | |
parent | 7dfe4ae0823238da216088f80de40b9ff6b1446d (diff) |
transmitting files between two conversations works. no error handling and no ui on the receiving end
Diffstat (limited to 'src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java')
-rw-r--r-- | src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java | 73 |
1 files changed, 62 insertions, 11 deletions
diff --git a/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java b/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java index d4cb432a..bf7c87ad 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java +++ b/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java @@ -2,6 +2,7 @@ package eu.siacs.conversations.xmpp.jingle; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -12,27 +13,29 @@ import java.security.NoSuchAlgorithmException; import java.util.Arrays; import eu.siacs.conversations.utils.CryptoHelper; +import eu.siacs.conversations.xml.Element; import android.util.Log; +import android.widget.Button; public class SocksConnection { - - private JingleConnection jingleConnection; private Socket socket; private String host; private String jid; + private String cid; private int port; private boolean isProxy = false; private String destination; private OutputStream outputStream; + private InputStream inputStream; private boolean isEstablished = false; - public SocksConnection(JingleConnection jingleConnection, String host, - String jid, int port, String type) { - this.jingleConnection = jingleConnection; - this.host = host; - this.jid = jid; - this.port = port; + public SocksConnection(JingleConnection jingleConnection, Element candidate) { + this.cid = candidate.getAttribute("cid"); + this.host = candidate.getAttribute("host"); + this.port = Integer.parseInt(candidate.getAttribute("port")); + String type = candidate.getAttribute("type"); + this.jid = candidate.getAttribute("jid"); this.isProxy = "proxy".equalsIgnoreCase(type); try { MessageDigest mDigest = MessageDigest.getInstance("SHA-1"); @@ -55,19 +58,19 @@ public class SocksConnection { public void run() { try { socket = new Socket(host, port); - InputStream is = socket.getInputStream(); + inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); byte[] login = { 0x05, 0x01, 0x00 }; byte[] expectedReply = { 0x05, 0x00 }; byte[] reply = new byte[2]; outputStream.write(login); - is.read(reply); + inputStream.read(reply); if (Arrays.equals(reply, expectedReply)) { String connect = "" + '\u0005' + '\u0001' + '\u0000' + '\u0003' + '\u0028' + destination + '\u0000' + '\u0000'; outputStream.write(connect.getBytes()); byte[] result = new byte[2]; - is.read(result); + inputStream.read(result); int status = result[1]; if (status == 0) { Log.d("xmppService", "established connection with "+host + ":" + port @@ -135,6 +138,50 @@ public class SocksConnection { }).start(); } + + public void receive(final JingleFile file, final OnFileTransmitted callback) { + new Thread(new Runnable() { + + @Override + public void run() { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + digest.reset(); + inputStream.skip(45); + file.getParentFile().mkdirs(); + file.createNewFile(); + FileOutputStream fileOutputStream = new FileOutputStream(file); + long remainingSize = file.getExpectedSize(); + byte[] buffer = new byte[8192]; + int count = buffer.length; + while(remainingSize > 0) { + Log.d("xmppService","remaning size:"+remainingSize); + if (remainingSize<=count) { + count = (int) remainingSize; + } + count = inputStream.read(buffer, 0, count); + fileOutputStream.write(buffer, 0, count); + digest.update(buffer, 0, count); + remainingSize-=count; + } + fileOutputStream.flush(); + fileOutputStream.close(); + file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest())); + Log.d("xmppService","transmitted filename was: "+file.getAbsolutePath()); + callback.onFileTransmitted(file); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }).start(); + } public boolean isProxy() { return this.isProxy; @@ -143,6 +190,10 @@ public class SocksConnection { public String getJid() { return this.jid; } + + public String getCid() { + return this.cid; + } public void disconnect() { if (this.socket!=null) { |