2007-01-04 18:25:30 +01:00
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* $RCSfile: JingleSession.java,v $
|
|
|
|
* $Revision: 1.20 $
|
|
|
|
* $Date: 2007/07/18 18:29:21 $
|
2007-01-04 18:25:30 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2002-2006 Jive Software. All rights reserved.
|
|
|
|
* ====================================================================
|
|
|
|
* The Jive Software License (based on Apache Software License, Version 1.1)
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. The end-user documentation included with the redistribution,
|
|
|
|
* if any, must include the following acknowledgment:
|
|
|
|
* "This product includes software developed by
|
|
|
|
* Jive Software (http://www.jivesoftware.com)."
|
|
|
|
* Alternately, this acknowledgment may appear in the software itself,
|
|
|
|
* if and wherever such third-party acknowledgments normally appear.
|
|
|
|
*
|
|
|
|
* 4. The names "Smack" and "Jive Software" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please
|
|
|
|
* contact webmaster@jivesoftware.com.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "Smack",
|
|
|
|
* nor may "Smack" appear in their name, without prior written
|
|
|
|
* permission of Jive Software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smackx.jingle;
|
|
|
|
|
2008-10-30 22:20:29 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
import org.jivesoftware.smack.ConnectionListener;
|
|
|
|
import org.jivesoftware.smack.PacketListener;
|
2010-02-09 12:55:56 +01:00
|
|
|
import org.jivesoftware.smack.Connection;
|
2007-01-04 18:25:30 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
2008-05-20 00:54:19 +02:00
|
|
|
import org.jivesoftware.smack.packet.PacketExtension;
|
2007-01-04 18:25:30 +01:00
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
2008-05-20 00:54:19 +02:00
|
|
|
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;
|
2008-10-30 22:20:29 +01:00
|
|
|
import org.jivesoftware.smackx.jingle.media.JingleMediaManager;
|
|
|
|
import org.jivesoftware.smackx.jingle.media.JingleMediaSession;
|
|
|
|
import org.jivesoftware.smackx.jingle.media.MediaNegotiator;
|
|
|
|
import org.jivesoftware.smackx.jingle.media.MediaReceivedListener;
|
|
|
|
import org.jivesoftware.smackx.jingle.media.PayloadType;
|
2008-05-20 00:54:19 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.nat.JingleTransportManager;
|
2007-01-04 18:25:30 +01:00
|
|
|
import org.jivesoftware.smackx.jingle.nat.TransportCandidate;
|
|
|
|
import org.jivesoftware.smackx.jingle.nat.TransportNegotiator;
|
2008-05-20 00:54:19 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.nat.TransportResolver;
|
2007-01-04 18:25:30 +01:00
|
|
|
import org.jivesoftware.smackx.packet.Jingle;
|
|
|
|
import org.jivesoftware.smackx.packet.JingleError;
|
|
|
|
|
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* An abstract Jingle session. <p/> This class contains some basic properties of
|
|
|
|
* every Jingle session. However, the concrete implementation can be found in
|
|
|
|
* subclasses.
|
|
|
|
*
|
2007-01-04 18:25:30 +01:00
|
|
|
* @author Alvaro Saurin
|
2008-05-20 00:54:19 +02:00
|
|
|
* @author Jeff Williams
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public class JingleSession extends JingleNegotiator implements MediaReceivedListener {
|
2007-01-04 18:25:30 +01:00
|
|
|
|
2008-10-30 22:20:29 +01:00
|
|
|
private static final SmackLogger LOGGER = SmackLogger.getLogger(JingleSession.class);
|
|
|
|
|
|
|
|
// static
|
2012-10-26 12:47:55 +02:00
|
|
|
private static final HashMap<Connection, JingleSession> sessions = new HashMap<Connection, JingleSession>();
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
private static final Random randomGenerator = new Random();
|
|
|
|
|
|
|
|
// non-static
|
|
|
|
|
|
|
|
private String initiator; // Who started the communication
|
|
|
|
|
|
|
|
private String responder; // The other endpoint
|
|
|
|
|
|
|
|
private String sid; // A unique id that identifies this session
|
|
|
|
|
2008-10-30 22:20:29 +01:00
|
|
|
ConnectionListener connectionListener;
|
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
PacketListener packetListener;
|
|
|
|
|
|
|
|
PacketFilter packetFilter;
|
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
protected List<JingleMediaManager> jingleMediaManagers = null;
|
2007-01-04 18:25:30 +01:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
private JingleSessionState sessionState;
|
|
|
|
|
|
|
|
private List<ContentNegotiator> contentNegotiators;
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private Connection connection;
|
2008-05-20 00:54:19 +02:00
|
|
|
|
|
|
|
private String sessionInitPacketID;
|
|
|
|
|
|
|
|
private Map<String, JingleMediaSession> mediaSessionMap;
|
2007-02-14 21:50:37 +01:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
/**
|
|
|
|
* Full featured JingleSession constructor
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param conn
|
2010-02-09 12:55:56 +01:00
|
|
|
* the Connection which is used
|
2008-05-20 00:54:19 +02:00
|
|
|
* @param initiator
|
|
|
|
* the initiator JID
|
|
|
|
* @param responder
|
|
|
|
* the responder JID
|
|
|
|
* @param sessionid
|
|
|
|
* the session ID
|
|
|
|
* @param jingleMediaManager
|
|
|
|
* the jingleMediaManager
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public JingleSession(Connection conn, String initiator, String responder, String sessionid,
|
2008-05-20 00:54:19 +02:00
|
|
|
List<JingleMediaManager> jingleMediaManagers) {
|
|
|
|
super();
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
this.initiator = initiator;
|
|
|
|
this.responder = responder;
|
|
|
|
this.sid = sessionid;
|
2008-05-20 00:54:19 +02:00
|
|
|
this.jingleMediaManagers = jingleMediaManagers;
|
|
|
|
this.setSession(this);
|
|
|
|
this.connection = conn;
|
2007-01-04 18:25:30 +01:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
// Initially, we don't known the session state.
|
|
|
|
setSessionState(JingleSessionStateUnknown.getInstance());
|
|
|
|
|
|
|
|
contentNegotiators = new ArrayList<ContentNegotiator>();
|
|
|
|
mediaSessionMap = new HashMap<String, JingleMediaSession>();
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
// Add the session to the list and register the listeneres
|
|
|
|
registerInstance();
|
|
|
|
installConnectionListeners(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* JingleSession constructor (for an outgoing Jingle session)
|
|
|
|
*
|
|
|
|
* @param conn
|
2010-02-09 12:55:56 +01:00
|
|
|
* Connection
|
2008-05-20 00:54:19 +02:00
|
|
|
* @param initiator
|
|
|
|
* the initiator JID
|
|
|
|
* @param responder
|
|
|
|
* the responder JID
|
|
|
|
* @param jingleMediaManager
|
|
|
|
* the jingleMediaManager
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public JingleSession(Connection conn, JingleSessionRequest request, String initiator, String responder,
|
2008-05-20 00:54:19 +02:00
|
|
|
List<JingleMediaManager> jingleMediaManagers) {
|
|
|
|
this(conn, initiator, responder, generateSessionId(), jingleMediaManagers);
|
2012-10-26 12:47:55 +02:00
|
|
|
//sessionRequest = request; // unused
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the session initiator
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
2007-01-04 18:25:30 +01:00
|
|
|
* @return the initiator
|
|
|
|
*/
|
|
|
|
public String getInitiator() {
|
|
|
|
return initiator;
|
|
|
|
}
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
public Connection getConnection() {
|
2008-05-20 00:54:19 +02:00
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
/**
|
|
|
|
* Set the session initiator
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param initiator
|
|
|
|
* the initiator to set
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
public void setInitiator(String initiator) {
|
|
|
|
this.initiator = initiator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Media Manager of this Jingle Session
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
2007-01-04 18:25:30 +01:00
|
|
|
* @return
|
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public List<JingleMediaManager> getMediaManagers() {
|
|
|
|
return jingleMediaManagers;
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the Media Manager of this Jingle Session
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
2007-01-04 18:25:30 +01:00
|
|
|
* @param jingleMediaManager
|
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public void setMediaManagers(List<JingleMediaManager> jingleMediaManagers) {
|
|
|
|
this.jingleMediaManagers = jingleMediaManagers;
|
2007-03-22 05:32:07 +01:00
|
|
|
}
|
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
/**
|
|
|
|
* Get the session responder
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
2007-01-04 18:25:30 +01:00
|
|
|
* @return the responder
|
|
|
|
*/
|
|
|
|
public String getResponder() {
|
|
|
|
return responder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the session responder.
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param responder
|
|
|
|
* the receptor to set
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
public void setResponder(String responder) {
|
|
|
|
this.responder = responder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the session ID
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
2007-01-04 18:25:30 +01:00
|
|
|
* @return the sid
|
|
|
|
*/
|
|
|
|
public String getSid() {
|
|
|
|
return sid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the session ID
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param sessionId
|
|
|
|
* the sid to set
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
protected void setSid(String sessionId) {
|
|
|
|
sid = sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a unique session ID.
|
|
|
|
*/
|
2007-03-12 04:25:06 +01:00
|
|
|
protected static String generateSessionId() {
|
2007-01-04 18:25:30 +01:00
|
|
|
return String.valueOf(Math.abs(randomGenerator.nextLong()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* Validate the state changes.
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
public void setSessionState(JingleSessionState stateIs) {
|
2007-02-14 21:50:37 +01:00
|
|
|
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("Session state change: " + sessionState + "->" + stateIs);
|
2008-05-20 00:54:19 +02:00
|
|
|
stateIs.enter();
|
|
|
|
sessionState = stateIs;
|
2007-02-14 21:50:37 +01:00
|
|
|
}
|
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
public JingleSessionState getSessionState() {
|
|
|
|
return sessionState;
|
2007-02-14 21:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* Return true if all of the media managers have finished
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
public boolean isFullyEstablished() {
|
2008-05-20 00:54:19 +02:00
|
|
|
boolean result = true;
|
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
if (!contentNegotiator.isFullyEstablished())
|
|
|
|
result = false;
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
return result;
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
// ----------------------------------------------------------------------------------------------------------
|
|
|
|
// Receive section
|
|
|
|
// ----------------------------------------------------------------------------------------------------------
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* Process and respond to an incoming packet. <p/> This method is called
|
|
|
|
* from the packet listener dispatcher when a new packet has arrived. The
|
|
|
|
* method is responsible for recognizing the packet type and, depending on
|
|
|
|
* the current state, delivering it to the right event handler and wait for
|
|
|
|
* a response. The response will be another Jingle packet that will be sent
|
|
|
|
* to the other end point.
|
|
|
|
*
|
|
|
|
* @param iq
|
|
|
|
* the packet received
|
2007-01-04 18:25:30 +01:00
|
|
|
* @return the new Jingle packet to send.
|
|
|
|
* @throws XMPPException
|
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public synchronized void receivePacketAndRespond(IQ iq) throws XMPPException {
|
|
|
|
List<IQ> responses = new ArrayList<IQ>();
|
2007-01-04 18:25:30 +01:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
String responseId = null;
|
2007-03-27 17:44:07 +02:00
|
|
|
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("Packet: " + iq.toXML());
|
2007-03-27 17:44:07 +02:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
try {
|
2007-04-06 06:37:44 +02:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
// Dispatch the packet to the JingleNegotiators and get back a list of the results.
|
|
|
|
responses.addAll(dispatchIncomingPacket(iq, null));
|
|
|
|
|
|
|
|
if (iq != null) {
|
|
|
|
responseId = iq.getPacketID();
|
|
|
|
|
|
|
|
// Send the IQ to each of the content negotiators for further processing.
|
|
|
|
// Each content negotiator may pass back a list of JingleContent for addition to the response packet.
|
|
|
|
|
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
2008-12-06 03:15:42 +01:00
|
|
|
// If at this point the content negotiator isn't started, it's because we sent a session-init jingle
|
|
|
|
// packet from startOutgoing() and we're waiting for the other side to let us know they're ready
|
|
|
|
// to take jingle packets. (This packet might be a session-terminate, but that will get handled
|
|
|
|
// later.
|
|
|
|
if (!contentNegotiator.isStarted()) {
|
|
|
|
contentNegotiator.start();
|
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
responses.addAll(contentNegotiator.dispatchIncomingPacket(iq, responseId));
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
// Acknowledge the IQ reception
|
|
|
|
// Not anymore. The state machine generates an appropriate response IQ that
|
|
|
|
// gets sent back at the end of this routine.
|
|
|
|
//sendAck(iq);
|
|
|
|
|
|
|
|
} catch (JingleException e) {
|
|
|
|
// Send an error message, if present
|
|
|
|
JingleError error = e.getError();
|
|
|
|
if (error != null) {
|
|
|
|
responses.add(createJingleError(iq, error));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the session end and close everything...
|
|
|
|
triggerSessionClosedOnError(e);
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
// // If the response is anything other than a RESULT then send it now.
|
|
|
|
// if ((response != null) && (!response.getType().equals(IQ.Type.RESULT))) {
|
|
|
|
// getConnection().sendPacket(response);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Loop through all of the responses and send them.
|
|
|
|
for (IQ response : responses) {
|
|
|
|
sendPacket(response);
|
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* Dispatch an incoming packet. The method is responsible for recognizing
|
|
|
|
* the packet type and, depending on the current state, delivering the
|
|
|
|
* packet to the right event handler and wait for a response.
|
|
|
|
*
|
|
|
|
* @param iq
|
|
|
|
* the packet received
|
2007-01-04 18:25:30 +01:00
|
|
|
* @return the new Jingle packet to send.
|
|
|
|
* @throws XMPPException
|
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public List<IQ> dispatchIncomingPacket(IQ iq, String id) throws XMPPException {
|
|
|
|
List<IQ> responses = new ArrayList<IQ>();
|
2007-01-04 18:25:30 +01:00
|
|
|
IQ response = null;
|
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
if (iq != null) {
|
|
|
|
if (iq.getType().equals(IQ.Type.ERROR)) {
|
|
|
|
// Process errors
|
|
|
|
// TODO getState().eventError(iq);
|
|
|
|
} else if (iq.getType().equals(IQ.Type.RESULT)) {
|
|
|
|
// Process ACKs
|
|
|
|
if (isExpectedId(iq.getPacketID())) {
|
|
|
|
|
|
|
|
// The other side provisionally accepted our session-initiate.
|
|
|
|
// Kick off some negotiators.
|
|
|
|
if (iq.getPacketID().equals(sessionInitPacketID)) {
|
|
|
|
startNegotiators();
|
|
|
|
}
|
|
|
|
removeExpectedId(iq.getPacketID());
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
} else if (iq instanceof Jingle) {
|
|
|
|
// It is not an error: it is a Jingle packet...
|
|
|
|
Jingle jin = (Jingle) iq;
|
|
|
|
JingleActionEnum action = jin.getAction();
|
|
|
|
|
|
|
|
// Depending on the state we're in we'll get different processing actions.
|
|
|
|
// (See Design Patterns AKA GoF State behavioral pattern.)
|
2008-12-06 03:15:42 +01:00
|
|
|
response = getSessionState().processJingle(this, jin, action);
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
if (response != null) {
|
|
|
|
// Save the packet id, for recognizing ACKs...
|
|
|
|
addExpectedId(response.getPacketID());
|
|
|
|
responses.add(response);
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
return responses;
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* Add a new content negotiator on behalf of a <content> section received.
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public void addContentNegotiator(ContentNegotiator inContentNegotiator) {
|
|
|
|
contentNegotiators.add(inContentNegotiator);
|
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------------
|
|
|
|
// Send section
|
|
|
|
// ----------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public void sendPacket(IQ iq) {
|
|
|
|
|
|
|
|
if (iq instanceof Jingle) {
|
|
|
|
|
|
|
|
sendFormattedJingle((Jingle) iq);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
getConnection().sendPacket(iq);
|
2007-03-20 19:52:57 +01:00
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* Complete and send a packet. Complete all the null fields in a Jingle
|
|
|
|
* reponse, using the session information we have.
|
|
|
|
*
|
|
|
|
* @param jout
|
|
|
|
* the Jingle packet we want to complete and send
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public Jingle sendFormattedJingle(Jingle jout) {
|
|
|
|
return sendFormattedJingle(null, jout);
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Complete and send a packet. Complete all the null fields in a Jingle
|
|
|
|
* reponse, using the session information we have or some info from the
|
|
|
|
* incoming packet.
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param iq
|
|
|
|
* The Jingle packet we are responing to
|
|
|
|
* @param jout
|
|
|
|
* the Jingle packet we want to complete and send
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
public Jingle sendFormattedJingle(IQ iq, Jingle jout) {
|
|
|
|
if (jout != null) {
|
|
|
|
if (jout.getInitiator() == null) {
|
|
|
|
jout.setInitiator(getInitiator());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jout.getResponder() == null) {
|
|
|
|
jout.setResponder(getResponder());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jout.getSid() == null) {
|
|
|
|
jout.setSid(getSid());
|
|
|
|
}
|
|
|
|
|
|
|
|
String me = getConnection().getUser();
|
|
|
|
String other = getResponder().equals(me) ? getInitiator() : getResponder();
|
|
|
|
|
|
|
|
if (jout.getTo() == null) {
|
|
|
|
if (iq != null) {
|
|
|
|
jout.setTo(iq.getFrom());
|
2008-05-20 00:54:19 +02:00
|
|
|
} else {
|
2007-01-04 18:25:30 +01:00
|
|
|
jout.setTo(other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jout.getFrom() == null) {
|
|
|
|
if (iq != null) {
|
|
|
|
jout.setFrom(iq.getTo());
|
2008-05-20 00:54:19 +02:00
|
|
|
} else {
|
2007-01-04 18:25:30 +01:00
|
|
|
jout.setFrom(me);
|
|
|
|
}
|
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
|
|
|
|
// The the packet.
|
2008-10-30 22:20:29 +01:00
|
|
|
if ((getConnection() != null) && (getConnection().isConnected()))
|
|
|
|
getConnection().sendPacket(jout);
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
return jout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* @param inJingle
|
|
|
|
* @param inAction
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
// private void sendUnknownStateAction(Jingle inJingle, JingleActionEnum inAction) {
|
|
|
|
//
|
|
|
|
// if (inAction == JingleActionEnum.SESSION_INITIATE) {
|
|
|
|
// // Prepare to receive and act on response packets.
|
|
|
|
// updatePacketListener();
|
|
|
|
//
|
|
|
|
// // Send the actual packet.
|
|
|
|
// sendPacket(inJingle);
|
|
|
|
//
|
|
|
|
// // Change to the PENDING state.
|
|
|
|
// setSessionState(JingleSessionStateEnum.PENDING);
|
|
|
|
// } else {
|
|
|
|
// throw new IllegalStateException("Only session-initiate allowed in the UNKNOWN state.");
|
|
|
|
// }
|
|
|
|
// }
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Acknowledge a IQ packet.
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param iq
|
|
|
|
* The IQ to acknowledge
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public IQ createAck(IQ iq) {
|
2007-01-04 18:25:30 +01:00
|
|
|
IQ result = null;
|
|
|
|
|
|
|
|
if (iq != null) {
|
|
|
|
// Don't acknowledge ACKs, errors...
|
|
|
|
if (iq.getType().equals(IQ.Type.SET)) {
|
2008-05-20 00:54:19 +02:00
|
|
|
IQ ack = createIQ(iq.getPacketID(), iq.getFrom(), iq.getTo(), IQ.Type.RESULT);
|
2007-01-04 18:25:30 +01:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
// No! Don't send it. Let it flow to the normal way IQ results get processed and sent.
|
|
|
|
// getConnection().sendPacket(ack);
|
2007-01-04 18:25:30 +01:00
|
|
|
result = ack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a content info message.
|
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
// public synchronized void sendContentInfo(ContentInfo ci) {
|
|
|
|
// sendPacket(new Jingle(new JingleContentInfo(ci)));
|
|
|
|
// }
|
2007-01-04 18:25:30 +01:00
|
|
|
/*
|
2008-05-20 00:54:19 +02:00
|
|
|
* (non-Javadoc)
|
|
|
|
*
|
|
|
|
* @see java.lang.Object#hashCode()
|
|
|
|
*/
|
2007-01-04 18:25:30 +01:00
|
|
|
public int hashCode() {
|
|
|
|
return Jingle.getSessionHash(getSid(), getInitiator());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-05-20 00:54:19 +02:00
|
|
|
* (non-Javadoc)
|
|
|
|
*
|
|
|
|
* @see java.lang.Object#equals(java.lang.Object)
|
|
|
|
*/
|
2007-01-04 18:25:30 +01:00
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (getClass() != obj.getClass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final JingleSession other = (JingleSession) obj;
|
|
|
|
|
|
|
|
if (initiator == null) {
|
|
|
|
if (other.initiator != null) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
} else if (!initiator.equals(other.initiator)) {
|
|
|
|
// Todo check behavior
|
|
|
|
// return false;
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (responder == null) {
|
|
|
|
if (other.responder != null) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
} else if (!responder.equals(other.responder)) {
|
2007-01-04 18:25:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sid == null) {
|
|
|
|
if (other.sid != null) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
} else if (!sid.equals(other.sid)) {
|
2007-01-04 18:25:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instances management
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean a session from the list.
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param connection
|
|
|
|
* The connection to clean up
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
private void unregisterInstanceFor(Connection connection) {
|
2007-01-04 18:25:30 +01:00
|
|
|
synchronized (sessions) {
|
|
|
|
sessions.remove(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register this instance.
|
|
|
|
*/
|
|
|
|
private void registerInstance() {
|
|
|
|
synchronized (sessions) {
|
|
|
|
sessions.put(getConnection(), this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the JingleSession related to a particular connection.
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param con
|
|
|
|
* A XMPP connection
|
2007-01-04 18:25:30 +01:00
|
|
|
* @return a Jingle session
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public static JingleSession getInstanceFor(Connection con) {
|
2007-01-04 18:25:30 +01:00
|
|
|
if (con == null) {
|
|
|
|
throw new IllegalArgumentException("Connection cannot be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
JingleSession result = null;
|
|
|
|
synchronized (sessions) {
|
|
|
|
if (sessions.containsKey(con)) {
|
2012-10-26 12:47:55 +02:00
|
|
|
result = sessions.get(con);
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure a session, setting some action listeners...
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param connection
|
|
|
|
* The connection to set up
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
private void installConnectionListeners(final Connection connection) {
|
2007-01-04 18:25:30 +01:00
|
|
|
if (connection != null) {
|
2008-10-30 22:20:29 +01:00
|
|
|
connectionListener = new ConnectionListener() {
|
2007-01-04 18:25:30 +01:00
|
|
|
public void connectionClosed() {
|
|
|
|
unregisterInstanceFor(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void connectionClosedOnError(java.lang.Exception e) {
|
|
|
|
unregisterInstanceFor(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectingIn(int i) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionSuccessful() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionFailed(Exception exception) {
|
|
|
|
}
|
2008-10-30 22:20:29 +01:00
|
|
|
};
|
|
|
|
connection.addConnectionListener(connectionListener);
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
2008-10-30 22:20:29 +01:00
|
|
|
|
|
|
|
private void removeConnectionListener() {
|
|
|
|
if (connectionListener != null) {
|
|
|
|
getConnection().removeConnectionListener(connectionListener);
|
|
|
|
|
|
|
|
LOGGER.debug("JINGLE SESSION: REMOVE CONNECTION LISTENER");
|
|
|
|
}
|
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the packet listener used for processing packet.
|
|
|
|
*/
|
|
|
|
protected void removePacketListener() {
|
|
|
|
if (packetListener != null) {
|
|
|
|
getConnection().removePacketListener(packetListener);
|
2007-02-23 17:07:23 +01:00
|
|
|
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("JINGLE SESSION: REMOVE PACKET LISTENER");
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Install the packet listener. The listener is responsible for responding
|
|
|
|
* to any packet that we receive...
|
|
|
|
*/
|
|
|
|
protected void updatePacketListener() {
|
|
|
|
removePacketListener();
|
|
|
|
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("UpdatePacketListener");
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
packetListener = new PacketListener() {
|
|
|
|
public void processPacket(Packet packet) {
|
|
|
|
try {
|
2008-05-20 00:54:19 +02:00
|
|
|
receivePacketAndRespond((IQ) packet);
|
|
|
|
} catch (XMPPException e) {
|
2007-01-04 18:25:30 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
packetFilter = new PacketFilter() {
|
|
|
|
public boolean accept(Packet packet) {
|
|
|
|
|
|
|
|
if (packet instanceof IQ) {
|
|
|
|
IQ iq = (IQ) packet;
|
|
|
|
|
|
|
|
String me = getConnection().getUser();
|
|
|
|
|
|
|
|
if (!iq.getTo().equals(me)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
String other = getResponder().equals(me) ? getInitiator() : getResponder();
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
if (iq.getFrom() == null || !iq.getFrom().equals(other == null ? "" : other)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iq instanceof Jingle) {
|
|
|
|
Jingle jin = (Jingle) iq;
|
|
|
|
|
|
|
|
String sid = jin.getSid();
|
2007-02-14 03:35:04 +01:00
|
|
|
if (sid == null || !sid.equals(getSid())) {
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("Ignored Jingle(SID) " + sid + "|" + getSid() + " :" + iq.toXML());
|
2007-01-04 18:25:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
String ini = jin.getInitiator();
|
|
|
|
if (!ini.equals(getInitiator())) {
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("Ignored Jingle(INI): " + iq.toXML());
|
2007-01-04 18:25:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
} else {
|
2007-01-04 18:25:30 +01:00
|
|
|
// We accept some non-Jingle IQ packets: ERRORs and ACKs
|
|
|
|
if (iq.getType().equals(IQ.Type.SET)) {
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("Ignored Jingle(TYPE): " + iq.toXML());
|
2007-01-04 18:25:30 +01:00
|
|
|
return false;
|
2008-05-20 00:54:19 +02:00
|
|
|
} else if (iq.getType().equals(IQ.Type.GET)) {
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("Ignored Jingle(TYPE): " + iq.toXML());
|
2007-01-04 18:25:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
getConnection().addPacketListener(packetListener, packetFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listeners
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a listener for jmf negotiation events
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param li
|
|
|
|
* The listener
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
public void addMediaListener(JingleMediaListener li) {
|
2008-05-20 00:54:19 +02:00
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
if (contentNegotiator.getMediaNegotiator() != null) {
|
|
|
|
contentNegotiator.getMediaNegotiator().addListener(li);
|
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a listener for jmf negotiation events
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param li
|
|
|
|
* The listener
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
public void removeMediaListener(JingleMediaListener li) {
|
2008-05-20 00:54:19 +02:00
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
if (contentNegotiator.getMediaNegotiator() != null) {
|
|
|
|
contentNegotiator.getMediaNegotiator().removeListener(li);
|
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a listener for transport negotiation events
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param li
|
|
|
|
* The listener
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
public void addTransportListener(JingleTransportListener li) {
|
2008-05-20 00:54:19 +02:00
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
if (contentNegotiator.getTransportNegotiator() != null) {
|
|
|
|
contentNegotiator.getTransportNegotiator().addListener(li);
|
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a listener for transport negotiation events
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param li
|
|
|
|
* The listener
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
|
|
|
public void removeTransportListener(JingleTransportListener li) {
|
2008-05-20 00:54:19 +02:00
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
if (contentNegotiator.getTransportNegotiator() != null) {
|
|
|
|
contentNegotiator.getTransportNegotiator().removeListener(li);
|
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
/**
|
|
|
|
* Setup the listeners that act on events coming from the lower level negotiators.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void setupListeners() {
|
|
|
|
|
|
|
|
JingleMediaListener jingleMediaListener = new JingleMediaListener() {
|
|
|
|
public void mediaClosed(PayloadType cand) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void mediaEstablished(PayloadType pt) {
|
|
|
|
if (isFullyEstablished()) {
|
|
|
|
Jingle jout = new Jingle(JingleActionEnum.SESSION_ACCEPT);
|
|
|
|
|
|
|
|
// Build up a response packet from each media manager.
|
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
if (contentNegotiator.getNegotiatorState() == JingleNegotiatorState.SUCCEEDED)
|
|
|
|
jout.addContent(contentNegotiator.getJingleContent());
|
|
|
|
}
|
|
|
|
// Send the "accept" and wait for the ACK
|
|
|
|
addExpectedId(jout.getPacketID());
|
|
|
|
sendPacket(jout);
|
|
|
|
|
|
|
|
//triggerSessionEstablished();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JingleTransportListener jingleTransportListener = new JingleTransportListener() {
|
|
|
|
|
|
|
|
public void transportEstablished(TransportCandidate local, TransportCandidate remote) {
|
|
|
|
if (isFullyEstablished()) {
|
2008-10-30 22:20:29 +01:00
|
|
|
|
|
|
|
// Indicate that this session is active.
|
|
|
|
setSessionState(JingleSessionStateActive.getInstance());
|
|
|
|
|
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
2008-05-20 00:54:19 +02:00
|
|
|
if (contentNegotiator.getNegotiatorState() == JingleNegotiatorState.SUCCEEDED)
|
|
|
|
contentNegotiator.triggerContentEstablished();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getSessionState().equals(JingleSessionStatePending.getInstance())) {
|
2008-10-30 22:20:29 +01:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
Jingle jout = new Jingle(JingleActionEnum.SESSION_ACCEPT);
|
|
|
|
|
|
|
|
// Build up a response packet from each media manager.
|
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
if (contentNegotiator.getNegotiatorState() == JingleNegotiatorState.SUCCEEDED)
|
|
|
|
jout.addContent(contentNegotiator.getJingleContent());
|
|
|
|
}
|
|
|
|
// Send the "accept" and wait for the ACK
|
|
|
|
addExpectedId(jout.getPacketID());
|
|
|
|
sendPacket(jout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void transportClosed(TransportCandidate cand) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void transportClosedOnError(XMPPException e) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
addMediaListener(jingleMediaListener);
|
|
|
|
addTransportListener(jingleTransportListener);
|
|
|
|
}
|
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
// Triggers
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger a session closed event.
|
|
|
|
*/
|
|
|
|
protected void triggerSessionClosed(String reason) {
|
2008-05-20 00:54:19 +02:00
|
|
|
// for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
//
|
|
|
|
// contentNegotiator.stopJingleMediaSession();
|
|
|
|
//
|
|
|
|
// for (TransportCandidate candidate : contentNegotiator.getTransportNegotiator().getOfferedCandidates())
|
|
|
|
// candidate.removeCandidateEcho();
|
|
|
|
// }
|
|
|
|
|
|
|
|
List<JingleListener> listeners = getListenersList();
|
|
|
|
for (JingleListener li : listeners) {
|
2007-01-04 18:25:30 +01:00
|
|
|
if (li instanceof JingleSessionListener) {
|
|
|
|
JingleSessionListener sli = (JingleSessionListener) li;
|
|
|
|
sli.sessionClosed(reason, this);
|
|
|
|
}
|
|
|
|
}
|
2007-03-03 06:20:17 +01:00
|
|
|
close();
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger a session closed event due to an error.
|
|
|
|
*/
|
|
|
|
protected void triggerSessionClosedOnError(XMPPException exc) {
|
2008-05-20 00:54:19 +02:00
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
|
|
|
|
contentNegotiator.stopJingleMediaSession();
|
|
|
|
|
|
|
|
for (TransportCandidate candidate : contentNegotiator.getTransportNegotiator().getOfferedCandidates())
|
|
|
|
candidate.removeCandidateEcho();
|
|
|
|
}
|
|
|
|
List<JingleListener> listeners = getListenersList();
|
|
|
|
for (JingleListener li : listeners) {
|
2007-01-04 18:25:30 +01:00
|
|
|
if (li instanceof JingleSessionListener) {
|
|
|
|
JingleSessionListener sli = (JingleSessionListener) li;
|
2007-02-07 05:57:23 +01:00
|
|
|
sli.sessionClosedOnError(exc, this);
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
2007-03-03 06:20:17 +01:00
|
|
|
close();
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger a session established event.
|
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
// protected void triggerSessionEstablished() {
|
|
|
|
// List<JingleListener> listeners = getListenersList();
|
|
|
|
// for (JingleListener li : listeners) {
|
|
|
|
// if (li instanceof JingleSessionListener) {
|
|
|
|
// JingleSessionListener sli = (JingleSessionListener) li;
|
|
|
|
// sli.sessionEstablished(this);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2007-04-06 06:37:44 +02:00
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* Trigger a media received event.
|
2007-04-06 06:37:44 +02:00
|
|
|
*/
|
|
|
|
protected void triggerMediaReceived(String participant) {
|
2008-05-20 00:54:19 +02:00
|
|
|
List<JingleListener> listeners = getListenersList();
|
|
|
|
for (JingleListener li : listeners) {
|
2007-04-06 06:37:44 +02:00
|
|
|
if (li instanceof JingleSessionListener) {
|
|
|
|
JingleSessionListener sli = (JingleSessionListener) li;
|
|
|
|
sli.sessionMediaReceived(this, participant);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
/**
|
|
|
|
* Trigger a session redirect event.
|
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
// protected void triggerSessionRedirect(String arg) {
|
|
|
|
// List<JingleListener> listeners = getListenersList();
|
|
|
|
// for (JingleListener li : listeners) {
|
|
|
|
// if (li instanceof JingleSessionListener) {
|
|
|
|
// JingleSessionListener sli = (JingleSessionListener) li;
|
|
|
|
// sli.sessionRedirected(arg, this);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2007-01-04 18:25:30 +01:00
|
|
|
/**
|
2008-05-20 00:54:19 +02:00
|
|
|
* Trigger a session decline event.
|
2007-01-04 18:25:30 +01:00
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
// protected void triggerSessionDeclined(String reason) {
|
|
|
|
// List<JingleListener> listeners = getListenersList();
|
|
|
|
// for (JingleListener li : listeners) {
|
|
|
|
// if (li instanceof JingleSessionListener) {
|
|
|
|
// JingleSessionListener sli = (JingleSessionListener) li;
|
|
|
|
// sli.sessionDeclined(reason, this);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
// for (TransportCandidate candidate : contentNegotiator.getTransportNegotiator().getOfferedCandidates())
|
|
|
|
// candidate.removeCandidateEcho();
|
|
|
|
// }
|
|
|
|
// }
|
2007-01-04 18:25:30 +01:00
|
|
|
/**
|
2007-04-06 18:43:22 +02:00
|
|
|
* Terminates the session with default reason.
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
2007-01-04 18:25:30 +01:00
|
|
|
* @throws XMPPException
|
|
|
|
*/
|
|
|
|
public void terminate() throws XMPPException {
|
2007-04-06 18:43:22 +02:00
|
|
|
terminate("Closed Locally");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminates the session with a custom reason.
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
2007-04-06 18:43:22 +02:00
|
|
|
* @throws XMPPException
|
|
|
|
*/
|
|
|
|
public void terminate(String reason) throws XMPPException {
|
2008-05-20 00:54:19 +02:00
|
|
|
if (isClosed())
|
|
|
|
return;
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("Terminate " + reason);
|
2008-05-20 00:54:19 +02:00
|
|
|
Jingle jout = new Jingle(JingleActionEnum.SESSION_TERMINATE);
|
2007-02-09 04:49:43 +01:00
|
|
|
jout.setType(IQ.Type.SET);
|
2008-05-20 00:54:19 +02:00
|
|
|
sendPacket(jout);
|
2007-04-06 18:43:22 +02:00
|
|
|
triggerSessionClosed(reason);
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminate negotiations.
|
|
|
|
*/
|
|
|
|
public void close() {
|
2008-05-20 00:54:19 +02:00
|
|
|
if (isClosed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Set the session state to ENDED.
|
|
|
|
setSessionState(JingleSessionStateEnded.getInstance());
|
|
|
|
|
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
|
|
|
|
contentNegotiator.stopJingleMediaSession();
|
|
|
|
|
|
|
|
for (TransportCandidate candidate : contentNegotiator.getTransportNegotiator().getOfferedCandidates())
|
|
|
|
candidate.removeCandidateEcho();
|
|
|
|
|
|
|
|
contentNegotiator.close();
|
|
|
|
}
|
2007-02-07 05:57:23 +01:00
|
|
|
removePacketListener();
|
2008-10-30 22:20:29 +01:00
|
|
|
removeConnectionListener();
|
|
|
|
getConnection().removeConnectionListener(connectionListener);
|
|
|
|
LOGGER.debug("Negotiation Closed: " + getConnection().getUser() + " " + sid);
|
2007-01-04 18:25:30 +01:00
|
|
|
super.close();
|
2008-05-20 00:54:19 +02:00
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|
|
|
|
|
2007-02-09 04:49:43 +01:00
|
|
|
public boolean isClosed() {
|
2008-05-20 00:54:19 +02:00
|
|
|
return getSessionState().equals(JingleSessionStateEnded.getInstance());
|
2007-02-09 04:49:43 +01:00
|
|
|
}
|
|
|
|
|
2007-01-04 18:25:30 +01:00
|
|
|
// Packet and error creation
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A convience method to create an IQ packet.
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param ID
|
|
|
|
* The packet ID of the
|
|
|
|
* @param to
|
|
|
|
* To whom the packet is addressed.
|
|
|
|
* @param from
|
|
|
|
* From whom the packet is sent.
|
|
|
|
* @param type
|
|
|
|
* The iq type of the packet.
|
2007-01-04 18:25:30 +01:00
|
|
|
* @return The created IQ packet.
|
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public static IQ createIQ(String ID, String to, String from, IQ.Type type) {
|
2007-01-04 18:25:30 +01:00
|
|
|
IQ iqPacket = new IQ() {
|
|
|
|
public String getChildElementXML() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
iqPacket.setPacketID(ID);
|
|
|
|
iqPacket.setTo(to);
|
|
|
|
iqPacket.setFrom(from);
|
|
|
|
iqPacket.setType(type);
|
|
|
|
|
|
|
|
return iqPacket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A convience method to create an error packet.
|
2008-05-20 00:54:19 +02:00
|
|
|
*
|
|
|
|
* @param ID
|
|
|
|
* The packet ID of the
|
|
|
|
* @param to
|
|
|
|
* To whom the packet is addressed.
|
|
|
|
* @param from
|
|
|
|
* From whom the packet is sent.
|
|
|
|
* @param errCode
|
|
|
|
* The error code.
|
|
|
|
* @param errStr
|
|
|
|
* The error string.
|
2007-01-04 18:25:30 +01:00
|
|
|
* @return The created IQ packet.
|
|
|
|
*/
|
2008-05-20 00:54:19 +02:00
|
|
|
public static IQ createError(String ID, String to, String from, int errCode, XMPPError error) {
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
IQ iqError = createIQ(ID, to, from, IQ.Type.ERROR);
|
|
|
|
iqError.setError(error);
|
|
|
|
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.debug("Created Error Packet:" + iqError.toXML());
|
2007-01-04 18:25:30 +01:00
|
|
|
|
|
|
|
return iqError;
|
|
|
|
}
|
2007-04-06 06:37:44 +02:00
|
|
|
|
2008-05-20 00:54:19 +02:00
|
|
|
/**
|
|
|
|
* Complete and send an error. Complete all the null fields in an IQ error
|
|
|
|
* reponse, using the sesssion information we have or some info from the
|
|
|
|
* incoming packet.
|
|
|
|
*
|
|
|
|
* @param iq
|
|
|
|
* The Jingle packet we are responing to
|
|
|
|
* @param error
|
|
|
|
* the IQ packet we want to complete and send
|
|
|
|
*/
|
|
|
|
public IQ createJingleError(IQ iq, JingleError jingleError) {
|
|
|
|
IQ errorPacket = null;
|
|
|
|
if (jingleError != null) {
|
|
|
|
errorPacket = createIQ(getSid(), iq.getFrom(), iq.getTo(), IQ.Type.ERROR);
|
|
|
|
|
|
|
|
List<PacketExtension> extList = new ArrayList<PacketExtension>();
|
|
|
|
extList.add(jingleError);
|
|
|
|
XMPPError error = new XMPPError(0, XMPPError.Type.CANCEL, jingleError.toString(), "", extList);
|
|
|
|
|
|
|
|
// Fill in the fields with the info from the Jingle packet
|
|
|
|
errorPacket.setPacketID(iq.getPacketID());
|
|
|
|
errorPacket.setError(error);
|
|
|
|
// errorPacket.addExtension(jingleError);
|
|
|
|
|
|
|
|
// NO! Let the normal state machinery do all of the sending.
|
|
|
|
// getConnection().sendPacket(perror);
|
2008-10-30 22:20:29 +01:00
|
|
|
LOGGER.error("Error sent: " + errorPacket.toXML());
|
2008-05-20 00:54:19 +02:00
|
|
|
}
|
|
|
|
return errorPacket;
|
|
|
|
}
|
|
|
|
|
2007-04-06 06:37:44 +02:00
|
|
|
/**
|
|
|
|
* Called when new Media is received.
|
|
|
|
*/
|
|
|
|
public void mediaReceived(String participant) {
|
|
|
|
triggerMediaReceived(participant);
|
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the starting point for intitiating a new session.
|
|
|
|
*
|
|
|
|
* @throws IllegalStateException
|
|
|
|
*/
|
|
|
|
public void startOutgoing() throws IllegalStateException {
|
|
|
|
|
|
|
|
updatePacketListener();
|
|
|
|
setSessionState(JingleSessionStatePending.getInstance());
|
|
|
|
|
|
|
|
Jingle jingle = new Jingle(JingleActionEnum.SESSION_INITIATE);
|
|
|
|
|
|
|
|
// Create a content negotiator for each media manager on the session.
|
|
|
|
for (JingleMediaManager mediaManager : getMediaManagers()) {
|
|
|
|
ContentNegotiator contentNeg = new ContentNegotiator(this, ContentNegotiator.INITIATOR, mediaManager.getName());
|
|
|
|
|
|
|
|
// Create the media negotiator for this content description.
|
|
|
|
contentNeg.setMediaNegotiator(new MediaNegotiator(this, mediaManager, mediaManager.getPayloads(), contentNeg));
|
|
|
|
|
|
|
|
JingleTransportManager transportManager = mediaManager.getTransportManager();
|
|
|
|
TransportResolver resolver = null;
|
|
|
|
try {
|
|
|
|
resolver = transportManager.getResolver(this);
|
|
|
|
} catch (XMPPException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolver.getType().equals(TransportResolver.Type.rawupd)) {
|
|
|
|
contentNeg.setTransportNegotiator(new TransportNegotiator.RawUdp(this, resolver, contentNeg));
|
|
|
|
}
|
|
|
|
if (resolver.getType().equals(TransportResolver.Type.ice)) {
|
|
|
|
contentNeg.setTransportNegotiator(new TransportNegotiator.Ice(this, resolver, contentNeg));
|
|
|
|
}
|
|
|
|
|
|
|
|
addContentNegotiator(contentNeg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give each of the content negotiators a chance to return a portion of the structure to make the Jingle packet.
|
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
jingle.addContent(contentNegotiator.getJingleContent());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the session-initiate packet ID, so that we can respond to it.
|
|
|
|
sessionInitPacketID = jingle.getPacketID();
|
|
|
|
|
|
|
|
sendPacket(jingle);
|
|
|
|
|
|
|
|
// Now setup to track the media negotiators, so that we know when (if) to send a session-accept.
|
|
|
|
setupListeners();
|
|
|
|
|
|
|
|
// Give each of the content negotiators a chance to start
|
|
|
|
// and return a portion of the structure to make the Jingle packet.
|
2008-12-06 03:15:42 +01:00
|
|
|
|
|
|
|
// Don't do this anymore. The problem is that the other side might not be ready.
|
|
|
|
// Later when we receive our first jingle packet from the other side we'll fire-up the negotiators
|
|
|
|
// before processing it. (See receivePacketAndRespond() above.
|
|
|
|
// for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
// contentNegotiator.start();
|
|
|
|
// }
|
2008-05-20 00:54:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the starting point for responding to a new session.
|
|
|
|
*/
|
|
|
|
public void startIncoming() {
|
|
|
|
|
|
|
|
//updatePacketListener();
|
|
|
|
}
|
2008-12-06 03:15:42 +01:00
|
|
|
|
|
|
|
protected void doStart() {
|
|
|
|
|
|
|
|
}
|
2008-05-20 00:54:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When we initiate a session we need to start a bunch of negotiators right after we receive the result
|
|
|
|
* packet for our session-initiate. This is where we start them.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private void startNegotiators() {
|
|
|
|
|
|
|
|
for (ContentNegotiator contentNegotiator : contentNegotiators) {
|
|
|
|
TransportNegotiator transNeg = contentNegotiator.getTransportNegotiator();
|
|
|
|
transNeg.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The jingle session may have one or more media managers that are trying to establish media sessions.
|
|
|
|
* When the media manager succeeds in creating a media session is registers it with the session by the
|
|
|
|
* media manager's static name. This routine is where the media manager does the registering.
|
|
|
|
*/
|
|
|
|
public void addJingleMediaSession(String mediaManagerName, JingleMediaSession mediaSession) {
|
|
|
|
mediaSessionMap.put(mediaManagerName, mediaSession);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The jingle session may have one or more media managers that are trying to establish media sessions.
|
|
|
|
* When the media manager succeeds in creating a media session is registers it with the session by the
|
|
|
|
* media manager's static name. This routine is where other objects can access the registered media sessions.
|
|
|
|
* NB: If the media manager has not succeeded in establishing a media session then this could return null.
|
|
|
|
*/
|
|
|
|
public JingleMediaSession getMediaSession(String mediaManagerName) {
|
|
|
|
return mediaSessionMap.get(mediaManagerName);
|
|
|
|
}
|
2007-01-04 18:25:30 +01:00
|
|
|
}
|