Preparing for some serious refactoring of the stun stack

This commit is contained in:
emcho 2010-01-31 13:01:51 +00:00
parent cb3228446f
commit 0d7d7f8a68
5 changed files with 17 additions and 245 deletions

View File

@ -36,8 +36,8 @@ class NetAccessManager
* All access points currently in use. The table maps
* <tt>TransportAddress</tt>es to <tt>Connector</tt>s
*/
private Hashtable<TransportAddress, NetAccessPoint> netAccessPoints
= new Hashtable<TransportAddress, NetAccessPoint>();
private Hashtable<TransportAddress, Connector> netAccessPoints
= new Hashtable<TransportAddress, Connector>();
/**
* A synchronized FIFO where incoming messages are stocked for processing.
@ -106,12 +106,12 @@ class NetAccessManager
String message,
Throwable error)
{
if (callingThread instanceof NetAccessPoint)
if (callingThread instanceof Connector)
{
NetAccessPoint ap = (NetAccessPoint)callingThread;
Connector ap = (Connector)callingThread;
//make sure nothing's left and notify user
removeNetAccessPoint(ap.getListenAddress());
removeConnector(ap.getListenAddress());
logger.log(Level.WARNING, "Removing connector:"+ ap);
}
else if( callingThread instanceof MessageProcessor )
@ -140,7 +140,7 @@ class NetAccessManager
*
* @throws IOException if we fail to setup the socket.
*/
protected void installUdpConnector(DatagramSocket socket)
protected void addSocket(DatagramSocket socket)
throws IOException
{
@ -151,7 +151,7 @@ class NetAccessManager
if(netAccessPoints.containsKey(localAddr))
return;
NetAccessPoint ap = new NetAccessPoint(socket, messageQueue, this);
Connector ap = new Connector(socket, messageQueue, this);
netAccessPoints.put(localAddr, ap);
@ -164,9 +164,9 @@ class NetAccessManager
*
* @param address the address of the connector to remove.
*/
protected void removeNetAccessPoint(TransportAddress address)
protected void removeConnector(TransportAddress address)
{
NetAccessPoint ap = netAccessPoints.remove(address);
Connector ap = netAccessPoints.remove(address);
if(ap != null)
ap.stop();
@ -272,7 +272,7 @@ class NetAccessManager
throws IOException, IllegalArgumentException, StunException
{
byte[] bytes = stunMessage.encode();
NetAccessPoint ap = netAccessPoints.get(srcAddr);
Connector ap = netAccessPoints.get(srcAddr);
if(ap == null)
throw new IllegalArgumentException(

View File

@ -1,228 +0,0 @@
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license. See terms of license at gnu.org.
*/
package org.ice4j.stack;
import java.io.*;
import java.net.*;
import java.util.logging.*;
import org.ice4j.*;
/**
* The Network Access Point is the most outward part of the stack. It is
* constructed around a datagram socket and takes care about forwarding incoming
* messages to the MessageProcessor as well as sending datagrams to the STUN server
* specified by the original NetAccessPointDescriptor.
*
* @author Emil Ivov
*/
class NetAccessPoint
implements Runnable
{
/**
* Our class logger.
*/
private static final Logger logger =
Logger.getLogger(NetAccessPoint.class.getName());
/**
* The message queue is where incoming messages are added.
*/
private MessageQueue messageQueue = null;
/**
* The socket object that used by this access point to access the network.
*/
private final DatagramSocket sock;
/**
* A flag that is set to false to exit the message processor.
*/
private boolean isRunning;
/**
* The instance to be notified if errors occur in the network listening
* thread.
*/
private ErrorHandler errorHandler = null;
/**
* The address that we are listening to.
*/
private final TransportAddress listenAddress;
/**
* Creates a network access point.
* @param socket the socket that this access point is supposed to use for
* communication.
* @param messageQueue the FIFO list where incoming messages should be queued
* @param errorHandler the instance to notify when errors occur.
*/
protected NetAccessPoint(DatagramSocket socket,
MessageQueue messageQueue,
ErrorHandler errorHandler)
{
this.sock = socket;
this.messageQueue = messageQueue;
this.errorHandler = errorHandler;
this.listenAddress = new TransportAddress(socket.getLocalAddress(),
socket.getLocalPort(), Transport.UDP);
}
/**
* Start the network listening thread.
*
* @throws IOException if we fail to setup the socket.
*/
void start()
throws IOException
{
synchronized(sock)
{
this.isRunning = true;
Thread thread = new Thread(this);
thread.start();
}
}
/**
* Returns the <tt>DatagramSocket</tt> that contains the port and address
* associated with this access point.
*
* @return the <tt>DatagramSocket</tt> associated with this AP.
*/
protected DatagramSocket getSocket()
{
return sock;
}
/**
* The listening thread's run method.
*/
public void run()
{
while (this.isRunning)
{
try
{
int bufsize = sock.getReceiveBufferSize();
byte message[] = new byte[bufsize];
DatagramPacket packet = new DatagramPacket(message, bufsize);
sock.receive(packet);
//get lost if we are no longer running.
if(!isRunning)
return;
logger.finest("received datagram");
RawMessage rawMessage = new RawMessage( message,
packet.getLength(),
new TransportAddress( packet.getAddress(),
packet.getPort(), Transport.UDP),
listenAddress);
messageQueue.add(rawMessage);
}
catch (SocketException ex)
{
if (isRunning)
{
logger.log(Level.WARNING,
"A net access point has gone useless:", ex);
stop();
//Something wrong has happened
errorHandler.handleFatalError(
this,
"A socket exception was thrown while trying to "
+ "receive a message.",
ex);
}
{
//The exception was most probably caused by calling
//this.stop() ....
}
}
catch (IOException ex)
{
logger.log(Level.WARNING,
"A net access point has gone useless:", ex);
errorHandler.handleError(ex.getMessage(), ex);
//do not stop the thread;
}
catch (Throwable ex)
{
logger.log(Level.WARNING,
"A net access point has gone useless:", ex);
stop();
errorHandler.handleFatalError(
this,
"Unknown error occurred while listening for messages!",
ex);
}
}
}
/**
* Makes the access point stop listening on its socket.
*
*/
synchronized void stop()
{
this.isRunning = false;
}
/**
* Sends message through this access point's socket.
*
* @param message the bytes to send.
* @param address message destination.
*
* @throws IOException if an exception occurs while sending the message.
*/
void sendMessage(byte[] message, TransportAddress address)
throws IOException
{
DatagramPacket datagramPacket = new DatagramPacket(
message, 0, message.length, address);
synchronized(sock)
{
sock.send(datagramPacket);
}
}
/**
* Returns a String representation of the object.
* @return a String representation of the object.
*/
public String toString()
{
return "org.ice4j.stack.AccessPoint@"
+ sock.getLocalSocketAddress()
+" status: "
+ (isRunning? "not":"")
+" running";
}
/**
* Returns the <tt>TransportAddress</tt> that this access point is bound
* on.
*
* @return the <tt>TransportAddress</tt> associated with this AP.
*/
TransportAddress getListenAddress()
{
return listenAddress;
}
}

View File

@ -72,10 +72,10 @@ 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 installNetAccessPoint(DatagramSocket sock)
public void addSocket(DatagramSocket sock)
throws IOException
{
netAccessManager.installUdpConnector(sock);
netAccessManager.addSocket(sock);
}
@ -84,9 +84,9 @@ public class StunStack
*
* @param localAddr the access point to remove
*/
public void removeNetAccessPoint(TransportAddress localAddr)
public void removeSocket(TransportAddress localAddr)
{
netAccessManager.removeNetAccessPoint(localAddr);
netAccessManager.removeConnector(localAddr);
}

View File

@ -169,7 +169,7 @@ public class NetworkConfigurationDiscoveryProcess
DatagramSocket sock = new DatagramSocket(localAddress);
stunStack.installNetAccessPoint(sock);
stunStack.addSocket(sock);
stunProvider = stunStack.getProvider();
requestSender = new BlockingRequestSender(stunProvider, localAddress);

View File

@ -112,7 +112,7 @@ public class SimpleAddressDetector
TransportAddress localAddress = new TransportAddress(
(InetSocketAddress)socket.getLocalSocketAddress(), Transport.UDP);
stunStack.installNetAccessPoint(socket);
stunStack.addSocket(socket);
requestSender = new BlockingRequestSender(stunProvider, localAddress);
StunMessageEvent evt = null;
@ -132,7 +132,7 @@ public class SimpleAddressDetector
}
finally
{
stunStack.removeNetAccessPoint(localAddress);
stunStack.removeSocket(localAddress);
}
if(evt != null)