diff --git a/src/org/ice4j/socket/MultiplexedDatagramSocket.java b/src/org/ice4j/socket/MultiplexedDatagramSocket.java
index f6d7902..d54ddb8 100644
--- a/src/org/ice4j/socket/MultiplexedDatagramSocket.java
+++ b/src/org/ice4j/socket/MultiplexedDatagramSocket.java
@@ -21,12 +21,11 @@ import java.util.*;
* MultiplexedDatagramSocket will not be received through the
* associated MultiplexingDatagramSocket.
*
- * @author Lubomir Marinov
+ * @author Lyubomir Marinov
*/
public class MultiplexedDatagramSocket
extends DelegatingDatagramSocket
{
-
/**
* The DatagramPacketFilter which determines which
* DatagramPackets read from the network by {@link #multiplexing}
@@ -45,8 +44,7 @@ public class MultiplexedDatagramSocket
* The list of DatagramPackets to be received through this
* DatagramSocket i.e. accepted by {@link #filter}.
*/
- final List received
- = new LinkedList();
+ final List received = new LinkedList();
/**
* Initializes a new MultiplexedDatagramSocket which is unbound and
diff --git a/src/org/ice4j/socket/MultiplexingDatagramSocket.java b/src/org/ice4j/socket/MultiplexingDatagramSocket.java
index ad0ff46..08d762e 100644
--- a/src/org/ice4j/socket/MultiplexingDatagramSocket.java
+++ b/src/org/ice4j/socket/MultiplexingDatagramSocket.java
@@ -19,12 +19,11 @@ import java.util.logging.*;
* get received through it but through associated
* MultiplexedDatagramSockets.
*
- * @author Lubomir Marinov
+ * @author Lyubomir Marinov
*/
public class MultiplexingDatagramSocket
extends SafeCloseDatagramSocket
{
-
/**
* The Logger used by the MultiplexingDatagramSocket class
* and its instances for logging output.
@@ -40,6 +39,94 @@ public class MultiplexingDatagramSocket
private static final MultiplexedDatagramSocket[] NO_SOCKETS
= new MultiplexedDatagramSocket[0];
+ /**
+ * Initializes a new DatagramPacket instance which is a clone of a
+ * specific DatagramPacket i.e. the properties of the clone
+ * DatagramPacket are clones of the specified
+ * DatagramPacket.
+ *
+ * @param p the DatagramPacket to clone
+ * @return a new DatagramPacket instance which is a clone of the
+ * specified DatagramPacket
+ */
+ public static DatagramPacket clone(DatagramPacket p)
+ {
+ synchronized (p)
+ {
+ byte[] data = p.getData().clone();
+ InetAddress address = p.getAddress();
+ int port = p.getPort();
+
+ if ((address == null) || (port < 0))
+ {
+ return new DatagramPacket(data, p.getOffset(), p.getLength());
+ }
+ else
+ {
+ return
+ new DatagramPacket(
+ data, p.getOffset(), p.getLength(),
+ address, port);
+ }
+ }
+ }
+
+ /**
+ * Copies the properties of a specific DatagramPacket to another
+ * DatagramPacket. The property values are not cloned.
+ *
+ * @param src the DatagramPacket which is to have its properties
+ * copied to dest
+ * @param dest the DatagramPacket which is to have its properties
+ * set to the value of the respective properties of src
+ */
+ public static void copy(DatagramPacket src, DatagramPacket dest)
+ {
+ synchronized (dest)
+ {
+ dest.setAddress(src.getAddress());
+ dest.setPort(src.getPort());
+
+ byte[] srcData = src.getData();
+
+ if (srcData == null)
+ {
+ dest.setLength(0);
+ }
+ else
+ {
+ byte[] destData = dest.getData();
+
+ if (destData == null)
+ {
+ dest.setLength(0);
+ }
+ else
+ {
+ int destOffset = dest.getOffset();
+ int destLength = destData.length - destOffset;
+ int srcLength = src.getLength();
+
+ if (destLength >= srcLength)
+ {
+ destLength = srcLength;
+ }
+ else if (logger.isLoggable(Level.WARNING))
+ {
+ logger.log(
+ Level.WARNING,
+ "Truncating received DatagramPacket data!");
+ }
+ System.arraycopy(
+ srcData, src.getOffset(),
+ destData, destOffset,
+ destLength);
+ dest.setLength(destLength);
+ }
+ }
+ }
+ }
+
/**
* The indicator which determines whether this DatagramSocket is
* currently reading from the network using
@@ -49,6 +136,12 @@ public class MultiplexingDatagramSocket
*/
private boolean inReceive = false;
+ /**
+ * The value with which {@link DatagramSocket#setReceiveBufferSize(int)} is
+ * to be invoked if {@link #setReceiveBufferSize} is true.
+ */
+ private int receiveBufferSize;
+
/**
* The list of DatagramPackets to be received through this
* DatagramSocket i.e. not accepted by the DatagramFilters
@@ -57,12 +150,6 @@ public class MultiplexingDatagramSocket
private final List received
= new LinkedList();
- /**
- * The value with which {@link DatagramSocket#setReceiveBufferSize(int)} is
- * to be invoked if {@link #setReceiveBufferSize} is true.
- */
- private int receiveBufferSize;
-
/**
* The Object which synchronizes the access to {@link #inReceive}.
*/
@@ -185,38 +272,6 @@ public class MultiplexingDatagramSocket
super(bindaddr);
}
- /**
- * Initializes a new DatagramPacket instance which is a clone of a
- * specific DatagramPacket i.e. the properties of the clone
- * DatagramPacket are clones of the specified
- * DatagramPacket.
- *
- * @param p the DatagramPacket to clone
- * @return a new DatagramPacket instance which is a clone of the
- * specified DatagramPacket
- */
- public static DatagramPacket clone(DatagramPacket p)
- {
- synchronized (p)
- {
- byte[] data = p.getData().clone();
- InetAddress address = p.getAddress();
- int port = p.getPort();
-
- if ((address == null) || (port < 0))
- {
- return new DatagramPacket(data, p.getOffset(), p.getLength());
- }
- else
- {
- return
- new DatagramPacket(
- data, p.getOffset(), p.getLength(),
- address, port);
- }
- }
- }
-
/**
* Closes a specific MultiplexedDatagramSocket which filters
* DatagramPackets away from this DatagramSocket.
@@ -234,7 +289,9 @@ public class MultiplexingDatagramSocket
if (sockets[i].equals(multiplexed))
{
if (socketCount == 1)
+ {
sockets = NO_SOCKETS;
+ }
else
{
MultiplexedDatagramSocket[] newSockets
@@ -253,56 +310,6 @@ public class MultiplexingDatagramSocket
}
}
- /**
- * Copies the properties of a specific DatagramPacket to another
- * DatagramPacket. The property values are not cloned.
- *
- * @param src the DatagramPacket which is to have its properties
- * copied to dest
- * @param dest the DatagramPacket which is to have its properties
- * set to the value of the respective properties of src
- */
- public static void copy(DatagramPacket src, DatagramPacket dest)
- {
- synchronized (dest)
- {
- dest.setAddress(src.getAddress());
- dest.setPort(src.getPort());
-
- byte[] srcData = src.getData();
-
- if (srcData == null)
- dest.setLength(0);
- else
- {
- byte[] destData = dest.getData();
-
- if (destData == null)
- dest.setLength(0);
- else
- {
- int destOffset = dest.getOffset();
- int destLength = destData.length - destOffset;
- int srcLength = src.getLength();
-
- if (destLength >= srcLength)
- destLength = srcLength;
- else if (logger.isLoggable(Level.WARNING))
- {
- logger.log(
- Level.WARNING,
- "Truncating received DatagramPacket data!");
- }
- System.arraycopy(
- srcData, src.getOffset(),
- destData, destOffset,
- destLength);
- dest.setLength(destLength);
- }
- }
- }
- }
-
/**
* Gets a MultiplexedDatagramSocket which filters
* DatagramPackets away from this DatagramSocket using a
@@ -374,7 +381,9 @@ public class MultiplexingDatagramSocket
int socketCount = sockets.length;
if (socketCount == 0)
+ {
sockets = new MultiplexedDatagramSocket[] { socket };
+ }
else
{
MultiplexedDatagramSocket[] newSockets
@@ -389,6 +398,15 @@ public class MultiplexingDatagramSocket
}
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int getSoTimeout()
+ {
+ return soTimeout;
+ }
+
/**
* Receives a datagram packet from this socket. The DatagramPackets
* returned by this method do not match any of the
@@ -406,14 +424,14 @@ public class MultiplexingDatagramSocket
*
* @param p the DatagramPacket into which to place the incoming
* data
- * @throws SocketTimeoutException if setSoTimeout was
- * previously called and the timeout has expired
* @throws IOException if an I/O error occurs
+ * @throws SocketTimeoutException if setSoTimeout(int) was
+ * previously called and the timeout has expired
* @see DatagramSocket#receive(DatagramPacket)
*/
@Override
public void receive(DatagramPacket p)
- throws SocketTimeoutException, IOException
+ throws IOException
{
receive(received, p, soTimeout);
}
@@ -433,24 +451,27 @@ public class MultiplexingDatagramSocket
* @param timeout the maximum time in milliseconds to wait for a
* packet. A timeout of zero is interpreted as an infinite
* timeout
- * @throws SocketTimeoutException if the timeout has expired
* @throws IOException if an I/O error occurs
+ * @throws SocketTimeoutException if timeout is positive and has
+ * expired
*/
private void receive(
List received,
DatagramPacket p,
int timeout)
- throws SocketTimeoutException, IOException
+ throws IOException
{
- long now, start = System.currentTimeMillis();
+ long startTime = System.currentTimeMillis();
DatagramPacket r = null;
do
{
- now = System.currentTimeMillis();
- if (timeout > 0 && now - start >= timeout)
- throw new SocketTimeoutException("Socket timeout");
+ long now = System.currentTimeMillis();
+ // If there is a packet which has been received from the network and
+ // is to merely be received from the list of received
+ // DatagramPackets, then let it be received and do not throw a
+ // SocketTimeoutException.
synchronized (received)
{
if (!received.isEmpty())
@@ -461,6 +482,25 @@ public class MultiplexingDatagramSocket
}
}
+ // Throw a SocketTimeoutException if the timeout is over/up.
+ long remainingTimeout;
+
+ if (timeout > 0)
+ {
+ remainingTimeout = timeout - (now - startTime);
+ if (remainingTimeout <= 0L)
+ {
+ throw new SocketTimeoutException(
+ Long.toString(remainingTimeout));
+ }
+ }
+ else
+ {
+ remainingTimeout = 1000L;
+ }
+
+ // Determine whether the caller will receive from the network or
+ // will wait for a previous caller to receive from the network.
boolean wait;
synchronized (receiveSyncRoot)
@@ -475,21 +515,19 @@ public class MultiplexingDatagramSocket
inReceive = true;
}
}
-
try
{
if (wait)
{
+ // The caller will wait for a previous caller to receive
+ // from the network.
synchronized (received)
{
if (received.isEmpty())
{
try
{
- received.wait(
- timeout > 0
- ? timeout - (now - start)
- : 1000);
+ received.wait(remainingTimeout);
}
catch (InterruptedException ie)
{
@@ -503,6 +541,7 @@ public class MultiplexingDatagramSocket
continue;
}
+ // The caller will receive from the network.
DatagramPacket c = clone(p);
synchronized (receiveSyncRoot)
@@ -523,6 +562,8 @@ public class MultiplexingDatagramSocket
}
super.receive(c);
+ // The caller received from the network. Copy/add the packet to
+ // the receive list of the sockets which accept it.
synchronized (socketsSyncRoot)
{
boolean accepted = false;
@@ -538,14 +579,10 @@ public class MultiplexingDatagramSocket
}
accepted = true;
- /*
- * Emil: Don't break because we want all filtering
- * sockets to get the received packet.
- */
- //break;
+ // Emil: Don't break because we want all filtering
+ // sockets to get the received packet.
}
}
-
if (!accepted)
{
synchronized (this.received)
@@ -577,12 +614,12 @@ public class MultiplexingDatagramSocket
* @param multiplexed the MultiplexedDatagramSocket which requests
* the receipt of a DatagramPacket from the network
* @param p the DatagramPacket to receive the data from the network
- * @throws SocketTimeoutException if setSoTimeout was
- * previously called and the timeout has expired
* @throws IOException if an I/O error occurs
+ * @throws SocketTimeoutException if setSoTimeout(int) was
+ * previously called on multiplexed and the timeout has expired
*/
void receive(MultiplexedDatagramSocket multiplexed, DatagramPacket p)
- throws SocketTimeoutException, IOException
+ throws IOException
{
receive(multiplexed.received, p, multiplexed.getSoTimeout());
}
@@ -605,6 +642,7 @@ public class MultiplexingDatagramSocket
else
{
super.setReceiveBufferSize(receiveBufferSize);
+
setReceiveBufferSize = false;
}
}
@@ -618,15 +656,7 @@ public class MultiplexingDatagramSocket
throws SocketException
{
super.setSoTimeout(timeout);
+
soTimeout = timeout;
}
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int getSoTimeout()
- {
- return soTimeout;
- }
}
diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java
index dec0177..28bf433 100644
--- a/src/org/ice4j/stack/NetAccessManager.java
+++ b/src/org/ice4j/stack/NetAccessManager.java
@@ -7,7 +7,6 @@
package org.ice4j.stack;
import java.io.*;
-import java.net.*;
import java.util.*;
import java.util.logging.*;
@@ -278,7 +277,7 @@ class NetAccessManager
{
//no null check - let it through as a NullPointerException
Transport transport
- = socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
+ = socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
TransportAddress localAddr
= new TransportAddress(
socket.getLocalAddress(),
@@ -299,6 +298,7 @@ class NetAccessManager
connector.start();
}
break;
+
case TCP:
List connectors = netTCPAccessPoints.get(localAddr);
if (connectors == null)
@@ -321,6 +321,7 @@ class NetAccessManager
connectors.add(connector);
connector.start();
}
+ break;
}
}
}
@@ -358,8 +359,7 @@ class NetAccessManager
* @param dst the remote address of the connector to remote. For TCP only,
* use null for UDP.
*/
- protected void removeSocket(TransportAddress src,
- TransportAddress dst)
+ protected void removeSocket(TransportAddress src, TransportAddress dst)
{
Connector connector = null;
@@ -367,24 +367,24 @@ class NetAccessManager
{
switch (src.getTransport())
{
- case UDP:
- connector = netUDPAccessPoints.remove(src);
- break;
- case TCP:
- List connectors = netTCPAccessPoints.remove(src);
- if (connectors != null)
- {
- connector
- = findTCPConnectorByRemoteAddress(connectors, dst);
+ case UDP:
+ connector = netUDPAccessPoints.remove(src);
+ break;
- if (connector != null)
- {
- connectors.remove(connector);
- if (connectors.isEmpty())
- netTCPAccessPoints.remove(src);
- }
+ case TCP:
+ List connectors = netTCPAccessPoints.remove(src);
+ if (connectors != null)
+ {
+ connector
+ = findTCPConnectorByRemoteAddress(connectors, dst);
+ if (connector != null)
+ {
+ connectors.remove(connector);
+ if (connectors.isEmpty())
+ netTCPAccessPoints.remove(src);
}
- break;
+ }
+ break;
}
}
@@ -489,8 +489,7 @@ class NetAccessManager
* address and a particular destination address, or null if there's
* none.
*/
- private Connector getConnector(TransportAddress src,
- TransportAddress dst)
+ private Connector getConnector(TransportAddress src, TransportAddress dst)
{
synchronized (connectorsSyncRoot)
{