From 747f58c35bfa2077368d95653c5e81ae8e78a5d5 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 22 Nov 2023 08:37:17 +0100 Subject: [PATCH] JingleConnectionManager: code clean up --- .../xmpp/jingle/JingleConnectionManager.java | 172 +++++++----------- 1 file changed, 70 insertions(+), 102 deletions(-) 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 48233c8d2..e334860bc 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java @@ -12,20 +12,6 @@ import com.google.common.collect.Collections2; import com.google.common.collect.ComparisonChain; import com.google.common.collect.ImmutableSet; -import java.lang.ref.WeakReference; -import java.security.SecureRandom; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - import eu.siacs.conversations.Config; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; @@ -39,7 +25,6 @@ import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xmpp.Jid; -import eu.siacs.conversations.xmpp.OnIqPacketReceived; import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.jingle.stanzas.Content; import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription; @@ -51,6 +36,19 @@ import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription; import eu.siacs.conversations.xmpp.stanzas.IqPacket; import eu.siacs.conversations.xmpp.stanzas.MessagePacket; +import java.lang.ref.WeakReference; +import java.security.SecureRandom; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + public class JingleConnectionManager extends AbstractConnectionManager { static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor(); @@ -170,8 +168,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { public boolean hasJingleRtpConnection(final Account account) { for (AbstractJingleConnection connection : this.connections.values()) { - if (connection instanceof JingleRtpConnection) { - final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection; + if (connection instanceof JingleRtpConnection rtpConnection) { if (rtpConnection.isTerminated()) { continue; } @@ -185,8 +182,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { public void notifyPhoneCallStarted() { for (AbstractJingleConnection connection : connections.values()) { - if (connection instanceof JingleRtpConnection) { - final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection; + if (connection instanceof JingleRtpConnection rtpConnection) { if (rtpConnection.isTerminated()) { continue; } @@ -195,7 +191,6 @@ public class JingleConnectionManager extends AbstractConnectionManager { } } - private Optional findMatchingSessionProposal( final Account account, final Jid with, final Set media) { synchronized (this.rtpSessionProposals) { @@ -221,8 +216,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { private boolean hasMatchingRtpSession( final Account account, final Jid with, final Set media) { for (AbstractJingleConnection connection : this.connections.values()) { - if (connection instanceof JingleRtpConnection) { - final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection; + if (connection instanceof JingleRtpConnection rtpConnection) { if (rtpConnection.isTerminated()) { continue; } @@ -282,8 +276,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { } if ("accept".equals(message.getName())) { for (AbstractJingleConnection connection : connections.values()) { - if (connection instanceof JingleRtpConnection) { - final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection; + if (connection instanceof JingleRtpConnection rtpConnection) { final AbstractJingleConnection.Id id = connection.getId(); if (id.account == account && id.sessionId.equals(sessionId)) { rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp); @@ -645,7 +638,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { final AbstractJingleConnection.Id id = connection.getId(); if (this.connections.remove(id) == null) { throw new IllegalStateException( - String.format("Unable to finish connection with id=%s", id.toString())); + String.format("Unable to finish connection with id=%s", id)); } // update chat UI to remove 'ongoing call' icon mXmppConnectionService.updateConversationUi(); @@ -654,8 +647,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { public boolean fireJingleRtpConnectionStateUpdates() { boolean firedUpdates = false; for (final AbstractJingleConnection connection : this.connections.values()) { - if (connection instanceof JingleRtpConnection) { - final JingleRtpConnection jingleRtpConnection = (JingleRtpConnection) connection; + if (connection instanceof JingleRtpConnection jingleRtpConnection) { if (jingleRtpConnection.isTerminated()) { continue; } @@ -674,63 +666,50 @@ public class JingleConnectionManager extends AbstractConnectionManager { listener.onPrimaryCandidateFound(false, null); return; } - if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) { - final Jid proxy = - account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS); - if (proxy != null) { - IqPacket iq = new IqPacket(IqPacket.TYPE.GET); - iq.setTo(proxy); - iq.query(Namespace.BYTE_STREAMS); - account.getXmppConnection() - .sendIqPacket( - iq, - new OnIqPacketReceived() { - - @Override - public void onIqPacketReceived( - Account account, IqPacket packet) { - final Element streamhost = - packet.query() - .findChild( - "streamhost", - Namespace.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(proxy); - candidate.setPriority( - 655360 + (initiator ? 30 : 0)); - primaryCandidates.put( - account.getJid().asBareJid(), candidate); - listener.onPrimaryCandidateFound(true, candidate); - } catch (final NumberFormatException e) { - listener.onPrimaryCandidateFound(false, null); - } - } else { - listener.onPrimaryCandidateFound(false, null); - } - } - }); - } else { - listener.onPrimaryCandidateFound(false, null); - } - - } else { + if (this.primaryCandidates.containsKey(account.getJid().asBareJid())) { listener.onPrimaryCandidateFound( true, this.primaryCandidates.get(account.getJid().asBareJid())); + return; } + + final Jid proxy = + account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS); + if (proxy == null) { + listener.onPrimaryCandidateFound(false, null); + return; + } + final IqPacket iq = new IqPacket(IqPacket.TYPE.GET); + iq.setTo(proxy); + iq.query(Namespace.BYTE_STREAMS); + account.getXmppConnection() + .sendIqPacket( + iq, + (a, response) -> { + final Element streamhost = + response.query() + .findChild("streamhost", Namespace.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(proxy); + candidate.setPriority(655360 + (initiator ? 30 : 0)); + primaryCandidates.put(a.getJid().asBareJid(), candidate); + listener.onPrimaryCandidateFound(true, candidate); + } catch (final NumberFormatException e) { + listener.onPrimaryCandidateFound(false, null); + } + } else { + listener.onPrimaryCandidateFound(false, null); + } + }); } public void retractSessionProposal(final Account account, final Jid with) { @@ -849,13 +828,9 @@ public class JingleConnectionManager extends AbstractConnectionManager { } if (sid != null) { for (final AbstractJingleConnection connection : this.connections.values()) { - if (connection instanceof JingleFileTransferConnection) { - final JingleFileTransferConnection fileTransfer = - (JingleFileTransferConnection) connection; + if (connection instanceof JingleFileTransferConnection fileTransfer) { final JingleTransport transport = fileTransfer.getTransport(); - if (transport instanceof JingleInBandTransport) { - final JingleInBandTransport inBandTransport = - (JingleInBandTransport) transport; + if (transport instanceof JingleInBandTransport inBandTransport) { if (inBandTransport.matches(account, sid)) { inBandTransport.deliverPayload(packet, payload); } @@ -864,7 +839,7 @@ public class JingleConnectionManager extends AbstractConnectionManager { } } } - Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString()); + Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet); account.getXmppConnection() .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null); } @@ -970,7 +945,8 @@ public class JingleConnectionManager extends AbstractConnectionManager { } } - public void failProceed(Account account, final Jid with, final String sessionId, final String message) { + public void failProceed( + Account account, final Jid with, final String sessionId, final String message) { final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with, sessionId); final AbstractJingleConnection existingJingleConnection = connections.get(id); @@ -1044,15 +1020,11 @@ public class JingleConnectionManager extends AbstractConnectionManager { FAILED; public RtpEndUserState toEndUserState() { - switch (this) { - case SEARCHING: - case SEARCHING_ACKNOWLEDGED: - return RtpEndUserState.FINDING_DEVICE; - case DISCOVERED: - return RtpEndUserState.RINGING; - default: - return RtpEndUserState.CONNECTIVITY_ERROR; - } + return switch (this) { + case SEARCHING, SEARCHING_ACKNOWLEDGED -> RtpEndUserState.FINDING_DEVICE; + case DISCOVERED -> RtpEndUserState.RINGING; + default -> RtpEndUserState.CONNECTIVITY_ERROR; + }; } } @@ -1062,10 +1034,6 @@ public class JingleConnectionManager extends AbstractConnectionManager { public final Set media; private final Account account; - private RtpSessionProposal(Account account, Jid with, String sessionId) { - this(account, with, sessionId, Collections.emptySet()); - } - private RtpSessionProposal(Account account, Jid with, String sessionId, Set media) { this.account = account; this.with = with;