From 0d7d7f8a68d2a3db69d68fb2945ace7d803ce35f Mon Sep 17 00:00:00 2001 From: emcho Date: Sun, 31 Jan 2010 13:01:51 +0000 Subject: [PATCH] Preparing for some serious refactoring of the stun stack --- src/org/ice4j/stack/NetAccessManager.java | 20 +- src/org/ice4j/stack/NetAccessPoint.java | 228 ------------------ src/org/ice4j/stack/StunStack.java | 8 +- .../NetworkConfigurationDiscoveryProcess.java | 2 +- .../stunclient/SimpleAddressDetector.java | 4 +- 5 files changed, 17 insertions(+), 245 deletions(-) delete mode 100644 src/org/ice4j/stack/NetAccessPoint.java diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java index f4d201f..676fb48 100644 --- a/src/org/ice4j/stack/NetAccessManager.java +++ b/src/org/ice4j/stack/NetAccessManager.java @@ -36,8 +36,8 @@ class NetAccessManager * All access points currently in use. The table maps * TransportAddresses to Connectors */ - private Hashtable netAccessPoints - = new Hashtable(); + private Hashtable netAccessPoints + = new Hashtable(); /** * 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( diff --git a/src/org/ice4j/stack/NetAccessPoint.java b/src/org/ice4j/stack/NetAccessPoint.java deleted file mode 100644 index 822c12f..0000000 --- a/src/org/ice4j/stack/NetAccessPoint.java +++ /dev/null @@ -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 DatagramSocket that contains the port and address - * associated with this access point. - * - * @return the DatagramSocket 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 TransportAddress that this access point is bound - * on. - * - * @return the TransportAddress associated with this AP. - */ - TransportAddress getListenAddress() - { - return listenAddress; - } -} diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index a5beed1..89d88fe 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -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); } diff --git a/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java b/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java index ecdd94b..9d40aca 100644 --- a/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java +++ b/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java @@ -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); diff --git a/src/org/ice4j/stunclient/SimpleAddressDetector.java b/src/org/ice4j/stunclient/SimpleAddressDetector.java index a4d7e98..16edc12 100644 --- a/src/org/ice4j/stunclient/SimpleAddressDetector.java +++ b/src/org/ice4j/stunclient/SimpleAddressDetector.java @@ -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)