From a616e34bd741630c8286c5aea14be1489d62bbc1 Mon Sep 17 00:00:00 2001 From: Thiago Camargo Date: Sat, 3 Mar 2007 07:31:53 +0000 Subject: [PATCH] More Refactoring in Negotiation Speed git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7364 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smackx/jingle/JingleNegotiator.java | 2 +- .../smackx/jingle/JingleSession.java | 4 +- .../jingle/nat/TransportNegotiator.java | 58 ++++++++++++------- jingle/media/test/JingleMediaTest.java | 4 +- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleNegotiator.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleNegotiator.java index 2409b3b9f..8de22bb69 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleNegotiator.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleNegotiator.java @@ -360,7 +360,7 @@ public abstract class JingleNegotiator { } public Jingle eventTerminate(Jingle jin) throws XMPPException { - if (neg != null) { + if (neg != null && !neg.invalidState()) { neg.close(); } return null; diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java index f7f06f380..2fa62637f 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java @@ -547,7 +547,7 @@ public abstract class JingleSession extends JingleNegotiator { * @param iq The Jingle packet we are responing to * @param error the IQ packet we want to complete and send */ - protected IQ sendFormattedError(IQ iq, JingleError error) { + public IQ sendFormattedError(IQ iq, JingleError error) { IQ perror = null; if (error != null) { perror = createIQ(getSid(), iq.getFrom(), iq.getTo(), IQ.Type.ERROR); @@ -1121,6 +1121,7 @@ public abstract class JingleSession extends JingleNegotiator { * @throws XMPPException */ public void terminate() throws XMPPException { + if (isClosed()) return; System.out.println("State: " + this.getState()); Jingle result = null; Jingle jout = new Jingle(Jingle.Action.SESSIONTERMINATE); @@ -1133,6 +1134,7 @@ public abstract class JingleSession extends JingleNegotiator { * Terminate negotiations. */ public void close() { + if (isClosed()) return; destroyMediaNeg(); destroyTransportNeg(); removePacketListener(); diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java index 188d9e4eb..143f88bad 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/nat/TransportNegotiator.java @@ -277,29 +277,43 @@ public abstract class TransportNegotiator extends JingleNegotiator { public void run() { // Sleep for some time, waiting for the candidates checks - try { - Thread.sleep(CANDIDATES_ACCEPT_PERIOD - + TransportResolver.CHECK_TIMEOUT); + + int totalTime= (CANDIDATES_ACCEPT_PERIOD + (TransportResolver.CHECK_TIMEOUT * (resolver.getCandidatesList().size()+1))); + int tries = (int)Math.ceil(totalTime/1000); + + for (int i = 0; i < tries; i++) { + try { + Thread.sleep(1000); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + + // Once we are in pending state, look for any valid remote + // candidate, and send an "accept" if we have one... + TransportCandidate bestRemote = getBestRemoteCandidate(); + State state = getState(); + + if (bestRemote != null && (state == pending || state == active)) { + // Accepting the remote candidate + Jingle jout = new Jingle(Jingle.Action.TRANSPORTACCEPT); + jout.addTransport(getJingleTransport(bestRemote)); + + // Send the packet + js.sendFormattedJingle(jin, jout); + + if (isEstablished()) { + setState(active); + break; + } + } } - catch (InterruptedException e) { - e.printStackTrace(); - } - - // Once we are in pending state, look for any valid remote - // candidate, and send an "accept" if we have one... - TransportCandidate bestRemote = getBestRemoteCandidate(); - State state = getState(); - - if (bestRemote != null && (state == pending || state == active)) { - // Accepting the remote candidate - Jingle jout = new Jingle(Jingle.Action.TRANSPORTACCEPT); - jout.addTransport(getJingleTransport(bestRemote)); - - // Send the packet - js.sendFormattedJingle(jin, jout); - - if (isEstablished()) { - setState(active); + if (getState() == null || !getState().equals(active)) { + try { + session.terminate(); + } + catch (XMPPException e) { + e.printStackTrace(); } } } diff --git a/jingle/media/test/JingleMediaTest.java b/jingle/media/test/JingleMediaTest.java index b836f723a..0119d58d8 100644 --- a/jingle/media/test/JingleMediaTest.java +++ b/jingle/media/test/JingleMediaTest.java @@ -129,10 +129,10 @@ public class JingleMediaTest extends SmackTestCase { x1, new BasicTransportManager()); MultiMediaManager jingleMediaManager0 = new MultiMediaManager(); -// jingleMediaManager0.addMediaManager(new SpeexMediaManager()); + jingleMediaManager0.addMediaManager(new SpeexMediaManager()); jingleMediaManager0.addMediaManager(new JmfMediaManager()); MultiMediaManager jingleMediaManager1 = new MultiMediaManager(); -// jingleMediaManager1.addMediaManager(new JmfMediaManager()); + jingleMediaManager1.addMediaManager(new JmfMediaManager()); jingleMediaManager1.addMediaManager(new SpeexMediaManager()); jm0.setMediaManager(jingleMediaManager0);