JingleConnectionManager: code clean up

This commit is contained in:
Daniel Gultsch 2023-11-22 08:37:17 +01:00
parent b183f49977
commit 747f58c35b
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -12,20 +12,6 @@ import com.google.common.collect.Collections2;
import com.google.common.collect.ComparisonChain; import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableSet; 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.Config;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact; 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.Element;
import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.Jid; import eu.siacs.conversations.xmpp.Jid;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.jingle.stanzas.Content; import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription; 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.IqPacket;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket; 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 { public class JingleConnectionManager extends AbstractConnectionManager {
static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE = static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE =
Executors.newSingleThreadScheduledExecutor(); Executors.newSingleThreadScheduledExecutor();
@ -170,8 +168,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
public boolean hasJingleRtpConnection(final Account account) { public boolean hasJingleRtpConnection(final Account account) {
for (AbstractJingleConnection connection : this.connections.values()) { for (AbstractJingleConnection connection : this.connections.values()) {
if (connection instanceof JingleRtpConnection) { if (connection instanceof JingleRtpConnection rtpConnection) {
final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
if (rtpConnection.isTerminated()) { if (rtpConnection.isTerminated()) {
continue; continue;
} }
@ -185,8 +182,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
public void notifyPhoneCallStarted() { public void notifyPhoneCallStarted() {
for (AbstractJingleConnection connection : connections.values()) { for (AbstractJingleConnection connection : connections.values()) {
if (connection instanceof JingleRtpConnection) { if (connection instanceof JingleRtpConnection rtpConnection) {
final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
if (rtpConnection.isTerminated()) { if (rtpConnection.isTerminated()) {
continue; continue;
} }
@ -195,7 +191,6 @@ public class JingleConnectionManager extends AbstractConnectionManager {
} }
} }
private Optional<RtpSessionProposal> findMatchingSessionProposal( private Optional<RtpSessionProposal> findMatchingSessionProposal(
final Account account, final Jid with, final Set<Media> media) { final Account account, final Jid with, final Set<Media> media) {
synchronized (this.rtpSessionProposals) { synchronized (this.rtpSessionProposals) {
@ -221,8 +216,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
private boolean hasMatchingRtpSession( private boolean hasMatchingRtpSession(
final Account account, final Jid with, final Set<Media> media) { final Account account, final Jid with, final Set<Media> media) {
for (AbstractJingleConnection connection : this.connections.values()) { for (AbstractJingleConnection connection : this.connections.values()) {
if (connection instanceof JingleRtpConnection) { if (connection instanceof JingleRtpConnection rtpConnection) {
final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
if (rtpConnection.isTerminated()) { if (rtpConnection.isTerminated()) {
continue; continue;
} }
@ -282,8 +276,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
} }
if ("accept".equals(message.getName())) { if ("accept".equals(message.getName())) {
for (AbstractJingleConnection connection : connections.values()) { for (AbstractJingleConnection connection : connections.values()) {
if (connection instanceof JingleRtpConnection) { if (connection instanceof JingleRtpConnection rtpConnection) {
final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
final AbstractJingleConnection.Id id = connection.getId(); final AbstractJingleConnection.Id id = connection.getId();
if (id.account == account && id.sessionId.equals(sessionId)) { if (id.account == account && id.sessionId.equals(sessionId)) {
rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp); rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
@ -645,7 +638,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
final AbstractJingleConnection.Id id = connection.getId(); final AbstractJingleConnection.Id id = connection.getId();
if (this.connections.remove(id) == null) { if (this.connections.remove(id) == null) {
throw new IllegalStateException( 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 // update chat UI to remove 'ongoing call' icon
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
@ -654,8 +647,7 @@ public class JingleConnectionManager extends AbstractConnectionManager {
public boolean fireJingleRtpConnectionStateUpdates() { public boolean fireJingleRtpConnectionStateUpdates() {
boolean firedUpdates = false; boolean firedUpdates = false;
for (final AbstractJingleConnection connection : this.connections.values()) { for (final AbstractJingleConnection connection : this.connections.values()) {
if (connection instanceof JingleRtpConnection) { if (connection instanceof JingleRtpConnection jingleRtpConnection) {
final JingleRtpConnection jingleRtpConnection = (JingleRtpConnection) connection;
if (jingleRtpConnection.isTerminated()) { if (jingleRtpConnection.isTerminated()) {
continue; continue;
} }
@ -674,63 +666,50 @@ public class JingleConnectionManager extends AbstractConnectionManager {
listener.onPrimaryCandidateFound(false, null); listener.onPrimaryCandidateFound(false, null);
return; return;
} }
if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) { 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 {
listener.onPrimaryCandidateFound( listener.onPrimaryCandidateFound(
true, this.primaryCandidates.get(account.getJid().asBareJid())); 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) { public void retractSessionProposal(final Account account, final Jid with) {
@ -849,13 +828,9 @@ public class JingleConnectionManager extends AbstractConnectionManager {
} }
if (sid != null) { if (sid != null) {
for (final AbstractJingleConnection connection : this.connections.values()) { for (final AbstractJingleConnection connection : this.connections.values()) {
if (connection instanceof JingleFileTransferConnection) { if (connection instanceof JingleFileTransferConnection fileTransfer) {
final JingleFileTransferConnection fileTransfer =
(JingleFileTransferConnection) connection;
final JingleTransport transport = fileTransfer.getTransport(); final JingleTransport transport = fileTransfer.getTransport();
if (transport instanceof JingleInBandTransport) { if (transport instanceof JingleInBandTransport inBandTransport) {
final JingleInBandTransport inBandTransport =
(JingleInBandTransport) transport;
if (inBandTransport.matches(account, sid)) { if (inBandTransport.matches(account, sid)) {
inBandTransport.deliverPayload(packet, payload); 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() account.getXmppConnection()
.sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null); .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 = final AbstractJingleConnection.Id id =
AbstractJingleConnection.Id.of(account, with, sessionId); AbstractJingleConnection.Id.of(account, with, sessionId);
final AbstractJingleConnection existingJingleConnection = connections.get(id); final AbstractJingleConnection existingJingleConnection = connections.get(id);
@ -1044,15 +1020,11 @@ public class JingleConnectionManager extends AbstractConnectionManager {
FAILED; FAILED;
public RtpEndUserState toEndUserState() { public RtpEndUserState toEndUserState() {
switch (this) { return switch (this) {
case SEARCHING: case SEARCHING, SEARCHING_ACKNOWLEDGED -> RtpEndUserState.FINDING_DEVICE;
case SEARCHING_ACKNOWLEDGED: case DISCOVERED -> RtpEndUserState.RINGING;
return RtpEndUserState.FINDING_DEVICE; default -> RtpEndUserState.CONNECTIVITY_ERROR;
case DISCOVERED: };
return RtpEndUserState.RINGING;
default:
return RtpEndUserState.CONNECTIVITY_ERROR;
}
} }
} }
@ -1062,10 +1034,6 @@ public class JingleConnectionManager extends AbstractConnectionManager {
public final Set<Media> media; public final Set<Media> media;
private final Account account; 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> media) { private RtpSessionProposal(Account account, Jid with, String sessionId, Set<Media> media) {
this.account = account; this.account = account;
this.with = with; this.with = with;