Clarifies, clean the source code. Adds javadocs. Fixes warnings.

This commit is contained in:
lubomir.marinov 2014-09-02 13:17:17 +00:00
parent 40541920f4
commit 04e0befbef
3 changed files with 175 additions and 148 deletions

View File

@ -21,12 +21,11 @@ import java.util.*;
* <tt>MultiplexedDatagramSocket</tt> will not be received through the * <tt>MultiplexedDatagramSocket</tt> will not be received through the
* associated <tt>MultiplexingDatagramSocket</tt>. * associated <tt>MultiplexingDatagramSocket</tt>.
* *
* @author Lubomir Marinov * @author Lyubomir Marinov
*/ */
public class MultiplexedDatagramSocket public class MultiplexedDatagramSocket
extends DelegatingDatagramSocket extends DelegatingDatagramSocket
{ {
/** /**
* The <tt>DatagramPacketFilter</tt> which determines which * The <tt>DatagramPacketFilter</tt> which determines which
* <tt>DatagramPacket</tt>s read from the network by {@link #multiplexing} * <tt>DatagramPacket</tt>s read from the network by {@link #multiplexing}
@ -45,8 +44,7 @@ public class MultiplexedDatagramSocket
* The list of <tt>DatagramPacket</tt>s to be received through this * The list of <tt>DatagramPacket</tt>s to be received through this
* <tt>DatagramSocket</tt> i.e. accepted by {@link #filter}. * <tt>DatagramSocket</tt> i.e. accepted by {@link #filter}.
*/ */
final List<DatagramPacket> received final List<DatagramPacket> received = new LinkedList<DatagramPacket>();
= new LinkedList<DatagramPacket>();
/** /**
* Initializes a new <tt>MultiplexedDatagramSocket</tt> which is unbound and * Initializes a new <tt>MultiplexedDatagramSocket</tt> which is unbound and

View File

@ -19,12 +19,11 @@ import java.util.logging.*;
* get received through it but through associated * get received through it but through associated
* <tt>MultiplexedDatagramSocket</tt>s. * <tt>MultiplexedDatagramSocket</tt>s.
* *
* @author Lubomir Marinov * @author Lyubomir Marinov
*/ */
public class MultiplexingDatagramSocket public class MultiplexingDatagramSocket
extends SafeCloseDatagramSocket extends SafeCloseDatagramSocket
{ {
/** /**
* The <tt>Logger</tt> used by the <tt>MultiplexingDatagramSocket</tt> class * The <tt>Logger</tt> used by the <tt>MultiplexingDatagramSocket</tt> class
* and its instances for logging output. * and its instances for logging output.
@ -40,6 +39,94 @@ public class MultiplexingDatagramSocket
private static final MultiplexedDatagramSocket[] NO_SOCKETS private static final MultiplexedDatagramSocket[] NO_SOCKETS
= new MultiplexedDatagramSocket[0]; = new MultiplexedDatagramSocket[0];
/**
* Initializes a new <tt>DatagramPacket</tt> instance which is a clone of a
* specific <tt>DatagramPacket</tt> i.e. the properties of the clone
* <tt>DatagramPacket</tt> are clones of the specified
* <tt>DatagramPacket</tt>.
*
* @param p the <tt>DatagramPacket</tt> to clone
* @return a new <tt>DatagramPacket</tt> instance which is a clone of the
* specified <tt>DatagramPacket</tt>
*/
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 <tt>DatagramPacket</tt> to another
* <tt>DatagramPacket</tt>. The property values are not cloned.
*
* @param src the <tt>DatagramPacket</tt> which is to have its properties
* copied to <tt>dest</tt>
* @param dest the <tt>DatagramPacket</tt> which is to have its properties
* set to the value of the respective properties of <tt>src</tt>
*/
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 <tt>DatagramSocket</tt> is * The indicator which determines whether this <tt>DatagramSocket</tt> is
* currently reading from the network using * currently reading from the network using
@ -49,6 +136,12 @@ public class MultiplexingDatagramSocket
*/ */
private boolean inReceive = false; private boolean inReceive = false;
/**
* The value with which {@link DatagramSocket#setReceiveBufferSize(int)} is
* to be invoked if {@link #setReceiveBufferSize} is <tt>true</tt>.
*/
private int receiveBufferSize;
/** /**
* The list of <tt>DatagramPacket</tt>s to be received through this * The list of <tt>DatagramPacket</tt>s to be received through this
* <tt>DatagramSocket</tt> i.e. not accepted by the <tt>DatagramFilter</tt>s * <tt>DatagramSocket</tt> i.e. not accepted by the <tt>DatagramFilter</tt>s
@ -57,12 +150,6 @@ public class MultiplexingDatagramSocket
private final List<DatagramPacket> received private final List<DatagramPacket> received
= new LinkedList<DatagramPacket>(); = new LinkedList<DatagramPacket>();
/**
* The value with which {@link DatagramSocket#setReceiveBufferSize(int)} is
* to be invoked if {@link #setReceiveBufferSize} is <tt>true</tt>.
*/
private int receiveBufferSize;
/** /**
* The <tt>Object</tt> which synchronizes the access to {@link #inReceive}. * The <tt>Object</tt> which synchronizes the access to {@link #inReceive}.
*/ */
@ -185,38 +272,6 @@ public class MultiplexingDatagramSocket
super(bindaddr); super(bindaddr);
} }
/**
* Initializes a new <tt>DatagramPacket</tt> instance which is a clone of a
* specific <tt>DatagramPacket</tt> i.e. the properties of the clone
* <tt>DatagramPacket</tt> are clones of the specified
* <tt>DatagramPacket</tt>.
*
* @param p the <tt>DatagramPacket</tt> to clone
* @return a new <tt>DatagramPacket</tt> instance which is a clone of the
* specified <tt>DatagramPacket</tt>
*/
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 <tt>MultiplexedDatagramSocket</tt> which filters * Closes a specific <tt>MultiplexedDatagramSocket</tt> which filters
* <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt>. * <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt>.
@ -234,7 +289,9 @@ public class MultiplexingDatagramSocket
if (sockets[i].equals(multiplexed)) if (sockets[i].equals(multiplexed))
{ {
if (socketCount == 1) if (socketCount == 1)
{
sockets = NO_SOCKETS; sockets = NO_SOCKETS;
}
else else
{ {
MultiplexedDatagramSocket[] newSockets MultiplexedDatagramSocket[] newSockets
@ -253,56 +310,6 @@ public class MultiplexingDatagramSocket
} }
} }
/**
* Copies the properties of a specific <tt>DatagramPacket</tt> to another
* <tt>DatagramPacket</tt>. The property values are not cloned.
*
* @param src the <tt>DatagramPacket</tt> which is to have its properties
* copied to <tt>dest</tt>
* @param dest the <tt>DatagramPacket</tt> which is to have its properties
* set to the value of the respective properties of <tt>src</tt>
*/
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 <tt>MultiplexedDatagramSocket</tt> which filters * Gets a <tt>MultiplexedDatagramSocket</tt> which filters
* <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt> using a * <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt> using a
@ -374,7 +381,9 @@ public class MultiplexingDatagramSocket
int socketCount = sockets.length; int socketCount = sockets.length;
if (socketCount == 0) if (socketCount == 0)
{
sockets = new MultiplexedDatagramSocket[] { socket }; sockets = new MultiplexedDatagramSocket[] { socket };
}
else else
{ {
MultiplexedDatagramSocket[] newSockets MultiplexedDatagramSocket[] newSockets
@ -389,6 +398,15 @@ public class MultiplexingDatagramSocket
} }
} }
/**
* {@inheritDoc}
*/
@Override
public int getSoTimeout()
{
return soTimeout;
}
/** /**
* Receives a datagram packet from this socket. The <tt>DatagramPacket</tt>s * Receives a datagram packet from this socket. The <tt>DatagramPacket</tt>s
* returned by this method do not match any of the * returned by this method do not match any of the
@ -406,14 +424,14 @@ public class MultiplexingDatagramSocket
* *
* @param p the <tt>DatagramPacket</tt> into which to place the incoming * @param p the <tt>DatagramPacket</tt> into which to place the incoming
* data * data
* @throws SocketTimeoutException if <tt>setSoTimeout</tt> was
* previously called and the timeout has expired
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
* @throws SocketTimeoutException if <tt>setSoTimeout(int)</tt> was
* previously called and the timeout has expired
* @see DatagramSocket#receive(DatagramPacket) * @see DatagramSocket#receive(DatagramPacket)
*/ */
@Override @Override
public void receive(DatagramPacket p) public void receive(DatagramPacket p)
throws SocketTimeoutException, IOException throws IOException
{ {
receive(received, p, soTimeout); receive(received, p, soTimeout);
} }
@ -433,24 +451,27 @@ public class MultiplexingDatagramSocket
* @param timeout the maximum time in milliseconds to wait for a * @param timeout the maximum time in milliseconds to wait for a
* packet. A timeout of zero is interpreted as an infinite * packet. A timeout of zero is interpreted as an infinite
* timeout * timeout
* @throws SocketTimeoutException if the timeout has expired
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
* @throws SocketTimeoutException if <tt>timeout</tt> is positive and has
* expired
*/ */
private void receive( private void receive(
List<DatagramPacket> received, List<DatagramPacket> received,
DatagramPacket p, DatagramPacket p,
int timeout) int timeout)
throws SocketTimeoutException, IOException throws IOException
{ {
long now, start = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
DatagramPacket r = null; DatagramPacket r = null;
do do
{ {
now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (timeout > 0 && now - start >= timeout)
throw new SocketTimeoutException("Socket timeout");
// 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) synchronized (received)
{ {
if (!received.isEmpty()) 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; boolean wait;
synchronized (receiveSyncRoot) synchronized (receiveSyncRoot)
@ -475,21 +515,19 @@ public class MultiplexingDatagramSocket
inReceive = true; inReceive = true;
} }
} }
try try
{ {
if (wait) if (wait)
{ {
// The caller will wait for a previous caller to receive
// from the network.
synchronized (received) synchronized (received)
{ {
if (received.isEmpty()) if (received.isEmpty())
{ {
try try
{ {
received.wait( received.wait(remainingTimeout);
timeout > 0
? timeout - (now - start)
: 1000);
} }
catch (InterruptedException ie) catch (InterruptedException ie)
{ {
@ -503,6 +541,7 @@ public class MultiplexingDatagramSocket
continue; continue;
} }
// The caller will receive from the network.
DatagramPacket c = clone(p); DatagramPacket c = clone(p);
synchronized (receiveSyncRoot) synchronized (receiveSyncRoot)
@ -523,6 +562,8 @@ public class MultiplexingDatagramSocket
} }
super.receive(c); 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) synchronized (socketsSyncRoot)
{ {
boolean accepted = false; boolean accepted = false;
@ -538,14 +579,10 @@ public class MultiplexingDatagramSocket
} }
accepted = true; accepted = true;
/* // Emil: Don't break because we want all filtering
* Emil: Don't break because we want all filtering // sockets to get the received packet.
* sockets to get the received packet.
*/
//break;
} }
} }
if (!accepted) if (!accepted)
{ {
synchronized (this.received) synchronized (this.received)
@ -577,12 +614,12 @@ public class MultiplexingDatagramSocket
* @param multiplexed the <tt>MultiplexedDatagramSocket</tt> which requests * @param multiplexed the <tt>MultiplexedDatagramSocket</tt> which requests
* the receipt of a <tt>DatagramPacket</tt> from the network * the receipt of a <tt>DatagramPacket</tt> from the network
* @param p the <tt>DatagramPacket</tt> to receive the data from the network * @param p the <tt>DatagramPacket</tt> to receive the data from the network
* @throws SocketTimeoutException if <tt>setSoTimeout</tt> was
* previously called and the timeout has expired
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
* @throws SocketTimeoutException if <tt>setSoTimeout(int)</tt> was
* previously called on <tt>multiplexed</tt> and the timeout has expired
*/ */
void receive(MultiplexedDatagramSocket multiplexed, DatagramPacket p) void receive(MultiplexedDatagramSocket multiplexed, DatagramPacket p)
throws SocketTimeoutException, IOException throws IOException
{ {
receive(multiplexed.received, p, multiplexed.getSoTimeout()); receive(multiplexed.received, p, multiplexed.getSoTimeout());
} }
@ -605,6 +642,7 @@ public class MultiplexingDatagramSocket
else else
{ {
super.setReceiveBufferSize(receiveBufferSize); super.setReceiveBufferSize(receiveBufferSize);
setReceiveBufferSize = false; setReceiveBufferSize = false;
} }
} }
@ -618,15 +656,7 @@ public class MultiplexingDatagramSocket
throws SocketException throws SocketException
{ {
super.setSoTimeout(timeout); super.setSoTimeout(timeout);
soTimeout = timeout; soTimeout = timeout;
} }
/**
* {@inheritDoc}
*/
@Override
public int getSoTimeout()
{
return soTimeout;
}
} }

View File

@ -7,7 +7,6 @@
package org.ice4j.stack; package org.ice4j.stack;
import java.io.*; import java.io.*;
import java.net.*;
import java.util.*; import java.util.*;
import java.util.logging.*; import java.util.logging.*;
@ -299,6 +298,7 @@ class NetAccessManager
connector.start(); connector.start();
} }
break; break;
case TCP: case TCP:
List<Connector> connectors = netTCPAccessPoints.get(localAddr); List<Connector> connectors = netTCPAccessPoints.get(localAddr);
if (connectors == null) if (connectors == null)
@ -321,6 +321,7 @@ class NetAccessManager
connectors.add(connector); connectors.add(connector);
connector.start(); connector.start();
} }
break;
} }
} }
} }
@ -358,8 +359,7 @@ class NetAccessManager
* @param dst the remote address of the connector to remote. For TCP only, * @param dst the remote address of the connector to remote. For TCP only,
* use <tt>null</tt> for UDP. * use <tt>null</tt> for UDP.
*/ */
protected void removeSocket(TransportAddress src, protected void removeSocket(TransportAddress src, TransportAddress dst)
TransportAddress dst)
{ {
Connector connector = null; Connector connector = null;
@ -370,13 +370,13 @@ class NetAccessManager
case UDP: case UDP:
connector = netUDPAccessPoints.remove(src); connector = netUDPAccessPoints.remove(src);
break; break;
case TCP: case TCP:
List<Connector> connectors = netTCPAccessPoints.remove(src); List<Connector> connectors = netTCPAccessPoints.remove(src);
if (connectors != null) if (connectors != null)
{ {
connector connector
= findTCPConnectorByRemoteAddress(connectors, dst); = findTCPConnectorByRemoteAddress(connectors, dst);
if (connector != null) if (connector != null)
{ {
connectors.remove(connector); connectors.remove(connector);
@ -489,8 +489,7 @@ class NetAccessManager
* address and a particular destination address, or <tt>null</tt> if there's * address and a particular destination address, or <tt>null</tt> if there's
* none. * none.
*/ */
private Connector getConnector(TransportAddress src, private Connector getConnector(TransportAddress src, TransportAddress dst)
TransportAddress dst)
{ {
synchronized (connectorsSyncRoot) synchronized (connectorsSyncRoot)
{ {