From ed660f73e929ab9d405d2214cc5285dc0c8f07dc Mon Sep 17 00:00:00 2001 From: Thiago Camargo Date: Wed, 14 Feb 2007 02:35:04 +0000 Subject: [PATCH] git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7104 b35dd754-fafc-0310-a699-88a17e54d16e --- .../build/projects/JingleExtension.iml | 12 +++ .../smackx/jingle/JingleManager.java | 97 ++++++++++--------- .../smackx/jingle/JingleSession.java | 21 ++-- .../smackx/jingle/JingleSessionRequest.java | 9 +- .../smackx/jingle/JingleManagerTest.java | 90 ++++++++--------- 5 files changed, 132 insertions(+), 97 deletions(-) diff --git a/jingle/extension/build/projects/JingleExtension.iml b/jingle/extension/build/projects/JingleExtension.iml index 156623a00..2f898249b 100644 --- a/jingle/extension/build/projects/JingleExtension.iml +++ b/jingle/extension/build/projects/JingleExtension.iml @@ -1,5 +1,16 @@ + + + + + + + + + + + @@ -30,6 +41,7 @@ + diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleManager.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleManager.java index 88af48df2..7cc154af1 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleManager.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleManager.java @@ -49,34 +49,34 @@ import java.util.List; * Jingle is a session establishment protocol defined in (XEP-0166). * It defines a framework for negotiating and managing out-of-band ( data that is send and receive through other connection than XMPP connection) data sessions over XMPP. * With this protocol you can setup VOIP Calls, Video Streaming, File transfers and whatever out-of-band session based transmission. - * + *

* To create a Jingle Session you need a Transport method and a Payload type. - * + *

* A transport method is how it will trasmit and receive network packets. Transport MUST have one or more candidates. * A transport candidate is an IP Address with a defined port, that other party must send data to. - * + *

* A supported payload type, is the data encoding format that the jmf will be transmitted. * For instance an Audio Payload "GSM". - * + *

* A Jingle session negociates a payload type and a pair of transport candidates. * Which means that when a Jingle Session is establhished you will have two defined transport candidates with addresses * and a defined Payload type. * In other words, you will have two IP address with their respective ports, and a Codec type defined. - * + *

* The JingleManager is a facade built upon Jabber Jingle (XEP-166) to allow the * use of Jingle. This implementation allows the user to simply * use this class for setting the Jingle parameters, create and receive Jingle Sessions. - * + *

* In order to use the Jingle, the user must provide a * TransportManager that will handle the resolution of potential IP addresses taht can be used to transport the streaming (jmf). * This TransportManager can be initialized with several default resolvers, * including a fixed solver that can be used when the address and port are know * in advance. * This API have ready to use Transport Managers, for instance: BasicTransportManager, STUNTransportManager, BridgedTransportManager. - * + *

* You should also especify a JingleMediaManager if you want that JingleManager assume Media control * Using a JingleMediaManager implementation is the easier way to implement a Jingle Application. - * + *

* Otherwise before creating an outgoing connection, the user must create jingle session * listeners that will be called when different events happen. The most * important event is sessionEstablished(), that will be called when all @@ -84,85 +84,85 @@ import java.util.List; * transmission as well as the remote and local addresses and ports for the * communication. See JingleSessionListener for a complete list of events that can be * observed. - * + *

* This is an example of how to use the JingleManager: * This example implements a Jingle VOIP Call between two users. - * + *

*

- *
+ * 

* To wait for an Incoming Jingle Session: - * + *

