Adds but does not use processUnreachable to ResponseCollector.

This commit is contained in:
lubomir.marinov 2010-02-13 00:13:52 +00:00
parent 3d5ead7341
commit fda00cfa8a
12 changed files with 158 additions and 126 deletions

View File

@ -6,18 +6,17 @@
*/
package org.ice4j;
import org.ice4j.*;
import java.net.*;
/**
* The interface is used as a callback when sending a request. The response
* collector is then used as a means of dispatching the response.
*
* <p>Organisation: <p> Louis Pasteur University, Strasbourg, France</p>
* <p>Network Research Team (http://www-r2.u-strasbg.fr)</p></p>
* <p>Organization: Louis Pasteur University, Strasbourg, France</p>
* <p>Network Research Team (http://www-r2.u-strasbg.fr)</p>
* @author Emil Ivov
* @version 0.1
*/
public interface ResponseCollector
{
/**
@ -32,4 +31,14 @@ public interface ResponseCollector
* by rfc3489) and that the request should be considered unanswered.
*/
public void processTimeout();
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be considered
* unanswered.
*
* @param exception the <tt>PortUnreachableException</tt> which signaled
* that the destination of the request was found to be unreachable
*/
public void processUnreachable(PortUnreachableException exception);
}

View File

@ -33,7 +33,7 @@ class Connector
/**
* The message queue is where incoming messages are added.
*/
private MessageQueue messageQueue = null;
private final MessageQueue messageQueue;
/**
* The socket object that used by this access point to access the network.
@ -55,7 +55,7 @@ class Connector
* The instance to be notified if errors occur in the network listening
* thread.
*/
private ErrorHandler errorHandler = null;
private final ErrorHandler errorHandler;
/**
* The address that we are listening to.
@ -160,6 +160,7 @@ class Connector
+ "receive a message.",
ex);
}
else
{
//The exception was most probably caused by calling
//this.stop() ....

View File

@ -9,8 +9,8 @@ package org.ice4j.stack;
/**
* Generic Error Handler.
*
* <p>Organisation: <p> Louis Pasteur University, Strasbourg, France.</p>
* <p>Network Research Team (http://www-r2.u-strasbg.fr)</p></p>
* <p>Organization: <p> Louis Pasteur University, Strasbourg, France.</p>
* <p>Network Research Team (http://www-r2.u-strasbg.fr)</p>
* @author Emil Ivov
* @version 0.1
*/
@ -32,5 +32,7 @@ interface ErrorHandler
* @param message a message describing the error.
* @param error the error itself.
*/
public void handleFatalError(Runnable callingThread, String message, Throwable error);
public void handleFatalError(Runnable callingThread,
String message,
Throwable error);
}

View File

