Preparing to merge StunStack and StunProvider as there is no good reason for them to live apart.

This commit is contained in:
emcho 2010-01-31 13:02:51 +00:00
parent 346e851012
commit 312b9d15b2
7 changed files with 172 additions and 68 deletions

View File

@ -8,6 +8,7 @@ package org.ice4j;
import java.net.*; import java.net.*;
import java.util.*; import java.util.*;
import java.util.logging.*;
import junit.framework.*; import junit.framework.*;
@ -36,13 +37,13 @@ public class MessageEventDispatchingTest extends TestCase
* The Address of the server. * The Address of the server.
*/ */
TransportAddress serverAddress TransportAddress serverAddress
= new TransportAddress("127.0.0.2", 5255, Transport.UDP); = new TransportAddress("127.0.0.1", 5255, Transport.UDP);
/** /**
* The address of the second server. * The address of the second server.
*/ */
TransportAddress serverAddress2 TransportAddress serverAddress2
= new TransportAddress("127.0.0.2", 5259, Transport.UDP); = new TransportAddress("127.0.0.1", 5259, Transport.UDP);
/** /**
* The socket that the client is using. * The socket that the client is using.
@ -91,8 +92,8 @@ public class MessageEventDispatchingTest extends TestCase
stunStack = StunStack.getInstance(); stunStack = StunStack.getInstance();
clientSock = new DatagramSocket(clientAddress); clientSock = new DatagramSocket(clientAddress);
serverSock = new DatagramSocket(clientAddress); serverSock = new DatagramSocket(serverAddress);
serverSock = new DatagramSocket(serverAddress2); serverSock2 = new DatagramSocket(serverAddress2);
stunStack.addSocket(clientSock); stunStack.addSocket(clientSock);
stunStack.addSocket(serverSock); stunStack.addSocket(serverSock);
@ -114,9 +115,13 @@ public class MessageEventDispatchingTest extends TestCase
*/ */
protected void tearDown() throws Exception protected void tearDown() throws Exception
{ {
stunStack.removeSocket(clientAddress);
stunStack.removeSocket(serverAddress);
stunStack.removeSocket(serverAddress2);
clientSock.close(); clientSock.close();
serverSock.close(); serverSock.close();
serverSock.close(); serverSock2.close();
requestCollector = null; requestCollector = null;
responseCollector = null; responseCollector = null;
@ -131,12 +136,14 @@ public class MessageEventDispatchingTest extends TestCase
*/ */
public void testClientTransactionTimeouts() throws Exception public void testClientTransactionTimeouts() throws Exception
{ {
String oldRetransValue = System.getProperty(
"org.ice4j.MAX_RETRANSMISSIONS");
System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "1");
stunStack.getProvider().sendRequest(bindingRequest, stunStack.getProvider().sendRequest(bindingRequest,
serverAddress, serverAddress,
clientAddress, clientAddress,
responseCollector); responseCollector);
Thread.sleep(12000); responseCollector.waitForTimeout();
assertEquals( assertEquals(
"No timeout was produced upon expiration of a client transaction", "No timeout was produced upon expiration of a client transaction",
@ -145,6 +152,14 @@ public class MessageEventDispatchingTest extends TestCase
assertEquals( assertEquals(
"No timeout was produced upon expiration of a client transaction", "No timeout was produced upon expiration of a client transaction",
responseCollector.receivedResponses.get(0), "timeout"); responseCollector.receivedResponses.get(0), "timeout");
//restore the retransmissions prop in case others are counting on
//defaults.
if(oldRetransValue != null)
System.getProperty( "org.ice4j.MAX_RETRANSMISSIONS",
oldRetransValue);
else
System.clearProperty("org.ice4j.MAX_RETRANSMISSIONS");
} }
/** /**
@ -162,7 +177,7 @@ public class MessageEventDispatchingTest extends TestCase
clientAddress, clientAddress,
responseCollector); responseCollector);
//wait for retransmissions //wait for retransmissions
Thread.sleep(50); requestCollector.waitForRequest();
//verify //verify
assertTrue("No MessageEvents have been dispatched", assertTrue("No MessageEvents have been dispatched",
@ -192,7 +207,8 @@ public class MessageEventDispatchingTest extends TestCase
clientAddress, clientAddress,
responseCollector); responseCollector);
//wait for retransmissions //wait for retransmissions
Thread.sleep(50); requestCollector.waitForRequest();
requestCollector2.waitForRequest();
//verify //verify
assertTrue( assertTrue(
@ -220,7 +236,7 @@ public class MessageEventDispatchingTest extends TestCase
responseCollector); responseCollector);
//wait for the message to arrive //wait for the message to arrive
Thread.sleep(50); requestCollector.waitForRequest();
StunMessageEvent evt = requestCollector.receivedRequests.get(0); StunMessageEvent evt = requestCollector.receivedRequests.get(0);
byte[] tid = evt.getMessage().getTransactionID(); byte[] tid = evt.getMessage().getTransactionID();
@ -230,7 +246,7 @@ public class MessageEventDispatchingTest extends TestCase
clientAddress); clientAddress);
//wait for retransmissions //wait for retransmissions
Thread.sleep(50); responseCollector.waitForResponse();
//verify that we got the response. //verify that we got the response.
assertTrue( assertTrue(
@ -254,7 +270,26 @@ public class MessageEventDispatchingTest extends TestCase
*/ */
public void requestReceived(StunMessageEvent evt) public void requestReceived(StunMessageEvent evt)
{ {
receivedRequests.add(evt); synchronized (this)
{
receivedRequests.add(evt);
notifyAll();
}
}
public void waitForRequest()
{
synchronized(this)
{
if (receivedRequests.size() > 0)
return;
try
{
wait(50);
}
catch (InterruptedException e)
{}
}
} }
} }
@ -275,7 +310,11 @@ public class MessageEventDispatchingTest extends TestCase
*/ */
public void processResponse(StunMessageEvent responseEvt) public void processResponse(StunMessageEvent responseEvt)
{ {
receivedResponses.add(responseEvt); synchronized(this)
{
receivedResponses.add(responseEvt);
notifyAll();
}
} }
/** /**
@ -283,7 +322,47 @@ public class MessageEventDispatchingTest extends TestCase
*/ */
public void processTimeout() public void processTimeout()
{ {
receivedResponses.add(new String("timeout")); synchronized(this)
{
receivedResponses.add(new String("timeout"));
notifyAll();
}
}
/**
* Waits for a short period of time for a response to arrive
*/
public void waitForResponse()
{
synchronized(this)
{
try
{
if (receivedResponses.size() > 0)
return;
wait(50);
}
catch (InterruptedException e)
{}
}
}
/**
* Waits for a long period of time for a timeout trigger to fire.
*/
public void waitForTimeout()
{
synchronized(this)
{
try
{
if (receivedResponses.size() > 0)
return;
wait(12000);
}
catch (InterruptedException e)
{}
}
} }
} }

