From 0a48f777acefa29443743f68f242136309169ba2 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 9 Jan 2015 14:42:58 +0100 Subject: [PATCH] code cleanup for jingle proxy discovery --- .../eu/siacs/conversations/utils/Xmlns.java | 1 + .../xmpp/jingle/JingleCandidate.java | 3 +- .../xmpp/jingle/JingleConnection.java | 91 ++++++++----------- .../xmpp/jingle/JingleConnectionManager.java | 76 +++++++--------- 4 files changed, 72 insertions(+), 99 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/utils/Xmlns.java b/src/main/java/eu/siacs/conversations/utils/Xmlns.java index 67de7c79e..17fd2d261 100644 --- a/src/main/java/eu/siacs/conversations/utils/Xmlns.java +++ b/src/main/java/eu/siacs/conversations/utils/Xmlns.java @@ -4,4 +4,5 @@ public final class Xmlns { public static final String BLOCKING = "urn:xmpp:blocking"; public static final String ROSTER = "jabber:iq:roster"; public static final String REGISTER = "jabber:iq:register"; + public static final String BYTE_STREAMS = "http://jabber.org/protocol/bytestreams"; } diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java index 281ea3ca3..dcadb92f1 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleCandidate.java @@ -85,8 +85,7 @@ public class JingleCandidate { } public boolean equalValues(JingleCandidate other) { - return other.getHost().equals(this.getHost()) - && (other.getPort() == this.getPort()); + return other != null && other.getHost().equals(this.getHost()) && (other.getPort() == this.getPort()); } public boolean isOurs() { diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index fe37596c7..cea4346c4 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -396,58 +396,48 @@ public class JingleConnection implements Downloadable { mJingleStatus = JINGLE_STATUS_ACCEPTED; this.mStatus = Downloadable.STATUS_DOWNLOADING; mXmppConnectionService.updateConversationUi(); - this.mJingleConnectionManager.getPrimaryCandidate(this.account, - new OnPrimaryCandidateFound() { + this.mJingleConnectionManager.getPrimaryCandidate(this.account, new OnPrimaryCandidateFound() { + @Override + public void onPrimaryCandidateFound(boolean success, final JingleCandidate candidate) { + final JinglePacket packet = bootstrapPacket("session-accept"); + final Content content = new Content(contentCreator,contentName); + content.setFileOffer(fileOffer); + content.setTransportId(transportId); + if (success && candidate != null && !equalCandidateExists(candidate)) { + final JingleSocks5Transport socksConnection = new JingleSocks5Transport( + JingleConnection.this, + candidate); + connections.put(candidate.getCid(), socksConnection); + socksConnection.connect(new OnTransportConnected() { - @Override - public void onPrimaryCandidateFound(boolean success, - final JingleCandidate candidate) { - final JinglePacket packet = bootstrapPacket("session-accept"); - final Content content = new Content(contentCreator, - contentName); - content.setFileOffer(fileOffer); - content.setTransportId(transportId); - if ((success) && (!equalCandidateExists(candidate))) { - final JingleSocks5Transport socksConnection = new JingleSocks5Transport( - JingleConnection.this, candidate); - connections.put(candidate.getCid(), socksConnection); - socksConnection.connect(new OnTransportConnected() { - - @Override - public void failed() { - Log.d(Config.LOGTAG, - "connection to our own primary candidate failed"); - content.socks5transport().setChildren( - getCandidatesAsElements()); - packet.setContent(content); - sendJinglePacket(packet); - connectNextCandidate(); - } - - @Override - public void established() { - Log.d(Config.LOGTAG, - "connected to primary candidate"); - mergeCandidate(candidate); - content.socks5transport().setChildren( - getCandidatesAsElements()); - packet.setContent(content); - sendJinglePacket(packet); - connectNextCandidate(); - } - }); - } else { - Log.d(Config.LOGTAG, - "did not find a primary candidate for ourself"); - content.socks5transport().setChildren( - getCandidatesAsElements()); + @Override + public void failed() { + Log.d(Config.LOGTAG,"connection to our own primary candidate failed"); + content.socks5transport().setChildren(getCandidatesAsElements()); packet.setContent(content); sendJinglePacket(packet); connectNextCandidate(); } - } - }); + @Override + public void established() { + Log.d(Config.LOGTAG, "connected to primary candidate"); + mergeCandidate(candidate); + content.socks5transport().setChildren(getCandidatesAsElements()); + packet.setContent(content); + sendJinglePacket(packet); + connectNextCandidate(); + } + }); + } else { + Log.d(Config.LOGTAG,"did not find a primary candidate for ourself"); + content.socks5transport().setChildren(getCandidatesAsElements()); + packet.setContent(content); + sendJinglePacket(packet); + connectNextCandidate(); + } + } + }); } private JinglePacket bootstrapPacket(String action) { @@ -479,16 +469,13 @@ public class JingleConnection implements Downloadable { Content content = packet.getJingleContent(); if (content.hasSocks5Transport()) { if (content.socks5transport().hasChild("activated")) { - if ((this.transport != null) - && (this.transport instanceof JingleSocks5Transport)) { + if ((this.transport != null) && (this.transport instanceof JingleSocks5Transport)) { onProxyActivated.success(); } else { - String cid = content.socks5transport() - .findChild("activated").getAttribute("cid"); + String cid = content.socks5transport().findChild("activated").getAttribute("cid"); Log.d(Config.LOGTAG, "received proxy activated (" + cid + ")prior to choosing our own transport"); - JingleSocks5Transport connection = this.connections - .get(cid); + JingleSocks5Transport connection = this.connections.get(cid); if (connection != null) { connection.setActivated(true); } else { diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java index 57c74ad9f..5dfa3ff40 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java @@ -12,6 +12,7 @@ import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.services.AbstractConnectionManager; import eu.siacs.conversations.services.XmppConnectionService; +import eu.siacs.conversations.utils.Xmlns; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.OnIqPacketReceived; import eu.siacs.conversations.xmpp.jid.InvalidJidException; @@ -80,49 +81,37 @@ public class JingleConnectionManager extends AbstractConnectionManager { return; } if (!this.primaryCandidates.containsKey(account.getJid().toBareJid())) { - String xmlns = "http://jabber.org/protocol/bytestreams"; - final String proxy = account.getXmppConnection() - .findDiscoItemByFeature(xmlns); + final String proxy = account.getXmppConnection().findDiscoItemByFeature(Xmlns.BYTE_STREAMS); if (proxy != null) { IqPacket iq = new IqPacket(IqPacket.TYPE.GET); iq.setAttribute("to", proxy); - iq.query(xmlns); - account.getXmppConnection().sendIqPacket(iq, - new OnIqPacketReceived() { + iq.query(Xmlns.BYTE_STREAMS); + account.getXmppConnection().sendIqPacket(iq,new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(Account account, - IqPacket packet) { - Element streamhost = packet - .query() - .findChild("streamhost", - "http://jabber.org/protocol/bytestreams"); - if (streamhost != null) { - JingleCandidate candidate = new JingleCandidate( - nextRandomId(), true); - candidate.setHost(streamhost - .getAttribute("host")); - candidate.setPort(Integer - .parseInt(streamhost - .getAttribute("port"))); - candidate - .setType(JingleCandidate.TYPE_PROXY); - try { - candidate.setJid(Jid.fromString(proxy)); - } catch (final InvalidJidException e) { - candidate.setJid(null); - } - candidate.setPriority(655360 + 65535); - primaryCandidates.put(account.getJid().toBareJid(), - candidate); - listener.onPrimaryCandidateFound(true, - candidate); - } else { - listener.onPrimaryCandidateFound(false, - null); - } + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + Element streamhost = packet.query().findChild("streamhost",Xmlns.BYTE_STREAMS); + final String host = streamhost == null ? null : streamhost.getAttribute("host"); + final String port = streamhost == null ? null : streamhost.getAttribute("port"); + if (host != null && port != null) { + try { + JingleCandidate candidate = new JingleCandidate(nextRandomId(), true); + candidate.setHost(host); + candidate.setPort(Integer.parseInt(port)); + candidate.setType(JingleCandidate.TYPE_PROXY); + candidate.setJid(Jid.fromString(proxy)); + candidate.setPriority(655360 + 65535); + primaryCandidates.put(account.getJid().toBareJid(),candidate); + listener.onPrimaryCandidateFound(true,candidate); + } catch (final NumberFormatException | InvalidJidException e) { + listener.onPrimaryCandidateFound(false,null); + return; } - }); + } else { + listener.onPrimaryCandidateFound(false,null); + } + } + }); } else { listener.onPrimaryCandidateFound(false, null); } @@ -141,12 +130,10 @@ public class JingleConnectionManager extends AbstractConnectionManager { String sid = null; Element payload = null; if (packet.hasChild("open", "http://jabber.org/protocol/ibb")) { - payload = packet - .findChild("open", "http://jabber.org/protocol/ibb"); + payload = packet.findChild("open", "http://jabber.org/protocol/ibb"); sid = payload.getAttribute("sid"); } else if (packet.hasChild("data", "http://jabber.org/protocol/ibb")) { - payload = packet - .findChild("data", "http://jabber.org/protocol/ibb"); + payload = packet.findChild("data", "http://jabber.org/protocol/ibb"); sid = payload.getAttribute("sid"); } if (sid != null) { @@ -161,10 +148,9 @@ public class JingleConnectionManager extends AbstractConnectionManager { } } } - Log.d(Config.LOGTAG, - "couldnt deliver payload: " + payload.toString()); + Log.d(Config.LOGTAG,"couldn't deliver payload: " + payload.toString()); } else { - Log.d(Config.LOGTAG, "no sid found in incomming ibb packet"); + Log.d(Config.LOGTAG, "no sid found in incoming ibb packet"); } }