1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +01:00

ECHO Refactoring

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7460 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Thiago Camargo 2007-03-12 03:25:06 +00:00 committed by thiago
parent 41fbc3906a
commit a078606ab4
21 changed files with 136 additions and 80 deletions

View file

@ -64,6 +64,7 @@ import org.jivesoftware.smackx.jingle.media.PayloadType;
import org.jivesoftware.smackx.jingle.nat.TransportCandidate;
import org.jivesoftware.smackx.jingle.nat.TransportNegotiator;
import org.jivesoftware.smackx.jingle.nat.TransportResolver;
import org.jivesoftware.smackx.jingle.nat.JingleTransportManager;
import org.jivesoftware.smackx.packet.Jingle;
import org.jivesoftware.smackx.packet.JingleContentDescription;
import org.jivesoftware.smackx.packet.JingleContentDescription.JinglePayloadType;
@ -96,21 +97,31 @@ public class IncomingJingleSession extends JingleSession {
/**
* Constructor for a Jingle incoming session
*
* @param conn the XMPP connection
* @param responder the responder
* @param resolver The transport resolver
* @param conn the XMPP connection
* @param responder the responder
* @param transportManager The transport manager
*/
protected IncomingJingleSession(XMPPConnection conn, String responder,
List payloadTypes, TransportResolver resolver) {
List payloadTypes, JingleTransportManager transportManager, String sid) {
super(conn, responder, conn.getUser());
setSid(sid);
// Create the states...
accepting = new Accepting(this);
pending = new Pending(this);
active = new Active(this);
TransportResolver resolver = null;
try {
resolver = transportManager.getResolver(this);
}
catch (XMPPException e) {
e.printStackTrace();
}
setMediaNeg(new MediaNegotiator(this, payloadTypes));
if (resolver.getType().equals(TransportResolver.Type.rawupd)) {
setTransportNeg(new TransportNegotiator.RawUdp(this, resolver));
@ -126,12 +137,12 @@ public class IncomingJingleSession extends JingleSession {
*
* @param conn the XMPP connection
* @param responder the responder
* @param resolver The transport resolver
* @param transportManager The transport manager
* @param jingleMediaManager The Media Manager for this Session
*/
protected IncomingJingleSession(XMPPConnection conn, String responder,
List payloadTypes, TransportResolver resolver, JingleMediaManager jingleMediaManager) {
this(conn, responder, payloadTypes, resolver);
List payloadTypes, JingleTransportManager transportManager, JingleMediaManager jingleMediaManager, String sid) {
this(conn, responder, payloadTypes, transportManager, sid);
this.jingleMediaManager = jingleMediaManager;
}

View file

@ -307,7 +307,7 @@ public class JingleManager implements JingleSessionListener {
*/
public JingleManager(XMPPConnection connection) {
this(connection, new JingleTransportManager() {
protected TransportResolver createResolver() {
protected TransportResolver createResolver(JingleSession session) {
return new BasicResolver();
}
});
@ -321,7 +321,7 @@ public class JingleManager implements JingleSessionListener {
*/
public JingleManager(XMPPConnection connection, final TransportResolver resolver) {
this(connection, new JingleTransportManager() {
protected TransportResolver createResolver() {
protected TransportResolver createResolver(JingleSession session) {
return resolver;
}
});
@ -496,7 +496,7 @@ public class JingleManager implements JingleSessionListener {
jingleSession.removeListener(this);
jingleSessions.remove(jingleSession);
jingleSession.close();
System.err.println("Declined:"+reason);
System.err.println("Declined:" + reason);
}
public void sessionRedirected(String redirection, JingleSession jingleSession) {
@ -609,12 +609,10 @@ public class JingleManager implements JingleSessionListener {
OutgoingJingleSession session;
TransportResolver resolver = jingleTransportManager.getResolver();
if (jingleMediaManager != null)
session = new OutgoingJingleSession(connection, responder, payloadTypes, resolver, jingleMediaManager);
session = new OutgoingJingleSession(connection, responder, payloadTypes, jingleTransportManager, jingleMediaManager);
else
session = new OutgoingJingleSession(connection, responder, payloadTypes, jingleTransportManager.getResolver());
session = new OutgoingJingleSession(connection, responder, payloadTypes, jingleTransportManager);
triggerSessionCreated(session);
@ -649,14 +647,12 @@ public class JingleManager implements JingleSessionListener {
IncomingJingleSession session;
TransportResolver resolver = jingleTransportManager.getResolver();
if (jingleMediaManager != null)
session = new IncomingJingleSession(connection, request
.getFrom(), payloadTypes, resolver, jingleMediaManager);
.getFrom(), payloadTypes, jingleTransportManager, jingleMediaManager, request.getSessionID());
else
session = new IncomingJingleSession(connection, request
.getFrom(), payloadTypes, resolver);
.getFrom(), payloadTypes, jingleTransportManager, request.getSessionID());
triggerSessionCreated(session);

View file

@ -239,7 +239,7 @@ public abstract class JingleSession extends JingleNegotiator {
/**
* Generate a unique session ID.
*/
protected String generateSessionId() {
protected static String generateSessionId() {
return String.valueOf(Math.abs(randomGenerator.nextLong()));
}

View file

@ -63,6 +63,7 @@ import org.jivesoftware.smackx.jingle.media.PayloadType;
import org.jivesoftware.smackx.jingle.nat.TransportCandidate;
import org.jivesoftware.smackx.jingle.nat.TransportNegotiator;
import org.jivesoftware.smackx.jingle.nat.TransportResolver;
import org.jivesoftware.smackx.jingle.nat.JingleTransportManager;
import org.jivesoftware.smackx.packet.Jingle;
import org.jivesoftware.smackx.packet.JingleContentDescription;
import org.jivesoftware.smackx.packet.JingleContentDescription.JinglePayloadType;
@ -93,13 +94,13 @@ public class OutgoingJingleSession extends JingleSession {
/**
* Constructor for a Jingle outgoing session.
*
* @param conn the XMPP connection
* @param responder the other endpoint
* @param payloadTypes A list of payload types, in order of preference.
* @param resolver The transport resolver.
* @param conn the XMPP connection
* @param responder the other endpoint
* @param payloadTypes A list of payload types, in order of preference.
* @param transportManager The transport manager.
*/
protected OutgoingJingleSession(XMPPConnection conn, String responder,
List payloadTypes, TransportResolver resolver) {
List payloadTypes, JingleTransportManager transportManager) {
super(conn, conn.getUser(), responder);
@ -110,6 +111,14 @@ public class OutgoingJingleSession extends JingleSession {
pending = new Pending(this);
active = new Active(this);
TransportResolver resolver = null;
try {
resolver = transportManager.getResolver(this);
}
catch (XMPPException e) {
e.printStackTrace();
}
// Create description and transport negotiatiors...
setMediaNeg(new MediaNegotiator(this, payloadTypes));
if (resolver.getType().equals(TransportResolver.Type.rawupd)) {
@ -126,12 +135,12 @@ public class OutgoingJingleSession extends JingleSession {
* @param conn the XMPP connection
* @param responder the other endpoint
* @param payloadTypes A list of payload types, in order of preference.
* @param resolver The transport resolver.
* @param transportManager The transport manager.
* @param jingleMediaManager The Media Manager for this Session
*/
protected OutgoingJingleSession(XMPPConnection conn, String responder,
List payloadTypes, TransportResolver resolver, JingleMediaManager jingleMediaManager) {
this(conn, responder, payloadTypes, resolver);
List payloadTypes, JingleTransportManager transportManager, JingleMediaManager jingleMediaManager) {
this(conn, responder, payloadTypes, transportManager);
this.jingleMediaManager = jingleMediaManager;
}

View file

@ -21,6 +21,7 @@
package org.jivesoftware.smackx.jingle.nat;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleSession;
import java.net.InetAddress;
import java.net.NetworkInterface;
@ -47,7 +48,7 @@ public class BasicResolver extends TransportResolver {
* The BasicResolver takes the IP addresses of the interfaces and uses the
* first non-loopback, non-linklocal and non-sitelocal address.
*/
public synchronized void resolve() throws XMPPException {
public synchronized void resolve(JingleSession session) throws XMPPException {
setResolveInit();

View file

@ -1,5 +1,7 @@
package org.jivesoftware.smackx.jingle.nat;
import org.jivesoftware.smackx.jingle.JingleSession;
/**
* $RCSfile$
* $Revision: $
@ -26,7 +28,7 @@ package org.jivesoftware.smackx.jingle.nat;
*/
public class BasicTransportManager extends JingleTransportManager{
protected TransportResolver createResolver() {
protected TransportResolver createResolver(JingleSession session) {
return new BasicResolver();
}
}

View file

@ -21,6 +21,7 @@ package org.jivesoftware.smackx.jingle.nat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleSession;
import java.util.Random;
@ -54,7 +55,7 @@ public class BridgedResolver extends TransportResolver{
* <p/>
* The BridgedResolver takes the IP addresse and ports of a jmf proxy service.
*/
public synchronized void resolve() throws XMPPException {
public synchronized void resolve(JingleSession session) throws XMPPException {
setResolveInit();
@ -67,7 +68,6 @@ public class BridgedResolver extends TransportResolver{
BasicResolver basicResolver = new BasicResolver();
basicResolver.initializeAndWait();
basicResolver.resolve();
TransportCandidate localCandidate = new TransportCandidate.Fixed(
rtpBridge.getIp(), rtpBridge.getPortA());

View file

@ -43,7 +43,7 @@ public class BridgedTransportManager extends JingleTransportManager implements J
}
// Return the correspondent resolver
protected TransportResolver createResolver() {
protected TransportResolver createResolver(JingleSession session) {
BridgedResolver bridgedResolver = new BridgedResolver(this.xmppConnection);
return bridgedResolver;
}

View file

@ -20,6 +20,7 @@
package org.jivesoftware.smackx.jingle.nat;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleSession;
/**
* The FixedResolver is a resolver where
@ -53,7 +54,7 @@ public class FixedResolver extends TransportResolver {
/**
* Resolve the IP address.
*/
public synchronized void resolve() throws XMPPException {
public synchronized void resolve(JingleSession session) throws XMPPException {
if (!isResolving()) {
setResolveInit();

View file

@ -25,6 +25,7 @@ import de.javawi.jstun.test.demo.ice.ICENegociator;
import de.javawi.jstun.util.UtilityException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.jingle.JingleSession;
import java.net.UnknownHostException;
import java.net.SocketException;
@ -76,7 +77,7 @@ public class ICEResolver extends TransportResolver {
/**
* Resolve the IP and obtain a valid transport method.
*/
public synchronized void resolve() throws XMPPException {
public synchronized void resolve(JingleSession session) throws XMPPException {
this.setResolveInit();
for (TransportCandidate candidate : this.getCandidatesList()) {
@ -105,7 +106,7 @@ public class ICEResolver extends TransportResolver {
transportCandidate.setLocalIp(candidate.getBase().getAddress().getInetAddress().getHostAddress());
transportCandidate.setPort(getFreePort());
try {
transportCandidate.addCandidateEcho();
transportCandidate.addCandidateEcho(session);
}
catch (SocketException e) {
e.printStackTrace();

View file

@ -40,9 +40,9 @@ public class ICETransportManager extends JingleTransportManager implements Jingl
}
}
protected TransportResolver createResolver() {
protected TransportResolver createResolver(JingleSession session) {
try {
iceResolver.resolve();
iceResolver.resolve(session);
}
catch (XMPPException e) {
e.printStackTrace();

View file

@ -20,6 +20,7 @@
package org.jivesoftware.smackx.jingle.nat;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleSession;
/**
* Transport manager for Jingle.
@ -53,8 +54,8 @@ public abstract class JingleTransportManager {
*
* @return
*/
public TransportResolver getResolver() throws XMPPException {
TransportResolver resolver = createResolver();
public TransportResolver getResolver(JingleSession session) throws XMPPException {
TransportResolver resolver = createResolver(session);
if (resolver == null) {
resolver = new BasicResolver();
}
@ -68,6 +69,6 @@ public abstract class JingleTransportManager {
*
* @return
*/
protected abstract TransportResolver createResolver();
protected abstract TransportResolver createResolver(JingleSession session);
}

View file

@ -23,6 +23,7 @@ import de.javawi.jstun.test.BindingLifetimeTest;
import de.javawi.jstun.test.DiscoveryInfo;
import de.javawi.jstun.test.DiscoveryTest;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleSession;
import org.xmlpull.mxp1.MXParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -271,7 +272,7 @@ public class STUNResolver extends TransportResolver {
/**
* Resolve the IP and obtain a valid transport method.
*/
public synchronized void resolve() throws XMPPException {
public synchronized void resolve(JingleSession session) throws XMPPException {
setResolveInit();

View file

@ -20,6 +20,7 @@
package org.jivesoftware.smackx.jingle.nat;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleSession;
/**
* A Jingle Transport Manager implementation to be used on NAT networks with STUN Service NOT Blocked.
@ -39,9 +40,9 @@ public class STUNTransportManager extends JingleTransportManager {
}
}
protected TransportResolver createResolver() {
protected TransportResolver createResolver(JingleSession session) {
try {
stunResolver.resolve();
stunResolver.resolve(session);
} catch (XMPPException e) {
e.printStackTrace();
}

View file

@ -53,6 +53,7 @@
package org.jivesoftware.smackx.jingle.nat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.jingle.JingleSession;
import java.io.IOException;
import java.net.*;
@ -60,6 +61,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Arrays;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
/**
* Transport candidate.
@ -97,8 +100,8 @@ public abstract class TransportCandidate {
// Listeners for events
private final List<TransportResolverListener.Checker> listeners = new ArrayList();
public void addCandidateEcho() throws SocketException, UnknownHostException {
candidateEcho = new CandidateEcho(this);
public void addCandidateEcho(JingleSession session) throws SocketException, UnknownHostException {
candidateEcho = new CandidateEcho(this, session);
echoThread = new Thread(candidateEcho);
echoThread.start();
}
@ -647,17 +650,52 @@ public abstract class TransportCandidate {
public class CandidateEcho implements Runnable {
DatagramSocket socket = null;
byte password[] = null;
String localUser = null;
String remoteUser = null;
String id = null;
byte send[] = null;
byte receive[] = null;
DatagramPacket sendPacket = null;
List<DatagramListener> listeners = new ArrayList<DatagramListener>();
List<ResultListener> resultListeners = new ArrayList<ResultListener>();
boolean enabled = true;
boolean ended = false;
long tries = 10;
public CandidateEcho(TransportCandidate candidate) throws UnknownHostException, SocketException {
public CandidateEcho(TransportCandidate candidate, JingleSession session) throws UnknownHostException, SocketException {
this.socket = new DatagramSocket(candidate.getPort(), InetAddress.getByName(candidate.getLocalIp()));
Random r = new Random();
password = longToByteArray((Math.abs(r.nextLong())));
this.localUser = session.getInitiator();
this.remoteUser = session.getResponder();
this.id = session.getSid();
int keySplitIndex = ((int) Math.ceil(((float) id.length()) / 2));
int size = 4 + localUser.length() * 2 + (id.length() - keySplitIndex) * 2;
ByteBuffer bufLocal = ByteBuffer.allocate(size);
// Create a character ByteBuffer Wrap
CharBuffer cbufLocal = bufLocal.asCharBuffer();
cbufLocal.append(id.substring(0, keySplitIndex));
cbufLocal.append(';');
cbufLocal.append(localUser);
size = 4 + remoteUser.length() * 2 + keySplitIndex * 2;
ByteBuffer bufRemote = ByteBuffer.allocate(size);
// Create a character ByteBuffer Wrap
CharBuffer cbufRemote = bufRemote.asCharBuffer();
cbufRemote.append(id.substring(keySplitIndex));
cbufRemote.append(';');
cbufRemote.append(remoteUser);
if (session.getConnection().getUser().equals(session.getInitiator())) {
this.send = bufLocal.array();
this.receive = bufRemote.array();
}
else {
this.receive = bufLocal.array();
this.send = bufRemote.array();
}
}
public void run() {
@ -665,7 +703,7 @@ public abstract class TransportCandidate {
System.out.println("Listening for ECHO: " + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort());
while (true) {
DatagramPacket packet = new DatagramPacket(new byte[8], 8);
DatagramPacket packet = new DatagramPacket(new byte[this.receive.length], this.receive.length);
socket.receive(packet);
@ -681,8 +719,10 @@ public abstract class TransportCandidate {
long delay = 1000 / tries / 2;
if (delay < 0) delay = 10;
if (!Arrays.equals(packet.getData(), password))
if (Arrays.equals(packet.getData(), receive))
for (int i = 0; i < tries; i++) {
packet.setData(send);
packet.setLength(send.length);
socket.send(packet);
if (!enabled) break;
try {
@ -731,7 +771,7 @@ public abstract class TransportCandidate {
DatagramListener listener = new DatagramListener() {
public boolean datagramReceived(DatagramPacket datagramPacket) {
if (datagramPacket.getAddress().equals(address) && datagramPacket.getPort() == port) {
if (Arrays.equals(datagramPacket.getData(), password)) {
if (Arrays.equals(datagramPacket.getData(), receive)) {
testResult.setResult(true);
ended = true;
return true;
@ -743,7 +783,7 @@ public abstract class TransportCandidate {
this.addListener(listener);
DatagramPacket packet = new DatagramPacket(password, password.length);
DatagramPacket packet = new DatagramPacket(send, send.length);
packet.setAddress(address);
packet.setPort(port);
@ -781,7 +821,7 @@ public abstract class TransportCandidate {
DatagramListener listener = new DatagramListener() {
public boolean datagramReceived(DatagramPacket datagramPacket) {
if (datagramPacket.getAddress().equals(address) && datagramPacket.getPort() == port) {
if (Arrays.equals(datagramPacket.getData(), password)) {
if (Arrays.equals(datagramPacket.getData(), receive)) {
TestResult testResult = new TestResult();
testResult.setResult(true);
fireTestResult(testResult);
@ -795,7 +835,7 @@ public abstract class TransportCandidate {
addListener(listener);
DatagramPacket packet = new DatagramPacket(password, password.length);
DatagramPacket packet = new DatagramPacket(send, send.length);
packet.setAddress(address);
packet.setPort(port);
@ -842,13 +882,4 @@ public abstract class TransportCandidate {
}
public static byte[] longToByteArray(long valor) {
byte[] result = new byte[8];
for (int i = 0; i < result.length; i++) {
result[7 - i] = (byte) (valor & 0xFF);
valor = valor >> 8;
}
return result;
}
}

View file

@ -507,7 +507,7 @@ public abstract class TransportNegotiator extends JingleNegotiator {
if (!(resolver.isResolving() || resolver.isResolved())) {
// Resolve our IP and port
System.out.println("RESOLVER CALLED");
resolver.resolve();
resolver.resolve(session);
}
}

View file

@ -53,6 +53,7 @@
package org.jivesoftware.smackx.jingle.nat;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleSession;
import java.io.IOException;
import java.net.ServerSocket;
@ -89,7 +90,7 @@ public abstract class TransportResolver {
public Type type = Type.rawupd;
// the time, in milliseconds, before a check aborts
public static final int CHECK_TIMEOUT = 5000;
public static final int CHECK_TIMEOUT = 3000;
// Listeners for events
private final ArrayList<TransportResolverListener> listeners = new ArrayList<TransportResolverListener>();
@ -126,7 +127,7 @@ public abstract class TransportResolver {
/**
* Start a the resolution.
*/
public abstract void resolve() throws XMPPException;
public abstract void resolve(JingleSession session) throws XMPPException;
/**
* Clear the list of candidates and start a new resolution process.

View file

@ -2,6 +2,7 @@ package org.jivesoftware.smackx.jingle;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.jingle.nat.BasicResolver;
import org.jivesoftware.smackx.jingle.nat.BasicTransportManager;
public class JingleSessionTest extends SmackTestCase {
@ -10,9 +11,9 @@ public class JingleSessionTest extends SmackTestCase {
}
public void testEqualsObject() {
JingleSession js1 = new OutgoingJingleSession(getConnection(0), "res1", null, new BasicResolver());
JingleSession js2 = new OutgoingJingleSession(getConnection(1), "res1", null, new BasicResolver());
JingleSession js3 = new OutgoingJingleSession(getConnection(2), "res2", null, new BasicResolver());
JingleSession js1 = new OutgoingJingleSession(getConnection(0), "res1", null, new BasicTransportManager());
JingleSession js2 = new OutgoingJingleSession(getConnection(1), "res1", null, new BasicTransportManager());
JingleSession js3 = new OutgoingJingleSession(getConnection(2), "res2", null, new BasicTransportManager());
System.out.println(js1.getSid());
System.out.println(js2.getSid());
@ -34,8 +35,8 @@ public class JingleSessionTest extends SmackTestCase {
String ini2 = "initiator2";
String sid2 = "sid2";
JingleSession js1 = new OutgoingJingleSession(getConnection(0), sid1, null, new BasicResolver());
JingleSession js2 = new OutgoingJingleSession(getConnection(1), sid2, null, new BasicResolver());
JingleSession js1 = new OutgoingJingleSession(getConnection(0), sid1, null, new BasicTransportManager());
JingleSession js2 = new OutgoingJingleSession(getConnection(1), sid2, null, new BasicTransportManager());
// For a packet, we should be able to get a session that handles that...
assertNotNull(JingleSession.getInstanceFor(getConnection(0)));

View file

@ -205,10 +205,9 @@ public class STUNResolverTest extends SmackTestCase {
});
try {
stunResolver.initialize();
stunResolver.initializeAndWait();
Thread.sleep(55000);
assertTrue(valCounter() > 0);
stunResolver.resolve();
}
catch (Exception e) {
e.printStackTrace();
@ -257,8 +256,8 @@ public class STUNResolverTest extends SmackTestCase {
};
// Explicit resolution
tr1.resolve();
tr2.resolve();
tr1.resolve(null);
tr2.resolve(null);
final JingleManager man0 = new JingleManager(getConnection(0), tr1);
final JingleManager man1 = new JingleManager(getConnection(1), tr2);
@ -356,8 +355,8 @@ public class STUNResolverTest extends SmackTestCase {
TransportCandidate.Fixed c2 = new TransportCandidate.Fixed("localhost", 22444);
try {
c1.addCandidateEcho();
c2.addCandidateEcho();
c1.addCandidateEcho(null);
c2.addCandidateEcho(null);
try {
Thread.sleep(100);

View file

@ -62,7 +62,7 @@ public class TransportCandidateTest extends SmackTestCase {
};
try {
tc.addCandidateEcho();
tc.addCandidateEcho(null);
assertTrue(tc.getCandidateEcho().test(InetAddress.getByName("localhost"),10020));
}
catch (SocketException e) {

View file

@ -36,7 +36,7 @@ public class TransportResolverTest extends SmackTestCase {
assertFalse(tr.isResolved());
try {
tr.resolve();
tr.resolve(null);
} catch (XMPPException e) {
e.printStackTrace();
fail("Error resolving");