* try { - * + *

* // Connect to a XMPP Server * XMPPConnection x1 = new XMPPConnection("xmpp.com"); * x1.connect(); * x1.login("juliet", "juliet"); - * + *

* // Create a JingleManager using a BasicResolver * final JingleManager jm1 = new JingleManager( * x1, new BasicTransportManager()); - * + *

* // Create a JingleMediaManager. In this case using Jingle Audio Media API * JingleMediaManager jingleMediaManager = new AudioMediaManager(); - * + *

* // Set the JingleMediaManager * jm1.setMediaManager(jingleMediaManager); - * + *

* // Listen for incoming calls * jm1.addJingleSessionRequestListener(new JingleSessionRequestListener() { * public void sessionRequested(JingleSessionRequest request) { - * + *

* try { * // Accept the call * IncomingJingleSession session = request.accept(); - * - * + *

+ *

* // Start the call * session.start(); * } catch (XMPPException e) { * e.printStackTrace(); * } - * + *

* } * }); - * + *

* Thread.sleep(15000); - * + *

* } catch (Exception e) { * e.printStackTrace(); * } - * + *

* To create an Outgoing Jingle Session: - * + *

* try { - * + *

* // Connect to a XMPP Server * XMPPConnection x0 = new XMPPConnection("xmpp.com"); * x0.connect(); * x0.login("romeo", "romeo"); - * + *

* // Create a JingleManager using a BasicResolver * final JingleManager jm0 = new JingleManager( * x0, new BasicTransportManager()); - * + *

* // Create a JingleMediaManager. In this case using Jingle Audio Media API * JingleMediaManager jingleMediaManager = new AudioMediaManager(); // Using Jingle Media API - * + *

* // Set the JingleMediaManager * jm0.setMediaManager(jingleMediaManager); - * + *

* // Create a new Jingle Call with a full JID * OutgoingJingleSession js0 = jm0.createOutgoingJingleSession("juliet@xmpp.com/Smack"); - * + *

* // Start the call * js0.start(); - * + *

* Thread.sleep(10000); * js0.terminate(); - * + *

* Thread.sleep(3000); - * + *

* } catch (Exception e) { * e.printStackTrace(); * } @@ -275,7 +275,8 @@ public class JingleManager implements JingleSessionListener { if (aux != null) try { aux.terminate(); - } catch (XMPPException e) { + } + catch (XMPPException e) { e.printStackTrace(); } } @@ -323,8 +324,8 @@ public class JingleManager implements JingleSessionListener { /** * Enables or disables the Jingle support on a given connection. - * - * + *

+ *

* Before starting any Jingle jmf session, check that the user can handle * it. Enable the Jingle support to indicate that this client handles Jingle * messages. @@ -334,7 +335,7 @@ public class JingleManager implements JingleSessionListener { * @param enabled indicates if the service will be enabled or disabled */ public synchronized static void setServiceEnabled(XMPPConnection connection, - boolean enabled) { + boolean enabled) { if (isServiceEnabled(connection) == enabled) { return; } @@ -342,7 +343,8 @@ public class JingleManager implements JingleSessionListener { if (enabled) { ServiceDiscoveryManager.getInstanceFor(connection).addFeature( Jingle.NAMESPACE); - } else { + } + else { ServiceDiscoveryManager.getInstanceFor(connection).removeFeature( Jingle.NAMESPACE); } @@ -475,7 +477,8 @@ public class JingleManager implements JingleSessionListener { for (CreatedJingleSessionListener createdJingleSessionListener : creationListeners) { try { createdJingleSessionListener.sessionCreated(jingleSession); - } catch (Exception e) { + } + catch (Exception e) { e.printStackTrace(); } } @@ -549,7 +552,8 @@ public class JingleManager implements JingleSessionListener { for (JingleSession jingleSession : sessions) try { jingleSession.terminate(); - } catch (XMPPException e) { + } + catch (XMPPException e) { e.printStackTrace(); } @@ -589,7 +593,7 @@ public class JingleManager implements JingleSessionListener { * @return The session on which the negotiation can be run. */ public OutgoingJingleSession createOutgoingJingleSession(String responder, - List payloadTypes) throws XMPPException { + List payloadTypes) throws XMPPException { if (responder == null || StringUtils.parseName(responder).length() <= 0 || StringUtils.parseServer(responder).length() <= 0 @@ -666,8 +670,10 @@ public class JingleManager implements JingleSessionListener { if (request == null) { throw new NullPointerException("JingleMediaManager is not defined"); } - if (jingleMediaManager == null) return null; - return createIncomingJingleSession(request, jingleMediaManager.getPayloads()); + if (jingleMediaManager != null) + return createIncomingJingleSession(request, jingleMediaManager.getPayloads()); + + return createIncomingJingleSession(request,null); } /** @@ -682,7 +688,8 @@ public class JingleManager implements JingleSessionListener { if (jingleSession.getResponder().equals(jid)) { return jingleSession; } - } else if (jingleSession instanceof IncomingJingleSession) { + } + else if (jingleSession instanceof IncomingJingleSession) { if (jingleSession.getInitiator().equals(jid)) { return jingleSession; } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java index 78bc2507d..f8182eb8b 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java @@ -118,7 +118,7 @@ public abstract class JingleSession extends JingleNegotiator { static int ccc = 0; - private boolean closed = false; + private boolean closed = false; /** * Full featured JingleSession constructor @@ -343,6 +343,8 @@ public abstract class JingleSession extends JingleNegotiator { public IQ dispatchIncomingPacket(IQ iq, String id) throws XMPPException { IQ jout = null; + if (iq != null) System.out.println("L: " + iq.toXML()); + if (invalidState()) { throw new IllegalStateException( "Illegal state in dispatch packet in Session manager."); @@ -377,12 +379,16 @@ public abstract class JingleSession extends JingleNegotiator { jout = getState().eventInfo(jin); } else if (action.equals(Jingle.Action.SESSIONINITIATE)) { - jout = getState().eventInitiate(jin); + if (getState() != null) + jout = getState().eventInitiate(jin); } else if (action.equals(Jingle.Action.SESSIONREDIRECT)) { jout = getState().eventRedirect(jin); } else if (action.equals(Jingle.Action.SESSIONTERMINATE)) { + + System.out.println("SESSION PACKET"); + jout = getState().eventTerminate(jin); } } @@ -417,6 +423,9 @@ public abstract class JingleSession extends JingleNegotiator { public synchronized IQ respond(IQ iq) throws XMPPException { IQ response = null; + if (iq != null) + System.out.println("TT: " + iq.toXML()); + if (isValid()) { String responseId = null; IQ sessionResponse = null; @@ -887,10 +896,10 @@ public abstract class JingleSession extends JingleNegotiator { if (iq instanceof Jingle) { Jingle jin = (Jingle) iq; - //System.out.println("Jingle: " + iq.toXML()); + System.out.println("Jingle: " + iq.toXML()); String sid = jin.getSid(); - if (!sid.equals(getSid())) { + if (sid == null || !sid.equals(getSid())) { System.out.println("Ignored Jingle(SID) " + sid + "|" + getSid() + " :" + iq.toXML()); return false; } @@ -1091,8 +1100,8 @@ public abstract class JingleSession extends JingleNegotiator { destroyMediaNeg(); destroyTransportNeg(); removePacketListener(); - System.out.println("Negociation Closed"); - closed=true; + System.out.println("Negociation Closed: "+getConnection().getUser()); + closed = true; super.close(); } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSessionRequest.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSessionRequest.java index e213f4b02..3bcde73f5 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSessionRequest.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSessionRequest.java @@ -77,7 +77,9 @@ public class JingleSessionRequest { pts); session.setInitialSessionRequest(this); // Acknowledge the IQ reception - session.sendAck(this.getJingle()); + session.setSid(this.getSessionID()); + //session.sendAck(this.getJingle()); + //session.respond(this.getJingle()); } return session; } @@ -93,6 +95,11 @@ public class JingleSessionRequest { synchronized (manager) { session = manager.createIncomingJingleSession(this); session.setInitialSessionRequest(this); + // Acknowledge the IQ reception + session.setSid(this.getSessionID()); + //session.sendAck(this.getJingle()); + //session.updatePacketListener(); + //session.respond(this.getJingle()); } return session; } diff --git a/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleManagerTest.java b/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleManagerTest.java index 580466d68..1701a6a21 100644 --- a/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleManagerTest.java +++ b/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleManagerTest.java @@ -68,6 +68,7 @@ import org.jivesoftware.smackx.jingle.media.PayloadType; import org.jivesoftware.smackx.jingle.nat.*; import org.jivesoftware.smackx.packet.Jingle; import org.jivesoftware.smackx.provider.JingleProvider; +import org.jivesoftware.jingleaudio.jmf.JmfMediaManager; import java.net.DatagramPacket; import java.net.DatagramSocket; @@ -361,7 +362,7 @@ public class JingleManagerTest extends SmackTestCase { } public void sessionEstablished(PayloadType pt, - TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) { + TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) { incCounter(); System.out .println("Responder: the session is fully established."); @@ -441,7 +442,7 @@ public class JingleManagerTest extends SmackTestCase { } public void sessionEstablished(PayloadType pt, - TransportCandidate rc, final TransportCandidate lc, JingleSession jingleSession) { + TransportCandidate rc, final TransportCandidate lc, JingleSession jingleSession) { incCounter(); System.out .println("Responder: the session is fully established."); @@ -481,7 +482,7 @@ public class JingleManagerTest extends SmackTestCase { } public void sessionEstablished(PayloadType pt, - TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) { + TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) { incCounter(); System.out.println("Initiator: the session is fully established."); System.out.println("+ Payload Type: " + pt.getId()); @@ -515,8 +516,8 @@ public class JingleManagerTest extends SmackTestCase { resetCounter(); try { - TransportResolver tr1 = new FixedResolver("127.0.0.1", 54222); - TransportResolver tr2 = new FixedResolver("127.0.0.1", 54567); + TransportResolver tr1 = new FixedResolver("127.0.0.1", 22222); + TransportResolver tr2 = new FixedResolver("127.0.0.1", 22444); final JingleManager man0 = new JingleManager(getConnection(0), tr1); final JingleManager man1 = new JingleManager(getConnection(1), tr2); @@ -527,16 +528,16 @@ public class JingleManagerTest extends SmackTestCase { */ public void sessionRequested(final JingleSessionRequest request) { System.out.println("Session request detected, from " - + request.getFrom() + ": rejecting."); + + request.getFrom()); // We reject the request try { - IncomingJingleSession session = request.accept(null); + IncomingJingleSession session = request.accept(getTestPayloads1()); + session.setInitialSessionRequest(request); session.start(); - session.terminate(); } catch (XMPPException e) { - e.printStackTrace(); + e.printStackTrace(); } } @@ -549,6 +550,7 @@ public class JingleManagerTest extends SmackTestCase { session0.addListener(new JingleSessionListener() { public void sessionClosed(String reason, JingleSession jingleSession) { + System.out.println("The session has been closed"); } public void sessionClosedOnError(XMPPException e, JingleSession jingleSession) { @@ -562,7 +564,7 @@ public class JingleManagerTest extends SmackTestCase { } public void sessionEstablished(PayloadType pt, - TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) { + TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) { } public void sessionRedirected(String redirection, JingleSession jingleSession) { @@ -571,6 +573,10 @@ public class JingleManagerTest extends SmackTestCase { session0.start(); + Thread.sleep(50000); + + session0.terminate(); + Thread.sleep(10000); assertTrue(valCounter() > 0); @@ -594,13 +600,10 @@ public class JingleManagerTest extends SmackTestCase { ProviderManager.getInstance().addIQProvider(RTPBridge.NAME, RTPBridge.NAMESPACE, new RTPBridge.Provider()); - XMPPConnection x2 = new XMPPConnection("thiago"); - x2.connect(); - x2.login("barata6", "barata6"); - - RTPBridge response = RTPBridge.getRTPBridge(x2, "102"); + RTPBridge response = RTPBridge.getRTPBridge(getConnection(0), "102"); class Listener implements Runnable { + private byte[] buf = new byte[5000]; private DatagramSocket dataSocket; private DatagramPacket packet; @@ -617,7 +620,8 @@ public class JingleManagerTest extends SmackTestCase { dataSocket.receive(packet); incCounter(); } - } catch (Exception e) { + } + catch (Exception e) { e.printStackTrace(); } } @@ -660,14 +664,18 @@ public class JingleManagerTest extends SmackTestCase { ds0.close(); ds1.close(); - } catch (Exception e) { + } + catch (Exception e) { e.printStackTrace(); - } finally { + } + finally { } - } catch (Exception e) { + } + catch (Exception e) { e.printStackTrace(); - } finally { + } + finally { } } @@ -683,13 +691,8 @@ public class JingleManagerTest extends SmackTestCase { XMPPConnection.DEBUG_ENABLED = true; - XMPPConnection x0 = new XMPPConnection("thiago"); - XMPPConnection x1 = new XMPPConnection("thiago"); - - x0.connect(); - x0.login("barata7", "barata7"); - x1.connect(); - x1.login("barata6", "barata6"); + XMPPConnection x0 = getConnection(0); + XMPPConnection x1 = getConnection(1); final JingleManager jm0 = new JingleManager( x0, new STUNResolver() { @@ -765,14 +768,15 @@ public class JingleManagerTest extends SmackTestCase { }); session.start(); - } catch (XMPPException e) { + } + catch (XMPPException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }); - OutgoingJingleSession js0 = jm0.createOutgoingJingleSession("barata6@thiago/Smack"); + OutgoingJingleSession js0 = jm0.createOutgoingJingleSession(x1.getUser()); js0.addListener(new JingleSessionListener() { @@ -807,7 +811,8 @@ public class JingleManagerTest extends SmackTestCase { assertTrue(valCounter() == 2); //Thread.sleep(15000); - } catch (Exception e) { + } + catch (Exception e) { e.printStackTrace(); } @@ -824,23 +829,16 @@ public class JingleManagerTest extends SmackTestCase { //XMPPConnection.DEBUG_ENABLED = true; - XMPPConnection x0 = new XMPPConnection("thiago"); - XMPPConnection x1 = new XMPPConnection("thiago"); - - x0.connect(); - x0.login("barata7", "barata7"); - x1.connect(); - x1.login("barata6", "barata6"); + XMPPConnection x0 = getConnection(0); + XMPPConnection x1 = getConnection(1); final JingleManager jm0 = new JingleManager( x0, new FixedResolver("127.0.0.1", 20004)); final JingleManager jm1 = new JingleManager( x1, new FixedResolver("127.0.0.1", 20040)); -// JingleManager jm0 = new JingleSessionManager( -// x0, new ICEResolver()); -// JingleManager jm1 = new JingleSessionManager( -// x1, new ICEResolver()); + //JingleManager jm0 = new ICETransportManager(x0, "stun.xten.net", 3478); + //JingleManager jm1 = new ICETransportManager(x1, "stun.xten.net", 3478); JingleMediaManager jingleMediaManager = new JingleMediaManager() { // Media Session Implementation @@ -890,14 +888,15 @@ public class JingleManagerTest extends SmackTestCase { IncomingJingleSession session = request.accept(jm1.getMediaManager().getPayloads()); session.start(request); - } catch (XMPPException e) { + } + catch (XMPPException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }); - OutgoingJingleSession js0 = jm0.createOutgoingJingleSession("barata6@thiago/Smack"); + OutgoingJingleSession js0 = jm0.createOutgoingJingleSession(x1.getUser()); js0.start(); @@ -911,7 +910,8 @@ public class JingleManagerTest extends SmackTestCase { assertTrue(valCounter() == 8); //Thread.sleep(15000); - } catch (Exception e) { + } + catch (Exception e) { e.printStackTrace(); } @@ -977,7 +977,7 @@ public class JingleManagerTest extends SmackTestCase { } public void sessionEstablished(PayloadType pt, - TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) { + TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) { } public void sessionRedirected(String redirection, JingleSession jingleSession) {