commit
dc4e05e5d9
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
*
|
||||
* Copyright @ 2015 Atlassian Pty Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.ice4j.ice.harvest;
|
||||
|
||||
/**
|
||||
* An abstract partial implementation of {@link CandidateHarvester}.
|
||||
*
|
||||
* @author Emil Ivov
|
||||
* @author Lyubomir Marinov
|
||||
* @author Boris Grozev
|
||||
*/
|
||||
public abstract class AbstractCandidateHarvester
|
||||
implements CandidateHarvester
|
||||
{
|
||||
/**
|
||||
* Manages statistics about harvesting time.
|
||||
*/
|
||||
private HarvestStatistics harvestStatistics = new HarvestStatistics();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public HarvestStatistics getHarvestStatistics()
|
||||
{
|
||||
return harvestStatistics;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean isHostHarvester()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
1152
src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java
Normal file
1152
src/main/java/org/ice4j/ice/harvest/AbstractTcpListener.java
Normal file
File diff suppressed because it is too large
Load Diff
648
src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java
Normal file
648
src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java
Normal file
@ -0,0 +1,648 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
*
|
||||
* Copyright @ 2015 Atlassian Pty Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.ice4j.ice.harvest;
|
||||
|
||||
import org.ice4j.*;
|
||||
import org.ice4j.attribute.*;
|
||||
import org.ice4j.ice.*;
|
||||
import org.ice4j.message.*;
|
||||
import org.ice4j.socket.*;
|
||||
import org.ice4j.stack.*;
|
||||
import org.ice4j.util.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.logging.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* A class which holds a {@link DatagramSocket} and runs a thread
|
||||
* ({@link #thread}) which perpetually reads from it.
|
||||
*
|
||||
* When a datagram from an unknown source is received, it is parsed as a STUN
|
||||
* Binding Request, and if it has a USERNAME attribute, its ufrag is extracted.
|
||||
* At this point, an implementing class may choose to create a mapping for
|
||||
* the remote address of the datagram, which will be used for further packets
|
||||
* from this address.
|
||||
*
|
||||
* @author Boris Grozev
|
||||
*/
|
||||
public abstract class AbstractUdpListener
|
||||
{
|
||||
/**
|
||||
* Our class logger.
|
||||
*/
|
||||
private static final Logger logger
|
||||
= Logger.getLogger(AbstractUdpListener.class.getName());
|
||||
|
||||
/**
|
||||
* The size for newly allocated <tt>Buffer</tt> instances. This limits the
|
||||
* maximum size of datagrams we can receive.
|
||||
*
|
||||
* XXX should we increase this in case of other MTUs, or set it dynamically
|
||||
* according to the available network interfaces?
|
||||
*/
|
||||
private static final int BUFFER_SIZE
|
||||
= /* assumed MTU */ 1500 - /* IPv4 header */ 20 - /* UDP header */ 8;
|
||||
|
||||
/**
|
||||
* The number of <tt>Buffer</tt> instances to keep in {@link #pool}.
|
||||
*/
|
||||
private static final int POOL_SIZE = 256;
|
||||
|
||||
/**
|
||||
* The name of the property which controls the size of the receive buffer
|
||||
* which {@link SinglePortUdpHarvester} will request for the sockets that
|
||||
* it creates.
|
||||
*/
|
||||
private static final String SO_RCVBUF_PNAME
|
||||
= AbstractUdpListener.class.getName() + ".SO_RCVBUF";
|
||||
|
||||
/**
|
||||
* Returns the list of {@link TransportAddress}es, one for each allowed IP
|
||||
* address found on each allowed network interface, with the given port.
|
||||
*
|
||||
* @param port the UDP port number.
|
||||
* @return the list of allowed transport addresses.
|
||||
*/
|
||||
public static List<TransportAddress> getAllowedAddresses(int port)
|
||||
{
|
||||
List<TransportAddress> addresses = new LinkedList<>();
|
||||
boolean isIPv6Disabled = StackProperties.getBoolean(
|
||||
StackProperties.DISABLE_IPv6,
|
||||
false);
|
||||
boolean isIPv6LinkLocalDisabled = StackProperties.getBoolean(
|
||||
StackProperties.DISABLE_LINK_LOCAL_ADDRESSES,
|
||||
false);
|
||||
|
||||
try
|
||||
{
|
||||
for (NetworkInterface iface
|
||||
: Collections.list(NetworkInterface.getNetworkInterfaces()))
|
||||
{
|
||||
if (NetworkUtils.isInterfaceLoopback(iface)
|
||||
|| !NetworkUtils.isInterfaceUp(iface)
|
||||
|| !HostCandidateHarvester.isInterfaceAllowed(iface))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Enumeration<InetAddress> ifaceAddresses
|
||||
= iface.getInetAddresses();
|
||||
while (ifaceAddresses.hasMoreElements())
|
||||
{
|
||||
InetAddress address = ifaceAddresses.nextElement();
|
||||
|
||||
if (!HostCandidateHarvester.isAddressAllowed(address))
|
||||
continue;
|
||||
|
||||
if (isIPv6Disabled && address instanceof Inet6Address)
|
||||
continue;
|
||||
if (isIPv6LinkLocalDisabled
|
||||
&& address instanceof Inet6Address
|
||||
&& address.isLinkLocalAddress())
|
||||
continue;
|
||||
|
||||
addresses.add(
|
||||
new TransportAddress(address, port, Transport.UDP));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SocketException se)
|
||||
{
|
||||
logger.info("Failed to get network interfaces: " + se);
|
||||
}
|
||||
|
||||
return addresses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to parse the bytes in <tt>buf</tt> at offset <tt>off</tt> (and
|
||||
* length <tt>len</tt>) as a STUN Binding Request message. If successful,
|
||||
* looks for a USERNAME attribute and returns the local username fragment
|
||||
* part (see RFC5245 Section 7.1.2.3).
|
||||
* In case of any failure returns <tt>null</tt>.
|
||||
*
|
||||
* @param buf the bytes.
|
||||
* @param off the offset.
|
||||
* @param len the length.
|
||||
* @return the local ufrag from the USERNAME attribute of the STUN message
|
||||
* contained in <tt>buf</tt>, or <tt>null</tt>.
|
||||
*/
|
||||
static String getUfrag(byte[] buf, int off, int len)
|
||||
{
|
||||
// RFC5389, Section 6:
|
||||
// All STUN messages MUST start with a 20-byte header followed by zero
|
||||
// or more Attributes.
|
||||
if (buf == null || buf.length < off + len || len < 20)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// RFC5389, Section 6:
|
||||
// The magic cookie field MUST contain the fixed value 0x2112A442 in
|
||||
// network byte order.
|
||||
if ( !( (buf[off + 4] & 0xFF) == 0x21 &&
|
||||
(buf[off + 5] & 0xFF) == 0x12 &&
|
||||
(buf[off + 6] & 0xFF) == 0xA4 &&
|
||||
(buf[off + 7] & 0xFF) == 0x42))
|
||||
{
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.fine("Not a STUN packet, magic cookie not found.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Message stunMessage
|
||||
= Message.decode(buf,
|
||||
(char) off,
|
||||
(char) len);
|
||||
|
||||
if (stunMessage.getMessageType()
|
||||
!= Message.BINDING_REQUEST)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
UsernameAttribute usernameAttribute
|
||||
= (UsernameAttribute)
|
||||
stunMessage.getAttribute(Attribute.USERNAME);
|
||||
if (usernameAttribute == null)
|
||||
return null;
|
||||
|
||||
String usernameString
|
||||
= new String(usernameAttribute.getUsername());
|
||||
return usernameString.split(":")[0];
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Catch everything. We are going to log, and then drop the packet
|
||||
// anyway.
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.fine("Failed to extract local ufrag: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The map which keeps the known remote addresses and their associated
|
||||
* candidateSockets.
|
||||
* {@link #thread} is the only thread which adds new entries, while
|
||||
* other threads remove entries when candidates are freed.
|
||||
*/
|
||||
private final Map<SocketAddress, MySocket> sockets
|
||||
= new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* A pool of <tt>Buffer</tt> instances used to avoid creating of new java
|
||||
* objects.
|
||||
*/
|
||||
private final ArrayBlockingQueue<Buffer> pool
|
||||
= new ArrayBlockingQueue<>(POOL_SIZE);
|
||||
|
||||
/**
|
||||
* The local address that this harvester is bound to.
|
||||
*/
|
||||
protected final TransportAddress localAddress;
|
||||
|
||||
/**
|
||||
* The "main" socket that this harvester reads from.
|
||||
*/
|
||||
private final DatagramSocket socket;
|
||||
|
||||
/**
|
||||
* The thread reading from {@link #socket}.
|
||||
*/
|
||||
private final Thread thread;
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to
|
||||
* bind on the specified local address.
|
||||
* @param localAddress the address to bind to.
|
||||
* @throws IOException if initialization fails.
|
||||
*/
|
||||
protected AbstractUdpListener(TransportAddress localAddress)
|
||||
throws IOException
|
||||
{
|
||||
this.localAddress = localAddress;
|
||||
socket = new DatagramSocket(localAddress);
|
||||
|
||||
int receiveBufferSize = StackProperties.getInt(SO_RCVBUF_PNAME, -1);
|
||||
if (receiveBufferSize > 0)
|
||||
{
|
||||
socket.setReceiveBufferSize(receiveBufferSize);
|
||||
}
|
||||
|
||||
String logMessage
|
||||
= "Initialized AbstractUdpListener with address " + localAddress;
|
||||
logMessage += ". Receive buffer size " + socket.getReceiveBufferSize();
|
||||
if (receiveBufferSize > 0)
|
||||
{
|
||||
logMessage += " (asked for " + receiveBufferSize + ")";
|
||||
}
|
||||
logger.info(logMessage);
|
||||
|
||||
thread = new Thread()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
AbstractUdpListener.this.runInHarvesterThread();
|
||||
}
|
||||
};
|
||||
|
||||
thread.setName(AbstractUdpListener.class.getName() + " thread for "
|
||||
+ localAddress);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Perpetually reads datagrams from {@link #socket} and handles them
|
||||
* accordingly.
|
||||
*
|
||||
* It is important that this blocks are little as possible (except on
|
||||
* socket.receive(), of course), because it could potentially delay the
|
||||
* reception of both ICE and media packets for the whole application.
|
||||
*/
|
||||
private void runInHarvesterThread()
|
||||
{
|
||||
Buffer buf;
|
||||
DatagramPacket pkt = null;
|
||||
MySocket destinationSocket;
|
||||
InetSocketAddress remoteAddress;
|
||||
|
||||
do
|
||||
{
|
||||
// TODO: implement stopping the thread with a switch?
|
||||
|
||||
buf = getFreeBuffer();
|
||||
|
||||
if (pkt == null)
|
||||
pkt = new DatagramPacket(buf.buffer, 0, buf.buffer.length);
|
||||
else
|
||||
pkt.setData(buf.buffer, 0, buf.buffer.length);
|
||||
|
||||
try
|
||||
{
|
||||
socket.receive(pkt);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.severe("Failed to receive from socket: " + ioe);
|
||||
break;
|
||||
}
|
||||
buf.len = pkt.getLength();
|
||||
|
||||
|
||||
remoteAddress = (InetSocketAddress) pkt.getSocketAddress();
|
||||
destinationSocket = sockets.get(remoteAddress);
|
||||
if (destinationSocket != null)
|
||||
{
|
||||
//make 'pkt' available for reading through destinationSocket
|
||||
destinationSocket.addBuffer(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Packet from an unknown source. Is it a STUN Binding Request?
|
||||
String ufrag = getUfrag(buf.buffer, 0, buf.len);
|
||||
if (ufrag == null)
|
||||
{
|
||||
// Not a STUN Binding Request or doesn't have a valid
|
||||
// USERNAME attribute. Drop it.
|
||||
continue;
|
||||
}
|
||||
|
||||
maybeAcceptNewSession(buf, remoteAddress, ufrag);
|
||||
// Maybe add to #sockets here in the base class?
|
||||
}
|
||||
}
|
||||
while (true);
|
||||
|
||||
// TODO we are all done, clean up.
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the reception of a STUN Binding Request with a valid USERNAME
|
||||
* attribute, from a "new" remote address (one which is not in
|
||||
* {@link #sockets}).
|
||||
* Implementations may choose to e.g. create a socket and pass it to their
|
||||
* ICE stack.
|
||||
*
|
||||
* Note that this is meant to only be executed by
|
||||
* {@link AbstractUdpListener}'s read thread, and should not be called from
|
||||
* implementing classes.
|
||||
*
|
||||
* @param buf the UDP payload of the first datagram received on the newly
|
||||
* accepted socket.
|
||||
* @param remoteAddress the remote address from which the datagram was
|
||||
* received.
|
||||
* @param ufrag the local ICE username fragment of the received STUN Binding
|
||||
* Request.
|
||||
*/
|
||||
protected abstract void maybeAcceptNewSession(
|
||||
Buffer buf,
|
||||
InetSocketAddress remoteAddress,
|
||||
String ufrag);
|
||||
|
||||
/**
|
||||
* Gets an unused <tt>Buffer</tt> instance, creating it if necessary.
|
||||
* @return an unused <tt>Buffer</tt> instance, creating it if necessary.
|
||||
*/
|
||||
private Buffer getFreeBuffer()
|
||||
{
|
||||
Buffer buf = pool.poll();
|
||||
if (buf == null)
|
||||
{
|
||||
buf = new Buffer(new byte[BUFFER_SIZE], 0);
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link MySocket} instance and associates it with the given
|
||||
* remote address. Returns the created instance.
|
||||
*
|
||||
* Note that this is meant to only execute in {@link AbstractUdpListener}'s
|
||||
* read thread.
|
||||
*
|
||||
* @param remoteAddress the remote address with which to associate the new
|
||||
* socket instance.
|
||||
* @return the created socket instance.
|
||||
*/
|
||||
protected MySocket addSocket(InetSocketAddress remoteAddress)
|
||||
throws SocketException
|
||||
{
|
||||
MySocket newSocket = new MySocket(remoteAddress);
|
||||
sockets.put(remoteAddress, newSocket);
|
||||
return newSocket;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements a <tt>DatagramSocket</tt> for the purposes of a specific
|
||||
* <tt>MyCandidate</tt>.
|
||||
*
|
||||
* It is not bound to a specific port, but shares the same local address
|
||||
* as the bound socket held by the harvester.
|
||||
*/
|
||||
protected class MySocket
|
||||
extends DatagramSocket
|
||||
{
|
||||
/**
|
||||
* The size of {@link #queue}.
|
||||
*/
|
||||
private static final int QUEUE_SIZE = 128;
|
||||
|
||||
/**
|
||||
* The FIFO which acts as a buffer for this socket.
|
||||
*/
|
||||
private final ArrayBlockingQueue<Buffer> queue
|
||||
= new ArrayBlockingQueue<>(QUEUE_SIZE);
|
||||
|
||||
/**
|
||||
* The {@link QueueStatistics} instance optionally used to collect and
|
||||
* print detailed statistics about {@link #queue}.
|
||||
*/
|
||||
private final QueueStatistics queueStatistics;
|
||||
|
||||
/**
|
||||
* The remote address that is associated with this socket.
|
||||
*/
|
||||
private SocketAddress remoteAddress;
|
||||
|
||||
/**
|
||||
* The flag which indicates that this <tt>DatagramSocket</tt> has been
|
||||
* closed.
|
||||
*/
|
||||
private boolean closed = false;
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>MySocket</tt> instance with the given
|
||||
* remote address.
|
||||
* @param remoteAddress the remote address to be associated with the
|
||||
* new instance.
|
||||
* @throws SocketException
|
||||
*/
|
||||
public MySocket(SocketAddress remoteAddress)
|
||||
throws SocketException
|
||||
{
|
||||
// unbound
|
||||
super((SocketAddress)null);
|
||||
|
||||
this.remoteAddress = remoteAddress;
|
||||
if (logger.isLoggable(Level.FINEST))
|
||||
{
|
||||
queueStatistics = new QueueStatistics(
|
||||
"SinglePort" + remoteAddress.toString().replace('/', '-'));
|
||||
}
|
||||
else
|
||||
{
|
||||
queueStatistics = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds pkt to this socket. If the queue is full, drops a packet. Does
|
||||
* not block.
|
||||
*/
|
||||
public void addBuffer(Buffer buf)
|
||||
{
|
||||
synchronized (queue)
|
||||
{
|
||||
// Drop the first rather than the current packet, so that
|
||||
// receivers can notice the loss earlier.
|
||||
if (queue.size() == QUEUE_SIZE)
|
||||
{
|
||||
logger.info("Dropping a packet because the queue is full.");
|
||||
if (queueStatistics != null)
|
||||
{
|
||||
queueStatistics.remove(System.currentTimeMillis());
|
||||
}
|
||||
queue.poll();
|
||||
}
|
||||
|
||||
queue.offer(buf);
|
||||
if (queueStatistics != null)
|
||||
{
|
||||
queueStatistics.add(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
queue.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the actual socket of the harvester.
|
||||
*/
|
||||
@Override
|
||||
public InetAddress getLocalAddress()
|
||||
{
|
||||
return localAddress.getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the actual socket of the harvester.
|
||||
*/
|
||||
@Override
|
||||
public int getLocalPort()
|
||||
{
|
||||
return localAddress.getPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the actual socket of the harvester.
|
||||
*/
|
||||
@Override
|
||||
public SocketAddress getLocalSocketAddress()
|
||||
{
|
||||
return localAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Removes the association of the remote address with this socket from
|
||||
* the harvester's map.
|
||||
*/
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
closed = true;
|
||||
|
||||
synchronized (queue)
|
||||
{
|
||||
// Wake up any threads still in receive()
|
||||
queue.notifyAll();
|
||||
}
|
||||
|
||||
// We could be called by the super-class constructor, in which
|
||||
// case this.removeAddress is not initialized yet.
|
||||
if (remoteAddress != null)
|
||||
AbstractUdpListener.this.sockets.remove(remoteAddress);
|
||||
|
||||
super.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the data from the first element of {@link #queue} into
|
||||
* <tt>p</tt>. Blocks until {@link #queue} has an element.
|
||||
* @param p
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void receive(DatagramPacket p)
|
||||
throws IOException
|
||||
{
|
||||
Buffer buf = null;
|
||||
|
||||
while (buf == null)
|
||||
{
|
||||
if (closed)
|
||||
throw new SocketException("Socket closed");
|
||||
|
||||
synchronized (queue)
|
||||
{
|
||||
if (queue.isEmpty())
|
||||
{
|
||||
try
|
||||
{
|
||||
queue.wait();
|
||||
}
|
||||
catch (InterruptedException ie)
|
||||
{}
|
||||
}
|
||||
|
||||
buf = queue.poll();
|
||||
if (queueStatistics != null)
|
||||
{
|
||||
queueStatistics.remove(System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byte[] pData = p.getData();
|
||||
|
||||
// XXX Should we use p.setData() here with a buffer of our own?
|
||||
if (pData == null || pData.length < buf.len)
|
||||
throw new IOException("packet buffer not available");
|
||||
|
||||
System.arraycopy(buf.buffer, 0, pData, 0, buf.len);
|
||||
p.setLength(buf.len);
|
||||
p.setSocketAddress(remoteAddress);
|
||||
|
||||
pool.offer(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the actual socket of the harvester.
|
||||
*/
|
||||
@Override
|
||||
public void send(DatagramPacket p)
|
||||
throws IOException
|
||||
{
|
||||
socket.send(p);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a buffer for the purposes of <tt>SinglePortUdpHarvester</tt>.
|
||||
* Wraps a byte[] and adds a length field specifying the number of elements
|
||||
* actually used.
|
||||
*/
|
||||
protected class Buffer
|
||||
{
|
||||
/**
|
||||
* The actual data.
|
||||
*/
|
||||
byte[] buffer;
|
||||
|
||||
/**
|
||||
* The number of elements of {@link #buffer} actually used.
|
||||
*/
|
||||
int len;
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>Buffer</tt> instance.
|
||||
* @param buffer the data.
|
||||
* @param len the length.
|
||||
*/
|
||||
private Buffer(byte[] buffer, int len)
|
||||
{
|
||||
this.buffer = buffer;
|
||||
this.len = len;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -28,14 +28,10 @@ import org.ice4j.ice.*;
|
||||
*
|
||||
* @author Emil Ivov
|
||||
* @author Lyubomir Marinov
|
||||
* @author Boris Grozev
|
||||
*/
|
||||
public abstract class CandidateHarvester
|
||||
public interface CandidateHarvester
|
||||
{
|
||||
/**
|
||||
* Manages statistics about harvesting time.
|
||||
*/
|
||||
private HarvestStatistics harvestStatistics = new HarvestStatistics();
|
||||
|
||||
/**
|
||||
* Gathers all candidate addresses of the type that this
|
||||
* <tt>CandidateHarvester</tt> supports. The gathered candidate addresses
|
||||
@ -52,7 +48,7 @@ public abstract class CandidateHarvester
|
||||
* as they are discovered, they should also be returned in order to make
|
||||
* sure that the gathering will be considered successful.
|
||||
*/
|
||||
public abstract Collection<LocalCandidate> harvest(Component component);
|
||||
Collection<LocalCandidate> harvest(Component component);
|
||||
|
||||
/**
|
||||
* Returns the statistics describing how well the various harvests of this
|
||||
@ -61,10 +57,7 @@ public abstract class CandidateHarvester
|
||||
* @return The {@link HarvestStatistics} describing this harvester's
|
||||
* harvests.
|
||||
*/
|
||||
public HarvestStatistics getHarvestStatistics()
|
||||
{
|
||||
return harvestStatistics;
|
||||
}
|
||||
HarvestStatistics getHarvestStatistics();
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this <tt>CandidateHarvester</tt> is to be
|
||||
@ -77,8 +70,5 @@ public abstract class CandidateHarvester
|
||||
* @return <tt>true</tt> if this <tt>CandidateHarvester</tt> is a harvester
|
||||
* for host candidates.
|
||||
*/
|
||||
public boolean isHostHarvester()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
boolean isHostHarvester();
|
||||
}
|
||||
|
||||
@ -41,7 +41,7 @@ import java.util.*;
|
||||
* @author Emil Ivov
|
||||
*/
|
||||
public class MappingCandidateHarvester
|
||||
extends CandidateHarvester
|
||||
extends AbstractCandidateHarvester
|
||||
{
|
||||
/**
|
||||
* The addresses that we will use as a mask
|
||||
|
||||
@ -43,17 +43,16 @@ import java.util.logging.Logger; // Disambiguation
|
||||
* and associates the component's local username fragment (ufrag) with this
|
||||
* candidate.
|
||||
*
|
||||
* When a datagram from an unknown source is received, it is parsed as a STUN
|
||||
* Binding Request, and if it has a USERNAME attribute, its ufrag is extracted.
|
||||
* If this ufrag is associated with a candidate of this harvester, a new socket
|
||||
* is created and added to the candidate, and the remote address of the datagram
|
||||
* is associated with this socket. This mapping is then used to de-multiplex
|
||||
* received datagrams based on their remote address.
|
||||
* When a STUN Binding Request with a given ufrag is received, if the ufrag
|
||||
* matches one of the registered candidates, then a new socket is created, which
|
||||
* is to receive further packets from the remote address, and the socket is
|
||||
* added to the candidate.
|
||||
*
|
||||
* @author Boris Grozev
|
||||
*/
|
||||
public class SinglePortUdpHarvester
|
||||
extends CandidateHarvester
|
||||
extends AbstractUdpListener
|
||||
implements CandidateHarvester
|
||||
{
|
||||
/**
|
||||
* Our class logger.
|
||||
@ -61,29 +60,6 @@ public class SinglePortUdpHarvester
|
||||
private static final Logger logger
|
||||
= Logger.getLogger(SinglePortUdpHarvester.class.getName());
|
||||
|
||||
/**
|
||||
* The size for newly allocated <tt>Buffer</tt> instances. This limits the
|
||||
* maximum size of datagrams we can receive.
|
||||
*
|
||||
* XXX should we increase this in case of other MTUs, or set it dynamically
|
||||
* according to the available network interfaces?
|
||||
*/
|
||||
private static final int BUFFER_SIZE
|
||||
= /* assumed MTU */ 1500 - /* IPv4 header */ 20 - /* UDP header */ 8;
|
||||
|
||||
/**
|
||||
* The number of <tt>Buffer</tt> instances to keep in {@link #pool}.
|
||||
*/
|
||||
private static final int POOL_SIZE = 256;
|
||||
|
||||
/**
|
||||
* The name of the property which controls the size of the receive buffer
|
||||
* which {@link SinglePortUdpHarvester} will request for the sockets that
|
||||
* it creates.
|
||||
*/
|
||||
private static final String SO_RCVBUF_PNAME
|
||||
= SinglePortUdpHarvester.class.getName() + ".SO_RCVBUF";
|
||||
|
||||
/**
|
||||
* Creates a new <tt>SinglePortUdpHarvester</tt> instance for each allowed
|
||||
* IP address found on each allowed network interface, with the given port.
|
||||
@ -91,58 +67,12 @@ public class SinglePortUdpHarvester
|
||||
* @param port the UDP port number to use.
|
||||
* @return the list of created <tt>SinglePortUdpHarvester</tt>s.
|
||||
*/
|
||||
public static List<SinglePortUdpHarvester>
|
||||
createHarvesters(int port)
|
||||
public static List<SinglePortUdpHarvester> createHarvesters(int port)
|
||||
{
|
||||
List<SinglePortUdpHarvester> harvesters = new LinkedList<>();
|
||||
|
||||
List<TransportAddress> addresses = new LinkedList<>();
|
||||
boolean isIPv6Disabled = StackProperties.getBoolean(
|
||||
StackProperties.DISABLE_IPv6,
|
||||
false);
|
||||
boolean isIPv6LinkLocalDisabled = StackProperties.getBoolean(
|
||||
StackProperties.DISABLE_LINK_LOCAL_ADDRESSES,
|
||||
false);
|
||||
|
||||
try
|
||||
{
|
||||
for (NetworkInterface iface
|
||||
: Collections.list(NetworkInterface.getNetworkInterfaces()))
|
||||
{
|
||||
if (NetworkUtils.isInterfaceLoopback(iface)
|
||||
|| !NetworkUtils.isInterfaceUp(iface)
|
||||
|| !HostCandidateHarvester.isInterfaceAllowed(iface))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Enumeration<InetAddress> ifaceAddresses
|
||||
= iface.getInetAddresses();
|
||||
while (ifaceAddresses.hasMoreElements())
|
||||
{
|
||||
InetAddress address = ifaceAddresses.nextElement();
|
||||
|
||||
if (!HostCandidateHarvester.isAddressAllowed(address))
|
||||
continue;
|
||||
|
||||
if (isIPv6Disabled && address instanceof Inet6Address)
|
||||
continue;
|
||||
if (isIPv6LinkLocalDisabled
|
||||
&& address instanceof Inet6Address
|
||||
&& address.isLinkLocalAddress())
|
||||
continue;
|
||||
|
||||
addresses.add(
|
||||
new TransportAddress(address, port, Transport.UDP));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SocketException se)
|
||||
{
|
||||
logger.info("Failed to get network interfaces: " + se);
|
||||
}
|
||||
|
||||
for (TransportAddress address : addresses)
|
||||
for (TransportAddress address
|
||||
: AbstractUdpListener.getAllowedAddresses(port))
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -159,15 +89,6 @@ public class SinglePortUdpHarvester
|
||||
return harvesters;
|
||||
}
|
||||
|
||||
/**
|
||||
* The map which keeps the known remote addresses and their associated
|
||||
* candidateSockets.
|
||||
* {@link #thread} is the only thread which adds new entries, while
|
||||
* other threads remove entries when candidates are freed.
|
||||
*/
|
||||
private final Map<SocketAddress, MySocket> sockets
|
||||
= new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* The map which keeps all currently active <tt>Candidate</tt>s created by
|
||||
* this harvester. The keys are the local username fragments (ufrags) of
|
||||
@ -177,26 +98,9 @@ public class SinglePortUdpHarvester
|
||||
= new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* A pool of <tt>Buffer</tt> instances used to avoid creating of new java
|
||||
* objects.
|
||||
* Manages statistics about harvesting time.
|
||||
*/
|
||||
private final ArrayBlockingQueue<Buffer> pool
|
||||
= new ArrayBlockingQueue<>(POOL_SIZE);
|
||||
|
||||
/**
|
||||
* The local address that this harvester is bound to.
|
||||
*/
|
||||
private final TransportAddress localAddress;
|
||||
|
||||
/**
|
||||
* The "main" socket that this harvester reads from.
|
||||
*/
|
||||
private final DatagramSocket socket;
|
||||
|
||||
/**
|
||||
* The thread reading from {@link #socket}.
|
||||
*/
|
||||
private final Thread thread;
|
||||
private HarvestStatistics harvestStatistics = new HarvestStatistics();
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to
|
||||
@ -207,228 +111,61 @@ public class SinglePortUdpHarvester
|
||||
public SinglePortUdpHarvester(TransportAddress localAddress)
|
||||
throws IOException
|
||||
{
|
||||
this.localAddress = localAddress;
|
||||
socket = new DatagramSocket(localAddress);
|
||||
|
||||
|
||||
int receiveBufferSize = StackProperties.getInt(SO_RCVBUF_PNAME, -1);
|
||||
if (receiveBufferSize > 0)
|
||||
{
|
||||
socket.setReceiveBufferSize(receiveBufferSize);
|
||||
}
|
||||
|
||||
String logMessage
|
||||
= "Initialized SinglePortUdpHarvester with address " + localAddress;
|
||||
logMessage += ". Receive buffer size " + socket.getReceiveBufferSize();
|
||||
if (receiveBufferSize > 0)
|
||||
{
|
||||
logMessage += " (asked for " + receiveBufferSize + ")";
|
||||
}
|
||||
logger.info(logMessage);
|
||||
|
||||
thread = new Thread()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
SinglePortUdpHarvester.this.runInHarvesterThread();
|
||||
}
|
||||
};
|
||||
|
||||
thread.setName(SinglePortUdpHarvester.class.getName() + " thread");
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
super(localAddress);
|
||||
logger.info("Initialized SinglePortUdpHarvester with address "
|
||||
+ localAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perpetually reads datagrams from {@link #socket} and handles them
|
||||
* accordingly.
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public HarvestStatistics getHarvestStatistics()
|
||||
{
|
||||
return harvestStatistics;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* It is important that this blocks are little as possible (except on
|
||||
* socket.receive(), of course), because it could potentially delay the
|
||||
* reception of both ICE and media packets for the whole application.
|
||||
* Looks for an ICE candidate registered with this harvester, which has a
|
||||
* local ufrag of {@code ufrag}, and if one is found it accepts the new
|
||||
* socket and adds it to the candidate.
|
||||
*/
|
||||
private void runInHarvesterThread()
|
||||
protected void maybeAcceptNewSession(Buffer buf,
|
||||
InetSocketAddress remoteAddress,
|
||||
String ufrag)
|
||||
{
|
||||
Buffer buf;
|
||||
DatagramPacket pkt = null;
|
||||
Component component;
|
||||
MyCandidate candidate;
|
||||
MySocket destinationSocket;
|
||||
InetSocketAddress remoteAddress;
|
||||
|
||||
do
|
||||
MyCandidate candidate = candidates.get(ufrag);
|
||||
if (candidate == null)
|
||||
{
|
||||
// TODO: implement stopping the thread with a switch?
|
||||
|
||||
buf = getFreeBuffer();
|
||||
|
||||
if (pkt == null)
|
||||
pkt = new DatagramPacket(buf.buffer, 0, buf.buffer.length);
|
||||
else
|
||||
pkt.setData(buf.buffer, 0, buf.buffer.length);
|
||||
|
||||
try
|
||||
{
|
||||
socket.receive(pkt);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.severe("Failed to receive from socket: " + ioe);
|
||||
break;
|
||||
}
|
||||
buf.len = pkt.getLength();
|
||||
|
||||
|
||||
remoteAddress = (InetSocketAddress) pkt.getSocketAddress();
|
||||
destinationSocket = sockets.get(remoteAddress);
|
||||
if (destinationSocket != null)
|
||||
{
|
||||
//make 'pkt' available for reading through destinationSocket
|
||||
destinationSocket.addBuffer(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Packet from an unknown source. Is it a STUN Binding Request?
|
||||
String ufrag = getUfrag(buf.buffer, 0, buf.len);
|
||||
if (ufrag == null)
|
||||
{
|
||||
// Not a STUN Binding Request or doesn't have a valid
|
||||
// USERNAME attribute. Drop it.
|
||||
continue;
|
||||
}
|
||||
|
||||
candidate = candidates.get(ufrag);
|
||||
component
|
||||
= candidate == null ? null : candidate.getParentComponent();
|
||||
if (component != null)
|
||||
{
|
||||
// This is a STUN Binding Request destined for this
|
||||
// specific Candidate/Component/Agent.
|
||||
|
||||
try
|
||||
{
|
||||
// 1. Create a socket for this remote address
|
||||
MySocket newSocket = new MySocket(remoteAddress);
|
||||
|
||||
// 2. Set-up de-multiplexing for future datagrams
|
||||
// with this address to this socket.
|
||||
sockets.put(remoteAddress, newSocket);
|
||||
|
||||
// 3. Let the candidate and its STUN stack no about the
|
||||
// new socket.
|
||||
candidate.addSocket(newSocket, remoteAddress);
|
||||
|
||||
// 4. Add the original datagram to the new socket.
|
||||
newSocket.addBuffer(buf);
|
||||
}
|
||||
catch (SocketException se)
|
||||
{
|
||||
logger.info("Could not create a socket: " + se);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.info("Failed to handle new socket: " + ioe);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// A STUN Binding Request with an unknown USERNAME. Drop it.
|
||||
}
|
||||
}
|
||||
}
|
||||
while (true);
|
||||
|
||||
// TODO we are all done, clean up.
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an unused <tt>Buffer</tt> instance, creating it if necessary.
|
||||
* @return an unused <tt>Buffer</tt> instance, creating it if necessary.
|
||||
*/
|
||||
private Buffer getFreeBuffer()
|
||||
{
|
||||
Buffer buf = pool.poll();
|
||||
if (buf == null)
|
||||
{
|
||||
buf = new Buffer(new byte[BUFFER_SIZE], 0);
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to parse the bytes in <tt>buf</tt> at offset <tt>off</tt> (and
|
||||
* length <tt>len</tt>) as a STUN Binding Request message. If successful,
|
||||
* looks for a USERNAME attribute and returns the local username fragment
|
||||
* part (see RFC5245 Section 7.1.2.3).
|
||||
* In case of any failure returns <tt>null</tt>.
|
||||
*
|
||||
* @param buf the bytes.
|
||||
* @param off the offset.
|
||||
* @param len the length.
|
||||
* @return the local ufrag from the USERNAME attribute of the STUN message
|
||||
* contained in <tt>buf</tt>, or <tt>null</tt>.
|
||||
*/
|
||||
private String getUfrag(byte[] buf, int off, int len)
|
||||
{
|
||||
// RFC5389, Section 6:
|
||||
// All STUN messages MUST start with a 20-byte header followed by zero
|
||||
// or more Attributes.
|
||||
if (buf == null || buf.length < off + len || len < 20)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// RFC5389, Section 6:
|
||||
// The magic cookie field MUST contain the fixed value 0x2112A442 in
|
||||
// network byte order.
|
||||
if ( !( (buf[off + 4] & 0xFF) == 0x21 &&
|
||||
(buf[off + 5] & 0xFF) == 0x12 &&
|
||||
(buf[off + 6] & 0xFF) == 0xA4 &&
|
||||
(buf[off + 7] & 0xFF) == 0x42))
|
||||
{
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.fine("Not a STUN packet, magic cookie not found.");
|
||||
}
|
||||
return null;
|
||||
// A STUN Binding Request with an unknown USERNAME. Drop it.
|
||||
return;
|
||||
}
|
||||
|
||||
// This is a STUN Binding Request destined for this
|
||||
// specific Candidate/Component/Agent.
|
||||
try
|
||||
{
|
||||
Message stunMessage
|
||||
= Message.decode(buf,
|
||||
(char) off,
|
||||
(char) len);
|
||||
// 1. Create a socket for this remote address
|
||||
// 2. Set-up de-multiplexing for future datagrams
|
||||
// with this address to this socket.
|
||||
MySocket newSocket = addSocket(remoteAddress);
|
||||
|
||||
if (stunMessage.getMessageType()
|
||||
!= Message.BINDING_REQUEST)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// 3. Let the candidate and its STUN stack no about the
|
||||
// new socket.
|
||||
candidate.addSocket(newSocket, remoteAddress);
|
||||
|
||||
UsernameAttribute usernameAttribute
|
||||
= (UsernameAttribute)
|
||||
stunMessage.getAttribute(Attribute.USERNAME);
|
||||
if (usernameAttribute == null)
|
||||
return null;
|
||||
|
||||
String usernameString
|
||||
= new String(usernameAttribute.getUsername());
|
||||
return usernameString.split(":")[0];
|
||||
// 4. Add the original datagram to the new socket.
|
||||
newSocket.addBuffer(buf);
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (SocketException se)
|
||||
{
|
||||
// Catch everything. We are going to log, and then drop the packet
|
||||
// anyway.
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
{
|
||||
logger.fine("Failed to extract local ufrag: " + e);
|
||||
}
|
||||
logger.info("Could not create a socket: " + se);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.info("Failed to handle new socket: " + ioe);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -471,249 +208,6 @@ public class SinglePortUdpHarvester
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements a <tt>DatagramSocket</tt> for the purposes of a specific
|
||||
* <tt>MyCandidate</tt>.
|
||||
*
|
||||
* It is not bound to a specific port, but shares the same local address
|
||||
* as the bound socket held by the harvester.
|
||||
*/
|
||||
private class MySocket
|
||||
extends DatagramSocket
|
||||
{
|
||||
/**
|
||||
* The size of {@link #queue}.
|
||||
*/
|
||||
private static final int QUEUE_SIZE = 128;
|
||||
|
||||
/**
|
||||
* The FIFO which acts as a buffer for this socket.
|
||||
*/
|
||||
private final ArrayBlockingQueue<Buffer> queue
|
||||
= new ArrayBlockingQueue<>(QUEUE_SIZE);
|
||||
|
||||
/**
|
||||
* The {@link QueueStatistics} instance optionally used to collect and
|
||||
* print detailed statistics about {@link #queue}.
|
||||
*/
|
||||
private final QueueStatistics queueStatistics;
|
||||
|
||||
/**
|
||||
* The remote address that is associated with this socket.
|
||||
*/
|
||||
private SocketAddress remoteAddress;
|
||||
|
||||
/**
|
||||
* The flag which indicates that this <tt>DatagramSocket</tt> has been
|
||||
* closed.
|
||||
*/
|
||||
private boolean closed = false;
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>MySocket</tt> instance with the given
|
||||
* remote address.
|
||||
* @param remoteAddress the remote address to be associated with the
|
||||
* new instance.
|
||||
* @throws SocketException
|
||||
*/
|
||||
public MySocket(SocketAddress remoteAddress)
|
||||
throws SocketException
|
||||
{
|
||||
// unbound
|
||||
super((SocketAddress)null);
|
||||
|
||||
this.remoteAddress = remoteAddress;
|
||||
if (logger.isLoggable(Level.FINEST))
|
||||
{
|
||||
queueStatistics = new QueueStatistics(
|
||||
"SinglePort" + remoteAddress.toString().replace('/', '-'));
|
||||
}
|
||||
else
|
||||
{
|
||||
queueStatistics = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds pkt to this socket. If the queue is full, drops a packet. Does
|
||||
* not block.
|
||||
*/
|
||||
private void addBuffer(Buffer buf)
|
||||
{
|
||||
synchronized (queue)
|
||||
{
|
||||
// Drop the first rather than the current packet, so that
|
||||
// receivers can notice the loss earlier.
|
||||
if (queue.size() == QUEUE_SIZE)
|
||||
{
|
||||
logger.info("Dropping a packet because the queue is full.");
|
||||
if (queueStatistics != null)
|
||||
{
|
||||
queueStatistics.remove(System.currentTimeMillis());
|
||||
}
|
||||
queue.poll();
|
||||
}
|
||||
|
||||
queue.offer(buf);
|
||||
if (queueStatistics != null)
|
||||
{
|
||||
queueStatistics.add(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
queue.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the actual socket of the harvester.
|
||||
*/
|
||||
@Override
|
||||
public InetAddress getLocalAddress()
|
||||
{
|
||||
return localAddress.getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the actual socket of the harvester.
|
||||
*/
|
||||
@Override
|
||||
public int getLocalPort()
|
||||
{
|
||||
return localAddress.getPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the actual socket of the harvester.
|
||||
*/
|
||||
@Override
|
||||
public SocketAddress getLocalSocketAddress()
|
||||
{
|
||||
return localAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Removes the association of the remote address with this socket from
|
||||
* the harvester's map.
|
||||
*/
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
closed = true;
|
||||
|
||||
synchronized (queue)
|
||||
{
|
||||
// Wake up any threads still in receive()
|
||||
queue.notifyAll();
|
||||
}
|
||||
|
||||
// We could be called by the super-class constructor, in which
|
||||
// case this.removeAddress is not initialized yet.
|
||||
if (remoteAddress != null)
|
||||
SinglePortUdpHarvester.this.sockets.remove(remoteAddress);
|
||||
|
||||
super.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the data from the first element of {@link #queue} into
|
||||
* <tt>p</tt>. Blocks until {@link #queue} has an element.
|
||||
* @param p
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void receive(DatagramPacket p)
|
||||
throws IOException
|
||||
{
|
||||
Buffer buf = null;
|
||||
|
||||
while (buf == null)
|
||||
{
|
||||
if (closed)
|
||||
throw new SocketException("Socket closed");
|
||||
|
||||
synchronized (queue)
|
||||
{
|
||||
if (queue.isEmpty())
|
||||
{
|
||||
try
|
||||
{
|
||||
queue.wait();
|
||||
}
|
||||
catch (InterruptedException ie)
|
||||
{}
|
||||
}
|
||||
|
||||
buf = queue.poll();
|
||||
if (queueStatistics != null)
|
||||
{
|
||||
queueStatistics.remove(System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byte[] pData = p.getData();
|
||||
|
||||
// XXX Should we use p.setData() here with a buffer of our own?
|
||||
if (pData == null || pData.length < buf.len)
|
||||
throw new IOException("packet buffer not available");
|
||||
|
||||
System.arraycopy(buf.buffer, 0, pData, 0, buf.len);
|
||||
p.setLength(buf.len);
|
||||
p.setSocketAddress(remoteAddress);
|
||||
|
||||
pool.offer(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the actual socket of the harvester.
|
||||
*/
|
||||
@Override
|
||||
public void send(DatagramPacket p)
|
||||
throws IOException
|
||||
{
|
||||
socket.send(p);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a buffer for the purposes of <tt>SinglePortUdpHarvester</tt>.
|
||||
* Wraps a byte[] and adds a length field specifying the number of elements
|
||||
* actually used.
|
||||
*/
|
||||
private class Buffer
|
||||
{
|
||||
/**
|
||||
* The actual data.
|
||||
*/
|
||||
byte[] buffer;
|
||||
|
||||
/**
|
||||
* The number of elements of {@link #buffer} actually used.
|
||||
*/
|
||||
int len;
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>Buffer</tt> instance.
|
||||
* @param buffer the data.
|
||||
* @param len the length.
|
||||
*/
|
||||
private Buffer(byte[] buffer, int len)
|
||||
{
|
||||
this.buffer = buffer;
|
||||
this.len = len;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements a <tt>Candidate</tt> for the purposes of this
|
||||
* <tt>SinglePortUdpHarvester</tt>.
|
||||
|
||||
@ -36,7 +36,7 @@ import org.ice4j.stack.*;
|
||||
* @author Lyubomir Marinov
|
||||
*/
|
||||
public class StunCandidateHarvester
|
||||
extends CandidateHarvester
|
||||
extends AbstractCandidateHarvester
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -34,7 +34,7 @@ import org.bitlet.weupnp.*;
|
||||
* @author Sebastien Vincent
|
||||
*/
|
||||
public class UPNPHarvester
|
||||
extends CandidateHarvester
|
||||
extends AbstractCandidateHarvester
|
||||
{
|
||||
/**
|
||||
* The logger.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user