From 81ad05d49b0593da7c3a3ef268120e44582aa8c6 Mon Sep 17 00:00:00 2001 From: Thiago Camargo Date: Wed, 14 Feb 2007 20:50:37 +0000 Subject: [PATCH] Jingle Refactoring git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7114 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smackx/jingle/IncomingJingleSession.java | 23 ++++-- .../smackx/jingle/JingleNegotiator.java | 9 ++- .../smackx/jingle/JingleSession.java | 71 ++++++++++++++----- .../listeners/JingleSessionStateListener.java | 47 ++++++++++++ .../smackx/jingle/JingleManagerTest.java | 3 +- jingle/media/test/JingleMediaTest.java | 2 - 6 files changed, 125 insertions(+), 30 deletions(-) create mode 100644 jingle/extension/source/org/jivesoftware/smackx/jingle/listeners/JingleSessionStateListener.java diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/IncomingJingleSession.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/IncomingJingleSession.java index 824dcd771..8b6f02db4 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/IncomingJingleSession.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/IncomingJingleSession.java @@ -57,6 +57,7 @@ import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smackx.jingle.listeners.JingleMediaListener; import org.jivesoftware.smackx.jingle.listeners.JingleTransportListener; +import org.jivesoftware.smackx.jingle.listeners.JingleSessionStateListener; import org.jivesoftware.smackx.jingle.media.JingleMediaManager; import org.jivesoftware.smackx.jingle.media.MediaNegotiator; import org.jivesoftware.smackx.jingle.media.PayloadType; @@ -68,12 +69,13 @@ import org.jivesoftware.smackx.packet.JingleContentDescription; import org.jivesoftware.smackx.packet.JingleContentDescription.JinglePayloadType; import org.jivesoftware.smackx.packet.JingleError; +import javax.swing.*; import java.util.List; /** * An incoming Jingle Session implementation. * This class has especific bahavior to accept and establish a received Jingle Session Request. - * + *

* This class is not directly used by users. Instead, users should refer to the * JingleManager class, that will create the appropiate instance... * @@ -90,6 +92,8 @@ public class IncomingJingleSession extends JingleSession { private JingleSessionRequest initialSessionRequest; + private boolean accepted = false; + /** * Constructor for a Jingle incoming session * @@ -98,7 +102,7 @@ public class IncomingJingleSession extends JingleSession { * @param resolver The transport resolver */ protected IncomingJingleSession(XMPPConnection conn, String responder, - List payloadTypes, TransportResolver resolver) { + List payloadTypes, TransportResolver resolver) { super(conn, responder, conn.getUser()); @@ -115,6 +119,7 @@ public class IncomingJingleSession extends JingleSession { if (resolver.getType().equals(TransportResolver.Type.ice)) { setTransportNeg(new TransportNegotiator.Ice(this, resolver)); } + } /** @@ -126,7 +131,7 @@ public class IncomingJingleSession extends JingleSession { * @param jingleMediaManager The Media Manager for this Session */ protected IncomingJingleSession(XMPPConnection conn, String responder, - List payloadTypes, TransportResolver resolver, JingleMediaManager jingleMediaManager) { + List payloadTypes, TransportResolver resolver, JingleMediaManager jingleMediaManager) { this(conn, responder, payloadTypes, resolver); this.jingleMediaManager = jingleMediaManager; } @@ -151,11 +156,13 @@ public class IncomingJingleSession extends JingleSession { updatePacketListener(); respond(packet); - } else { + } + else { throw new IllegalStateException( "Session request with null Jingle packet."); } - } else { + } + else { throw new IllegalStateException("Starting session without null state."); } } @@ -249,7 +256,7 @@ public class IncomingJingleSession extends JingleSession { jingleTransportListener = new JingleTransportListener() { public void transportEstablished(TransportCandidate local, - TransportCandidate remote) { + TransportCandidate remote) { checkFullyEstablished(); } @@ -340,7 +347,8 @@ public class IncomingJingleSession extends JingleSession { .getAcceptedLocalCandidate())) { setState(active); } - } else { + } + else { throw new JingleException(JingleError.MALFORMED_STANZA); } } @@ -374,6 +382,7 @@ public class IncomingJingleSession extends JingleSession { * "Active" state: we have an agreement about the session. */ public class Active extends JingleNegotiator.State { + public Active(JingleNegotiator neg) { super(neg); } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleNegotiator.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleNegotiator.java index ef5580d12..b8b219b85 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleNegotiator.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleNegotiator.java @@ -93,7 +93,8 @@ public abstract class JingleNegotiator { public Class getStateClass() { if (state != null) { return state.getClass(); - } else { + } + else { return Object.class; } } @@ -122,6 +123,7 @@ public abstract class JingleNegotiator { /** * Add expected ID + * * @param id */ public void addExpectedId(String id) { @@ -130,19 +132,22 @@ public abstract class JingleNegotiator { /** * Check if the passed ID is the expected ID + * * @param id * @return */ public boolean isExpectedId(String id) { if (id != null) { return id.equals(expectedAckId); - } else { + } + else { return false; } } /** * Remove and expected ID + * * @param id */ public void removeExpectedId(String id) { diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java index f8182eb8b..10abb74f6 100644 --- a/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/JingleSession.java @@ -60,10 +60,7 @@ import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.XMPPError; -import org.jivesoftware.smackx.jingle.listeners.JingleListener; -import org.jivesoftware.smackx.jingle.listeners.JingleMediaListener; -import org.jivesoftware.smackx.jingle.listeners.JingleSessionListener; -import org.jivesoftware.smackx.jingle.listeners.JingleTransportListener; +import org.jivesoftware.smackx.jingle.listeners.*; import org.jivesoftware.smackx.jingle.media.*; import org.jivesoftware.smackx.jingle.nat.TransportCandidate; import org.jivesoftware.smackx.jingle.nat.TransportNegotiator; @@ -74,10 +71,7 @@ import org.jivesoftware.smackx.packet.JingleContentInfo; import org.jivesoftware.smackx.packet.JingleError; import org.jivesoftware.smackx.packet.JingleTransport.JingleTransportCandidate; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Random; +import java.util.*; /** * An abstract Jingle session. @@ -116,10 +110,10 @@ public abstract class JingleSession extends JingleNegotiator { protected JingleMediaSession jingleMediaSession = null; - static int ccc = 0; - private boolean closed = false; + private List stateListeners = new ArrayList(); + /** * Full featured JingleSession constructor * @@ -278,6 +272,54 @@ public abstract class JingleSession extends JingleNegotiator { } } + /** + * Adds a State Listener for the Session. It will be called twice every time the Session State changed. One before State change and other after. + * + * @param listener listener to be added + */ + public void addStateListener(JingleSessionStateListener listener) { + stateListeners.add(listener); + } + + /** + * Removes a JingleStateListener + * + * @param listener listener to be removed + */ + public void removedStateListener(JingleSessionStateListener listener) { + stateListeners.remove(listener); + } + + /** + * Removes all JingleSessionStateListeners. + */ + public void removeAllStateListeners() { + stateListeners.clear(); + } + + /** + * Overides JingleNegiociator Method to add listener capabilities + * @param newState new State + */ + protected void setState(State newState) { + boolean proceed = true; + State old = getState(); + + for (JingleSessionStateListener listener : stateListeners) + try { + listener.beforeChange(old, newState); + } + catch (JingleException e) { + proceed = false; + } + + if (proceed) + super.setState(newState); + + for (JingleSessionStateListener listener : stateListeners) + listener.afterChanged(old, getState()); + } + /** * Obtain the transport negotiator for this session. * @@ -343,8 +385,6 @@ 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."); @@ -423,9 +463,6 @@ 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; @@ -875,8 +912,6 @@ public abstract class JingleSession extends JingleNegotiator { packetFilter = new PacketFilter() { public boolean accept(Packet packet) { - final int cc = ccc++; - if (packet instanceof IQ) { IQ iq = (IQ) packet; @@ -1100,7 +1135,7 @@ public abstract class JingleSession extends JingleNegotiator { destroyMediaNeg(); destroyTransportNeg(); removePacketListener(); - System.out.println("Negociation Closed: "+getConnection().getUser()); + System.out.println("Negociation Closed: " + getConnection().getUser()); closed = true; super.close(); } diff --git a/jingle/extension/source/org/jivesoftware/smackx/jingle/listeners/JingleSessionStateListener.java b/jingle/extension/source/org/jivesoftware/smackx/jingle/listeners/JingleSessionStateListener.java new file mode 100644 index 000000000..cfc9c5377 --- /dev/null +++ b/jingle/extension/source/org/jivesoftware/smackx/jingle/listeners/JingleSessionStateListener.java @@ -0,0 +1,47 @@ +/** + * $RCSfile$ + * $Revision: $ + * $Date: $11-07-2006 + * + * Copyright 2003-2006 Jive Software. + * + * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jivesoftware.smackx.jingle.listeners; + +import org.jivesoftware.smackx.jingle.JingleNegotiator; + +/** + * Used to Listen for Jingle Session State Changes + */ +public interface JingleSessionStateListener { + + /** + * Called before the State changing. If you want to cancel the State change, you MAY throw a JingleException. + * + * @param old old State + * @param newOne new State + * @throws JingleNegotiator.JingleException + * Exception. If you want to cancel the State change, you MAY throw a JingleException. + */ + public void beforeChange(JingleNegotiator.State old, JingleNegotiator.State newOne) throws JingleNegotiator.JingleException; + + /** + * Called after State Changing. + * @param old old State + * @param newOne new State + */ + public void afterChanged(JingleNegotiator.State old, JingleNegotiator.State newOne); + +} diff --git a/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleManagerTest.java b/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleManagerTest.java index 1701a6a21..a34abc8a8 100644 --- a/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleManagerTest.java +++ b/jingle/extension/test/org/jivesoftware/smackx/jingle/JingleManagerTest.java @@ -535,6 +535,7 @@ public class JingleManagerTest extends SmackTestCase { IncomingJingleSession session = request.accept(getTestPayloads1()); session.setInitialSessionRequest(request); session.start(); + session.terminate(); } catch (XMPPException e) { e.printStackTrace(); @@ -575,7 +576,7 @@ public class JingleManagerTest extends SmackTestCase { Thread.sleep(50000); - session0.terminate(); + //session0.terminate(); Thread.sleep(10000); diff --git a/jingle/media/test/JingleMediaTest.java b/jingle/media/test/JingleMediaTest.java index c42db49b9..983c04b0c 100644 --- a/jingle/media/test/JingleMediaTest.java +++ b/jingle/media/test/JingleMediaTest.java @@ -48,8 +48,6 @@ public class JingleMediaTest extends SmackTestCase { try { - //XMPPConnection.DEBUG_ENABLED = true; - XMPPConnection x0 = getConnection(0); XMPPConnection x1 = getConnection(1);