1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 01:29:38 +02:00

More improvements to JS5B test (still fails)

This commit is contained in:
vanitasvitae 2017-08-01 17:12:27 +02:00
parent 22a7e38f66
commit 1c72fdfa14
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
6 changed files with 103 additions and 33 deletions

View file

@ -71,9 +71,7 @@ public final class JingleManager extends Manager {
private final ConcurrentHashMap<FullJidAndSessionId, JingleSession> jingleSessions = new ConcurrentHashMap<>();
public static boolean ALLOW_MULTIPLE_CONTENT_PER_SESSION = false;
private JingleManager(XMPPConnection connection) {
private JingleManager(final XMPPConnection connection) {
super(connection);
connection.registerIQRequestHandler(
@ -91,7 +89,7 @@ public final class JingleManager extends Manager {
// We have not seen this session before.
// Either it is fresh, or unknown.
if (session == null) {
LOGGER.log(Level.INFO, connection().getUser().asFullJidOrThrow() + " received unknown session: " + jingle.getFrom().asFullJidOrThrow() + " " + jingle.getSid());
if (jingle.getAction() == JingleAction.session_initiate) {
//fresh. phew!
try {
@ -266,6 +264,12 @@ public final class JingleManager extends Manager {
return session;
}
public void addSession(JingleSession session) {
if (!jingleSessions.containsValue(session)) {
jingleSessions.put(new FullJidAndSessionId(session.getPeer(), session.getSessionId()), session);
}
}
public void removeSession(JingleSession session) {
jingleSessions.remove(new FullJidAndSessionId(session.getPeer(), session.getSessionId()));
}

View file

@ -18,6 +18,8 @@ package org.jivesoftware.smackx.jingle.components;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackFuture;
@ -34,6 +36,8 @@ import org.jivesoftware.smackx.jingle.element.JingleElement;
*/
public abstract class JingleTransport<D extends JingleContentTransportElement> extends SmackFuture<BytestreamSession> {
private static final Logger LOGGER = Logger.getLogger(JingleTransport.class.getName());
private JingleContent parent;
private final ArrayList<JingleTransportCandidate<?>> candidates = new ArrayList<>();
@ -45,6 +49,7 @@ public abstract class JingleTransport<D extends JingleContentTransportElement> e
public abstract D getElement();
public void addCandidate(JingleTransportCandidate<?> candidate) {
LOGGER.log(Level.INFO, "Insert candidate.");
// Insert sorted by descending priority
// Empty list -> insert
@ -59,7 +64,7 @@ public abstract class JingleTransport<D extends JingleContentTransportElement> e
JingleTransportCandidate<?> c = candidates.get(i);
// list already contains element -> return
if (c == candidate) {
if (c == candidate || c.equals(candidate)) {
return;
}
@ -67,6 +72,7 @@ public abstract class JingleTransport<D extends JingleContentTransportElement> e
if (c.getPriority() <= candidate.getPriority()) {
candidates.add(i, candidate);
candidate.setParent(this);
return;
}
}
}
@ -85,6 +91,7 @@ public abstract class JingleTransport<D extends JingleContentTransportElement> e
public void setPeersProposal(JingleTransport<?> peersProposal) {
this.peersProposal = peersProposal;
peersProposal.setParent(getParent());
peersProposal.isPeersProposal = true;
}

View file

@ -83,6 +83,7 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
this(other.getSid(),
Socks5Utils.createDigest(other.getSid(), content.getParent().getInitiator(), content.getParent().getResponder()),
other.mode, candidates);
setPeersProposal(other);
}
public JingleS5BTransport(String sid, String dstAddr, Bytestream.Mode mode, List<JingleTransportCandidate<?>> candidates) {
@ -96,6 +97,10 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
}
}
public JingleS5BTransport(JingleS5BTransport transport) {
this(transport.sid, transport.dstAddr, transport.mode, transport.getCandidates());
}
@Override
public JingleS5BTransportElement getElement() {
JingleS5BTransportElement.Builder builder = JingleS5BTransportElement.getBuilder()
@ -161,10 +166,10 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
}
public JingleS5BTransportCandidate connectToCandidates(int timeout) {
for (JingleTransportCandidate<?> c : getCandidates()) {
for (JingleTransportCandidate<?> c : getPeersProposal().getCandidates()) {
int _timeout = timeout / getCandidates().size(); //TODO: Wise?
try {
return ((JingleS5BTransportCandidate) c).connect(_timeout);
return ((JingleS5BTransportCandidate) c).connect(_timeout, true);
} catch (IOException | TimeoutException | InterruptedException | SmackException | XMPPException e) {
LOGGER.log(Level.WARNING, "Exception while connecting to candidate: " + e, e);
}
@ -179,7 +184,7 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
JingleS5BTransport peers = (JingleS5BTransport) getPeersProposal();
JingleSession session = getParent().getParent();
if (getSelectedCandidate() == null || peers.getSelectedCandidate() == null) {
if (getSelectedCandidate() == null || peers == null || peers.getSelectedCandidate() == null) {
// Not yet ready if we or peer did not yet decide on a candidate.
LOGGER.log(Level.INFO, "Not ready.");
return;
@ -217,7 +222,7 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
boolean isProxy = nominated.getType() == JingleS5BTransportCandidateElement.Type.proxy;
try {
nominated = nominated.connect(MAX_TIMEOUT);
nominated = nominated.connect(MAX_TIMEOUT, false);
} catch (InterruptedException | IOException | XMPPException | SmackException | TimeoutException e) {
LOGGER.log(Level.INFO, "Could not connect to our candidate.", e);
callback.onTransportFailed(new S5BTransportException.CandidateError(e));
@ -255,7 +260,7 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
}
}
LOGGER.log(Level.INFO, "Start transmission.");
LOGGER.log(Level.INFO, "Start transmission on " + nominated.getCandidateId());
this.bytestreamSession = new Socks5BytestreamSession(nominated.getSocket(), !isProxy);
callback.onTransportReady(this.bytestreamSession);
@ -265,7 +270,7 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
LOGGER.log(Level.INFO, "Our choice, so their candidate was used.");
boolean isProxy = nominated.getType() == JingleS5BTransportCandidateElement.Type.proxy;
if (!isProxy) {
LOGGER.log(Level.INFO, "Direct connection.");
LOGGER.log(Level.INFO, "Start transmission on " + nominated.getCandidateId());
this.bytestreamSession = new Socks5BytestreamSession(nominated.getSocket(), true);
callback.onTransportReady(this.bytestreamSession);
} else {
@ -310,7 +315,8 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
// Received second candidate-used -> out-of-order!
if (peers.getSelectedCandidate() != null) {
try {
jingleManager.getConnection().createStanzaCollectorAndSend(JingleElement.createJingleErrorOutOfOrder(wrapping));
jingleManager.getConnection().sendStanza(JingleElement.createJingleErrorOutOfOrder(wrapping));
//jingleManager.getConnection().createStanzaCollectorAndSend(JingleElement.createJingleErrorOutOfOrder(wrapping));
} catch (SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not respond to candidate-used transport-info: " + e, e);
}
@ -320,12 +326,14 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
Iterator<JingleTransportCandidate<?>> ourCandidates = getCandidates().iterator();
while (ourCandidates.hasNext()) {
JingleS5BTransportCandidate candidate = (JingleS5BTransportCandidate) ourCandidates.next();
LOGGER.log(Level.INFO, "CandidateID: " + candidate.getCandidateId() + " " + candidateId);
if (candidate.getCandidateId().equals(candidateId)) {
peers.setSelectedCandidate(candidate);
}
}
if (peers.getSelectedCandidate() == null) {
LOGGER.log(Level.SEVERE, "ILLEGAL CANDIDATE ID!!!");
//TODO: Alert! Illegal candidateId!
}

View file

@ -19,13 +19,17 @@ package org.jivesoftware.smackx.jingle.transport.jingle_s5b;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Client;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5ClientForInitiator;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.jingle.components.JingleContent;
import org.jivesoftware.smackx.jingle.components.JingleSession;
import org.jivesoftware.smackx.jingle.components.JingleTransport;
import org.jivesoftware.smackx.jingle.components.JingleTransportCandidate;
import org.jivesoftware.smackx.jingle.transport.jingle_s5b.element.JingleS5BTransportCandidateElement;
@ -33,6 +37,7 @@ import org.jivesoftware.smackx.jingle.transport.jingle_s5b.element.JingleS5BTran
* Jingle SOCKS5Bytestream transport candidate.
*/
public class JingleS5BTransportCandidate extends JingleTransportCandidate<JingleS5BTransportCandidateElement> {
private static final Logger LOGGER = Logger.getLogger(JingleS5BTransportCandidate.class.getName());
private final String candidateId;
private final Bytestream.StreamHost streamHost;
@ -75,16 +80,20 @@ public class JingleS5BTransportCandidate extends JingleTransportCandidate<Jingle
getPriority(), getType());
}
public JingleS5BTransportCandidate connect(int timeout) throws InterruptedException, TimeoutException, SmackException, XMPPException, IOException {
public JingleS5BTransportCandidate connect(int timeout, boolean peersProposal) throws InterruptedException, TimeoutException, SmackException, XMPPException, IOException {
Socks5Client client;
if (getParent().isPeersProposal()) {
if (peersProposal) {
LOGGER.log(Level.INFO, "Connect to foreign candidate " + getCandidateId());
client = new Socks5Client(getStreamHost(), ((JingleS5BTransport) getParent()).getDstAddr());
}
else {
JingleSession session = getParent().getParent().getParent();
LOGGER.log(Level.INFO, "Connect to our candidate " + getCandidateId());
JingleTransport<?> transport = getParent();
JingleContent content = transport.getParent();
JingleSession session = content.getParent();
client = new Socks5ClientForInitiator(getStreamHost(), ((JingleS5BTransport) getParent()).getDstAddr(),
session.getJingleManager().getConnection(), session.getSessionId(), session.getPeer());
session.getJingleManager().getConnection(), ((JingleS5BTransport) getParent()).getSid(), getStreamHost().getJID());
}
this.socket = client.getSocket(timeout);

View file

@ -137,6 +137,8 @@ public final class JingleS5BTransportManager extends Manager implements JingleTr
candidates.add(new JingleS5BTransportCandidate(StringUtils.randomString(16), host, 0, JingleS5BTransportCandidateElement.Type.proxy));
}
LOGGER.log(Level.INFO, "Collected candidates.");
return candidates;
}
@ -191,9 +193,14 @@ public final class JingleS5BTransportManager extends Manager implements JingleTr
JingleElement.Builder jb = JingleElement.getBuilder()
.setSessionId(session.getSessionId())
.setInitiator(session.getInitiator())
.setAction(JingleAction.transport_info);
if (session.isInitiator()) {
jb.setInitiator(session.getInitiator());
} else {
jb.setResponder(session.getResponder());
}
JingleContentElement.Builder cb = JingleContentElement.getBuilder()
.setCreator(content.getCreator())
.setName(content.getName())