diff --git a/src/org/ice4j/StunMessageEvent.java b/src/org/ice4j/StunMessageEvent.java index 0f59bc7..49b782f 100644 --- a/src/org/ice4j/StunMessageEvent.java +++ b/src/org/ice4j/StunMessageEvent.java @@ -6,11 +6,9 @@ */ package org.ice4j; -import java.util.EventObject; -import java.net.InetSocketAddress; +import java.util.*; import org.ice4j.message.*; -import org.ice4j.stack.*; /** @@ -20,12 +18,8 @@ import org.ice4j.stack.*; * as well as the Descriptor of the NetAccessPoint that received it (In case the * stack is used on more than one ports/addresses). * - *
Organisation:
Louis Pasteur University, Strasbourg, France
- *Network Research Team (http://www-r2.u-strasbg.fr)
* @author Emil Ivov - * @version 0.1 */ - public class StunMessageEvent extends EventObject { @@ -37,12 +31,12 @@ public class StunMessageEvent /** * The message itself. */ - private Message message = null; + private Message message = null; /** * The sending address. */ - private TransportAddress remoteAddress = null; + private TransportAddress remoteAddress = null; /** * Constructs a StunMessageEvent according to the specified message. diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index 01e8220..f1c994e 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -8,6 +8,8 @@ package org.ice4j.ice.harvest; import org.ice4j.ice.*; +import org.ice4j.stack.*; +import org.ice4j.stunclient.*; /** * A StunCandidateHarvester gathers STUN Candidates for a @@ -28,5 +30,8 @@ public class StunCandidateHarvester public void harvest(Component component) { //todo implement + StunClient client = new StunClient(stack); + + } } diff --git a/src/org/ice4j/stack/MessageEventHandler.java b/src/org/ice4j/stack/MessageEventHandler.java index 4c83458..f2ee345 100644 --- a/src/org/ice4j/stack/MessageEventHandler.java +++ b/src/org/ice4j/stack/MessageEventHandler.java @@ -13,10 +13,7 @@ import org.ice4j.*; * NetAccessManager (and more precisely - MessageProcessors). This is our * way of keeping scalable network and stun layers. * - *Organisation:
Louis Pasteur University, Strasbourg, France
- *Network Research Team (http://www-r2.u-strasbg.fr)
* @author Emil Ivov - * @version 0.1 */ interface MessageEventHandler diff --git a/src/org/ice4j/stack/MessageProcessor.java b/src/org/ice4j/stack/MessageProcessor.java index d2d14ec..d9a2451 100644 --- a/src/org/ice4j/stack/MessageProcessor.java +++ b/src/org/ice4j/stack/MessageProcessor.java @@ -12,26 +12,43 @@ import org.ice4j.*; import org.ice4j.message.*; /** - * The class is used to parse and dispatch incoming messages in a multithreaded + * The class is used to parse and dispatch incoming messages in a multi-thread * manner. * - *Organisation: Louis Pasteur University, Strasbourg, France
- *Network Research Team (http://www-r2.u-strasbg.fr)
* @author Emil Ivov - * @version 0.1 */ - class MessageProcessor implements Runnable { - private static final Logger logger = - Logger.getLogger(MessageProcessor.class.getName()); + /** + * Our class logger. + */ + private static final Logger logger + = Logger.getLogger(MessageProcessor.class.getName()); + /** + * The queue where we store incoming messages until they are collected. + */ private MessageQueue messageQueue = null; + + /** + * The listener that will be retrieving MessageEvents + */ private MessageEventHandler messageHandler = null; + + /** + * The listener that will be collecting error notifications. + */ private ErrorHandler errorHandler = null; - private boolean isRunning = false; + /** + * The flag that indicates whether we are still running. + */ + private boolean isRunning = false; + + /** + * A reference to the thread that we use to execute ourselves. + */ private Thread runningThread = null; /** @@ -62,10 +79,8 @@ class MessageProcessor throw new IllegalArgumentException( "The error handler may not be " + null); - - this.messageQueue = queue; - this.messageHandler = messageHandler; + this.messageHandler = messageHandler; this.errorHandler = errorHandler; } @@ -74,8 +89,8 @@ class MessageProcessor */ public void run() { - //add an extra try/catch block that handles uncatched errors and helps avoid - //having dead threads in our pools. + //add an extra try/catch block that handles uncatched errors and helps + //avoid having dead threads in our pools. try { while (isRunning) @@ -89,8 +104,7 @@ class MessageProcessor { if(isRunning()) logger.log(Level.WARNING, - "A net access point has gone useless:", - ex); + "A net access point has gone useless:", ex); //nothing to do here since we test whether we are running //just beneath ... } @@ -106,10 +120,10 @@ class MessageProcessor Message stunMessage = null; try { - stunMessage = - Message.decode(rawMessage.getBytes(), - (char) 0, - (char) rawMessage.getMessageLength()); + stunMessage + = Message.decode(rawMessage.getBytes(), + (char) 0, + (char) rawMessage.getMessageLength()); } catch (StunException ex) { diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java index 9c3aded..81d4c54 100644 --- a/src/org/ice4j/stack/NetAccessManager.java +++ b/src/org/ice4j/stack/NetAccessManager.java @@ -7,10 +7,9 @@ package org.ice4j.stack; -import java.util.Hashtable; import java.io.*; +import java.net.*; import java.util.*; -import java.net.DatagramSocket; import java.util.logging.*; import org.ice4j.*; @@ -22,32 +21,34 @@ import org.ice4j.message.*; * Instances that operate with the NetAccessManager are only supposed to * understand STUN talk and shouldn't be aware of datagrams sockets, and etc. * - *Organisation: Louis Pasteur University, Strasbourg, France
- *Network Research Team (http://www-r2.u-strasbg.fr)
* @author Emil Ivov - * @version 0.1 */ class NetAccessManager implements ErrorHandler { + /** + * Our class logger + */ private static final Logger logger = Logger.getLogger(NetAccessManager.class.getName()); /** - * All access points currently in use. The table maps NetAccessPointDescriptor-s - * to NetAccessPoint-s + * All access points currently in use. The table maps + * NetAccessPointDescriptors to NetAccessPoints */ - private HashtableOrganisation: Louis Pasteur University, Strasbourg, France
- *Network Research Team (http://www-r2.u-strasbg.fr)
* @author Emil Ivov - * @version 0.1 */ class NetAccessPoint implements Runnable { + /** + * Our class logger. + */ private static final Logger logger = Logger.getLogger(NetAccessPoint.class.getName()); + + /** + * Creates a new access point. + */ NetAccessPoint() { } - /** - * Max datagram size. - */ - private static final int MAX_DATAGRAM_SIZE = 8 * 1024; - - /** * The message queue is where incoming messages are added. */ @@ -50,32 +47,16 @@ class NetAccessPoint */ protected DatagramSocket sock; - /** - * Indicates whether the access point is using a socket that was created - * by someone else. The variable is set to true when the AP's socket is - * set using theuseExternalSocket() method and is consulted
- * inside the stop method. When its value is true, the AP's socket is not
- * closed when stop()ing the AP.
- *
- * This variable is part of bug fix reported by Dave Stuart - SipQuest
- */
- private boolean isUsingExternalSocket = false;
-
/**
* A flag that is set to false to exit the message processor.
*/
private boolean isRunning;
- /**
- * The descriptor used to create this access point.
- */
- private NetAccessPointDescriptor apDescriptor = null;
-
/**
* The instance to be notified if errors occur in the network listening
* thread.
*/
- private ErrorHandler errorHandler = null;
+ private ErrorHandler errorHandler = null;
/**
* Used for locking socket operations
@@ -84,15 +65,16 @@ class NetAccessPoint
/**
* Creates a network access point.
- * @param apDescriptor the address and port where to bind.
+ * @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.
*/
- NetAccessPoint(NetAccessPointDescriptor apDescriptor,
- MessageQueue messageQueue,
- ErrorHandler errorHandler)
+ protected NetAccessPoint(DatagramSocket socket,
+ MessageQueue messageQueue,
+ ErrorHandler errorHandler)
{
- this.apDescriptor = apDescriptor;
+ this.sock = socket;
this.messageQueue = messageQueue;
this.errorHandler = errorHandler;
}
@@ -106,19 +88,8 @@ class NetAccessPoint
void start()
throws IOException
{
- synchronized(socketLock){
-
- //do not create the socket earlier as someone might want to set an
- // existing one was != null fixed (Ranga)
- if (sock == null)
- {
- this.sock = new DatagramSocket(getDescriptor().getAddress().
- getSocketAddress());
- this.isUsingExternalSocket = false;
- logger.info("Bound a socket on ap: " + toString());
- }
-
- sock.setReceiveBufferSize(MAX_DATAGRAM_SIZE);
+ synchronized(socketLock)
+ {
this.isRunning = true;
Thread thread = new Thread(this);
thread.start();
@@ -126,13 +97,14 @@ class NetAccessPoint
}
/**
- * Returns the NetAccessPointDescriptor that contains the port and address
- * associated with this accesspoint.
- * @return the NetAccessPointDescriptor associated with this AP.
+ * Returns the DatagramSocket that contains the port and address
+ * associated with this access point.
+ *
+ * @return the DatagramSocket associated with this AP.
*/
- NetAccessPointDescriptor getDescriptor()
+ protected DatagramSocket getSocket()
{
- return apDescriptor;
+ return sock;
}
/**
@@ -149,6 +121,11 @@ class NetAccessPoint
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,
@@ -172,10 +149,9 @@ class NetAccessPoint
+ "receive a message.",
ex);
}
- else
{
- //The exception was most probably caused by calling this.stop()
- // ....
+ //The exception was most probably caused by calling
+ //this.stop() ....
}
}
catch (IOException ex)
@@ -200,101 +176,31 @@ class NetAccessPoint
}
}
-
/**
- * Shut down the access point. Close the socket for recieving
- * incoming messages. The method won't close the socket if it was created
- * outside the stack (Bug Report - Dave Stuart - SipQuest). It also seems
- * that sometimes sockets don't immdeiately free their port and trying
- * to bind to that port immediately after gets us an exception. We therefore
- * null the socket, wait for 200 miliseconds and call the garbage collector
- * this seems to be working fine but is not what one could call neat
- * behaviour, so if you'd like to disable it - set the
- * org.ice4j.stack.HARD_SOCK_CLOSE property to anything different from
- * true. If you'd want us to wait more or less - set
- * org.ice4j.stack.WAIT_FOR_SOCK_CLOSE to the appropriate number of
- * miliseconds.
+ * Makes the access point stop listening on its socket.
*
*/
synchronized void stop()
{
this.isRunning = false;
-
- //avoid a needless null pointer exception
- if (sock != null
- && isUsingExternalSocket == false)
- {
- synchronized (socketLock)
- {
- sock.close();
-
- logger.info("Closed socket on ap " + toString());
- sock = null;
- String hardSocketClose =
- System.getProperty("org.ice4j.stack.HARD_SOCK_CLOSE");
-
- //sometimes sockts stay open even after closing them. make sure
- //this doesn't happen here. (unless the user doesn't want us to)
- if (hardSocketClose == null
- || hardSocketClose.equalsIgnoreCase("true"))
- {
-
- int waitForSockClose = 200;
- try
- {
- String waitForSockCloseStr =
- System.getProperty(
- "org.ice4j.stack.WAIT_FOR_SOCK_CLOSE");
- if (waitForSockCloseStr != null &&
- waitForSockCloseStr.length() > 0){
-
- waitForSockClose =
- Integer.parseInt(System.getProperty(
- waitForSockCloseStr));
- }
- }
- catch (Throwable t)
- {
- logger.log(
- Level.WARNING,
- "Failed to parse wait_for_sock_close prop", t);
-
- if (waitForSockClose < 0)
- {
- waitForSockClose = 200;
- }
- }
-
- //wait
- try
- {
- wait(waitForSockClose);
- }
- catch (InterruptedException t)
- {
- logger.warning("Interrupted waiting for sock close.");
- }
- System.gc();
- }
- }
- }
}
/**
* 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.getSocketAddress());
- synchronized(socketLock){
+ message, 0, message.length, address.getSocketAddress());
+
+ synchronized(socketLock)
+ {
sock.send(datagramPacket);
}
}
@@ -313,8 +219,9 @@ class NetAccessPoint
}
/**
- * Sets a socket for the access point to use. This socket will not be closed
- * when the AP is stop()ed (Bug Report - Dave Stuart - SipQuest).
+ * Sets a socket for the access point to use. This socket will not be
+ * closed when the AP is stop()ed (Bug Report - Dave Stuart).
+ *
* @param socket the socket that the AP should use.
*/
void useExternalSocket(DatagramSocket socket)
diff --git a/src/org/ice4j/stack/RawMessage.java b/src/org/ice4j/stack/RawMessage.java
index a7b6a86..455d1ba 100644
--- a/src/org/ice4j/stack/RawMessage.java
+++ b/src/org/ice4j/stack/RawMessage.java
@@ -15,11 +15,7 @@ import org.ice4j.*;
* of the host that sent it and the address and port where it was received
* (locally).
*
- * Organisation:
Louis Pasteur University, Strasbourg, France
- *Network Research Team (http://www-r2.u-strasbg.fr)
- * * @author Emil Ivov - * @version 0.1 */ class RawMessage { @@ -31,34 +27,29 @@ class RawMessage /** * The length of the message. */ - private int messageLength = -1; + private int messageLength = -1; + /** - * The addres and port where the message was sent from. + * The address and port where the message was sent from. */ private InetSocketAddress remoteAddress = null; - - /** - * The descriptor of the net access point that received the message. - */ - private NetAccessPointDescriptor receivingAccessPoint = null; - /** * Constructs a raw message with the specified field values. All parameters * are cloned before being assigned to class members. * - * @param messageBytes the message itself. - * @param remoteAddress the address where the message came from. - * @param remotePort the port where the message came from. - * @param netApDescriptor the access point the received the message.s + * @param messageBytes the message itself. + * @param messageLength the number of bytes currently stored in the + * messageBytes array. + * @param remoteAddress the address where the message came from. + * @param remotePort the port where the message came from. * * @throws NullPointerException if one or more of the parameters were null. */ - RawMessage(byte[] messageBytes, - int messageLength, - InetAddress remoteAddress, - int remotePort, - NetAccessPointDescriptor netApDescriptor) + RawMessage(byte[] messageBytes, + InetAddress remoteAddress, + int messageLength, + int remotePort) { //... don't do a null check - let it throw an NP exception @@ -68,8 +59,6 @@ class RawMessage 0, messageBytes.length); this.remoteAddress = new InetSocketAddress(remoteAddress, remotePort); - this.receivingAccessPoint = - (NetAccessPointDescriptor)netApDescriptor.clone(); } /** @@ -92,7 +81,6 @@ class RawMessage return messageLength; } - /** * Returns the address and port of the host that sent the message * @return the [address]:[port] pair that sent the message. @@ -101,13 +89,4 @@ class RawMessage { return this.remoteAddress; } - - /** - * Returns a descriptor of the access point that received the message. - * @return a descriptor of the access point that received the message. - */ - NetAccessPointDescriptor getNetAccessPoint() - { - return this.receivingAccessPoint; - } } diff --git a/src/org/ice4j/stack/StunProvider.java b/src/org/ice4j/stack/StunProvider.java index 3b44fbc..f7df534 100644 --- a/src/org/ice4j/stack/StunProvider.java +++ b/src/org/ice4j/stack/StunProvider.java @@ -1,4 +1,3 @@ - /* * Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal. * @@ -7,9 +6,9 @@ */ package org.ice4j.stack; +import java.io.*; import java.util.*; import java.util.logging.*; -import java.io.*; import org.ice4j.*; import org.ice4j.message.*; @@ -20,40 +19,40 @@ import org.ice4j.message.*; * responses. In the Stun4J implementation it is the transaction layer that * ensures reliable delivery. * - *Organisation:
Louis Pasteur University, Strasbourg, France
- *Network Research Team (http://www-r2.u-strasbg.fr)
* @author Emil Ivov - * @version 0.1 */ public class StunProvider implements MessageEventHandler { + /** + * Our class logger. + */ private static final Logger logger = Logger.getLogger(StunProvider.class.getName()); /** * Stores active client transactions mapped against TransactionID-s. */ - private HashtableOrganisation:
Louis Pasteur University, Strasbourg, France
- *Network Research Team (http://www-r2.u-strasbg.fr)
* @author Emil Ivov - * @version 0.1 */ public class SimpleAddressDetector { + /** + * Our class logger. + */ private static final Logger logger = Logger.getLogger(SimpleAddressDetector.class.getName()); + /** * The stack to use for STUN communication. */ @@ -47,7 +47,7 @@ public class SimpleAddressDetector private TransportAddress serverAddress = null; /** - * A utility used to flatten the multithreaded architecture of the Stack + * A utility used to flatten the multi-threaded architecture of the Stack * and execute the discovery process in a synchronized manner */ private BlockingRequestSender requestSender = null; @@ -98,9 +98,12 @@ public class SimpleAddressDetector /** * Creates a listening point from the following address and attempts to - * discover how it is mapped so that using inside the application is possible. + * discover how it is mapped so that using inside the application is + * possible. + * * @param address the [address]:[port] pair where ther request should be * sent from. + * * @return a StunAddress object containing the mapped address or null if * discovery failed. * @throws IOException if address is already bound. @@ -153,7 +156,9 @@ public class SimpleAddressDetector } /* in STUN bis, the response contains a XOR-MAPPED-ADDRESS */ - XorMappedAddressAttribute xorAtt = (XorMappedAddressAttribute)res.getAttribute(Attribute.XOR_MAPPED_ADDRESS); + XorMappedAddressAttribute xorAtt = (XorMappedAddressAttribute)res + .getAttribute(Attribute.XOR_MAPPED_ADDRESS); + if(xorAtt != null) { byte xoring[] = new byte[16]; @@ -219,7 +224,8 @@ public class SimpleAddressDetector } /* in STUN bis, the response contains a XOR-MAPPED-ADDRESS */ - XorMappedAddressAttribute xorAtt = (XorMappedAddressAttribute)res.getAttribute(Attribute.XOR_MAPPED_ADDRESS); + XorMappedAddressAttribute xorAtt = (XorMappedAddressAttribute)res + .getAttribute(Attribute.XOR_MAPPED_ADDRESS); if(xorAtt != null) { byte xoring[] = new byte[16]; @@ -250,20 +256,4 @@ public class SimpleAddressDetector { return getMappingFor(new TransportAddress(port)); } - - -/* - public static void main(String[] args) - throws Exception - { - SimpleAddressDetector detector = new SimpleAddressDetector( - new StunAddress("stun01.sipphone.com", 3478)); - detector.start(); - StunAddress mappedAddr = detector.getMappingFor(5060); - - System.out.println("address is " + mappedAddr); - - detector.shutDown(); - } -*/ } diff --git a/src/org/ice4j/stunclient/StunClient.java b/src/org/ice4j/stunclient/StunClient.java index 8220661..6af6ff7 100755 --- a/src/org/ice4j/stunclient/StunClient.java +++ b/src/org/ice4j/stunclient/StunClient.java @@ -17,8 +17,6 @@ import org.ice4j.stunclient.*; * and receive responses * * @author Namal Senarathne - * - * TODO : The constructor must have a reference to ICE Agent */ public class StunClient implements ResponseCollector @@ -44,6 +42,10 @@ public class StunClient */ private HashMap