View File

@ -34,7 +34,7 @@ public class StunTestSuite
public static Test suite() public static Test suite()
{ {
TestSuite suite = new TestSuite(); TestSuite suite = new TestSuite();
/*
//attributes //attributes
suite.addTestSuite(org.ice4j.attribute. suite.addTestSuite(org.ice4j.attribute.
AddressAttributeTest.class); AddressAttributeTest.class);
@ -59,13 +59,13 @@ public class StunTestSuite
//messages //messages
suite.addTestSuite(org.ice4j.message.MessageFactoryTest.class); suite.addTestSuite(org.ice4j.message.MessageFactoryTest.class);
suite.addTestSuite(org.ice4j.message.MessageTest.class); suite.addTestSuite(org.ice4j.message.MessageTest.class);
*/
//stack //stack
suite.addTestSuite(org.ice4j.stack.ShallowStackTest.class); suite.addTestSuite(org.ice4j.stack.ShallowStackTest.class);
/*
//event dispatching //event dispatching
suite.addTestSuite(org.ice4j.MessageEventDispatchingTest.class); suite.addTestSuite(org.ice4j.MessageEventDispatchingTest.class);
/*
//transactions //transactions
suite.addTestSuite(org.ice4j.TransactionSupportTests.class); suite.addTestSuite(org.ice4j.TransactionSupportTests.class);

View File

@ -52,8 +52,8 @@ public class OptionalAttributeAttributeTest extends TestCase
/** /**
* Test whetner sample binary arrays are correctly decoded. * Test whether sample binary arrays are correctly decoded.
* @throws StunException * @throws StunException if anything goes wrong.
*/ */
public void testDecodeAttributeBody() throws StunException { public void testDecodeAttributeBody() throws StunException {

View File

@ -1,8 +1,8 @@
/* /*
* Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal. * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
* *
* Distributable under LGPL license. * Distributable under LGPL license. See terms of license at gnu.org.
* See terms of license at gnu.org.
*/ */
package org.ice4j.attribute; package org.ice4j.attribute;
@ -84,7 +84,7 @@ public class UnknownAttributesAttributeTest extends TestCase {
/** /**
* Tests whether a sample binary array is properly decoded. * Tests whether a sample binary array is properly decoded.
* @throws StunException * @throws StunException if anything goes wrong.
*/ */
public void testDecodeAttributeBody() throws StunException { public void testDecodeAttributeBody() throws StunException {
//a copy of the array in the fixture: //a copy of the array in the fixture:
@ -224,7 +224,7 @@ public class UnknownAttributesAttributeTest extends TestCase {
unknownAttributesAttribute.addAttributeID(expectedId1); unknownAttributesAttribute.addAttributeID(expectedId1);
unknownAttributesAttribute.addAttributeID(expectedId2); unknownAttributesAttribute.addAttributeID(expectedId2);
Iterator iterator = unknownAttributesAttribute.getAttributes(); Iterator<Character> iterator = unknownAttributesAttribute.getAttributes();
actualId1 = ((Character)iterator.next()).charValue(); actualId1 = ((Character)iterator.next()).charValue();
actualId2 = ((Character)iterator.next()).charValue();; actualId2 = ((Character)iterator.next()).charValue();;

View File

@ -64,7 +64,7 @@ public class XorOnlyTest extends TestCase
/** /**
* Test positive and negative XorOnly.equals() returns * Test positive and negative XorOnly.equals() returns
* @throws Exteption if decoding fails * @throws Exception if decoding fails
*/ */
public void testEquals() throws Exception public void testEquals() throws Exception
{ {

View File

@ -28,7 +28,7 @@ public class DatagramCollector
synchronized (this) synchronized (this)
{ {
notify(); notifyAll();
} }
} }
@ -45,15 +45,6 @@ public class DatagramCollector
receivedPacket = new DatagramPacket(new byte[4096], 4096); receivedPacket = new DatagramPacket(new byte[4096], 4096);
new Thread(this).start(); new Thread(this).start();
//give the guy a chance to start
try
{
Thread.sleep(200);
}
catch (InterruptedException ex)
{
}
} }
public void waitForPacket() public void waitForPacket()
@ -62,7 +53,7 @@ public class DatagramCollector
{ {
try try
{ {
wait(1000); wait(50);
} }
catch (InterruptedException e) catch (InterruptedException e)
{ {

View File

@ -40,7 +40,8 @@ public class ShallowStackTest extends TestCase {
private DatagramPacket bindingRequestPacket private DatagramPacket bindingRequestPacket
= new DatagramPacket(new byte[4096], 4096); = new DatagramPacket(new byte[4096], 4096);
public ShallowStackTest(String name) { public ShallowStackTest(String name)
{
super(name); super(name);
} }
@ -60,12 +61,9 @@ public class ShallowStackTest extends TestCase {
stunStack = StunStack.getInstance(); stunStack = StunStack.getInstance();
stunProvider = stunStack.getProvider(); stunProvider = stunStack.getProvider();
System.out.println("binding 1!!!");
//access point //access point
localSock = new DatagramSocket(localAddress); localSock = new DatagramSocket(localAddress);
stunStack.addSocket(localSock); stunStack.addSocket(localSock);
System.out.println("binding 2!!!");
//init the dummy server //init the dummy server
dummyServerSocket = new DatagramSocket( dummyServerAddress ); dummyServerSocket = new DatagramSocket( dummyServerAddress );
@ -75,13 +73,10 @@ System.out.println("binding 2!!!");
throws Exception throws Exception
{ {
msgFixture.tearDown(); msgFixture.tearDown();
System.out.println("tearing down!!!");
stunStack.removeSocket(localAddress); stunStack.removeSocket(localAddress);
System.out.println("unbinding 1!!!");
localSock.close(); localSock.close();
System.out.println("unbinding 2!!!");
dummyServerSocket.close(); dummyServerSocket.close();
@ -89,6 +84,7 @@ System.out.println("unbinding 2!!!");
super.tearDown(); super.tearDown();
} }
/** /**
* Sends a binding request using the stack to a bare socket, and verifies * Sends a binding request using the stack to a bare socket, and verifies
* that it is received and that the contents of the datagram corresponds to * that it is received and that the contents of the datagram corresponds to
@ -100,22 +96,22 @@ System.out.println("unbinding 2!!!");
throws Exception throws Exception
{ {
Request bindingRequest = MessageFactory.createBindingRequest(); Request bindingRequest = MessageFactory.createBindingRequest();
System.out.println("test 1!!!");
dgramCollector.startListening(dummyServerSocket); dgramCollector.startListening(dummyServerSocket);
System.out.println("test 2!!!");
stunProvider.sendRequest(bindingRequest, stunProvider.sendRequest(bindingRequest,
dummyServerAddress, dummyServerAddress,
localAddress, localAddress,
new SimpleResponseCollector()); new SimpleResponseCollector());
System.out.println("test 3!!!");
//wait for its arrival //wait for its arrival
dgramCollector.waitForPacket(); dgramCollector.waitForPacket();
System.out.println("test 4!!!");
DatagramPacket receivedPacket = dgramCollector.collectPacket(); DatagramPacket receivedPacket = dgramCollector.collectPacket();
System.out.println("test 5!!!");
assertTrue("The stack did not properly send a Binding Request", assertTrue("The stack did not properly send a Binding Request",
(receivedPacket.getLength() > 0)); (receivedPacket.getLength() > 0));
System.out.println("test 6!!!");
Request receivedRequest = Request receivedRequest =
(Request)Request.decode(receivedPacket.getData(), (Request)Request.decode(receivedPacket.getData(),
(char)0, (char)0,
@ -124,13 +120,13 @@ System.out.println("test 6!!!");
+"one that was sent.", +"one that was sent.",
bindingRequest, //expected bindingRequest, //expected
receivedRequest); // actual receivedRequest); // actual
System.out.println("test 7!!!");
//wait for retransmissions //wait for retransmissions
dgramCollector.startListening(dummyServerSocket); dgramCollector.startListening(dummyServerSocket);
System.out.println("test 8!!!");
dgramCollector.waitForPacket(); dgramCollector.waitForPacket();
System.out.println("test 9!!!");
receivedPacket = dgramCollector.collectPacket(); receivedPacket = dgramCollector.collectPacket();
assertTrue("The stack did not retransmit a Binding Request", assertTrue("The stack did not retransmit a Binding Request",
@ -143,9 +139,6 @@ System.out.println("test 9!!!");
assertEquals("The retransmitted request did not match the original.", assertEquals("The retransmitted request did not match the original.",
bindingRequest, //expected bindingRequest, //expected
receivedRequest); // actual receivedRequest); // actual
} }
/** /**
@ -166,7 +159,7 @@ System.out.println("test 9!!!");
localAddress)); localAddress));
//wait for the packet to arrive //wait for the packet to arrive
try{ Thread.sleep(50); }catch (InterruptedException ex){} requestCollector.waitForRequest();
Request collectedRequest = requestCollector.collectedRequest; Request collectedRequest = requestCollector.collectedRequest;
@ -199,12 +192,7 @@ System.out.println("test 9!!!");
localAddress)); localAddress));
//wait for the packet to arrive //wait for the packet to arrive
try{ requestCollector.waitForRequest();
synchronized(this)
{
wait(50);
}
}catch (InterruptedException ex){}
Request collectedRequest = requestCollector.collectedRequest; Request collectedRequest = requestCollector.collectedRequest;
@ -231,7 +219,7 @@ System.out.println("test 9!!!");
dummyServerAddress); dummyServerAddress);
//wait for its arrival //wait for its arrival
try{ Thread.sleep(50); }catch (InterruptedException ex){} dgramCollector.waitForPacket();
DatagramPacket receivedPacket = dgramCollector.collectPacket(); DatagramPacket receivedPacket = dgramCollector.collectPacket();
@ -261,7 +249,7 @@ System.out.println("test 9!!!");
collector); collector);
//wait for its arrival //wait for its arrival
try{ Thread.sleep(50); }catch (InterruptedException ex){} collector.waitForResponse();
//create the right response //create the right response
byte response[] = new byte[msgFixture.bindingResponse.length]; byte response[] = new byte[msgFixture.bindingResponse.length];
@ -282,7 +270,7 @@ System.out.println("test 9!!!");
localAddress)); localAddress));
//wait for the packet to arrive //wait for the packet to arrive
try{ Thread.sleep(50); }catch (InterruptedException ex){} collector.waitForResponse();
Response collectedResponse = collector.collectedResponse; Response collectedResponse = collector.collectedResponse;
@ -300,12 +288,37 @@ System.out.println("test 9!!!");
public void processResponse(StunMessageEvent evt) public void processResponse(StunMessageEvent evt)
{ {
collectedResponse = (Response)evt.getMessage(); collectedResponse = (Response)evt.getMessage();
logger.info("Received response."); logger.finest("Received response.");
synchronized(this)
{
notifyAll();
}
} }
public void processTimeout() public void processTimeout()
{ {
logger.info("Timeout"); logger.info("Timeout");
synchronized(this)
{
notifyAll();
}
}
public void waitForResponse()
{
synchronized(this)
{
try
{
wait(50);
}
catch (InterruptedException e)
{
logger.log(Level.INFO, "oops", e);
}
}
} }
} }
@ -313,11 +326,32 @@ System.out.println("test 9!!!");
implements RequestListener implements RequestListener
{ {
private Request collectedRequest = null; private Request collectedRequest = null;
public void requestReceived(StunMessageEvent evt) public void requestReceived(StunMessageEvent evt)
{ {
collectedRequest = (Request)evt.getMessage(); collectedRequest = (Request)evt.getMessage();
stunProvider.removeRequestListener(this); stunProvider.removeRequestListener(this);
logger.info("Received request."); logger.finest("Received request.");
synchronized(this)
{
notifyAll();
}
}
public void waitForRequest()
{
synchronized(this)
{
try
{
wait(50);
}
catch (InterruptedException e)
{
logger.log(Level.INFO, "oops", e);
}
}
} }
} }
/* /*