transmitting files between two conversations works. no error handling and no ui on the receiving end

This commit is contained in:
Daniel Gultsch 2014-04-13 21:10:36 +02:00
parent 7dfe4ae082
commit 084ab51b1d
6 changed files with 136 additions and 36 deletions

View File

@ -10,7 +10,6 @@ import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
import android.util.LruCache; import android.util.LruCache;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
@ -81,7 +80,7 @@ public class FileBackend {
Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE); Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE);
boolean success = scalledBitmap.compress(Bitmap.CompressFormat.WEBP,75,os); boolean success = scalledBitmap.compress(Bitmap.CompressFormat.WEBP,75,os);
if (!success) { if (!success) {
Log.d("xmppService", "couldnt compress"); //Log.d("xmppService", "couldnt compress");
} }
os.close(); os.close();
return file; return file;
@ -104,7 +103,6 @@ public class FileBackend {
public Bitmap getThumbnailFromMessage(Message message, int size) { public Bitmap getThumbnailFromMessage(Message message, int size) {
Bitmap thumbnail = thumbnailCache.get(message.getUuid()); Bitmap thumbnail = thumbnailCache.get(message.getUuid());
if (thumbnail==null) { if (thumbnail==null) {
Log.d("xmppService","creating new thumbnail" + message.getUuid());
Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message) Bitmap fullsize = BitmapFactory.decodeFile(getJingleFile(message)
.getAbsolutePath()); .getAbsolutePath());
thumbnail = resize(fullsize, size); thumbnail = resize(fullsize, size);

View File

@ -1059,12 +1059,10 @@ public class XmppConnectionService extends Service {
OnConversationListChangedListener listener) { OnConversationListChangedListener listener) {
this.convChangedListener = listener; this.convChangedListener = listener;
this.convChangedListenerCount++; this.convChangedListenerCount++;
Log.d(LOGTAG,"registered on conv changed in backend ("+convChangedListenerCount+")");
} }
public void removeOnConversationListChangedListener() { public void removeOnConversationListChangedListener() {
this.convChangedListenerCount--; this.convChangedListenerCount--;
Log.d(LOGTAG,"someone on conv changed listener removed listener ("+convChangedListenerCount+")");
if (this.convChangedListenerCount==0) { if (this.convChangedListenerCount==0) {
this.convChangedListener = null; this.convChangedListener = null;
} }

View File

@ -29,6 +29,7 @@ public class JingleConnection {
public static final int STATUS_TERMINATED = 2; public static final int STATUS_TERMINATED = 2;
public static final int STATUS_CANCELED = 3; public static final int STATUS_CANCELED = 3;
public static final int STATUS_FINISHED = 4; public static final int STATUS_FINISHED = 4;
public static final int STATUS_TRANSMITTING = 5;
public static final int STATUS_FAILED = 99; public static final int STATUS_FAILED = 99;
private int status = -1; private int status = -1;
@ -64,7 +65,7 @@ public class JingleConnection {
} }
public String getAccountJid() { public String getAccountJid() {
return this.account.getJid(); return this.account.getFullJid();
} }
public String getCounterPart() { public String getCounterPart() {
@ -113,6 +114,7 @@ public class JingleConnection {
} }
public void init(Account account, JinglePacket packet) { public void init(Account account, JinglePacket packet) {
this.status = STATUS_INITIATED;
Conversation conversation = this.mXmppConnectionService.findOrCreateConversation(account, packet.getFrom().split("/")[0], false); Conversation conversation = this.mXmppConnectionService.findOrCreateConversation(account, packet.getFrom().split("/")[0], false);
this.message = new Message(conversation, "receiving image file", Message.ENCRYPTION_NONE); this.message = new Message(conversation, "receiving image file", Message.ENCRYPTION_NONE);
this.message.setType(Message.TYPE_IMAGE); this.message.setType(Message.TYPE_IMAGE);
@ -140,6 +142,7 @@ public class JingleConnection {
} else { } else {
Log.d("xmppService","no file offer was attached. aborting"); Log.d("xmppService","no file offer was attached. aborting");
} }
Log.d("xmppService","session Id "+getSessionId());
} }
private void sendInitRequest() { private void sendInitRequest() {
@ -172,13 +175,12 @@ public class JingleConnection {
JinglePacket packet = bootstrapPacket(); JinglePacket packet = bootstrapPacket();
packet.setAction("session-accept"); packet.setAction("session-accept");
packet.setContent(content); packet.setContent(content);
Log.d("xmppService","sending session accept: "+packet.toString());
account.getXmppConnection().sendIqPacket(packet, new OnIqPacketReceived() { account.getXmppConnection().sendIqPacket(packet, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() != IqPacket.TYPE_ERROR) { if (packet.getType() != IqPacket.TYPE_ERROR) {
Log.d("xmppService","opsing side has acked our session-accept"); status = STATUS_ACCEPTED;
connectWithCandidates(); connectWithCandidates();
} }
} }
@ -209,20 +211,31 @@ public class JingleConnection {
private void transportInfo(JinglePacket packet) { private void transportInfo(JinglePacket packet) {
Content content = packet.getJingleContent(); Content content = packet.getJingleContent();
Log.d("xmppService","transport info : "+content.toString());
String cid = content.getUsedCandidate(); String cid = content.getUsedCandidate();
IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
if (cid!=null) { if (cid!=null) {
Log.d("xmppService","candidate used by counterpart:"+cid); Log.d("xmppService","candidate used by counterpart:"+cid);
this.candidatesUsedByCounterpart.add(cid); this.candidatesUsedByCounterpart.add(cid);
if (this.connections.containsKey(cid)) { if (this.connections.containsKey(cid)) {
this.connect(this.connections.get(cid)); SocksConnection connection = this.connections.get(cid);
if (connection.isEstablished()) {
if (status!=STATUS_TRANSMITTING) {
this.connect(connection);
} else {
Log.d("xmppService","ignoring canditate used because we are already transmitting");
}
} else {
Log.d("xmppService","not yet connected. check when callback comes back");
}
} else {
Log.d("xmppService","candidate not yet in list of connections");
} }
} }
IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
account.getXmppConnection().sendIqPacket(response, null); account.getXmppConnection().sendIqPacket(response, null);
} }
private void connect(final SocksConnection connection) { private void connect(final SocksConnection connection) {
this.status = STATUS_TRANSMITTING;
final OnFileTransmitted callback = new OnFileTransmitted() { final OnFileTransmitted callback = new OnFileTransmitted() {
@Override @Override
@ -230,12 +243,12 @@ public class JingleConnection {
Log.d("xmppService","sucessfully transmitted file. sha1:"+file.getSha1Sum()); Log.d("xmppService","sucessfully transmitted file. sha1:"+file.getSha1Sum());
} }
}; };
if (connection.isProxy()) { if ((connection.isProxy()&&(connection.getCid().equals(mJingleConnectionManager.getPrimaryCandidateId(account))))) {
Log.d("xmppService","candidate "+connection.getCid()+" was our proxy and needs activation");
IqPacket activation = new IqPacket(IqPacket.TYPE_SET); IqPacket activation = new IqPacket(IqPacket.TYPE_SET);
activation.setTo(connection.getJid()); activation.setTo(connection.getJid());
activation.query("http://jabber.org/protocol/bytestreams").setAttribute("sid", this.getSessionId()); activation.query("http://jabber.org/protocol/bytestreams").setAttribute("sid", this.getSessionId());
activation.query().addChild("activate").setContent(this.getResponder()); activation.query().addChild("activate").setContent(this.getResponder());
Log.d("xmppService","connection is proxy. need to activate "+activation.toString());
this.account.getXmppConnection().sendIqPacket(activation, new OnIqPacketReceived() { this.account.getXmppConnection().sendIqPacket(activation, new OnIqPacketReceived() {
@Override @Override
@ -245,9 +258,9 @@ public class JingleConnection {
Log.d("xmppService","we were initiating. sending file"); Log.d("xmppService","we were initiating. sending file");
connection.send(file,callback); connection.send(file,callback);
} else { } else {
connection.receive(file,callback);
Log.d("xmppService","we were responding. receiving file"); Log.d("xmppService","we were responding. receiving file");
} }
} }
}); });
} else { } else {
@ -256,6 +269,7 @@ public class JingleConnection {
connection.send(file,callback); connection.send(file,callback);
} else { } else {
Log.d("xmppService","we were responding. receiving file"); Log.d("xmppService","we were responding. receiving file");
connection.receive(file,callback);
} }
} }
} }
@ -273,14 +287,9 @@ public class JingleConnection {
} }
private void connectWithCandidates() { private void connectWithCandidates() {
for(Element canditate : this.candidates) { for(Element candidate : this.candidates) {
final SocksConnection socksConnection = new SocksConnection(this,candidate);
String host = canditate.getAttribute("host"); connections.put(socksConnection.getCid(), socksConnection);
int port = Integer.parseInt(canditate.getAttribute("port"));
String type = canditate.getAttribute("type");
String jid = canditate.getAttribute("jid");
SocksConnection socksConnection = new SocksConnection(this, host, jid, port,type);
connections.put(canditate.getAttribute("cid"), socksConnection);
socksConnection.connect(new OnSocksConnection() { socksConnection.connect(new OnSocksConnection() {
@Override @Override
@ -290,7 +299,15 @@ public class JingleConnection {
@Override @Override
public void established() { public void established() {
Log.d("xmppService","established socks5"); if (candidatesUsedByCounterpart.contains(socksConnection.getCid())) {
if (status!=STATUS_TRANSMITTING) {
connect(socksConnection);
} else {
Log.d("xmppService","ignoring cuz already transmitting");
}
} else {
sendCandidateUsed(socksConnection.getCid());
}
} }
}); });
} }
@ -306,7 +323,13 @@ public class JingleConnection {
} }
private void sendCandidateUsed(String cid) { private void sendCandidateUsed(String cid) {
JinglePacket packet = bootstrapPacket();
packet.setAction("transport-info");
Content content = new Content();
content.setUsedCandidate(this.content.getTransportId(), cid);
packet.setContent(content);
Log.d("xmppService","send using candidate: "+packet.toString());
this.account.getXmppConnection().sendIqPacket(packet, responseListener);
} }
public String getInitiator() { public String getInitiator() {

View File

@ -35,13 +35,16 @@ public class JingleConnectionManager {
if (packet.isAction("session-initiate")) { if (packet.isAction("session-initiate")) {
JingleConnection connection = new JingleConnection(this); JingleConnection connection = new JingleConnection(this);
connection.init(account,packet); connection.init(account,packet);
connections.add(connection);
} else { } else {
for (JingleConnection connection : connections) { for (JingleConnection connection : connections) {
if (connection.getAccountJid().equals(account.getJid()) && connection if (connection.getAccountJid().equals(account.getFullJid()) && connection
.getSessionId().equals(packet.getSessionId()) && connection .getSessionId().equals(packet.getSessionId()) && connection
.getCounterPart().equals(packet.getFrom())) { .getCounterPart().equals(packet.getFrom())) {
connection.deliverPacket(packet); connection.deliverPacket(packet);
return; return;
} else {
Log.d("xmppService","no match sid:"+connection.getSessionId()+"="+packet.getSessionId()+" counterpart:"+connection.getCounterPart()+"="+packet.getFrom()+" account:"+connection.getAccountJid()+"="+packet.getTo());
} }
} }
Log.d("xmppService","delivering packet failed "+packet.toString()); Log.d("xmppService","delivering packet failed "+packet.toString());
@ -118,6 +121,14 @@ public class JingleConnectionManager {
this.primaryCandidates.get(account.getJid())); this.primaryCandidates.get(account.getJid()));
} }
} }
public String getPrimaryCandidateId(Account account) {
if (this.primaryCandidates.containsKey(account.getJid())) {
return this.primaryCandidates.get(account.getJid()).getAttribute("cid");
} else {
return null;
}
}
public String nextRandomId() { public String nextRandomId() {
return new BigInteger(50, random).toString(32); return new BigInteger(50, random).toString(32);

View File

@ -2,6 +2,7 @@ package eu.siacs.conversations.xmpp.jingle;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -12,27 +13,29 @@ import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.xml.Element;
import android.util.Log; import android.util.Log;
import android.widget.Button;
public class SocksConnection { public class SocksConnection {
private JingleConnection jingleConnection;
private Socket socket; private Socket socket;
private String host; private String host;
private String jid; private String jid;
private String cid;
private int port; private int port;
private boolean isProxy = false; private boolean isProxy = false;
private String destination; private String destination;
private OutputStream outputStream; private OutputStream outputStream;
private InputStream inputStream;
private boolean isEstablished = false; private boolean isEstablished = false;
public SocksConnection(JingleConnection jingleConnection, String host, public SocksConnection(JingleConnection jingleConnection, Element candidate) {
String jid, int port, String type) { this.cid = candidate.getAttribute("cid");
this.jingleConnection = jingleConnection; this.host = candidate.getAttribute("host");
this.host = host; this.port = Integer.parseInt(candidate.getAttribute("port"));
this.jid = jid; String type = candidate.getAttribute("type");
this.port = port; this.jid = candidate.getAttribute("jid");
this.isProxy = "proxy".equalsIgnoreCase(type); this.isProxy = "proxy".equalsIgnoreCase(type);
try { try {
MessageDigest mDigest = MessageDigest.getInstance("SHA-1"); MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
@ -55,19 +58,19 @@ public class SocksConnection {
public void run() { public void run() {
try { try {
socket = new Socket(host, port); socket = new Socket(host, port);
InputStream is = socket.getInputStream(); inputStream = socket.getInputStream();
outputStream = socket.getOutputStream(); outputStream = socket.getOutputStream();
byte[] login = { 0x05, 0x01, 0x00 }; byte[] login = { 0x05, 0x01, 0x00 };
byte[] expectedReply = { 0x05, 0x00 }; byte[] expectedReply = { 0x05, 0x00 };
byte[] reply = new byte[2]; byte[] reply = new byte[2];
outputStream.write(login); outputStream.write(login);
is.read(reply); inputStream.read(reply);
if (Arrays.equals(reply, expectedReply)) { if (Arrays.equals(reply, expectedReply)) {
String connect = "" + '\u0005' + '\u0001' + '\u0000' + '\u0003' String connect = "" + '\u0005' + '\u0001' + '\u0000' + '\u0003'
+ '\u0028' + destination + '\u0000' + '\u0000'; + '\u0028' + destination + '\u0000' + '\u0000';
outputStream.write(connect.getBytes()); outputStream.write(connect.getBytes());
byte[] result = new byte[2]; byte[] result = new byte[2];
is.read(result); inputStream.read(result);
int status = result[1]; int status = result[1];
if (status == 0) { if (status == 0) {
Log.d("xmppService", "established connection with "+host + ":" + port Log.d("xmppService", "established connection with "+host + ":" + port
@ -135,6 +138,50 @@ public class SocksConnection {
}).start(); }).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() { public boolean isProxy() {
return this.isProxy; return this.isProxy;
@ -143,6 +190,10 @@ public class SocksConnection {
public String getJid() { public String getJid() {
return this.jid; return this.jid;
} }
public String getCid() {
return this.cid;
}
public void disconnect() { public void disconnect() {
if (this.socket!=null) { if (this.socket!=null) {

View File

@ -56,6 +56,14 @@ public class Content extends Element {
} }
} }
public String getTransportId() {
Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1");
if (transport==null) {
return null;
}
return transport.getAttribute("sid");
}
public String getUsedCandidate() { public String getUsedCandidate() {
Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1"); Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1");
if (transport==null) { if (transport==null) {
@ -68,6 +76,17 @@ public class Content extends Element {
return usedCandidate.getAttribute("cid"); return usedCandidate.getAttribute("cid");
} }
} }
public void setUsedCandidate(String transportId, String cid) {
Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1");
if (transport==null) {
transport = this.addChild("transport", "urn:xmpp:jingle:transports:s5b:1");
}
transport.setAttribute("sid", transportId);
transport.clearChildren();
Element usedCandidate = transport.addChild("candidate-used");
usedCandidate.setAttribute("cid",cid);
}
public void addCandidate(Element candidate) { public void addCandidate(Element candidate) {
Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1"); Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1");