@ -15,7 +15,6 @@ import org.ice4j.*;
*
* @author Emil Ivov
*/
interface MessageEventHandler
{
/**

View File

@ -14,8 +14,8 @@ import java.util.logging.Logger;
* access is synchronized and message delivery ( remove() ) is blocking in the
* case of an empty queue.
*
* <p>Organisation: Louis Pasteur University, Strasbourg, France</p>
* <p>Network Research Team (http://www-r2.u-strasbg.fr)</p></p>
* <p>Organization: Louis Pasteur University, Strasbourg, France</p>
* <p>Network Research Team (http://www-r2.u-strasbg.fr)</p>
*
* @author Emil Ivov
* @version 0.1
@ -37,7 +37,6 @@ class MessageQueue
*/
MessageQueue()
{
}
/**

View File

@ -24,7 +24,6 @@ import org.ice4j.message.*;
*
* @author Emil Ivov
*/
class NetAccessManager
implements ErrorHandler
{
@ -33,6 +32,7 @@ class NetAccessManager
*/
private static final Logger logger =
Logger.getLogger(NetAccessManager.class.getName());
/**
* All access points currently in use. The table maps
* <tt>TransportAddress</tt>es to <tt>Connector</tt>s
@ -43,7 +43,7 @@ class NetAccessManager
/**
* A synchronized FIFO where incoming messages are stocked for processing.
*/
private MessageQueue messageQueue = new MessageQueue();
private final MessageQueue messageQueue = new MessageQueue();
/**
* A thread pool of message processors.
@ -52,7 +52,7 @@ class NetAccessManager
= new Vector<MessageProcessor>();
/**
* The instance that should be notified whan an incoming message has been
* The instance that should be notified when an incoming message has been
* processed and ready for delivery
*/
private MessageEventHandler messageEventHandler = null;
@ -119,7 +119,7 @@ class NetAccessManager
{
MessageProcessor mp = (MessageProcessor)callingThread;
logger.log( Level.WARNING, "A message processor has unexpectedly "
+"stopped. AP:" + mp.toString(), error);
+"stopped. AP:" + mp, error);
//make sure the guy's dead.
mp.stop();
@ -144,7 +144,6 @@ class NetAccessManager
protected void addSocket(DatagramSocket socket)
throws IOException
{
//no null check - let it through a null pointer exception
TransportAddress localAddr = new TransportAddress(
socket.getLocalAddress(), socket.getLocalPort(), Transport.UDP);
@ -159,7 +158,6 @@ class NetAccessManager
ap.start();
}
/**
* Stops and deletes the specified access point.
*
@ -216,7 +214,6 @@ class NetAccessManager
fillUpThreadPool(initialThreadPoolSize);
}
/**
* Starts all message processors
*
@ -236,13 +233,12 @@ class NetAccessManager
mp.start();
}
}
/**
* Starts all message processors
*
* @param newSize the new thread poolsize
* @param newSize the new thread pool size
*/
private void shrinkThreadPool(int newSize)
{
@ -281,5 +277,4 @@ class NetAccessManager
ap.sendMessage(bytes, remoteAddr);
}
}

View File

@ -6,8 +6,9 @@
*/
package org.ice4j.stack;
import java.util.logging.*;
import java.io.*;
import java.net.*;
import java.util.logging.*;
import org.ice4j.*;
import org.ice4j.message.*;
@ -93,34 +94,34 @@ class StunClientTransaction
/**
* The <tt>StunStack</tt> that created us.
*/
private StunStack stackCallback = null;
private final StunStack stackCallback;
/**
* The request that we are retransmitting.
*/
private Request request = null;
private final Request request;
/**
* The destination of the request.
*/
private TransportAddress requestDestination= null;
private final TransportAddress requestDestination;
/**
* The id of the transaction.
*/
private TransactionID transactionID = null;
private final TransactionID transactionID;
/**
* The <tt>TransportAddress</tt> through which the original request was sent
* and that we are supposed to be retransmitting through.
*/
private TransportAddress localAddress = null;
private final TransportAddress localAddress;
/**
* The instance to notify when a response has been received in the current
* transaction or when it has timed out.
*/
private ResponseCollector responseCollector = null;
private final ResponseCollector responseCollector;
/**
* Determines whether the transaction is active or not.
@ -191,8 +192,8 @@ class StunClientTransaction
retransmissionCounter ++)
{
waitFor(nextWaitInterval);
//did someone tell us to get lost?
//did someone tell us to get lost?
if(cancelled)
return;
@ -209,7 +210,7 @@ class StunClientTransaction
//has failed
logger.log(Level.WARNING,
"A client tran retransmission failed", ex);
};
}
}
//before stating that a transaction has timeout-ed we should first wait
@ -221,7 +222,6 @@ class StunClientTransaction
responseCollector.processTimeout();
stackCallback.removeClientTransaction(this);
}
/**
@ -231,9 +231,8 @@ class StunClientTransaction
* @throws IOException if an error occurs while sending message bytes
* through the network socket.
* @throws IllegalArgumentException if the apDescriptor references an
* access point that had not been installed,
* @throws StunException if message encoding fails,
*
* access point that had not been installed
* @throws StunException if message encoding fails
*/
void sendRequest()
throws StunException, IllegalArgumentException, IOException

View File

@ -46,14 +46,14 @@ public class StunStack
/**
* Stores active client transactions mapped against TransactionID-s.
*/
private Hashtable<TransactionID, StunClientTransaction> clientTransactions
private final Hashtable<TransactionID, StunClientTransaction> clientTransactions
= new Hashtable<TransactionID, StunClientTransaction>();
/**
* Currently open server transactions. The vector contains transaction ids
* for transactions corresponding to all non-answered received requests.
*/
private Hashtable<TransactionID, StunServerTransaction> serverTransactions
private final Hashtable<TransactionID, StunServerTransaction> serverTransactions
= new Hashtable<TransactionID, StunServerTransaction>();
/**
@ -61,7 +61,6 @@ public class StunStack
*/
private EventDispatcher eventDispatcher = new EventDispatcher();
/**
* Returns a reference to the singleton StunStack instance. If the stack
* had not yet been initialized, a new instance will be created.
@ -95,12 +94,11 @@ public class StunStack
* @param sock The socket that the new access point should represent.
* @throws IOException if we fail to setup the socket.
*/
public void addSocket(DatagramSocket sock)
public void addSocket(DatagramSocket sock)
throws IOException
{
netAccessManager.addSocket(sock);
}
{
netAccessManager.addSocket(sock);
}
/**
* Stops and deletes the connector listening on the specified local address.
@ -201,8 +199,7 @@ public class StunStack
* through the network socket.
* @throws IllegalArgumentException if the apDescriptor references an
* access point that had not been installed,
* @throws StunException if message encoding fails,
*
* @throws StunException if message encoding fails
*/
public void sendRequest( Request request,
TransportAddress sendTo,
@ -235,7 +232,7 @@ public class StunStack
* through the network socket.
* @throws IllegalArgumentException if the apDescriptor references an
* access point that had not been installed,
* @throws StunException if message encoding fails,
* @throws StunException if message encoding fails
*/
public void sendResponse(byte[] transactionID,
Response response,
@ -260,9 +257,6 @@ public class StunStack
{
sTran.sendResponse(response, sendThrough, sendTo);
}
}
/**
@ -294,7 +288,6 @@ public class StunStack
* like to listen on.
* @param listener The ConfigurationChangeListener to be added
*/
public void addRequestListener( TransportAddress localAddress,
RequestListener listener)
{
@ -407,7 +400,6 @@ public class StunStack
+ clientTransactions.toString());
}
}
}
/**
@ -424,7 +416,6 @@ public class StunStack
StunClientTransaction tran = clientTransactions.remove(item);
if(tran != null)
tran.cancel();
}
tids = serverTransactions.keys();
@ -434,7 +425,6 @@ public class StunStack
StunServerTransaction tran = serverTransactions.remove(item);
if(tran != null)
tran.expire();
}
}
}

View File

@ -7,6 +7,7 @@
package org.ice4j.stunclient;
import java.io.*;
import java.net.*;
import java.util.logging.*;
import org.ice4j.*;
@ -44,12 +45,12 @@ class BlockingRequestSender
/**
* The stack that we are using to send requests through.
*/
private StunStack stunStack = null;
private final StunStack stunStack;
/**
* The transport address that we are bound on.
*/
private TransportAddress localAddress = null;
private final TransportAddress localAddress;
/**
* The <tt>StunMessageEvent</tt> that contains the response matching our
@ -58,14 +59,14 @@ class BlockingRequestSender
private StunMessageEvent responseEvent = null;
/**
* Determines whether this request sender has comleted its coarse.
* Determines whether this request sender has completed its course.
*/
private boolean ended = false;
/**
* A lock object that we are using to synchronize sending.
*/
private Object sendLock = new Object();
private final Object sendLock = new Object();
/**
* Creates a new request sender.
@ -107,6 +108,25 @@ class BlockingRequestSender
}
}
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be considered
* unanswered.
*
* @param exception the <tt>PortUnreachableException</tt> which signaled
* that the destination of the request was found to be unreachable
* @see ResponseCollector#processUnreachable(PortUnreachableException)
*/
public synchronized void processUnreachable(
PortUnreachableException exception)
{
synchronized(sendLock)
{
ended = true;
notifyAll();
}
}
/**
* Sends the specified request and blocks until a response has been
* received or the request transaction has timed out.

View File

@ -283,74 +283,75 @@ public class MessageEventDispatchingTest extends TestCase
/**
* A utility class we use to collect incoming responses.
*/
private class PlainResponseCollector implements ResponseCollector
private class PlainResponseCollector
implements ResponseCollector
{
/**
*
*/
public Vector<Object> receivedResponses = new Vector<Object>();
public final Vector<Object> receivedResponses = new Vector<Object>();
/**
* Stores incoming requests.
*
* @param responseEvt the event containing the incoming request.
*/
public void processResponse(StunMessageEvent responseEvt)
public synchronized void processResponse(StunMessageEvent responseEvt)
{
synchronized(this)
{
receivedResponses.add(responseEvt);
notifyAll();
}
receivedResponses.add(responseEvt);
notifyAll();
}
/**
* Indicates that no response has been received.
*/
public void processTimeout()
public synchronized void processTimeout()
{
synchronized(this)
{
receivedResponses.add(new String("timeout"));
notifyAll();
}
receivedResponses.add("timeout");
notifyAll();
}
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be
* considered unanswered.
*
* @param exception the <tt>PortUnreachableException</tt> which signaled
* that the destination of the request was found to be unreachable
* @see ResponseCollector#processUnreachable(PortUnreachableException)
*/
public synchronized void processUnreachable(
PortUnreachableException exception)
{
receivedResponses.add("unreachable");
notifyAll();
}
/**
* Waits for a short period of time for a response to arrive
*/
public void waitForResponse()
public synchronized void waitForResponse()
{
synchronized(this)
try
{
try
{
if (receivedResponses.size() > 0)
return;
if (receivedResponses.size() == 0)
wait(50);
}
catch (InterruptedException e)
{}
}
catch (InterruptedException e)
{
}
}
/**
* Waits for a long period of time for a timeout trigger to fire.
*/
public void waitForTimeout()
public synchronized void waitForTimeout()
{
synchronized(this)
try
{
try
{
if (receivedResponses.size() > 0)
return;
if (receivedResponses.size() == 0)
wait(12000);
}
catch (InterruptedException e)
{}
}
catch (InterruptedException e)
{
}
}
}
}

