From b2aa0e3352d7f538894d4fdf2c24349420d619f5 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 3 Apr 2020 08:16:55 +0200 Subject: [PATCH] use final varible to mark initiator once connection object has been created --- .../xmpp/jingle/AbstractJingleConnection.java | 8 ++++- .../xmpp/jingle/JingleConnectionManager.java | 13 ++++---- .../jingle/JingleFileTransferConnection.java | 33 ++++++++----------- .../xmpp/jingle/JingleRtpConnection.java | 4 +-- .../xmpp/jingle/stanzas/JinglePacket.java | 15 ++++++++- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/AbstractJingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/AbstractJingleConnection.java index a426edd2e..65fce4205 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/AbstractJingleConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/AbstractJingleConnection.java @@ -14,11 +14,17 @@ public abstract class AbstractJingleConnection { protected final JingleConnectionManager jingleConnectionManager; protected final XmppConnectionService xmppConnectionService; protected final Id id; + protected final Jid initiator; - public AbstractJingleConnection(final JingleConnectionManager jingleConnectionManager, final Id id) { + AbstractJingleConnection(final JingleConnectionManager jingleConnectionManager, final Id id, final Jid initiator) { this.jingleConnectionManager = jingleConnectionManager; this.xmppConnectionService = jingleConnectionManager.getXmppConnectionService(); this.id = id; + this.initiator = initiator; + } + + boolean isInitiator() { + return initiator.equals(id.account.getJid()); } abstract void deliverPacket(JinglePacket jinglePacket); 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 74082d03e..20fbae868 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java @@ -22,7 +22,6 @@ import eu.siacs.conversations.xmpp.jingle.stanzas.Content; import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription; import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket; import eu.siacs.conversations.xmpp.stanzas.IqPacket; -import eu.siacs.conversations.xmpp.stanzas.MessagePacket; import rocks.xmpp.addr.Jid; public class JingleConnectionManager extends AbstractConnectionManager { @@ -40,13 +39,14 @@ public class JingleConnectionManager extends AbstractConnectionManager { if (existingJingleConnection != null) { existingJingleConnection.deliverPacket(packet); } else if (packet.getAction() == JinglePacket.Action.SESSION_INITIATE) { + final Jid from = packet.getFrom(); final Content content = packet.getJingleContent(); final String descriptionNamespace = content == null ? null : content.getDescriptionNamespace(); final AbstractJingleConnection connection; if (FileTransferDescription.NAMESPACES.contains(descriptionNamespace)) { - connection = new JingleFileTransferConnection(this, id); + connection = new JingleFileTransferConnection(this, id, from); } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace)) { - connection = new JingleRtpConnection(this, id); + connection = new JingleRtpConnection(this, id, from); } else { //TODO return feature-not-implemented return; @@ -89,7 +89,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { final Element description = message.findChild("description"); final String namespace = description == null ? null : description.getNamespace(); if (Namespace.JINGLE_APPS_RTP.equals(namespace)) { - final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id); + final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, with); this.connections.put(id, rtpConnection); rtpConnection.deliveryMessage(to, from, message); } else { @@ -107,11 +107,12 @@ public class JingleConnectionManager extends AbstractConnectionManager { if (old != null) { old.cancel(); } + final Account account = message.getConversation().getAccount(); final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message); - final JingleFileTransferConnection connection = new JingleFileTransferConnection(this, id); + final JingleFileTransferConnection connection = new JingleFileTransferConnection(this, id, account.getJid()); mXmppConnectionService.markMessage(message, Message.STATUS_WAITING); - connection.init(message); this.connections.put(id, connection); + connection.init(message); } void finishConnection(final AbstractJingleConnection connection) { diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java index f70bbc575..f2cb85d27 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java @@ -66,7 +66,6 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple private int mJingleStatus = JINGLE_STATUS_OFFERED; //migrate to enum private int mStatus = Transferable.STATUS_UNKNOWN; private Message message; - private Jid initiator; private Jid responder; private List candidates = new ArrayList<>(); private ConcurrentHashMap connections = new ConcurrentHashMap<>(); @@ -178,7 +177,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple @Override public void success() { - if (initiator.equals(id.account.getJid())) { + if (isInitiator()) { Log.d(Config.LOGTAG, "we were initiating. sending file"); transport.send(file, onFileTransmissionStatusChanged); } else { @@ -191,14 +190,14 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple public void failed() { Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": proxy activation failed"); proxyActivationFailed = true; - if (initiating()) { + if (isInitiator()) { sendFallbackToIbb(); } } }; - public JingleFileTransferConnection(JingleConnectionManager jingleConnectionManager, Id id) { - super(jingleConnectionManager, id); + public JingleFileTransferConnection(JingleConnectionManager jingleConnectionManager, Id id, Jid initiator) { + super(jingleConnectionManager, id, initiator); } private static long parseLong(final Element element, final long l) { @@ -213,13 +212,11 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple } } + //TODO get rid and use isInitiator() instead private boolean responding() { return responder != null && responder.equals(id.account.getJid()); } - private boolean initiating() { - return initiator.equals(id.account.getJid()); - } InputStream getFileInputStream() { return this.mFileInputStream; @@ -346,7 +343,6 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple this.remoteSupportsOmemoJet = remoteFeatures.contains(Namespace.JINGLE_ENCRYPTED_TRANSPORT_OMEMO); this.message.setTransferable(this); this.mStatus = Transferable.STATUS_UPLOADING; - this.initiator = this.id.account.getJid(); this.responder = this.id.with; this.transportId = JingleConnectionManager.nextRandomId(); this.setupDescription(remoteVersion); @@ -354,7 +350,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple this.sendInitRequest(); } else { gatherAndConnectDirectCandidates(); - this.jingleConnectionManager.getPrimaryCandidate(id.account, initiating(), (success, candidate) -> { + this.jingleConnectionManager.getPrimaryCandidate(id.account, isInitiator(), (success, candidate) -> { if (success) { final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate); connections.put(candidate.getCid(), socksConnection); @@ -432,7 +428,6 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple this.mStatus = Transferable.STATUS_OFFER; this.message.setTransferable(this); this.message.setCounterpart(this.id.with); - this.initiator = this.id.with; this.responder = this.id.account.getJid(); final Content content = packet.getJingleContent(); final GenericTransportInfo transportInfo = content.getTransport(); @@ -643,7 +638,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple private void sendAcceptSocks() { gatherAndConnectDirectCandidates(); - this.jingleConnectionManager.getPrimaryCandidate(this.id.account, initiating(), (success, candidate) -> { + this.jingleConnectionManager.getPrimaryCandidate(this.id.account, isInitiator(), (success, candidate) -> { final JinglePacket packet = bootstrapPacket(JinglePacket.Action.SESSION_ACCEPT); final Content content = new Content(contentCreator, contentName); content.setDescription(this.description); @@ -810,7 +805,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple if (connection == null) { Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": could not find suitable candidate"); this.disconnectSocks5Connections(); - if (initiating()) { + if (isInitiator()) { this.sendFallbackToIbb(); } } else { @@ -852,7 +847,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple + " was a proxy. waiting for other party to activate"); } } else { - if (initiating()) { + if (isInitiator()) { Log.d(Config.LOGTAG, "we were initiating. sending file"); connection.send(file, onFileTransmissionStatusChanged); } else { @@ -882,7 +877,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple } else if (connection.getCandidate().getPriority() == currentConnection .getCandidate().getPriority()) { // Log.d(Config.LOGTAG,"found two candidates with same priority"); - if (initiating()) { + if (isInitiator()) { if (currentConnection.getCandidate().isOurs()) { connection = currentConnection; } @@ -920,7 +915,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple private void receiveFallbackToIbb(final JinglePacket packet, final IbbTransportInfo transportInfo) { - if (initiating()) { + if (isInitiator()) { Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received out of order transport-replace (we were initiating)"); respondToIqWithOutOfOrder(packet); return; @@ -950,7 +945,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple respondToIq(packet, true); - if (initiating()) { + if (isInitiator()) { this.sendJinglePacket(answer, (account, response) -> { if (response.getType() == IqPacket.TYPE.RESULT) { Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + " recipient ACKed our transport-accept. creating ibb"); @@ -992,7 +987,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple } respondToIq(packet, true); //might be receive instead if we are not initiating - if (initiating()) { + if (isInitiator()) { this.transport.connect(onIbbTransportConnected); } } else { @@ -1002,7 +997,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple } private void receiveSuccess() { - if (initiating()) { + if (isInitiator()) { this.mJingleStatus = JINGLE_STATUS_FINISHED; this.xmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED); this.disconnectSocks5Connections(); diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleRtpConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleRtpConnection.java index c600162c2..1d30e22bc 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleRtpConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleRtpConnection.java @@ -29,8 +29,8 @@ public class JingleRtpConnection extends AbstractJingleConnection { private State state = State.NULL; - public JingleRtpConnection(JingleConnectionManager jingleConnectionManager, Id id) { - super(jingleConnectionManager, id); + public JingleRtpConnection(JingleConnectionManager jingleConnectionManager, Id id, Jid initiator) { + super(jingleConnectionManager, id, initiator); } @Override diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java index a8a366ecf..b90333126 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java @@ -8,6 +8,7 @@ import com.google.common.base.Preconditions; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xmpp.stanzas.IqPacket; +import rocks.xmpp.addr.Jid; public class JinglePacket extends IqPacket { @@ -52,9 +53,21 @@ public class JinglePacket extends IqPacket { jingle.addChild(reason); } + //RECOMMENDED for session-initiate, NOT RECOMMENDED otherwise + public void setInitiator(final Jid initiator) { + Preconditions.checkArgument(initiator.isFullJid(), "initiator should be a full JID"); + findChild("jingle", Namespace.JINGLE).setAttribute("initiator", initiator.toEscapedString()); + } + + //RECOMMENDED for session-accept, NOT RECOMMENDED otherwise + public void setResponder(Jid responder) { + Preconditions.checkArgument(responder.isFullJid(), "responder should be a full JID"); + findChild("jingle", Namespace.JINGLE).setAttribute("responder", responder.toEscapedString()); + } + public Element getJingleChild(final String name) { final Element jingle = findChild("jingle", Namespace.JINGLE); - return jingle == null ? null : jingle.findChild(name); + return jingle == null ? null : jingle.findChild(name); } public void setJingleChild(final Element child) {