View File

@ -86,8 +86,6 @@ public class TransactionSupportTests extends TestCase
"");
System.setProperty("org.ice4j.ORIGINAL_WAIT_INTERVAL",
"");
}
protected void tearDown() throws Exception
@ -164,8 +162,6 @@ public class TransactionSupportTests extends TestCase
oldRetransValue);
else
System.clearProperty("org.ice4j.MAX_WAIT_INTERVAL");
}
/**
@ -464,9 +460,10 @@ public class TransactionSupportTests extends TestCase
}
}
private class PlainResponseCollector implements ResponseCollector{
public Vector<Object> receivedResponses = new Vector<Object>();
private class PlainResponseCollector
implements ResponseCollector
{
public final Vector<Object> receivedResponses = new Vector<Object>();
public void processResponse(StunMessageEvent responseEvt)
{
@ -475,10 +472,24 @@ public class TransactionSupportTests extends TestCase
public void processTimeout()
{
receivedResponses.add(new String("timeout"));
receivedResponses.add("timeout");
}
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be
* considered unanswered.
*
* @param exception the <tt>PortUnreachableException</tt> which signaled
* that the destination of the request was found to be unreachable
* @see ResponseCollector#processUnreachable(PortUnreachableException)
*/
public void processUnreachable(PortUnreachableException exception)
{
receivedResponses.add("unreachable");
}
}
/*
public TransactionSupportTests(String name)
{

View File

@ -283,40 +283,46 @@ public class ShallowStackTest extends TestCase {
implements ResponseCollector
{
Response collectedResponse = null;
public void processResponse(StunMessageEvent evt)
public synchronized void processResponse(StunMessageEvent evt)
{
synchronized(this)
{
collectedResponse = (Response)evt.getMessage();
logger.finest("Received response.");
notifyAll();
}
collectedResponse = (Response)evt.getMessage();
logger.finest("Received response.");
notifyAll();
}
public void processTimeout()
public synchronized void processTimeout()
{
logger.info("Timeout");
synchronized(this)
{
notifyAll();
}
notifyAll();
}
public void waitForResponse()
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be
* considered unanswered.
*
* @param exception the <tt>PortUnreachableException</tt> which signaled
* that the destination of the request was found to be unreachable
* @see ResponseCollector#processUnreachable(PortUnreachableException)
*/
public synchronized void processUnreachable(
PortUnreachableException exception)
{
synchronized(this)
logger.info("Unreachable");
notifyAll();
}
public synchronized void waitForResponse()
{
try
{
try
{
if (collectedResponse != null)
return;
if (collectedResponse == null)
wait(50);
}
catch (InterruptedException e)
{
logger.log(Level.INFO, "oops", e);
}
}
catch (InterruptedException e)
{
logger.log(Level.INFO, "oops", e);
}
}
}