Implements relaying data through a TURN server for RelayedCandidate.
This commit is contained in:
parent
cad31b0304
commit
8928f9daa6
@ -67,8 +67,8 @@ public class Component
|
|||||||
/**
|
/**
|
||||||
* The list locally gathered candidates for this media stream.
|
* The list locally gathered candidates for this media stream.
|
||||||
*/
|
*/
|
||||||
private List<LocalCandidate> localCandidates
|
private final List<LocalCandidate> localCandidates
|
||||||
= new LinkedList<LocalCandidate>();
|
= new LinkedList<LocalCandidate>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of candidates that the peer agent sent for this stream.
|
* The list of candidates that the peer agent sent for this stream.
|
||||||
@ -79,8 +79,8 @@ public class Component
|
|||||||
* A <tt>Comparator</tt> that we use for sorting <tt>Candidate</tt>s by
|
* A <tt>Comparator</tt> that we use for sorting <tt>Candidate</tt>s by
|
||||||
* their priority.
|
* their priority.
|
||||||
*/
|
*/
|
||||||
private CandidatePrioritizer candidatePrioritizer
|
private final CandidatePrioritizer candidatePrioritizer
|
||||||
= new CandidatePrioritizer();
|
= new CandidatePrioritizer();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default <tt>Candidate</tt> for this component or in other words, the
|
* The default <tt>Candidate</tt> for this component or in other words, the
|
||||||
@ -368,9 +368,7 @@ public class Component
|
|||||||
|
|
||||||
//first compute the actual priorities
|
//first compute the actual priorities
|
||||||
for (Candidate cand : candidates)
|
for (Candidate cand : candidates)
|
||||||
{
|
|
||||||
cand.computePriority();
|
cand.computePriority();
|
||||||
}
|
|
||||||
|
|
||||||
//sort
|
//sort
|
||||||
Arrays.sort(candidates, candidatePrioritizer);
|
Arrays.sort(candidates, candidatePrioritizer);
|
||||||
@ -452,31 +450,38 @@ public class Component
|
|||||||
*/
|
*/
|
||||||
protected void eliminateRedundantCandidates()
|
protected void eliminateRedundantCandidates()
|
||||||
{
|
{
|
||||||
List<LocalCandidate> candidatesCopy = getLocalCandidates();
|
/*
|
||||||
|
* Find and remove all candidates that have the same address and base as
|
||||||
for (Candidate cand : candidatesCopy)
|
* cand and a lower priority. The algorithm implemented bellow does rely
|
||||||
|
* on localCandidates being ordered in decreasing order (as said in its
|
||||||
|
* javadoc that the eliminateRedundantCandidates method is called only
|
||||||
|
* after prioritizeCandidates.
|
||||||
|
*/
|
||||||
|
synchronized (localCandidates)
|
||||||
{
|
{
|
||||||
//find and remove all candidates that have the same address and
|
for (int i = 0; i < localCandidates.size(); i++)
|
||||||
//base as cand and a lower priority.
|
|
||||||
synchronized(localCandidates)
|
|
||||||
{
|
{
|
||||||
Iterator<LocalCandidate> localCandsIter
|
LocalCandidate cand = localCandidates.get(i);
|
||||||
= localCandidates.iterator();
|
|
||||||
|
|
||||||
while (localCandsIter.hasNext())
|
for (int j = i + 1; j < localCandidates.size();)
|
||||||
{
|
{
|
||||||
Candidate cand2 = localCandsIter.next();
|
LocalCandidate cand2 = localCandidates.get(j);
|
||||||
|
|
||||||
if( cand != cand2
|
if ((cand != cand2)
|
||||||
&& cand.getTransportAddress()
|
&& cand.getTransportAddress().equals(
|
||||||
.equals(cand2.getTransportAddress())
|
cand2.getTransportAddress())
|
||||||
&& cand.getBase().equals(cand2.getBase())
|
&& cand.getBase().equals(cand2.getBase())
|
||||||
&& cand.getPriority() >= cand2.getPriority() )
|
&& (cand.getPriority() >= cand2.getPriority()))
|
||||||
{
|
{
|
||||||
localCandsIter.remove();
|
localCandidates.remove(j);
|
||||||
if(logger.isLoggable(Level.FINEST))
|
if(logger.isLoggable(Level.FINEST))
|
||||||
logger.finest("eliminating redundant cand: "+ cand2);
|
{
|
||||||
|
logger.finest(
|
||||||
|
"eliminating redundant cand: "+ cand2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -572,12 +577,76 @@ public class Component
|
|||||||
{
|
{
|
||||||
synchronized (localCandidates)
|
synchronized (localCandidates)
|
||||||
{
|
{
|
||||||
Iterator<LocalCandidate> cndIter = localCandidates.iterator();
|
/*
|
||||||
while( cndIter.hasNext())
|
* Since the sockets of the non-HostCandidate LocalCandidates may
|
||||||
|
* depend on the socket of the HostCandidate for which they have
|
||||||
|
* been harvested, order the freeing.
|
||||||
|
*/
|
||||||
|
CandidateType[] candidateTypes
|
||||||
|
= new CandidateType[]
|
||||||
|
{
|
||||||
|
CandidateType.RELAYED_CANDIDATE,
|
||||||
|
CandidateType.PEER_REFLEXIVE_CANDIDATE,
|
||||||
|
CandidateType.SERVER_REFLEXIVE_CANDIDATE
|
||||||
|
};
|
||||||
|
|
||||||
|
for (CandidateType candidateType : candidateTypes)
|
||||||
{
|
{
|
||||||
LocalCandidate candidate = cndIter.next();
|
Iterator<LocalCandidate> localCandidateIter
|
||||||
candidate.free();
|
= localCandidates.iterator();
|
||||||
cndIter.remove();
|
|
||||||
|
while (localCandidateIter.hasNext())
|
||||||
|
{
|
||||||
|
LocalCandidate localCandidate = localCandidateIter.next();
|
||||||
|
|
||||||
|
if (candidateType.equals(localCandidate.getType()))
|
||||||
|
{
|
||||||
|
free(localCandidate);
|
||||||
|
localCandidateIter.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free whatever's left.
|
||||||
|
Iterator<LocalCandidate> localCandidateIter
|
||||||
|
= localCandidates.iterator();
|
||||||
|
|
||||||
|
while (localCandidateIter.hasNext())
|
||||||
|
{
|
||||||
|
LocalCandidate localCandidate = localCandidateIter.next();
|
||||||
|
|
||||||
|
free(localCandidate);
|
||||||
|
localCandidateIter.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees a specific <tt>LocalCandidate</tt> and swallows any
|
||||||
|
* <tt>Throwable</tt> it throws while freeing itself in order to prevent its
|
||||||
|
* failure to affect the rest of the execution.
|
||||||
|
*
|
||||||
|
* @param localCandidate the <tt>LocalCandidate</tt> to be freed
|
||||||
|
*/
|
||||||
|
private void free(LocalCandidate localCandidate)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
localCandidate.free();
|
||||||
|
}
|
||||||
|
catch (Throwable t)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Don't let the failing of a single LocalCandidate to free itself
|
||||||
|
* to fail the freeing of the other LocalCandidates.
|
||||||
|
*/
|
||||||
|
if (t instanceof ThreadDeath)
|
||||||
|
throw (ThreadDeath) t;
|
||||||
|
if (logger.isLoggable(Level.INFO))
|
||||||
|
{
|
||||||
|
logger.log(
|
||||||
|
Level.INFO,
|
||||||
|
"Failed to free LocalCandidate: " + localCandidate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,31 +48,6 @@ public abstract class LocalCandidate
|
|||||||
super(transportAddress, parentComponent, type);
|
super(transportAddress, parentComponent, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the actual/host <tt>DatagramSocket</tt> which implements the
|
|
||||||
* <tt>DatagramSocket</tt>s exposed by this <tt>LocalCandidate</tt>. The
|
|
||||||
* default implementation is supposed to be good enough for the general case
|
|
||||||
* and does not try to be universal - it returns the <tt>hostSocket</tt> of
|
|
||||||
* the <tt>base</tt> of this <tt>LocalCandidate</tt> if the <tt>base</tt> is
|
|
||||||
* different than <tt>this</tt> or the <tt>socket</tt> of this
|
|
||||||
* <tt>LocalCandidate</tt> if it equals its <tt>base</tt>. The row reasoning
|
|
||||||
* for the implementation is that if any <tt>Candidate</tt> knows about the
|
|
||||||
* actual/host <tt>DatagramSocket</tt>, this <tt>LocalCandidate</tt> would
|
|
||||||
* be based on it rather be related to it in any other way.
|
|
||||||
*
|
|
||||||
* @return the actual/host <tt>DatagramSocket</tt> which implements the
|
|
||||||
* <tt>DatagramSocket</tt>s exposed by this <tt>LocalCandidate</tt>
|
|
||||||
*/
|
|
||||||
protected DatagramSocket getHostSocket()
|
|
||||||
{
|
|
||||||
LocalCandidate base = getBase();
|
|
||||||
|
|
||||||
return
|
|
||||||
((base == this) || (base == null))
|
|
||||||
? getSocket()
|
|
||||||
: base.getHostSocket();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the <tt>DatagramSocket</tt> associated with this <tt>Candidate</tt>.
|
* Gets the <tt>DatagramSocket</tt> associated with this <tt>Candidate</tt>.
|
||||||
*
|
*
|
||||||
@ -96,14 +71,14 @@ public abstract class LocalCandidate
|
|||||||
*/
|
*/
|
||||||
public DatagramSocket getStunSocket(TransportAddress serverAddress)
|
public DatagramSocket getStunSocket(TransportAddress serverAddress)
|
||||||
{
|
{
|
||||||
DatagramSocket hostSocket = getHostSocket();
|
DatagramSocket hostSocket = getSocket();
|
||||||
DatagramSocket stunSocket = null;
|
DatagramSocket stunSocket = null;
|
||||||
Throwable exception = null;
|
|
||||||
|
|
||||||
if (hostSocket instanceof MultiplexingDatagramSocket)
|
if (hostSocket instanceof MultiplexingDatagramSocket)
|
||||||
{
|
{
|
||||||
DatagramPacketFilter stunDatagramPacketFilter
|
DatagramPacketFilter stunDatagramPacketFilter
|
||||||
= createStunDatagramPacketFilter(serverAddress);
|
= createStunDatagramPacketFilter(serverAddress);
|
||||||
|
Throwable exception = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -119,11 +94,24 @@ public abstract class LocalCandidate
|
|||||||
sex);
|
sex);
|
||||||
exception = sex;
|
exception = sex;
|
||||||
}
|
}
|
||||||
|
if (stunSocket == null)
|
||||||
|
{
|
||||||
|
throw
|
||||||
|
new IllegalStateException(
|
||||||
|
"Failed to acquire DatagramSocket"
|
||||||
|
+ " specific to STUN communication",
|
||||||
|
exception);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (stunSocket == null)
|
|
||||||
throw new IllegalArgumentException("hostCand", exception);
|
|
||||||
else
|
else
|
||||||
return stunSocket;
|
{
|
||||||
|
throw
|
||||||
|
new IllegalStateException(
|
||||||
|
"The socket of "
|
||||||
|
+ getClass().getSimpleName()
|
||||||
|
+ " must be a MultiplexingDatagramSocket instance");
|
||||||
|
}
|
||||||
|
return stunSocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -145,22 +133,33 @@ public abstract class LocalCandidate
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Frees resources allocated by this candidate such as its
|
* Frees resources allocated by this candidate such as its
|
||||||
* <tt>DatagramSocket</tt> for example.
|
* <tt>DatagramSocket</tt>, for example. The <tt>socket</tt> of this
|
||||||
|
* <tt>LocalCandidate</tt> is closed only if it is not the <tt>socket</tt>
|
||||||
|
* of the <tt>base</tt> of this <tt>LocalCandidate</tt>.
|
||||||
*/
|
*/
|
||||||
protected void free()
|
protected void free()
|
||||||
{
|
{
|
||||||
//remove our socket from the stack.
|
// Close the socket associated with this LocalCandidate.
|
||||||
StunStack.getInstance().removeSocket(getTransportAddress());
|
|
||||||
|
|
||||||
// Close the socket associated with this Candidate.
|
|
||||||
DatagramSocket socket = getSocket();
|
DatagramSocket socket = getSocket();
|
||||||
|
|
||||||
/*
|
|
||||||
* Allow this LocalCandiate implementation to not create a socket if it
|
|
||||||
* still hasn't created one.
|
|
||||||
*/
|
|
||||||
if (socket != null)
|
if (socket != null)
|
||||||
socket.close();
|
{
|
||||||
|
LocalCandidate base = getBase();
|
||||||
|
|
||||||
|
if ((base == null)
|
||||||
|
|| (base == this)
|
||||||
|
|| (base.getSocket() != socket))
|
||||||
|
{
|
||||||
|
//remove our socket from the stack.
|
||||||
|
StunStack.getInstance().removeSocket(getTransportAddress());
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Allow this LocalCandiate implementation to not create a
|
||||||
|
* socket if it still hasn't created one.
|
||||||
|
*/
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -25,12 +25,18 @@ import org.ice4j.socket.*;
|
|||||||
public class RelayedCandidate
|
public class RelayedCandidate
|
||||||
extends LocalCandidate
|
extends LocalCandidate
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <tt>RelayedCandidateDatagramSocket</tt> which represents the
|
* The <tt>RelayedCandidateDatagramSocket</tt> of this
|
||||||
* application-purposed <tt>DatagramSocket</tt> associated with this
|
* <tt>RelayedCandidate</tt>.
|
||||||
|
*/
|
||||||
|
private RelayedCandidateDatagramSocket relayedCandidateDatagramSocket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The application-purposed <tt>DatagramSocket</tt> associated with this
|
||||||
* <tt>Candidate</tt>.
|
* <tt>Candidate</tt>.
|
||||||
*/
|
*/
|
||||||
private RelayedCandidateDatagramSocket socket;
|
private DatagramSocket socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <tt>TurnCandidateHarvest</tt> which has harvested this
|
* The <tt>TurnCandidateHarvest</tt> which has harvested this
|
||||||
@ -71,29 +77,40 @@ public class RelayedCandidate
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the actual/host <tt>DatagramSocket</tt> which implements the
|
* Gets the <tt>RelayedCandidateDatagramSocket</tt> of this
|
||||||
* <tt>DatagramSocket</tt>s exposed by this <tt>LocalCandidate</tt>. The
|
* <tt>RelayedCandidate</tt>.
|
||||||
* default implementation is supposed to be good enough for the general case
|
* <p>
|
||||||
* and does not try to be universal - it returns the <tt>hostSocket</tt> of
|
* <b>Note</b>: The method is part of the internal API of
|
||||||
* the <tt>base</tt> of this <tt>LocalCandidate</tt> if the <tt>base</tt> is
|
* <tt>RelayedCandidate</tt> and <tt>TurnCandidateHarvest</tt> and is not
|
||||||
* different than <tt>this</tt> or the <tt>socket</tt> of this
|
* intended for public use.
|
||||||
* <tt>LocalCandidate</tt> if it equals its <tt>base</tt>. The row reasoning
|
* </p>
|
||||||
* for the implementation is that if any <tt>Candidate</tt> knows about the
|
|
||||||
* actual/host <tt>DatagramSocket</tt>, this <tt>LocalCandidate</tt> would
|
|
||||||
* be based on it rather be related to it in any other way.
|
|
||||||
*
|
*
|
||||||
* @return the actual/host <tt>DatagramSocket</tt> which implements the
|
* @return the <tt>RelayedCandidateDatagramSocket</tt> of this
|
||||||
* <tt>DatagramSocket</tt>s exposed by this <tt>LocalCandidate</tt>
|
* <tt>RelayedCandidate</tt>
|
||||||
* @see LocalCandidate#getHostSocket()
|
|
||||||
*/
|
*/
|
||||||
@Override
|
public synchronized RelayedCandidateDatagramSocket
|
||||||
protected DatagramSocket getHostSocket()
|
getRelayedCandidateDatagramSocket()
|
||||||
{
|
{
|
||||||
return turnCandidateHarvest.hostCandidate.getSocket();
|
if (relayedCandidateDatagramSocket == null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
relayedCandidateDatagramSocket
|
||||||
|
= new RelayedCandidateDatagramSocket(
|
||||||
|
this,
|
||||||
|
turnCandidateHarvest);
|
||||||
|
}
|
||||||
|
catch (SocketException sex)
|
||||||
|
{
|
||||||
|
throw new UndeclaredThrowableException(sex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return relayedCandidateDatagramSocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the <tt>DatagramSocket</tt> associated with this <tt>Candidate</tt>.
|
* Gets the application-purposed <tt>DatagramSocket</tt> associated with
|
||||||
|
* this <tt>Candidate</tt>.
|
||||||
*
|
*
|
||||||
* @return the <tt>DatagramSocket</tt> associated with this
|
* @return the <tt>DatagramSocket</tt> associated with this
|
||||||
* <tt>Candidate</tt>
|
* <tt>Candidate</tt>
|
||||||
@ -105,7 +122,9 @@ public class RelayedCandidate
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
socket = new RelayedCandidateDatagramSocket();
|
socket
|
||||||
|
= new MultiplexingDatagramSocket(
|
||||||
|
getRelayedCandidateDatagramSocket());
|
||||||
}
|
}
|
||||||
catch (SocketException sex)
|
catch (SocketException sex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -37,16 +37,28 @@ public class StunCandidateHarvest
|
|||||||
= Logger.getLogger(StunCandidateHarvest.class.getName());
|
= Logger.getLogger(StunCandidateHarvest.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The constant which defines an empty array with <tt>Candidate</tt> element
|
* The constant which defines an empty array with <tt>LocalCandidate</tt>
|
||||||
* type. Explicitly defined in order to reduce unnecessary allocations.
|
* element type. Explicitly defined in order to reduce unnecessary
|
||||||
|
* allocations.
|
||||||
*/
|
*/
|
||||||
private static final Candidate[] NO_CANDIDATES = new Candidate[0];
|
private static final LocalCandidate[] NO_CANDIDATES = new LocalCandidate[0];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The value of the <tt>sendKeepAliveMessage</tt> property of
|
||||||
|
* <tt>StunCandidateHarvest</tt> which specifies that no sending of STUN
|
||||||
|
* keep-alive messages is to performed for the purposes of keeping the
|
||||||
|
* <tt>Candidate</tt>s harvested by the <tt>StunCandidateHarvester</tt> in
|
||||||
|
* question alive.
|
||||||
|
*/
|
||||||
|
protected static final long SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED
|
||||||
|
= 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of <tt>Candidate</tt>s harvested for {@link #hostCandidate} by
|
* The list of <tt>Candidate</tt>s harvested for {@link #hostCandidate} by
|
||||||
* this harvest.
|
* this harvest.
|
||||||
*/
|
*/
|
||||||
private final List<Candidate> candidates = new LinkedList<Candidate>();
|
private final List<LocalCandidate> candidates
|
||||||
|
= new LinkedList<LocalCandidate>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The indicator which determines whether this <tt>StunCandidateHarvest</tt>
|
* The indicator which determines whether this <tt>StunCandidateHarvest</tt>
|
||||||
@ -84,6 +96,38 @@ public class StunCandidateHarvest
|
|||||||
private final Map<TransactionID, Request> requests
|
private final Map<TransactionID, Request> requests
|
||||||
= new HashMap<TransactionID, Request>();
|
= new HashMap<TransactionID, Request>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interval in milliseconds at which a new STUN keep-alive message is to
|
||||||
|
* be sent to the STUN server associated with the
|
||||||
|
* <tt>StunCandidateHarvester</tt> of this instance in order to keep one of
|
||||||
|
* the <tt>Candidate</tt>s harvested by this instance alive.
|
||||||
|
*/
|
||||||
|
private long sendKeepAliveMessageInterval
|
||||||
|
= SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>Object</tt> used to synchronize access to the members related to
|
||||||
|
* the sending of STUN keep-alive messages to the STUN server associated
|
||||||
|
* with the <tt>StunCandidateHarvester</tt> of this instance.
|
||||||
|
*/
|
||||||
|
private final Object sendKeepAliveMessageSyncRoot = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>Thread</tt> which sends the STUN keep-alive messages to the STUN
|
||||||
|
* server associated with the <tt>StunCandidateHarvester</tt> of this
|
||||||
|
* instance in order to keep the <tt>Candidate</tt>s harvested by this
|
||||||
|
* instance alive.
|
||||||
|
*/
|
||||||
|
private Thread sendKeepAliveMessageThread;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The time stamp of the last call to {@link #sendKeepAliveMessage()} which
|
||||||
|
* completed without throwing an exception (note: it doesn't mean that the
|
||||||
|
* keep-alive message was a STUN <tt>Request</tt> and it received a success
|
||||||
|
* STUN <tt>Response</tt>).
|
||||||
|
*/
|
||||||
|
private long sendKeepAliveMessageTimeStamp = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>StunCandidateHarvest</tt> which is to represent the
|
* Initializes a new <tt>StunCandidateHarvest</tt> which is to represent the
|
||||||
* harvesting of STUN <tt>Candidate</tt>s for a specific
|
* harvesting of STUN <tt>Candidate</tt>s for a specific
|
||||||
@ -206,6 +250,38 @@ public class StunCandidateHarvest
|
|||||||
createServerReflexiveCandidate(response);
|
createServerReflexiveCandidate(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new STUN <tt>Message</tt> to be sent to the STUN server
|
||||||
|
* associated with the <tt>StunCandidateHarvester</tt> of this instance in
|
||||||
|
* order to keep a specific <tt>LocalCandidate</tt> (harvested by this
|
||||||
|
* instance) alive.
|
||||||
|
*
|
||||||
|
* @param candidate the <tt>LocalCandidate</tt> (harvested by this instance)
|
||||||
|
* to create a new keep-alive STUN message for
|
||||||
|
* @return a new keep-alive STUN <tt>Message</tt> for the specified
|
||||||
|
* <tt>candidate</tt> or <tt>null</tt> if no keep-alive sending is to occur
|
||||||
|
* @throws StunException if anything goes wrong while creating the new
|
||||||
|
* keep-alive STUN <tt>Message</tt> for the specified <tt>candidate</tt>
|
||||||
|
* or the candidate is of an unsupported <tt>CandidateType</tt>
|
||||||
|
*/
|
||||||
|
protected Message createKeepAliveMessage(LocalCandidate candidate)
|
||||||
|
throws StunException
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* We'll not be keeping a STUN Binding alive for now. If we decide to in
|
||||||
|
* the future, we'll have to create a Binding Indication and add support
|
||||||
|
* for sending it.
|
||||||
|
*/
|
||||||
|
if (CandidateType.SERVER_REFLEXIVE_CANDIDATE.equals(
|
||||||
|
candidate.getType()))
|
||||||
|
return null;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw
|
||||||
|
new StunException(StunException.ILLEGAL_ARGUMENT, "candidate");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new <tt>Request</tt> instance which is to be sent by this
|
* Creates a new <tt>Request</tt> instance which is to be sent by this
|
||||||
* <tt>StunCandidateHarvest</tt> in order to retry a specific
|
* <tt>StunCandidateHarvest</tt> in order to retry a specific
|
||||||
@ -246,6 +322,29 @@ public class StunCandidateHarvest
|
|||||||
return MessageFactory.createBindingRequest();
|
return MessageFactory.createBindingRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and starts the {@link #sendKeepAliveMessageThread} which is to
|
||||||
|
* send STUN keep-alive <tt>Message</tt>s to the STUN server associated with
|
||||||
|
* the <tt>StunCandidateHarvester</tt> of this instance in order to keep the
|
||||||
|
* <tt>Candidate</tt>s harvested by this instance alive.
|
||||||
|
*/
|
||||||
|
private void createSendKeepAliveMessageThread()
|
||||||
|
{
|
||||||
|
synchronized (sendKeepAliveMessageSyncRoot)
|
||||||
|
{
|
||||||
|
sendKeepAliveMessageThread
|
||||||
|
= new Thread()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
runInSendKeepAliveMessageThread();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
sendKeepAliveMessageThread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a <tt>ServerReflexiveCandidate</tt> using {@link #hostCandidate}
|
* Creates a <tt>ServerReflexiveCandidate</tt> using {@link #hostCandidate}
|
||||||
* as its base and the <tt>XOR-MAPPED-ADDRESS</tt> attribute in
|
* as its base and the <tt>XOR-MAPPED-ADDRESS</tt> attribute in
|
||||||
@ -312,7 +411,7 @@ public class StunCandidateHarvest
|
|||||||
* @return an array containing the <tt>Candidate</tt>s harvested for
|
* @return an array containing the <tt>Candidate</tt>s harvested for
|
||||||
* {@link #hostCandidate} during this harvest
|
* {@link #hostCandidate} during this harvest
|
||||||
*/
|
*/
|
||||||
Candidate[] getCandidates()
|
LocalCandidate[] getCandidates()
|
||||||
{
|
{
|
||||||
return candidates.toArray(NO_CANDIDATES);
|
return candidates.toArray(NO_CANDIDATES);
|
||||||
}
|
}
|
||||||
@ -363,7 +462,8 @@ public class StunCandidateHarvest
|
|||||||
private boolean processChallenge(
|
private boolean processChallenge(
|
||||||
byte[] realm,
|
byte[] realm,
|
||||||
byte[] nonce,
|
byte[] nonce,
|
||||||
Request request)
|
Request request,
|
||||||
|
TransactionID requestTransactionID)
|
||||||
throws StunException
|
throws StunException
|
||||||
{
|
{
|
||||||
UsernameAttribute usernameAttribute
|
UsernameAttribute usernameAttribute
|
||||||
@ -442,7 +542,27 @@ public class StunCandidateHarvest
|
|||||||
|
|
||||||
if (retryRequest != null)
|
if (retryRequest != null)
|
||||||
{
|
{
|
||||||
retryRequestTransactionID = sendRequest(retryRequest, false);
|
if (requestTransactionID != null)
|
||||||
|
{
|
||||||
|
Object applicationData
|
||||||
|
= requestTransactionID.getApplicationData();
|
||||||
|
|
||||||
|
if (applicationData != null)
|
||||||
|
{
|
||||||
|
byte[] retryRequestTransactionIDAsBytes
|
||||||
|
= retryRequest.getTransactionID();
|
||||||
|
|
||||||
|
retryRequestTransactionID
|
||||||
|
= (retryRequestTransactionIDAsBytes == null)
|
||||||
|
? TransactionID.createNewTransactionID()
|
||||||
|
: TransactionID.createTransactionID(
|
||||||
|
retryRequestTransactionIDAsBytes);
|
||||||
|
retryRequestTransactionID.setApplicationData(
|
||||||
|
applicationData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
retryRequestTransactionID
|
||||||
|
= sendRequest(retryRequest, false, retryRequestTransactionID);
|
||||||
}
|
}
|
||||||
return (retryRequestTransactionID != null);
|
return (retryRequestTransactionID != null);
|
||||||
}
|
}
|
||||||
@ -462,7 +582,10 @@ public class StunCandidateHarvest
|
|||||||
* @throws StunException if anything goes wrong while processing the
|
* @throws StunException if anything goes wrong while processing the
|
||||||
* challenge
|
* challenge
|
||||||
*/
|
*/
|
||||||
private boolean processChallenge(Response response, Request request)
|
private boolean processChallenge(
|
||||||
|
Response response,
|
||||||
|
Request request,
|
||||||
|
TransactionID transactionID)
|
||||||
throws StunException
|
throws StunException
|
||||||
{
|
{
|
||||||
boolean retried = false;
|
boolean retried = false;
|
||||||
@ -513,7 +636,8 @@ public class StunCandidateHarvest
|
|||||||
= processChallenge(
|
= processChallenge(
|
||||||
realmAttribute.getRealm(),
|
realmAttribute.getRealm(),
|
||||||
nonceAttribute.getNonce(),
|
nonceAttribute.getNonce(),
|
||||||
request);
|
request,
|
||||||
|
transactionID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -521,6 +645,29 @@ public class StunCandidateHarvest
|
|||||||
return retried;
|
return retried;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies this <tt>StunCandidateHarvest</tt> that a specific
|
||||||
|
* <tt>Request</tt> has either received an error <tt>Response</tt> or has
|
||||||
|
* failed to receive any <tt>Response</tt>. Allows extenders to override and
|
||||||
|
* process unhandled error <tt>Response</tt>s or failures. The default
|
||||||
|
* implementation does no processing.
|
||||||
|
*
|
||||||
|
* @param response the error <tt>Response</tt> which has been received for
|
||||||
|
* <tt>request</tt>
|
||||||
|
* @param request the <tt>Request</tt> to which <tt>Response</tt> responds
|
||||||
|
* @return <tt>true</tt> if the error or failure condition has been
|
||||||
|
* processed and this instance can continue its execution (e.g. the
|
||||||
|
* resolution of the candidate) as if it was expected; otherwise,
|
||||||
|
* <tt>false</tt>
|
||||||
|
*/
|
||||||
|
protected boolean processErrorOrFailure(
|
||||||
|
Response response,
|
||||||
|
Request request,
|
||||||
|
TransactionID transactionID)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notifies this <tt>ResponseCollector</tt> that a transaction described by
|
* Notifies this <tt>ResponseCollector</tt> that a transaction described by
|
||||||
* the specified <tt>BaseStunMessageEvent</tt> has failed. The possible
|
* the specified <tt>BaseStunMessageEvent</tt> has failed. The possible
|
||||||
@ -554,7 +701,18 @@ public class StunCandidateHarvest
|
|||||||
if (message instanceof Request)
|
if (message instanceof Request)
|
||||||
request = (Request) message;
|
request = (Request) message;
|
||||||
}
|
}
|
||||||
completedResolvingCandidate(request, null);
|
|
||||||
|
boolean completedResolvingCandidate = true;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (processErrorOrFailure(null, request, transactionID))
|
||||||
|
completedResolvingCandidate = false;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (completedResolvingCandidate)
|
||||||
|
completedResolvingCandidate(request, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -625,7 +783,7 @@ public class StunCandidateHarvest
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
createCandidates(response);
|
processSuccess(response, request, transactionID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -641,11 +799,17 @@ public class StunCandidateHarvest
|
|||||||
switch (errorCodeAttr.getErrorNumber())
|
switch (errorCodeAttr.getErrorNumber())
|
||||||
{
|
{
|
||||||
case 1: // 401 Unauthorized
|
case 1: // 401 Unauthorized
|
||||||
if (processUnauthorized(response, request))
|
if (processUnauthorized(
|
||||||
|
response,
|
||||||
|
request,
|
||||||
|
transactionID))
|
||||||
completedResolvingCandidate = false;
|
completedResolvingCandidate = false;
|
||||||
break;
|
break;
|
||||||
case 38: // 438 Stale Nonce
|
case 38: // 438 Stale Nonce
|
||||||
if (processStaleNonce(response, request))
|
if (processStaleNonce(
|
||||||
|
response,
|
||||||
|
request,
|
||||||
|
transactionID))
|
||||||
completedResolvingCandidate = false;
|
completedResolvingCandidate = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -655,6 +819,12 @@ public class StunCandidateHarvest
|
|||||||
completedResolvingCandidate = true;
|
completedResolvingCandidate = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (completedResolvingCandidate
|
||||||
|
&& processErrorOrFailure(
|
||||||
|
response,
|
||||||
|
request,
|
||||||
|
transactionID))
|
||||||
|
completedResolvingCandidate = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -675,7 +845,10 @@ public class StunCandidateHarvest
|
|||||||
* @return <tt>true</tt> if the specified STUN error <tt>response</tt> was
|
* @return <tt>true</tt> if the specified STUN error <tt>response</tt> was
|
||||||
* successfully handled; <tt>false</tt>, otherwise
|
* successfully handled; <tt>false</tt>, otherwise
|
||||||
*/
|
*/
|
||||||
private boolean processStaleNonce(Response response, Request request)
|
private boolean processStaleNonce(
|
||||||
|
Response response,
|
||||||
|
Request request,
|
||||||
|
TransactionID transactionID)
|
||||||
throws StunException
|
throws StunException
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@ -709,7 +882,26 @@ public class StunCandidateHarvest
|
|||||||
else
|
else
|
||||||
challenge = false;
|
challenge = false;
|
||||||
|
|
||||||
return (challenge && processChallenge(response, request));
|
return
|
||||||
|
(challenge && processChallenge(response, request, transactionID));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a specific STUN success <tt>Response</tt> to a specific STUN
|
||||||
|
* <tt>Request</tt>.
|
||||||
|
*
|
||||||
|
* @param response the received STUN success <tt>Response</tt> which is to
|
||||||
|
* be handled
|
||||||
|
* @param request the STUN <tt>Request</tt> to which <tt>response</tt>
|
||||||
|
* responds
|
||||||
|
*/
|
||||||
|
protected void processSuccess(
|
||||||
|
Response response,
|
||||||
|
Request request,
|
||||||
|
TransactionID transactionID)
|
||||||
|
{
|
||||||
|
if (!completedResolvingCandidate)
|
||||||
|
createCandidates(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -723,7 +915,10 @@ public class StunCandidateHarvest
|
|||||||
* @return <tt>true</tt> if the specified STUN error <tt>response</tt> was
|
* @return <tt>true</tt> if the specified STUN error <tt>response</tt> was
|
||||||
* successfully handled; <tt>false</tt>, otherwise
|
* successfully handled; <tt>false</tt>, otherwise
|
||||||
*/
|
*/
|
||||||
private boolean processUnauthorized(Response response, Request request)
|
private boolean processUnauthorized(
|
||||||
|
Response response,
|
||||||
|
Request request,
|
||||||
|
TransactionID transactionID)
|
||||||
throws StunException
|
throws StunException
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@ -758,7 +953,162 @@ public class StunCandidateHarvest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (challenge && processChallenge(response, request));
|
return
|
||||||
|
(challenge && processChallenge(response, request, transactionID));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs in {@link #sendKeepAliveMessageThread} and sends STUN
|
||||||
|
* keep-alive <tt>Message</tt>s to the STUN server associated with the
|
||||||
|
* <tt>StunCandidateHarvester</tt> of this instance
|
||||||
|
*/
|
||||||
|
private void runInSendKeepAliveMessageThread()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
synchronized (sendKeepAliveMessageSyncRoot)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Since we're going to #wait, make sure we're not canceled
|
||||||
|
* yet.
|
||||||
|
*/
|
||||||
|
if (sendKeepAliveMessageInterval
|
||||||
|
== SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED)
|
||||||
|
break;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Determine the amount of milliseconds that we'll have to
|
||||||
|
* #wait.
|
||||||
|
*/
|
||||||
|
long timeout;
|
||||||
|
|
||||||
|
if (sendKeepAliveMessageTimeStamp == -1)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* If we're just starting, don't just go and send a new
|
||||||
|
* STUN keep-alive message but rather wait for the whole
|
||||||
|
* interval.
|
||||||
|
*/
|
||||||
|
timeout = sendKeepAliveMessageInterval;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
timeout
|
||||||
|
= sendKeepAliveMessageTimeStamp
|
||||||
|
+ sendKeepAliveMessageInterval
|
||||||
|
- System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
// At long last, #wait if necessary.
|
||||||
|
if (timeout > 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
sendKeepAliveMessageSyncRoot.wait(timeout);
|
||||||
|
}
|
||||||
|
catch (InterruptedException iex)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Apart from being the time to send the STUN keep-alive
|
||||||
|
* message, it could be that we've experienced a
|
||||||
|
* spurious wake-up or that we've been canceled.
|
||||||
|
*/
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sendKeepAliveMessageTimeStamp = System.currentTimeMillis();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
sendKeepAliveMessage();
|
||||||
|
}
|
||||||
|
catch (StunException sex)
|
||||||
|
{
|
||||||
|
logger.log(
|
||||||
|
Level.INFO,
|
||||||
|
"Failed to send STUN keep-alive message.",
|
||||||
|
sex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
synchronized (sendKeepAliveMessageSyncRoot)
|
||||||
|
{
|
||||||
|
if (sendKeepAliveMessageThread == Thread.currentThread())
|
||||||
|
sendKeepAliveMessageThread = null;
|
||||||
|
/*
|
||||||
|
* Well, if the currentThread is finishing and this instance is
|
||||||
|
* still to send keep-alive messages, we'd better start another
|
||||||
|
* Thread for the purpose to continue the work that the
|
||||||
|
* currentThread was supposed to carry out.
|
||||||
|
*/
|
||||||
|
if ((sendKeepAliveMessageThread == null)
|
||||||
|
&& (sendKeepAliveMessageInterval
|
||||||
|
!= SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED))
|
||||||
|
createSendKeepAliveMessageThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a new STUN <tt>Message</tt> to the STUN server associated with the
|
||||||
|
* <tt>StunCandidateHarvester</tt> of this instance in order to keep a
|
||||||
|
* <tt>LocalCandidate</tt> harvested by this instance alive.
|
||||||
|
*
|
||||||
|
* @throws StunException if anything goes wrong while sending a new
|
||||||
|
* keep-alive STUN <tt>Message</tt>
|
||||||
|
*/
|
||||||
|
protected void sendKeepAliveMessage()
|
||||||
|
throws StunException
|
||||||
|
{
|
||||||
|
for (LocalCandidate candidate : getCandidates())
|
||||||
|
if (sendKeepAliveMessage(candidate))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a new STUN <tt>Message</tt> to the STUN server associated with the
|
||||||
|
* <tt>StunCandidateHarvester</tt> of this instance in order to keep a
|
||||||
|
* specific <tt>LocalCandidate</tt> alive.
|
||||||
|
*
|
||||||
|
* @param candidate the <tt>LocalCandidate</tt> to send a new keep-alive
|
||||||
|
* STUN <tt>Message</tt> for
|
||||||
|
* @return <tt>true</tt> if a new STUN <tt>Message</tt> was sent to the
|
||||||
|
* STUN server associated with the <tt>StunCandidateHarvester</tt> of this
|
||||||
|
* instance or <tt>false</tt> if the STUN kee-alive functionality was not
|
||||||
|
* been used for the specified <tt>candidate</tt>
|
||||||
|
* @throws StunException if anything goes wrong while sending the new
|
||||||
|
* keep-alive STUN <tt>Message</tt> for the specified <tt>candidate</tt>
|
||||||
|
*/
|
||||||
|
protected boolean sendKeepAliveMessage(LocalCandidate candidate)
|
||||||
|
throws StunException
|
||||||
|
{
|
||||||
|
Message keepAliveMessage = createKeepAliveMessage(candidate);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The #createKeepAliveMessage method javadoc says it returns null when
|
||||||
|
* the STUN keep-alive functionality of this StunCandidateHarvest is to
|
||||||
|
* not be utilized.
|
||||||
|
*/
|
||||||
|
if (keepAliveMessage == null)
|
||||||
|
return false;
|
||||||
|
if (keepAliveMessage instanceof Request)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(sendRequest((Request) keepAliveMessage, false, null) != null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw
|
||||||
|
new StunException(
|
||||||
|
StunException.ILLEGAL_ARGUMENT,
|
||||||
|
"Failed to create keep-alive STUN message "
|
||||||
|
+ "for candidate: "
|
||||||
|
+ candidate);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -777,7 +1127,10 @@ public class StunCandidateHarvest
|
|||||||
* <tt>Request</tt> to the STUN server associated with this
|
* <tt>Request</tt> to the STUN server associated with this
|
||||||
* <tt>StunCandidateHarvest</tt>
|
* <tt>StunCandidateHarvest</tt>
|
||||||
*/
|
*/
|
||||||
protected TransactionID sendRequest(Request request, boolean firstRequest)
|
protected TransactionID sendRequest(
|
||||||
|
Request request,
|
||||||
|
boolean firstRequest,
|
||||||
|
TransactionID transactionID)
|
||||||
throws StunException
|
throws StunException
|
||||||
{
|
{
|
||||||
if (!firstRequest && (longTermCredentialSession != null))
|
if (!firstRequest && (longTermCredentialSession != null))
|
||||||
@ -788,8 +1141,15 @@ public class StunCandidateHarvest
|
|||||||
TransportAddress hostCandidateTransportAddress
|
TransportAddress hostCandidateTransportAddress
|
||||||
= hostCandidate.getTransportAddress();
|
= hostCandidate.getTransportAddress();
|
||||||
|
|
||||||
TransactionID transactionID;
|
if (transactionID == null)
|
||||||
|
{
|
||||||
|
byte[] transactionIDAsBytes = request.getTransactionID();
|
||||||
|
|
||||||
|
transactionID
|
||||||
|
= (transactionIDAsBytes == null)
|
||||||
|
? TransactionID.createNewTransactionID()
|
||||||
|
: TransactionID.createTransactionID(transactionIDAsBytes);
|
||||||
|
}
|
||||||
synchronized (requests)
|
synchronized (requests)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -800,7 +1160,8 @@ public class StunCandidateHarvest
|
|||||||
request,
|
request,
|
||||||
stunServer,
|
stunServer,
|
||||||
hostCandidateTransportAddress,
|
hostCandidateTransportAddress,
|
||||||
this);
|
this,
|
||||||
|
transactionID);
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException iaex)
|
catch (IllegalArgumentException iaex)
|
||||||
{
|
{
|
||||||
@ -842,6 +1203,41 @@ public class StunCandidateHarvest
|
|||||||
return transactionID;
|
return transactionID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the interval in milliseconds at which a new STUN keep-alive message
|
||||||
|
* is to be sent to the STUN server associated with the
|
||||||
|
* <tt>StunCandidateHarvester</tt> of this instance in order to keep one of
|
||||||
|
* the <tt>Candidate</tt>s harvested by this instance alive.
|
||||||
|
*
|
||||||
|
* @param sendKeepAliveMessageInterval the interval in milliseconds at which
|
||||||
|
* a new STUN keep-alive message is to be sent to the STUN server associated
|
||||||
|
* with the <tt>StunCandidateHarvester</tt> of this instance in order to
|
||||||
|
* keep one of the <tt>Candidate</tt>s harvested by this instance alive or
|
||||||
|
* {@link #SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED} if the keep-alive
|
||||||
|
* functionality is to not be utilized
|
||||||
|
*/
|
||||||
|
protected void setSendKeepAliveMessageInterval(
|
||||||
|
long sendKeepAliveMessageInterval)
|
||||||
|
{
|
||||||
|
if ((sendKeepAliveMessageInterval
|
||||||
|
!= SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED)
|
||||||
|
&& (sendKeepAliveMessageInterval < 1))
|
||||||
|
throw new IllegalArgumentException("sendKeepAliveMessageInterval");
|
||||||
|
|
||||||
|
synchronized (sendKeepAliveMessageSyncRoot)
|
||||||
|
{
|
||||||
|
this.sendKeepAliveMessageInterval = sendKeepAliveMessageInterval;
|
||||||
|
if (sendKeepAliveMessageThread == null)
|
||||||
|
{
|
||||||
|
if (this.sendKeepAliveMessageInterval
|
||||||
|
!= SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED)
|
||||||
|
createSendKeepAliveMessageThread();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
sendKeepAliveMessageSyncRoot.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the harvesting of <tt>Candidate</tt>s to be performed for
|
* Starts the harvesting of <tt>Candidate</tt>s to be performed for
|
||||||
* {@link #hostCandidate}.
|
* {@link #hostCandidate}.
|
||||||
@ -865,7 +1261,7 @@ public class StunCandidateHarvest
|
|||||||
// Short-Term Credential Mechanism
|
// Short-Term Credential Mechanism
|
||||||
addShortTermCredentialAttributes(requestToStartResolvingCandidate);
|
addShortTermCredentialAttributes(requestToStartResolvingCandidate);
|
||||||
|
|
||||||
sendRequest(requestToStartResolvingCandidate, true);
|
sendRequest(requestToStartResolvingCandidate, true, null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@ -63,7 +63,7 @@ public class StunCandidateHarvester
|
|||||||
/**
|
/**
|
||||||
* The stack to use for STUN communication.
|
* The stack to use for STUN communication.
|
||||||
*/
|
*/
|
||||||
protected final StunStack stunStack = StunStack.getInstance();
|
public final StunStack stunStack = StunStack.getInstance();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new STUN harvester that will be running against the specified
|
* Creates a new STUN harvester that will be running against the specified
|
||||||
|
|||||||
@ -7,10 +7,15 @@
|
|||||||
*/
|
*/
|
||||||
package org.ice4j.ice.harvest;
|
package org.ice4j.ice.harvest;
|
||||||
|
|
||||||
|
import java.lang.reflect.*;
|
||||||
|
import java.util.logging.*;
|
||||||
|
|
||||||
import org.ice4j.*;
|
import org.ice4j.*;
|
||||||
import org.ice4j.attribute.*;
|
import org.ice4j.attribute.*;
|
||||||
import org.ice4j.ice.*;
|
import org.ice4j.ice.*;
|
||||||
import org.ice4j.message.*;
|
import org.ice4j.message.*;
|
||||||
|
import org.ice4j.socket.*;
|
||||||
|
import org.ice4j.stack.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the harvesting of TURN <tt>Candidates</tt> for a specific
|
* Represents the harvesting of TURN <tt>Candidates</tt> for a specific
|
||||||
@ -23,6 +28,13 @@ public class TurnCandidateHarvest
|
|||||||
extends StunCandidateHarvest
|
extends StunCandidateHarvest
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>Logger</tt> used by the <tt>TurnCandidateHarvest</tt> class and
|
||||||
|
* its instances for logging output.
|
||||||
|
*/
|
||||||
|
private static final Logger logger
|
||||||
|
= Logger.getLogger(TurnCandidateHarvest.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <tt>Request</tt> created by the last call to
|
* The <tt>Request</tt> created by the last call to
|
||||||
* {@link #createRequestToStartResolvingCandidate()}.
|
* {@link #createRequestToStartResolvingCandidate()}.
|
||||||
@ -47,6 +59,48 @@ public class TurnCandidateHarvest
|
|||||||
super(harvester, hostCandidate);
|
super(harvester, hostCandidate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies this <tt>TurnCandidateHarvest</tt> that a specific
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> is closing and that this instance
|
||||||
|
* is to delete the associated TURN Allocation.
|
||||||
|
* <p>
|
||||||
|
* <b>Note</b>: The method is part of the internal API of
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> and <tt>TurnCandidateHarvest</tt>
|
||||||
|
* and is not intended for public use.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param relayedCandidateSocket the <tt>RelayedCandidateDatagramSocket</tt>
|
||||||
|
* which notifies this instance and which requests that the associated TURN
|
||||||
|
* Allocation be deleted
|
||||||
|
*/
|
||||||
|
public void close(RelayedCandidateDatagramSocket relayedCandidateSocket)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* FIXME As far as logic goes, it seems that it is possible to send a
|
||||||
|
* TURN Refresh, cancel the STUN keep-alive functionality here and only
|
||||||
|
* then receive the response to the TURN Refresh which will enable the
|
||||||
|
* STUN keep-alive functionality again.
|
||||||
|
*/
|
||||||
|
setSendKeepAliveMessageInterval(
|
||||||
|
SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TURN Refresh with a LIFETIME value equal to zero deletes the TURN
|
||||||
|
* Allocation.
|
||||||
|
*/
|
||||||
|
try
|
||||||
|
{
|
||||||
|
sendRequest(MessageFactory.createRefreshRequest(0), false, null);
|
||||||
|
}
|
||||||
|
catch (StunException sex)
|
||||||
|
{
|
||||||
|
logger.log(
|
||||||
|
Level.INFO,
|
||||||
|
"Failed to send TURN Refresh request to delete Allocation",
|
||||||
|
sex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Completes the harvesting of <tt>Candidate</tt>s for
|
* Completes the harvesting of <tt>Candidate</tt>s for
|
||||||
* {@link #hostCandidate}. Notifies {@link #harvester} about the completion
|
* {@link #hostCandidate}. Notifies {@link #harvester} about the completion
|
||||||
@ -111,6 +165,54 @@ public class TurnCandidateHarvest
|
|||||||
super.createCandidates(response);
|
super.createCandidates(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new STUN <tt>Message</tt> to be sent to the STUN server
|
||||||
|
* associated with the <tt>StunCandidateHarvester</tt> of this instance in
|
||||||
|
* order to keep a specific <tt>LocalCandidate</tt> (harvested by this
|
||||||
|
* instance) alive.
|
||||||
|
*
|
||||||
|
* @param candidate the <tt>LocalCandidate</tt> (harvested by this instance)
|
||||||
|
* to create a new keep-alive STUN message for
|
||||||
|
* @return a new keep-alive STUN <tt>Message</tt> for the specified
|
||||||
|
* <tt>candidate</tt> or <tt>null</tt> if no keep-alive sending is to occur
|
||||||
|
* @throws StunException if anything goes wrong while creating the new
|
||||||
|
* keep-alive STUN <tt>Message</tt> for the specified <tt>candidate</tt>
|
||||||
|
* or the candidate is of an unsupported <tt>CandidateType</tt>
|
||||||
|
* @see StunCandidateHarvest#createKeepAliveMessage(LocalCandidate)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Message createKeepAliveMessage(LocalCandidate candidate)
|
||||||
|
throws StunException
|
||||||
|
{
|
||||||
|
switch (candidate.getType())
|
||||||
|
{
|
||||||
|
case RELAYED_CANDIDATE:
|
||||||
|
return MessageFactory.createRefreshRequest();
|
||||||
|
case SERVER_REFLEXIVE_CANDIDATE:
|
||||||
|
/*
|
||||||
|
* RFC 5245: The Refresh requests will also refresh the server
|
||||||
|
* reflexive candidate.
|
||||||
|
*/
|
||||||
|
boolean existsRelayedCandidate = false;
|
||||||
|
|
||||||
|
for (Candidate aCandidate : getCandidates())
|
||||||
|
{
|
||||||
|
if (CandidateType.RELAYED_CANDIDATE.equals(
|
||||||
|
aCandidate.getType()))
|
||||||
|
{
|
||||||
|
existsRelayedCandidate = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
existsRelayedCandidate
|
||||||
|
? null
|
||||||
|
: super.createKeepAliveMessage(candidate);
|
||||||
|
default:
|
||||||
|
return super.createKeepAliveMessage(candidate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a <tt>RelayedCandidate</tt> using the
|
* Creates a <tt>RelayedCandidate</tt> using the
|
||||||
* <tt>XOR-RELAYED-ADDRESS</tt> attribute in a specific STUN
|
* <tt>XOR-RELAYED-ADDRESS</tt> attribute in a specific STUN
|
||||||
@ -137,7 +239,17 @@ public class TurnCandidateHarvest
|
|||||||
getMappedAddress(response));
|
getMappedAddress(response));
|
||||||
|
|
||||||
if (relayedCandidate != null)
|
if (relayedCandidate != null)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* The ICE connectivity checks will utilize STUN on the
|
||||||
|
* (application-purposed) socket of the RelayedCandidate and
|
||||||
|
* will not add it to the StunStack so we have to do it.
|
||||||
|
*/
|
||||||
|
harvester.stunStack.addSocket(
|
||||||
|
relayedCandidate.getStunSocket(null));
|
||||||
|
|
||||||
addCandidate(relayedCandidate);
|
addCandidate(relayedCandidate);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,6 +320,44 @@ public class TurnCandidateHarvest
|
|||||||
MessageFactory.createAllocateRequest(
|
MessageFactory.createAllocateRequest(
|
||||||
(byte) requestedTransport,
|
(byte) requestedTransport,
|
||||||
rFlag);
|
rFlag);
|
||||||
|
|
||||||
|
case Message.CREATEPERMISSION_REQUEST:
|
||||||
|
XorPeerAddressAttribute peerAddressAttribute
|
||||||
|
= (XorPeerAddressAttribute)
|
||||||
|
request.getAttribute(Attribute.XOR_PEER_ADDRESS);
|
||||||
|
TransportAddress peerAddress
|
||||||
|
= peerAddressAttribute.getAddress(request.getTransactionID());
|
||||||
|
byte[] retryTransactionID
|
||||||
|
= TransactionID.createNewTransactionID().getBytes();
|
||||||
|
Request retryCreatePermissionRequest
|
||||||
|
= MessageFactory.createCreatePermissionRequest(
|
||||||
|
peerAddress,
|
||||||
|
retryTransactionID);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
retryCreatePermissionRequest.setTransactionID(
|
||||||
|
retryTransactionID);
|
||||||
|
}
|
||||||
|
catch (StunException sex)
|
||||||
|
{
|
||||||
|
throw new UndeclaredThrowableException(sex);
|
||||||
|
}
|
||||||
|
return retryCreatePermissionRequest;
|
||||||
|
|
||||||
|
case Message.REFRESH_REQUEST:
|
||||||
|
LifetimeAttribute lifetimeAttribute
|
||||||
|
= (LifetimeAttribute) request.getAttribute(Attribute.LIFETIME);
|
||||||
|
|
||||||
|
if (lifetimeAttribute == null)
|
||||||
|
return MessageFactory.createRefreshRequest();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return
|
||||||
|
MessageFactory.createRefreshRequest(
|
||||||
|
lifetimeAttribute.getLifetime());
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return super.createRequestToRetry(request);
|
return super.createRequestToRetry(request);
|
||||||
}
|
}
|
||||||
@ -242,4 +392,126 @@ public class TurnCandidateHarvest
|
|||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies this <tt>StunCandidateHarvest</tt> that a specific
|
||||||
|
* <tt>Request</tt> has either received an error <tt>Response</tt> or has
|
||||||
|
* failed to receive any <tt>Response</tt>.
|
||||||
|
*
|
||||||
|
* @param response the error <tt>Response</tt> which has been received for
|
||||||
|
* <tt>request</tt>
|
||||||
|
* @param request the <tt>Request</tt> to which <tt>Response</tt> responds
|
||||||
|
* @return <tt>true</tt> if the error or failure condition has been
|
||||||
|
* processed and this instance can continue its execution (e.g. the
|
||||||
|
* resolution of the candidate) as if it was expected; otherwise,
|
||||||
|
* <tt>false</tt>
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected boolean processErrorOrFailure(
|
||||||
|
Response response,
|
||||||
|
Request request,
|
||||||
|
TransactionID transactionID)
|
||||||
|
{
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TurnCandidateHarvest uses the applicationData of TransactionID to
|
||||||
|
* deliver the results of Requests sent by
|
||||||
|
* RelayedCandidateDatagramSocket back to it.
|
||||||
|
*/
|
||||||
|
Object applicationData = transactionID.getApplicationData();
|
||||||
|
|
||||||
|
if ((applicationData instanceof RelayedCandidateDatagramSocket)
|
||||||
|
&& ((RelayedCandidateDatagramSocket) applicationData)
|
||||||
|
.processErrorOrFailure(response, request))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return super.processErrorOrFailure(response, request, transactionID);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a specific STUN success <tt>Response</tt> to a specific STUN
|
||||||
|
* <tt>Request</tt>.
|
||||||
|
*
|
||||||
|
* @param response the received STUN success <tt>Response</tt> which is to
|
||||||
|
* be handled
|
||||||
|
* @param request the STUN <tt>Request</tt> to which <tt>response</tt>
|
||||||
|
* responds
|
||||||
|
* @see StunCandidateHarvest#processSuccess(Response, Request)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void processSuccess(
|
||||||
|
Response response,
|
||||||
|
Request request,
|
||||||
|
TransactionID transactionID)
|
||||||
|
{
|
||||||
|
super.processSuccess(response, request, transactionID);
|
||||||
|
|
||||||
|
LifetimeAttribute lifetimeAttribute;
|
||||||
|
int lifetime /* minutes */ = -1;
|
||||||
|
|
||||||
|
switch (response.getMessageType())
|
||||||
|
{
|
||||||
|
case Message.ALLOCATE_RESPONSE:
|
||||||
|
// The default lifetime of an allocation is 10 minutes.
|
||||||
|
lifetimeAttribute
|
||||||
|
= (LifetimeAttribute) response.getAttribute(Attribute.LIFETIME);
|
||||||
|
lifetime
|
||||||
|
= (lifetimeAttribute == null)
|
||||||
|
? (10 * 60)
|
||||||
|
: lifetimeAttribute.getLifetime();
|
||||||
|
break;
|
||||||
|
case Message.REFRESH_RESPONSE:
|
||||||
|
lifetimeAttribute
|
||||||
|
= (LifetimeAttribute) response.getAttribute(Attribute.LIFETIME);
|
||||||
|
if (lifetimeAttribute != null)
|
||||||
|
lifetime = lifetimeAttribute.getLifetime();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (lifetime >= 0)
|
||||||
|
{
|
||||||
|
setSendKeepAliveMessageInterval(
|
||||||
|
/* milliseconds */ 1000L * lifetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TurnCandidateHarvest uses the applicationData of TransactionID to
|
||||||
|
* deliver the results of Requests sent by
|
||||||
|
* RelayedCandidateDatagramSocket back to it.
|
||||||
|
*/
|
||||||
|
Object applicationData = transactionID.getApplicationData();
|
||||||
|
|
||||||
|
if (applicationData instanceof RelayedCandidateDatagramSocket)
|
||||||
|
{
|
||||||
|
((RelayedCandidateDatagramSocket) applicationData)
|
||||||
|
.processSuccess(response, request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a specific <tt>Request</tt> on behalf of a specific
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> to the TURN server associated
|
||||||
|
* with this <tt>TurnCandidateHarvest</tt>.
|
||||||
|
*
|
||||||
|
* @param relayedCandidateDatagramSocket the
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> which sends the specified
|
||||||
|
* <tt>Request</tt> and which is to be notified of the result
|
||||||
|
* @param request the <tt>Request</tt> to be sent to the TURN server
|
||||||
|
* associated with this <tt>TurnCandidateHarvest</tt>
|
||||||
|
* @return an array of <tt>byte</tt>s which represents the ID of the
|
||||||
|
* transaction with which the specified <tt>Request</tt> has been sent to
|
||||||
|
* the TURN server
|
||||||
|
* @throws StunException if anything goes wrong while sending the specified
|
||||||
|
* <tt>Request</tt>
|
||||||
|
*/
|
||||||
|
public byte[] sendRequest(
|
||||||
|
RelayedCandidateDatagramSocket relayedCandidateDatagramSocket,
|
||||||
|
Request request)
|
||||||
|
throws StunException
|
||||||
|
{
|
||||||
|
TransactionID transactionID = TransactionID.createNewTransactionID();
|
||||||
|
|
||||||
|
transactionID.setApplicationData(relayedCandidateDatagramSocket);
|
||||||
|
transactionID = sendRequest(request, false, transactionID);
|
||||||
|
return (transactionID == null) ? null : transactionID.getBytes();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import org.ice4j.stack.*;
|
|||||||
*
|
*
|
||||||
* @author Emil Ivov
|
* @author Emil Ivov
|
||||||
* @author Sebastien Vincent
|
* @author Sebastien Vincent
|
||||||
|
* @author Lubomir Marinov
|
||||||
*/
|
*/
|
||||||
public abstract class Message
|
public abstract class Message
|
||||||
{
|
{
|
||||||
@ -51,8 +52,9 @@ public abstract class Message
|
|||||||
/* TURN methods */
|
/* TURN methods */
|
||||||
public static final char TURN_METHOD_ALLOCATE = 0x0003;
|
public static final char TURN_METHOD_ALLOCATE = 0x0003;
|
||||||
public static final char TURN_METHOD_REFRESH = 0x0004;
|
public static final char TURN_METHOD_REFRESH = 0x0004;
|
||||||
public static final char TURN_METHOD_SEND = 0x0006;;
|
public static final char TURN_METHOD_SEND = 0x0006;
|
||||||
public static final char TURN_METHOD_DATA = 0x0007;;
|
public static final char TURN_METHOD_DATA = 0x0007;
|
||||||
|
public static final char TURN_METHOD_CREATEPERMISSION = 0x0008;
|
||||||
public static final char TURN_METHOD_CHANNELBIND = 0x0009;
|
public static final char TURN_METHOD_CHANNELBIND = 0x0009;
|
||||||
|
|
||||||
public static final char ALLOCATE_REQUEST = (TURN_METHOD_ALLOCATE | STUN_REQUEST);
|
public static final char ALLOCATE_REQUEST = (TURN_METHOD_ALLOCATE | STUN_REQUEST);
|
||||||
@ -64,6 +66,9 @@ public abstract class Message
|
|||||||
public static final char CHANNELBIND_REQUEST = (TURN_METHOD_CHANNELBIND | STUN_REQUEST);
|
public static final char CHANNELBIND_REQUEST = (TURN_METHOD_CHANNELBIND | STUN_REQUEST);
|
||||||
public static final char CHANNELBIND_RESPONSE = (TURN_METHOD_CHANNELBIND | STUN_SUCCESS_RESP);
|
public static final char CHANNELBIND_RESPONSE = (TURN_METHOD_CHANNELBIND | STUN_SUCCESS_RESP);
|
||||||
public static final char CHANNELBIND_ERROR_RESPONSE = (TURN_METHOD_CHANNELBIND | STUN_ERROR_RESP);
|
public static final char CHANNELBIND_ERROR_RESPONSE = (TURN_METHOD_CHANNELBIND | STUN_ERROR_RESP);
|
||||||
|
public static final char CREATEPERMISSION_REQUEST = (TURN_METHOD_CREATEPERMISSION | STUN_REQUEST);
|
||||||
|
public static final char CREATEPERMISSION_RESPONSE = (TURN_METHOD_CREATEPERMISSION | STUN_SUCCESS_RESP);
|
||||||
|
public static final char CREATEPERMISSION_ERROR_RESPONSE = (TURN_METHOD_CREATEPERMISSION | STUN_ERROR_RESP);
|
||||||
public static final char SEND_INDICATION = (TURN_METHOD_SEND | STUN_INDICATION);
|
public static final char SEND_INDICATION = (TURN_METHOD_SEND | STUN_INDICATION);
|
||||||
public static final char DATA_INDICATION = (TURN_METHOD_DATA | STUN_INDICATION);
|
public static final char DATA_INDICATION = (TURN_METHOD_DATA | STUN_INDICATION);
|
||||||
|
|
||||||
@ -116,7 +121,7 @@ public abstract class Message
|
|||||||
private static boolean rfc3489CompatibilityMode = false;
|
private static boolean rfc3489CompatibilityMode = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Desribes which attributes are present in which messages. An
|
* Describes which attributes are present in which messages. An
|
||||||
* M indicates that inclusion of the attribute in the message is
|
* M indicates that inclusion of the attribute in the message is
|
||||||
* mandatory, O means its optional, C means it's conditional based on
|
* mandatory, O means its optional, C means it's conditional based on
|
||||||
* some other aspect of the message, and N/A means that the attribute is
|
* some other aspect of the message, and N/A means that the attribute is
|
||||||
@ -440,6 +445,16 @@ public abstract class Message
|
|||||||
case CHANNELBIND_REQUEST: msgIndex = CHANNELBIND_REQUEST_PRESENTITY_INDEX; break;
|
case CHANNELBIND_REQUEST: msgIndex = CHANNELBIND_REQUEST_PRESENTITY_INDEX; break;
|
||||||
case SEND_INDICATION: msgIndex = SEND_INDICATION_PRESENTITY_INDEX; break;
|
case SEND_INDICATION: msgIndex = SEND_INDICATION_PRESENTITY_INDEX; break;
|
||||||
case DATA_INDICATION: msgIndex = DATA_INDICATION_PRESENTITY_INDEX; break;
|
case DATA_INDICATION: msgIndex = DATA_INDICATION_PRESENTITY_INDEX; break;
|
||||||
|
default:
|
||||||
|
if (logger.isLoggable(Level.FINE))
|
||||||
|
{
|
||||||
|
logger.log(
|
||||||
|
Level.FINE,
|
||||||
|
"Attribute presentity not defined for STUN message type: "
|
||||||
|
+ ((int) messageType)
|
||||||
|
+ ". Will assume optional.");
|
||||||
|
}
|
||||||
|
return O;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (attributeType)
|
switch (attributeType)
|
||||||
@ -494,12 +509,20 @@ public abstract class Message
|
|||||||
case BINDING_REQUEST: return "BINDING-REQUEST";
|
case BINDING_REQUEST: return "BINDING-REQUEST";
|
||||||
case BINDING_SUCCESS_RESPONSE: return "BINDING-RESPONSE";
|
case BINDING_SUCCESS_RESPONSE: return "BINDING-RESPONSE";
|
||||||
case BINDING_ERROR_RESPONSE: return "BINDING-ERROR-RESPONSE";
|
case BINDING_ERROR_RESPONSE: return "BINDING-ERROR-RESPONSE";
|
||||||
|
case CREATEPERMISSION_REQUEST: return "CREATE-PERMISSION-REQUEST";
|
||||||
|
case CREATEPERMISSION_RESPONSE: return "CREATE-PERMISSION-RESPONSE";
|
||||||
|
case CREATEPERMISSION_ERROR_RESPONSE:
|
||||||
|
return "CREATE-PERMISSION-ERROR-RESPONSE";
|
||||||
|
case DATA_INDICATION: return "DATA-INDICATION";
|
||||||
|
case REFRESH_REQUEST: return "REFRESH-REQUEST";
|
||||||
|
case REFRESH_RESPONSE: return "REFRESH-RESPONSE";
|
||||||
|
case REFRESH_ERROR_RESPONSE: return "REFRESH-ERROR-RESPONSE";
|
||||||
|
case SEND_INDICATION: return "SEND-INDICATION";
|
||||||
case SHARED_SECRET_REQUEST: return "SHARED-SECRET-REQUEST";
|
case SHARED_SECRET_REQUEST: return "SHARED-SECRET-REQUEST";
|
||||||
case SHARED_SECRET_RESPONSE: return "SHARED-SECRET-RESPONSE";
|
case SHARED_SECRET_RESPONSE: return "SHARED-SECRET-RESPONSE";
|
||||||
case SHARED_SECRET_ERROR_RESPONSE: return "SHARED-SECRET-ERROR-RESPONSE";
|
case SHARED_SECRET_ERROR_RESPONSE: return "SHARED-SECRET-ERROR-RESPONSE";
|
||||||
|
default: return "UNKNOWN-MESSAGE";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "UNKNOWN-MESSAGE";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -691,17 +714,11 @@ public abstract class Message
|
|||||||
| (binMessage[offset++]&0xFF));
|
| (binMessage[offset++]&0xFF));
|
||||||
Message message;
|
Message message;
|
||||||
if (Message.isResponseType(messageType))
|
if (Message.isResponseType(messageType))
|
||||||
{
|
|
||||||
message = new Response();
|
message = new Response();
|
||||||
}
|
|
||||||
else if(Message.isRequestType(messageType))
|
else if(Message.isRequestType(messageType))
|
||||||
{
|
|
||||||
message = new Request();
|
message = new Request();
|
||||||
}
|
|
||||||
else /* indication */
|
else /* indication */
|
||||||
{
|
|
||||||
message = new Indication();
|
message = new Indication();
|
||||||
}
|
|
||||||
|
|
||||||
message.setMessageType(messageType);
|
message.setMessageType(messageType);
|
||||||
|
|
||||||
@ -715,9 +732,16 @@ public abstract class Message
|
|||||||
|
|
||||||
if(arrayLen - offset - TRANSACTION_ID_LENGTH < length)
|
if(arrayLen - offset - TRANSACTION_ID_LENGTH < length)
|
||||||
{
|
{
|
||||||
throw new StunException( StunException.ILLEGAL_ARGUMENT,
|
throw
|
||||||
"The given binary array does not seem to "
|
new StunException(
|
||||||
+ "contain a whole StunMessage");
|
StunException.ILLEGAL_ARGUMENT,
|
||||||
|
"The given binary array does not seem to contain"
|
||||||
|
+ " a whole StunMessage: given "
|
||||||
|
+ ((int) arrayLen)
|
||||||
|
+ " bytes of "
|
||||||
|
+ message.getName()
|
||||||
|
+ " but expecting "
|
||||||
|
+ (offset + TRANSACTION_ID_LENGTH + length));
|
||||||
}
|
}
|
||||||
|
|
||||||
byte tranID[] = new byte[TRANSACTION_ID_LENGTH];
|
byte tranID[] = new byte[TRANSACTION_ID_LENGTH];
|
||||||
@ -846,7 +870,6 @@ public abstract class Message
|
|||||||
protected void validateAttributePresentity()
|
protected void validateAttributePresentity()
|
||||||
throws IllegalStateException
|
throws IllegalStateException
|
||||||
{
|
{
|
||||||
//the
|
|
||||||
if(! rfc3489CompatibilityMode )
|
if(! rfc3489CompatibilityMode )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
@ -18,9 +18,15 @@ import org.ice4j.attribute.*;
|
|||||||
*
|
*
|
||||||
* @author Emil Ivov
|
* @author Emil Ivov
|
||||||
* @author Sebastien Vincent
|
* @author Sebastien Vincent
|
||||||
|
* @author Lubomir Marinov
|
||||||
*/
|
*/
|
||||||
public class MessageFactory
|
public class MessageFactory
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>Logger</tt> used by the <tt>MessageFactory</tt> class and its
|
||||||
|
* instances.
|
||||||
|
*/
|
||||||
private static final Logger logger
|
private static final Logger logger
|
||||||
= Logger.getLogger(MessageFactory.class.getName());
|
= Logger.getLogger(MessageFactory.class.getName());
|
||||||
|
|
||||||
@ -434,6 +440,32 @@ public class MessageFactory
|
|||||||
request.addAttribute(messageIntegrityAttribute);
|
request.addAttribute(messageIntegrityAttribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new TURN Refresh <tt>Request</tt> without any optional
|
||||||
|
* attributes such as LIFETIME.
|
||||||
|
*
|
||||||
|
* @return a new TURN Refresh <tt>Request</tt> without any optional
|
||||||
|
* attributes such as LIFETIME
|
||||||
|
*/
|
||||||
|
public static Request createRefreshRequest()
|
||||||
|
{
|
||||||
|
Request refreshRequest = new Request();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
refreshRequest.setMessageType(Message.REFRESH_REQUEST);
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException iaex)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* We don't actually expect the exception to happen so we're
|
||||||
|
* ignoring it.
|
||||||
|
*/
|
||||||
|
logger.log(Level.FINE, "Failed to set message type.", iaex);
|
||||||
|
}
|
||||||
|
return refreshRequest;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a refresh request.
|
* Create a refresh request.
|
||||||
*
|
*
|
||||||
@ -498,6 +530,40 @@ public class MessageFactory
|
|||||||
return channelBindRequest;
|
return channelBindRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new TURN CreatePermission <tt>Request</tt> with a specific
|
||||||
|
* value for its XOR-PEER-ADDRESS attribute.
|
||||||
|
*
|
||||||
|
* @param peerAddress the value to assigned to the XOR-PEER-ADDRESS
|
||||||
|
* attribute
|
||||||
|
* @param transactionID the ID of the transaction which is to be used for
|
||||||
|
* the assignment of <tt>peerAddress</tt> to the XOR-PEER-ADDRESS attribute
|
||||||
|
* @return a new TURN CreatePermission <tt>Request</tt> with the specified
|
||||||
|
* value for its XOR-PEER-ADDRESS attribute
|
||||||
|
*/
|
||||||
|
public static Request createCreatePermissionRequest(
|
||||||
|
TransportAddress peerAddress,
|
||||||
|
byte[] transactionID)
|
||||||
|
{
|
||||||
|
Request createPermissionRequest = new Request();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
createPermissionRequest.setMessageType(
|
||||||
|
Message.CREATEPERMISSION_REQUEST);
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException iaex)
|
||||||
|
{
|
||||||
|
// Expected to not happen because we are the creators.
|
||||||
|
logger.log(Level.FINE, "Failed to set message type.", iaex);
|
||||||
|
}
|
||||||
|
createPermissionRequest.addAttribute(
|
||||||
|
AttributeFactory.createXorPeerAddressAttribute(
|
||||||
|
peerAddress,
|
||||||
|
transactionID));
|
||||||
|
return createPermissionRequest;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a Send Indication.
|
* Create a Send Indication.
|
||||||
*
|
*
|
||||||
|
|||||||
712
src/org/ice4j/socket/DelegatingDatagramSocket.java
Normal file
712
src/org/ice4j/socket/DelegatingDatagramSocket.java
Normal file
@ -0,0 +1,712 @@
|
|||||||
|
/*
|
||||||
|
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||||
|
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||||
|
*
|
||||||
|
* Distributable under LGPL license.
|
||||||
|
* See terms of license at gnu.org.
|
||||||
|
*/
|
||||||
|
package org.ice4j.socket;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.channels.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements a <tt>DatagramSocket</tt> which delegates its calls to a specific
|
||||||
|
* <tt>DatagramSocket</tt>.
|
||||||
|
*
|
||||||
|
* @author Lubomir Marinov
|
||||||
|
*/
|
||||||
|
public class DelegatingDatagramSocket
|
||||||
|
extends DatagramSocket
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>DatagramSocket</tt> to which this
|
||||||
|
* <tt>DelegatingDatagramSocket</tt> delegates its calls.
|
||||||
|
*/
|
||||||
|
protected final DatagramSocket delegate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance and binds it
|
||||||
|
* to any available port on the local host machine. The socket will be
|
||||||
|
* bound to the wildcard address, an IP address chosen by the kernel.
|
||||||
|
*
|
||||||
|
* @throws SocketException if the socket could not be opened, or the socket
|
||||||
|
* could not bind to the specified local port
|
||||||
|
* @see DatagramSocket#DatagramSocket()
|
||||||
|
*/
|
||||||
|
public DelegatingDatagramSocket()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.delegate = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance which to
|
||||||
|
* implement the <tt>DatagramSocket</tt> functionality by delegating to a
|
||||||
|
* specific <tt>DatagramSocket</tt>.
|
||||||
|
*
|
||||||
|
* @param delegate the <tt>DatagramSocket</tt> to which the new instance is
|
||||||
|
* to delegate
|
||||||
|
* @throws SocketException if anything goes wrong while initializing the new
|
||||||
|
* <tt>DelegatingDatagramSocket</tt> instance
|
||||||
|
*/
|
||||||
|
public DelegatingDatagramSocket(DatagramSocket delegate)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
super(/* bindaddr */ (SocketAddress) null);
|
||||||
|
|
||||||
|
this.delegate = delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance and binds
|
||||||
|
* it to the specified port on the local host machine. The socket will be
|
||||||
|
* bound to the wildcard address, an IP address chosen by the kernel.
|
||||||
|
*
|
||||||
|
* @param port the port to bind the new socket to
|
||||||
|
* @throws SocketException if the socket could not be opened, or the socket
|
||||||
|
* could not bind to the specified local port
|
||||||
|
* @see DatagramSocket#DatagramSocket(int)
|
||||||
|
*/
|
||||||
|
public DelegatingDatagramSocket(int port)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
super(port);
|
||||||
|
|
||||||
|
this.delegate = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance bound to the
|
||||||
|
* specified local address. The local port must be between 0 and 65535
|
||||||
|
* inclusive. If the IP address is 0.0.0.0, the socket will be bound to the
|
||||||
|
* wildcard address, an IP address chosen by the kernel.
|
||||||
|
*
|
||||||
|
* @param port the local port to bind the new socket to
|
||||||
|
* @param laddr the local address to bind the new socket to
|
||||||
|
* @throws SocketException if the socket could not be opened, or the socket
|
||||||
|
* could not bind to the specified local port
|
||||||
|
* @see DatagramSocket#DatagramSocket(int, InetAddress)
|
||||||
|
*/
|
||||||
|
public DelegatingDatagramSocket(int port, InetAddress laddr)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
super(port, laddr);
|
||||||
|
|
||||||
|
this.delegate = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a datagram socket, bound to the specified local socket address.
|
||||||
|
* <p>
|
||||||
|
* If, if the address is <tt>null</tt>, creates an unbound socket.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
|
||||||
|
* unbound socket
|
||||||
|
* @throws SocketException if the socket could not be opened, or the socket
|
||||||
|
* could not bind to the specified local port
|
||||||
|
*/
|
||||||
|
public DelegatingDatagramSocket(SocketAddress bindaddr)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
super(bindaddr);
|
||||||
|
|
||||||
|
this.delegate = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds this <tt>DatagramSocket</tt> to a specific address and port.
|
||||||
|
* <p>
|
||||||
|
* If the address is <tt>null</tt>, then the system will pick up an
|
||||||
|
* ephemeral port and a valid local address to bind the socket.
|
||||||
|
*</p>
|
||||||
|
*
|
||||||
|
* @param addr the address and port to bind to
|
||||||
|
* @throws SocketException if any error happens during the bind, or if the
|
||||||
|
* socket is already bound
|
||||||
|
* @throws SecurityException if a security manager exists and its
|
||||||
|
* <tt>checkListen</tt> method doesn't allow the operation
|
||||||
|
* @throws IllegalArgumentException if <tt>addr</tt> is a
|
||||||
|
* <tt>SocketAddress</tt> subclass not supported by this socket
|
||||||
|
* @see DatagramSocket#bind(SocketAddress)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void bind(SocketAddress addr)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.bind(addr);
|
||||||
|
else
|
||||||
|
delegate.bind(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes this datagram socket.
|
||||||
|
* <p>
|
||||||
|
* Any thread currently blocked in {@link #receive(DatagramPacket)} upon
|
||||||
|
* this socket will throw a {@link SocketException}.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @see DatagramSocket#close()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close()
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.close();
|
||||||
|
else
|
||||||
|
delegate.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects the socket to a remote address for this socket. When a socket is
|
||||||
|
* connected to a remote address, packets may only be sent to or received
|
||||||
|
* from that address. By default a datagram socket is not connected.
|
||||||
|
* <p>
|
||||||
|
* If the remote destination to which the socket is connected does not
|
||||||
|
* exist, or is otherwise unreachable, and if an ICMP destination
|
||||||
|
* unreachable packet has been received for that address, then a subsequent
|
||||||
|
* call to {@link #send(DatagramPacket)} or {@link #receive(DatagramPacket)}
|
||||||
|
* may throw a <tt>PortUnreachableException</tt>. Note, there is no
|
||||||
|
* guarantee that the exception will be thrown.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param address the remote address for the socket
|
||||||
|
* @param port the remote port for the socket
|
||||||
|
* @throws IllegalArgumentException if the address is <tt>null</tt>, or the
|
||||||
|
* port is out of range
|
||||||
|
* @throws SecurityException if the caller is not allowed to send datagrams
|
||||||
|
* to and receive datagrams from the address and port
|
||||||
|
* @see DatagramSocket#connect(InetAddress, int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void connect(InetAddress address, int port)
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.connect(address, port);
|
||||||
|
else
|
||||||
|
delegate.connect(address, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects this socket to a remote socket address.
|
||||||
|
*
|
||||||
|
* @param addr the remote address
|
||||||
|
* @throws SocketException if the connect fails
|
||||||
|
* @throws IllegalArgumentException if <tt>addr</tt> is <tt>null</tt> or a
|
||||||
|
* <tt>SocketAddress</tt> subclass not supported by this socket
|
||||||
|
* @see DatagramSocket#connect(SocketAddress)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void connect(SocketAddress addr)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.connect(addr);
|
||||||
|
else
|
||||||
|
delegate.connect(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnects the socket. This does nothing if the socket is not connected.
|
||||||
|
*
|
||||||
|
* @see DatagramSocket#disconnect()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void disconnect()
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.disconnect();
|
||||||
|
else
|
||||||
|
delegate.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests if <tt>SO_BROADCAST</tt> is enabled.
|
||||||
|
*
|
||||||
|
* @return a <tt>boolean</tt> indicating whether or not
|
||||||
|
* <tt>SO_BROADCAST</tt> is enabled
|
||||||
|
* @throws SocketException if there is an error in the underlying protocol,
|
||||||
|
* such as an UDP error
|
||||||
|
* @see DatagramSocket#getBroadcast()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean getBroadcast()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null) ? super.getBroadcast() : delegate.getBroadcast();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the unique {@link DatagramChannel} object associated with this
|
||||||
|
* datagram socket, if any.
|
||||||
|
* <p>
|
||||||
|
* A datagram socket will have a channel if, and only if, the channel itself
|
||||||
|
* was created via the {@link DatagramChannel#open()} method
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return the datagram channel associated with this datagram socket, or
|
||||||
|
* <tt>null</tt> if this socket was not created for a channel
|
||||||
|
* @see DatagramSocket#getChannel()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public DatagramChannel getChannel()
|
||||||
|
{
|
||||||
|
return (delegate == null) ? super.getChannel() : delegate.getChannel();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the address to which this socket is connected. Returns
|
||||||
|
* <tt>null</tt> if the socket is not connected.
|
||||||
|
*
|
||||||
|
* @return the address to which this socket is connected
|
||||||
|
* @see DatagramSocket#getInetAddress()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public InetAddress getInetAddress()
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null)
|
||||||
|
? super.getInetAddress()
|
||||||
|
: delegate.getInetAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the local address to which the socket is bound.
|
||||||
|
* <p>
|
||||||
|
* If there is a security manager, its <tt>checkConnect</tt> method is first
|
||||||
|
* called with the host address and <tt>-1</tt> as its arguments to see if
|
||||||
|
* the operation is allowed.
|
||||||
|
*
|
||||||
|
* @return the local address to which the socket is bound, or an
|
||||||
|
* <tt>InetAddress</tt> representing any local address if either the socket
|
||||||
|
* is not bound, or the security manager <tt>checkConnect</tt> method does
|
||||||
|
* not allow the operation
|
||||||
|
* @see DatagramSocket#getLocalAddress()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public InetAddress getLocalAddress()
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null)
|
||||||
|
? super.getLocalAddress()
|
||||||
|
: delegate.getLocalAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the port number on the local host to which this socket is bound.
|
||||||
|
*
|
||||||
|
* @return the port number on the local host to which this socket is bound
|
||||||
|
* @see DatagramSocket#getLocalPort()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getLocalPort()
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null) ? super.getLocalPort() : delegate.getLocalPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the address of the endpoint this socket is bound to, or
|
||||||
|
* <tt>null</tt> if it is not bound yet.
|
||||||
|
*
|
||||||
|
* @return a <tt>SocketAddress</tt> representing the local endpoint of this
|
||||||
|
* socket, or <tt>null</tt> if it is not bound yet
|
||||||
|
* @see DatagramSocket#getLocalSocketAddress()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketAddress getLocalSocketAddress()
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null)
|
||||||
|
? super.getLocalSocketAddress()
|
||||||
|
: delegate.getLocalSocketAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the port for this socket. Returns <tt>-1</tt> if the socket is
|
||||||
|
* not connected.
|
||||||
|
*
|
||||||
|
* @return the port to which this socket is connected
|
||||||
|
* @see DatagramSocket#getPort()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getPort()
|
||||||
|
{
|
||||||
|
return (delegate == null) ? super.getPort() : delegate.getPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of the <tt>SO_RCVBUF</tt> option for this
|
||||||
|
* <tt>DatagramSocket</tt>, that is the buffer size used by the platform for
|
||||||
|
* input on this <tt>DatagramSocket</tt>.
|
||||||
|
*
|
||||||
|
* @return the value of the <tt>SO_RCVBUF</tt> option for this
|
||||||
|
* <tt>DatagramSocket</tt>
|
||||||
|
* @throws SocketException if there is an error in the underlying protocol,
|
||||||
|
* such as an UDP error
|
||||||
|
* @see DatagramSocket#getReceiveBufferSize()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getReceiveBufferSize()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null)
|
||||||
|
? super.getReceiveBufferSize()
|
||||||
|
: delegate.getReceiveBufferSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the address of the endpoint this socket is connected to, or
|
||||||
|
* <tt>null</tt> if it is unconnected.
|
||||||
|
*
|
||||||
|
* @return a <tt>SocketAddress</tt> representing the remote endpoint of this
|
||||||
|
* socket, or <tt>null</tt> if it is not connected yet
|
||||||
|
* @see DatagramSocket#getRemoteSocketAddress()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketAddress getRemoteSocketAddress()
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null)
|
||||||
|
? super.getRemoteSocketAddress()
|
||||||
|
: delegate.getRemoteSocketAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests if <tt>SO_REUSEADDR</tt> is enabled.
|
||||||
|
*
|
||||||
|
* @return a <tt>boolean</tt> indicating whether or not
|
||||||
|
* <tt>SO_REUSEADDR</tt> is enabled
|
||||||
|
* @throws SocketException if there is an error in the underlying protocol,
|
||||||
|
* such as an UDP error
|
||||||
|
* @see DatagramSocket#getReuseAddress()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean getReuseAddress()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate ==null)
|
||||||
|
? super.getReuseAddress()
|
||||||
|
: delegate.getReuseAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of the <tt>SO_SNDBUF</tt> option for this
|
||||||
|
* <tt>DatagramSocket</tt>, that is the buffer size used by the platform for
|
||||||
|
* output on this <tt>DatagramSocket</tt>.
|
||||||
|
*
|
||||||
|
* @return the value of the <tt>SO_SNDBUF</tt> option for this
|
||||||
|
* <tt>DatagramSocket</tt>
|
||||||
|
* @throws SocketException if there is an error in the underlying protocol,
|
||||||
|
* such as an UDP error
|
||||||
|
* @see DatagramSocket#getSendBufferSize()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getSendBufferSize()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null)
|
||||||
|
? super.getSendBufferSize()
|
||||||
|
: delegate.getSendBufferSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves setting for <tt>SO_TIMEOUT</tt>. Zero returned implies that
|
||||||
|
* the option is disabled (i.e., timeout of infinity).
|
||||||
|
*
|
||||||
|
* @return the setting for <tt>SO_TIMEOUT</tt>
|
||||||
|
* @throws SocketException if there is an error in the underlying protocol,
|
||||||
|
* such as an UDP error
|
||||||
|
* @see DatagramSocket#getSoTimeout()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getSoTimeout()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null) ? super.getSoTimeout() : delegate.getSoTimeout();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the traffic class or type-of-service in the IP datagram header for
|
||||||
|
* packets sent from this <tt>DatagramSocket</tt>.
|
||||||
|
* <p>
|
||||||
|
* As the underlying network implementation may ignore the traffic class or
|
||||||
|
* type-of-service set using {@link #setTrafficClass(int)} this method may
|
||||||
|
* return a different value than was previously set using the
|
||||||
|
* {@link #setTrafficClass(int)} method on this <tt>DatagramSocket</tt>.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return the traffic class or type-of-service already set
|
||||||
|
* @throws SocketException if there is an error obtaining the traffic class
|
||||||
|
* or type-of-service value
|
||||||
|
* @see DatagramSocket#getTrafficClass()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getTrafficClass()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(delegate == null)
|
||||||
|
? super.getTrafficClass()
|
||||||
|
: delegate.getTrafficClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the binding state of the socket.
|
||||||
|
*
|
||||||
|
* @return <tt>true</tt> if the socket successfully bound to an address;
|
||||||
|
* otherwise, <tt>false</tt>
|
||||||
|
* @see DatagramSocket#isBound()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isBound()
|
||||||
|
{
|
||||||
|
return (delegate == null) ? super.isBound() : delegate.isBound();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the socket is closed or not.
|
||||||
|
*
|
||||||
|
* @return <tt>true</tt> if the socket has been closed; otherwise,
|
||||||
|
* <tt>false</tt>
|
||||||
|
* @see DatagramSocket#isClosed()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isClosed()
|
||||||
|
{
|
||||||
|
return (delegate == null) ? super.isClosed() : this.isClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the connection state of the socket.
|
||||||
|
*
|
||||||
|
* @return <tt>true</tt> if the socket successfully connected to a server;
|
||||||
|
* otherwise, <tt>false</tt>
|
||||||
|
* @see DatagramSocket#isConnected()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isConnected()
|
||||||
|
{
|
||||||
|
return (delegate == null) ? super.isConnected() : this.isConnected();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receives a datagram packet from this socket. When this method returns,
|
||||||
|
* the <tt>DatagramPacket</tt>'s buffer is filled with the data received.
|
||||||
|
* The datagram packet also contains the sender's IP address, and the port
|
||||||
|
* number on the sender's machine.
|
||||||
|
* <p>
|
||||||
|
* This method blocks until a datagram is received. The <tt>length</tt>
|
||||||
|
* field of the datagram packet object contains the length of the received
|
||||||
|
* message. If the message is longer than the packet's length, the message
|
||||||
|
* is truncated.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* If there is a security manager, a packet cannot be received if the
|
||||||
|
* security manager's <tt>checkAccept</tt> method does not allow it.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
|
||||||
|
* data
|
||||||
|
* @throws IOException if an I/O error occurs
|
||||||
|
* @see DatagramSocket#receive(DatagramPacket)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void receive(DatagramPacket p)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.receive(p);
|
||||||
|
else
|
||||||
|
delegate.receive(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a datagram packet from this socket. The <tt>DatagramPacket</tt>
|
||||||
|
* includes information indicating the data to be sent, its length, the IP
|
||||||
|
* address of the remote host, and the port number on the remote host.
|
||||||
|
* <p>
|
||||||
|
* If there is a security manager, and the socket is not currently connected
|
||||||
|
* to a remote address, this method first performs some security checks.
|
||||||
|
* First, if <tt>p.getAddress().isMulticastAddress()</tt> is true, this
|
||||||
|
* method calls the security manager's <tt>checkMulticast</tt> method with
|
||||||
|
* <tt>p.getAddress()</tt> as its argument. If the evaluation of that
|
||||||
|
* expression is <tt>false</tt>, this method instead calls the security
|
||||||
|
* manager's <tt>checkConnect</tt> method with arguments
|
||||||
|
* <tt>p.getAddress().getHostAddress()</tt> and <tt>p.getPort()</tt>. Each
|
||||||
|
* call to a security manager method could result in a
|
||||||
|
* <tt>SecurityException</tt> if the operation is not allowed.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param p the <tt>DatagramPacket</tt> to be sent
|
||||||
|
* @throws IOException if an I/O error occurs
|
||||||
|
* @see DatagramSocket#send(DatagramPacket)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void send(DatagramPacket p)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.send(p);
|
||||||
|
else
|
||||||
|
delegate.send(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables/disables <tt>SO_BROADCAST</tt>.
|
||||||
|
*
|
||||||
|
* @param on whether or not to have broadcast turned on
|
||||||
|
* @throws SocketException if there is an error in the underlying protocol,
|
||||||
|
* such as an UDP error
|
||||||
|
* @see DatagramSocket#setBroadcast(boolean)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setBroadcast(boolean on)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.setBroadcast(on);
|
||||||
|
else
|
||||||
|
delegate.setBroadcast(on);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the <tt>SO_RCVBUF</tt> option to the specified value for this
|
||||||
|
* <tt>DatagramSocket</tt>. The <tt>SO_RCVBUF</tt> option is used by the
|
||||||
|
* network implementation as a hint to size the underlying network I/O
|
||||||
|
* buffers. The <tt>SO_RCVBUF</tt> setting may also be used by the network
|
||||||
|
* implementation to determine the maximum size of the packet that can be
|
||||||
|
* received on this socket.
|
||||||
|
* <p>
|
||||||
|
* Because <tt>SO_RCVBUF</tt> is a hint, applications that want to verify
|
||||||
|
* what size the buffers were set to should call
|
||||||
|
* {@link #getReceiveBufferSize()}.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param size the size to which to set the receive buffer size. The value
|
||||||
|
* must be greater than zero
|
||||||
|
* @throws SocketException if there is an error in the underlying protocol,
|
||||||
|
* such as an UDP error
|
||||||
|
* @throws IllegalArgumentException if the value is zero or is negative
|
||||||
|
* @see DatagramSocket#setReceiveBufferSize(int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setReceiveBufferSize(int size)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.setReceiveBufferSize(size);
|
||||||
|
else
|
||||||
|
delegate.setReceiveBufferSize(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables/disables the <tt>SO_REUSEADDR</tt> socket option.
|
||||||
|
*
|
||||||
|
* @param on whether to enable or disable the <tt>SO_REUSEADDR</tt> socket
|
||||||
|
* option
|
||||||
|
* @throws SocketException if an error occurs enabling or disabling the
|
||||||
|
* <tt>SO_RESUEADDR</tt> socket option, or the socket is closed
|
||||||
|
* @see DatagramSocket#setReuseAddress(boolean)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setReuseAddress(boolean on)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.setReuseAddress(on);
|
||||||
|
else
|
||||||
|
delegate.setReuseAddress(on);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the <tt>SO_SNDBUF</tt> option to the specified value for this
|
||||||
|
* <tt>DatagramSocket</tt>. The <tt>SO_SNDBUF</tt> option is used by the
|
||||||
|
* network implementation as a hint to size the underlying network I/O
|
||||||
|
* buffers. The <tt>SO_SNDBUF</tt> setting may also be used by the network
|
||||||
|
* implementation to determine the maximum size of the packet that can be
|
||||||
|
* sent on this socket.
|
||||||
|
* <p>
|
||||||
|
* As <tt>SO_SNDBUF</tt> is a hint, applications that want to verify what
|
||||||
|
* size the buffer is should call {@link #getSendBufferSize()}.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* Increasing the buffer size may allow multiple outgoing packets to be
|
||||||
|
* queued by the network implementation when the send rate is high.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param size the size to which to set the send buffer size. The value must
|
||||||
|
* be greater than zero
|
||||||
|
* @throws SocketException if there is an error in the underlying protocol,
|
||||||
|
* such as an UDP error
|
||||||
|
* @throws IllegalArgumentException if the value is zero or is negative
|
||||||
|
* @see DatagramSocket#setSendBufferSize(int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setSendBufferSize(int size)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.setSendBufferSize(size);
|
||||||
|
else
|
||||||
|
delegate.setSendBufferSize(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables/disables <tt>SO_TIMEOUT</tt> with the specified timeout, in
|
||||||
|
* milliseconds. With this option set to a non-zero timeout, a call to
|
||||||
|
* {@link #receive(DatagramPacket)} for this <tt>DatagramSocket</tt> will
|
||||||
|
* block for only this amount of time. If the timeout expires, a
|
||||||
|
* <tt>SocketTimeoutException</tt> is raised, though the
|
||||||
|
* <tt>DatagramSocket</tt> is still valid. The option must be enabled prior
|
||||||
|
* to entering the blocking operation to have effect. The timeout must be
|
||||||
|
* greater than zero. A timeout of zero is interpreted as an infinite
|
||||||
|
* timeout.
|
||||||
|
*
|
||||||
|
* @param timeout the specified timeout in milliseconds
|
||||||
|
* @throws SocketException if there is an error in the underlying protocol,
|
||||||
|
* such as an UDP error
|
||||||
|
* @see DatagramSocket#setSoTimeout(int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setSoTimeout(int timeout)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.setSoTimeout(timeout);
|
||||||
|
else
|
||||||
|
delegate.setSoTimeout(timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets traffic class or type-of-service octet in the IP datagram header for
|
||||||
|
* datagrams sent from this <tt>DatagramSocket</tt>. As the underlying
|
||||||
|
* network implementation may ignore this value applications should consider
|
||||||
|
* it a hint.
|
||||||
|
*
|
||||||
|
* @param tc an <tt>int</tt> value for the bitset
|
||||||
|
* @throws SocketException if there is an error setting the traffic class or
|
||||||
|
* type-of-service
|
||||||
|
* @see DatagramSocket#setTrafficClass(int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setTrafficClass(int tc)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
super.setTrafficClass(tc);
|
||||||
|
else
|
||||||
|
delegate.setTrafficClass(tc);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,7 +9,6 @@ package org.ice4j.socket;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.channels.*;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,7 +24,7 @@ import java.util.*;
|
|||||||
* @author Lubomir Marinov
|
* @author Lubomir Marinov
|
||||||
*/
|
*/
|
||||||
public class MultiplexedDatagramSocket
|
public class MultiplexedDatagramSocket
|
||||||
extends DatagramSocket
|
extends DelegatingDatagramSocket
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -76,7 +75,7 @@ public class MultiplexedDatagramSocket
|
|||||||
* yet. That is why MultiplexedDatagramSocket does not currently support
|
* yet. That is why MultiplexedDatagramSocket does not currently support
|
||||||
* bind at construction time.
|
* bind at construction time.
|
||||||
*/
|
*/
|
||||||
super((SocketAddress) null);
|
super(multiplexing);
|
||||||
|
|
||||||
if (multiplexing == null)
|
if (multiplexing == null)
|
||||||
throw new NullPointerException("multiplexing");
|
throw new NullPointerException("multiplexing");
|
||||||
@ -85,29 +84,6 @@ public class MultiplexedDatagramSocket
|
|||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Binds this <tt>DatagramSocket</tt> to a specific address and port.
|
|
||||||
* <p>
|
|
||||||
* If the address is <tt>null</tt>, then the system will pick up an
|
|
||||||
* ephemeral port and a valid local address to bind the socket.
|
|
||||||
*</p>
|
|
||||||
*
|
|
||||||
* @param addr the address and port to bind to
|
|
||||||
* @throws SocketException if any error happens during the bind, or if the
|
|
||||||
* socket is already bound
|
|
||||||
* @throws SecurityException if a security manager exists and its
|
|
||||||
* <tt>checkListen</tt> method doesn't allow the operation
|
|
||||||
* @throws IllegalArgumentException if <tt>addr</tt> is a
|
|
||||||
* <tt>SocketAddress</tt> subclass not supported by this socket
|
|
||||||
* @see DatagramSocket#bind(SocketAddress)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void bind(SocketAddress addr)
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
multiplexing.bind(addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes this datagram socket.
|
* Closes this datagram socket.
|
||||||
* <p>
|
* <p>
|
||||||
@ -123,94 +99,6 @@ public class MultiplexedDatagramSocket
|
|||||||
multiplexing.close(this);
|
multiplexing.close(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Connects the socket to a remote address for this socket. When a socket is
|
|
||||||
* connected to a remote address, packets may only be sent to or received
|
|
||||||
* from that address. By default a datagram socket is not connected.
|
|
||||||
* <p>
|
|
||||||
* If the remote destination to which the socket is connected does not
|
|
||||||
* exist, or is otherwise unreachable, and if an ICMP destination
|
|
||||||
* unreachable packet has been received for that address, then a subsequent
|
|
||||||
* call to {@link #send(DatagramPacket)} or {@link #receive(DatagramPacket)}
|
|
||||||
* may throw a <tt>PortUnreachableException</tt>. Note, there is no
|
|
||||||
* guarantee that the exception will be thrown.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param address the remote address for the socket
|
|
||||||
* @param port the remote port for the socket
|
|
||||||
* @throws IllegalArgumentException if the address is <tt>null</tt>, or the
|
|
||||||
* port is out of range
|
|
||||||
* @throws SecurityException if the caller is not allowed to send datagrams
|
|
||||||
* to and receive datagrams from the address and port
|
|
||||||
* @see DatagramSocket#connect(InetAddress, int)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void connect(InetAddress address, int port)
|
|
||||||
{
|
|
||||||
multiplexing.connect(address, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connects this socket to a remote socket address.
|
|
||||||
*
|
|
||||||
* @param addr the remote address
|
|
||||||
* @throws SocketException if the connect fails
|
|
||||||
* @throws IllegalArgumentException if <tt>addr</tt> is <tt>null</tt> or a
|
|
||||||
* <tt>SocketAddress</tt> subclass not supported by this socket
|
|
||||||
* @see DatagramSocket#connect(SocketAddress)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void connect(SocketAddress addr)
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
multiplexing.connect(addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Disconnects the socket. This does nothing if the socket is not connected.
|
|
||||||
*
|
|
||||||
* @see DatagramSocket#disconnect()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void disconnect()
|
|
||||||
{
|
|
||||||
multiplexing.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests if <tt>SO_BROADCAST</tt> is enabled.
|
|
||||||
*
|
|
||||||
* @return a <tt>boolean</tt> indicating whether or not
|
|
||||||
* <tt>SO_BROADCAST</tt> is enabled
|
|
||||||
* @throws SocketException if there is an error in the underlying protocol,
|
|
||||||
* such as an UDP error
|
|
||||||
* @see DatagramSocket#getBroadcast()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean getBroadcast()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return multiplexing.getBroadcast();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the unique {@link DatagramChannel} object associated with this
|
|
||||||
* datagram socket, if any.
|
|
||||||
* <p>
|
|
||||||
* A datagram socket will have a channel if, and only if, the channel itself
|
|
||||||
* was created via the {@link DatagramChannel#open()} method
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @return the datagram channel associated with this datagram socket, or
|
|
||||||
* <tt>null</tt> if this socket was not created for a channel
|
|
||||||
* @see DatagramSocket#getChannel()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public DatagramChannel getChannel()
|
|
||||||
{
|
|
||||||
return multiplexing.getChannel();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the <tt>DatagramPacketFilter</tt> which determines which
|
* Gets the <tt>DatagramPacketFilter</tt> which determines which
|
||||||
* <tt>DatagramPacket</tt>s read from the network are to be received through
|
* <tt>DatagramPacket</tt>s read from the network are to be received through
|
||||||
@ -225,220 +113,6 @@ public class MultiplexedDatagramSocket
|
|||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the address to which this socket is connected. Returns
|
|
||||||
* <tt>null</tt> if the socket is not connected.
|
|
||||||
*
|
|
||||||
* @return the address to which this socket is connected
|
|
||||||
* @see DatagramSocket#getInetAddress()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public InetAddress getInetAddress()
|
|
||||||
{
|
|
||||||
return multiplexing.getInetAddress();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the local address to which the socket is bound.
|
|
||||||
* <p>
|
|
||||||
* If there is a security manager, its <tt>checkConnect</tt> method is first
|
|
||||||
* called with the host address and <tt>-1</tt> as its arguments to see if
|
|
||||||
* the operation is allowed.
|
|
||||||
*
|
|
||||||
* @return the local address to which the socket is bound, or an
|
|
||||||
* <tt>InetAddress</tt> representing any local address if either the socket
|
|
||||||
* is not bound, or the security manager <tt>checkConnect</tt> method does
|
|
||||||
* not allow the operation
|
|
||||||
* @see DatagramSocket#getLocalAddress()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public InetAddress getLocalAddress()
|
|
||||||
{
|
|
||||||
return multiplexing.getLocalAddress();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the port number on the local host to which this socket is bound.
|
|
||||||
*
|
|
||||||
* @return the port number on the local host to which this socket is bound
|
|
||||||
* @see DatagramSocket#getLocalPort()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getLocalPort()
|
|
||||||
{
|
|
||||||
return multiplexing.getLocalPort();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the address of the endpoint this socket is bound to, or
|
|
||||||
* <tt>null</tt> if it is not bound yet.
|
|
||||||
*
|
|
||||||
* @return a <tt>SocketAddress</tt> representing the local endpoint of this
|
|
||||||
* socket, or <tt>null</tt> if it is not bound yet
|
|
||||||
* @see DatagramSocket#getLocalSocketAddress()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public SocketAddress getLocalSocketAddress()
|
|
||||||
{
|
|
||||||
return multiplexing.getLocalSocketAddress();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the port for this socket. Returns <tt>-1</tt> if the socket is
|
|
||||||
* not connected.
|
|
||||||
*
|
|
||||||
* @return the port to which this socket is connected
|
|
||||||
* @see DatagramSocket#getPort()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getPort()
|
|
||||||
{
|
|
||||||
return multiplexing.getPort();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the value of the <tt>SO_RCVBUF</tt> option for this
|
|
||||||
* <tt>DatagramSocket</tt>, that is the buffer size used by the platform for
|
|
||||||
* input on this <tt>DatagramSocket</tt>.
|
|
||||||
*
|
|
||||||
* @return the value of the <tt>SO_RCVBUF</tt> option for this
|
|
||||||
* <tt>DatagramSocket</tt>
|
|
||||||
* @throws SocketException if there is an error in the underlying protocol,
|
|
||||||
* such as an UDP error
|
|
||||||
* @see DatagramSocket#getReceiveBufferSize()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getReceiveBufferSize()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return multiplexing.getReceiveBufferSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the address of the endpoint this socket is connected to, or
|
|
||||||
* <tt>null</tt> if it is unconnected.
|
|
||||||
*
|
|
||||||
* @return a <tt>SocketAddress</tt> representing the remote endpoint of this
|
|
||||||
* socket, or <tt>null</tt> if it is not connected yet
|
|
||||||
* @see DatagramSocket#getRemoteSocketAddress()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public SocketAddress getRemoteSocketAddress()
|
|
||||||
{
|
|
||||||
return multiplexing.getRemoteSocketAddress();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests if <tt>SO_REUSEADDR</tt> is enabled.
|
|
||||||
*
|
|
||||||
* @return a <tt>boolean</tt> indicating whether or not
|
|
||||||
* <tt>SO_REUSEADDR</tt> is enabled
|
|
||||||
* @throws SocketException if there is an error in the underlying protocol,
|
|
||||||
* such as an UDP error
|
|
||||||
* @see DatagramSocket#getReuseAddress()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean getReuseAddress()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return multiplexing.getReuseAddress();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the value of the <tt>SO_SNDBUF</tt> option for this
|
|
||||||
* <tt>DatagramSocket</tt>, that is the buffer size used by the platform for
|
|
||||||
* output on this <tt>DatagramSocket</tt>.
|
|
||||||
*
|
|
||||||
* @return the value of the <tt>SO_SNDBUF</tt> option for this
|
|
||||||
* <tt>DatagramSocket</tt>
|
|
||||||
* @throws SocketException if there is an error in the underlying protocol,
|
|
||||||
* such as an UDP error
|
|
||||||
* @see DatagramSocket#getSendBufferSize()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getSendBufferSize()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return multiplexing.getSendBufferSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves setting for <tt>SO_TIMEOUT</tt>. Zero returned implies that
|
|
||||||
* the option is disabled (i.e., timeout of infinity).
|
|
||||||
*
|
|
||||||
* @return the setting for <tt>SO_TIMEOUT</tt>
|
|
||||||
* @throws SocketException if there is an error in the underlying protocol,
|
|
||||||
* such as an UDP error
|
|
||||||
* @see DatagramSocket#getSoTimeout()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getSoTimeout()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return multiplexing.getSoTimeout();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the traffic class or type-of-service in the IP datagram header for
|
|
||||||
* packets sent from this <tt>DatagramSocket</tt>.
|
|
||||||
* <p>
|
|
||||||
* As the underlying network implementation may ignore the traffic class or
|
|
||||||
* type-of-service set using {@link #setTrafficClass(int)} this method may
|
|
||||||
* return a different value than was previously set using the
|
|
||||||
* {@link #setTrafficClass(int)} method on this <tt>DatagramSocket</tt>.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @return the traffic class or type-of-service already set
|
|
||||||
* @throws SocketException if there is an error obtaining the traffic class
|
|
||||||
* or type-of-service value
|
|
||||||
* @see DatagramSocket#getTrafficClass()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getTrafficClass()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return multiplexing.getTrafficClass();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the binding state of the socket.
|
|
||||||
*
|
|
||||||
* @return <tt>true</tt> if the socket successfully bound to an address;
|
|
||||||
* otherwise, <tt>false</tt>
|
|
||||||
* @see DatagramSocket#isBound()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isBound()
|
|
||||||
{
|
|
||||||
return multiplexing.isBound();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns whether the socket is closed or not.
|
|
||||||
*
|
|
||||||
* @return <tt>true</tt> if the socket has been closed; otherwise,
|
|
||||||
* <tt>false</tt>
|
|
||||||
* @see DatagramSocket#isClosed()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isClosed()
|
|
||||||
{
|
|
||||||
return multiplexing.isClosed();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the connection state of the socket.
|
|
||||||
*
|
|
||||||
* @return <tt>true</tt> if the socket successfully connected to a server;
|
|
||||||
* otherwise, <tt>false</tt>
|
|
||||||
* @see DatagramSocket#isConnected()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isConnected()
|
|
||||||
{
|
|
||||||
return multiplexing.isConnected();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Receives a datagram packet from this socket. When this method returns,
|
* Receives a datagram packet from this socket. When this method returns,
|
||||||
* the <tt>DatagramPacket</tt>'s buffer is filled with the data received.
|
* the <tt>DatagramPacket</tt>'s buffer is filled with the data received.
|
||||||
@ -466,161 +140,4 @@ public class MultiplexedDatagramSocket
|
|||||||
{
|
{
|
||||||
multiplexing.receive(this, p);
|
multiplexing.receive(this, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a datagram packet from this socket. The <tt>DatagramPacket</tt>
|
|
||||||
* includes information indicating the data to be sent, its length, the IP
|
|
||||||
* address of the remote host, and the port number on the remote host.
|
|
||||||
* <p>
|
|
||||||
* If there is a security manager, and the socket is not currently connected
|
|
||||||
* to a remote address, this method first performs some security checks.
|
|
||||||
* First, if <tt>p.getAddress().isMulticastAddress()</tt> is true, this
|
|
||||||
* method calls the security manager's <tt>checkMulticast</tt> method with
|
|
||||||
* <tt>p.getAddress()</tt> as its argument. If the evaluation of that
|
|
||||||
* expression is <tt>false</tt>, this method instead calls the security
|
|
||||||
* manager's <tt>checkConnect</tt> method with arguments
|
|
||||||
* <tt>p.getAddress().getHostAddress()</tt> and <tt>p.getPort()</tt>. Each
|
|
||||||
* call to a security manager method could result in a
|
|
||||||
* <tt>SecurityException</tt> if the operation is not allowed.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param p the <tt>DatagramPacket</tt> to be sent
|
|
||||||
* @throws IOException if an I/O error occurs
|
|
||||||
* @see DatagramSocket#send(DatagramPacket)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void send(DatagramPacket p)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
multiplexing.send(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enables/disables <tt>SO_BROADCAST</tt>.
|
|
||||||
*
|
|
||||||
* @param on whether or not to have broadcast turned on
|
|
||||||
* @throws SocketException if there is an error in the underlying protocol,
|
|
||||||
* such as an UDP error
|
|
||||||
* @see DatagramSocket#setBroadcast(boolean)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setBroadcast(boolean on)
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
multiplexing.setBroadcast(on);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the <tt>SO_RCVBUF</tt> option to the specified value for this
|
|
||||||
* <tt>DatagramSocket</tt>. The <tt>SO_RCVBUF</tt> option is used by the
|
|
||||||
* network implementation as a hint to size the underlying network I/O
|
|
||||||
* buffers. The <tt>SO_RCVBUF</tt> setting may also be used by the network
|
|
||||||
* implementation to determine the maximum size of the packet that can be
|
|
||||||
* received on this socket.
|
|
||||||
* <p>
|
|
||||||
* Because <tt>SO_RCVBUF</tt> is a hint, applications that want to verify
|
|
||||||
* what size the buffers were set to should call
|
|
||||||
* {@link #getReceiveBufferSize()}.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param size the size to which to set the receive buffer size. The value
|
|
||||||
* must be greater than zero
|
|
||||||
* @throws SocketException if there is an error in the underlying protocol,
|
|
||||||
* such as an UDP error
|
|
||||||
* @throws IllegalArgumentException if the value is zero or is negative
|
|
||||||
* @see DatagramSocket#setReceiveBufferSize(int)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setReceiveBufferSize(int size)
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
multiplexing.setReceiveBufferSize(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enables/disables the <tt>SO_REUSEADDR</tt> socket option.
|
|
||||||
*
|
|
||||||
* @param on whether to enable or disable the <tt>SO_REUSEADDR</tt> socket
|
|
||||||
* option
|
|
||||||
* @throws SocketException if an error occurs enabling or disabling the
|
|
||||||
* <tt>SO_RESUEADDR</tt> socket option, or the socket is closed
|
|
||||||
* @see DatagramSocket#setReuseAddress(boolean)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setReuseAddress(boolean on)
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
multiplexing.setReuseAddress(on);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the <tt>SO_SNDBUF</tt> option to the specified value for this
|
|
||||||
* <tt>DatagramSocket</tt>. The <tt>SO_SNDBUF</tt> option is used by the
|
|
||||||
* network implementation as a hint to size the underlying network I/O
|
|
||||||
* buffers. The <tt>SO_SNDBUF</tt> setting may also be used by the network
|
|
||||||
* implementation to determine the maximum size of the packet that can be
|
|
||||||
* sent on this socket.
|
|
||||||
* <p>
|
|
||||||
* As <tt>SO_SNDBUF</tt> is a hint, applications that want to verify what
|
|
||||||
* size the buffer is should call {@link #getSendBufferSize()}.
|
|
||||||
* </p>
|
|
||||||
* <p>
|
|
||||||
* Increasing the buffer size may allow multiple outgoing packets to be
|
|
||||||
* queued by the network implementation when the send rate is high.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param size the size to which to set the send buffer size. The value must
|
|
||||||
* be greater than zero
|
|
||||||
* @throws SocketException if there is an error in the underlying protocol,
|
|
||||||
* such as an UDP error
|
|
||||||
* @throws IllegalArgumentException if the value is zero or is negative
|
|
||||||
* @see DatagramSocket#setSendBufferSize(int)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setSendBufferSize(int size)
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
multiplexing.setSendBufferSize(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enables/disables <tt>SO_TIMEOUT</tt> with the specified timeout, in
|
|
||||||
* milliseconds. With this option set to a non-zero timeout, a call to
|
|
||||||
* {@link #receive(DatagramPacket)} for this <tt>DatagramSocket</tt> will
|
|
||||||
* block for only this amount of time. If the timeout expires, a
|
|
||||||
* <tt>SocketTimeoutException</tt> is raised, though the
|
|
||||||
* <tt>DatagramSocket</tt> is still valid. The option must be enabled prior
|
|
||||||
* to entering the blocking operation to have effect. The timeout must be
|
|
||||||
* greater than zero. A timeout of zero is interpreted as an infinite
|
|
||||||
* timeout.
|
|
||||||
*
|
|
||||||
* @param timeout the specified timeout in milliseconds
|
|
||||||
* @throws SocketException if there is an error in the underlying protocol,
|
|
||||||
* such as an UDP error
|
|
||||||
* @see DatagramSocket#setSoTimeout(int)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setSoTimeout(int timeout)
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
multiplexing.setSoTimeout(timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets traffic class or type-of-service octet in the IP datagram header for
|
|
||||||
* datagrams sent from this <tt>DatagramSocket</tt>. As the underlying
|
|
||||||
* network implementation may ignore this value applications should consider
|
|
||||||
* it a hint.
|
|
||||||
*
|
|
||||||
* @param tc an <tt>int</tt> value for the bitset
|
|
||||||
* @throws SocketException if there is an error setting the traffic class or
|
|
||||||
* type-of-service
|
|
||||||
* @see DatagramSocket#setTrafficClass(int)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setTrafficClass(int tc)
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
multiplexing.setTrafficClass(tc);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ package org.ice4j.socket;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.logging.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a <tt>DatagramSocket</tt> which allows filtering
|
* Represents a <tt>DatagramSocket</tt> which allows filtering
|
||||||
@ -24,6 +25,13 @@ public class MultiplexingDatagramSocket
|
|||||||
extends SafeCloseDatagramSocket
|
extends SafeCloseDatagramSocket
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>Logger</tt> used by the <tt>MultiplexingDatagramSocket</tt> class
|
||||||
|
* and its instances for logging output.
|
||||||
|
*/
|
||||||
|
private static final Logger logger
|
||||||
|
= Logger.getLogger(MultiplexingDatagramSocket.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The constant which represents an empty array with
|
* The constant which represents an empty array with
|
||||||
* <tt>MultiplexedDatagramSocket</tt> element type. Explicitly defined in
|
* <tt>MultiplexedDatagramSocket</tt> element type. Explicitly defined in
|
||||||
@ -84,23 +92,18 @@ public class MultiplexingDatagramSocket
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
|
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
|
||||||
* to enable <tt>DatagramPacket</tt> filtering, bound to the specified local
|
* to enable <tt>DatagramPacket</tt> filtering on a specific
|
||||||
* socket address.
|
* <tt>DatagramSocket</tt>.
|
||||||
* <p>
|
|
||||||
* If the specified local socket address is <tt>null</tt>, creates an
|
|
||||||
* unbound socket.
|
|
||||||
* </p>
|
|
||||||
*
|
*
|
||||||
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
|
* @param delegate the <tt>DatagramSocket</tt> on which
|
||||||
* unbound socket
|
* <tt>DatagramPacket</tt> filtering is to be enabled by the new instance
|
||||||
* @throws SocketException if the socket could not be opened, or the socket
|
* @throws SocketException if anything goes wrong while initializing the new
|
||||||
* could not bind to the specified local port
|
* instance
|
||||||
* @see DatagramSocket#DatagramSocket(SocketAddress)
|
|
||||||
*/
|
*/
|
||||||
public MultiplexingDatagramSocket(SocketAddress bindaddr)
|
public MultiplexingDatagramSocket(DatagramSocket delegate)
|
||||||
throws SocketException
|
throws SocketException
|
||||||
{
|
{
|
||||||
super(bindaddr);
|
super(delegate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -139,6 +142,27 @@ public class MultiplexingDatagramSocket
|
|||||||
super(port, laddr);
|
super(port, laddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
|
||||||
|
* to enable <tt>DatagramPacket</tt> filtering, bound to the specified local
|
||||||
|
* socket address.
|
||||||
|
* <p>
|
||||||
|
* If the specified local socket address is <tt>null</tt>, creates an
|
||||||
|
* unbound socket.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
|
||||||
|
* unbound socket
|
||||||
|
* @throws SocketException if the socket could not be opened, or the socket
|
||||||
|
* could not bind to the specified local port
|
||||||
|
* @see DatagramSocket#DatagramSocket(SocketAddress)
|
||||||
|
*/
|
||||||
|
public MultiplexingDatagramSocket(SocketAddress bindaddr)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
super(bindaddr);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>DatagramPacket</tt> instance which is a clone of a
|
* Initializes a new <tt>DatagramPacket</tt> instance which is a clone of a
|
||||||
* specific <tt>DatagramPacket</tt> i.e. the properties of the clone
|
* specific <tt>DatagramPacket</tt> i.e. the properties of the clone
|
||||||
@ -149,20 +173,17 @@ public class MultiplexingDatagramSocket
|
|||||||
* @return a new <tt>DatagramPacket</tt> instance which is a clone of the
|
* @return a new <tt>DatagramPacket</tt> instance which is a clone of the
|
||||||
* specified <tt>DatagramPacket</tt>
|
* specified <tt>DatagramPacket</tt>
|
||||||
*/
|
*/
|
||||||
static DatagramPacket clone(DatagramPacket p)
|
public static DatagramPacket clone(DatagramPacket p)
|
||||||
{
|
{
|
||||||
synchronized (p)
|
synchronized (p)
|
||||||
{
|
{
|
||||||
byte[] pData = p.getData();
|
byte[] pData = p.getData();
|
||||||
byte[] cData = (pData == null) ? pData : pData.clone();
|
byte[] cData = (pData == null) ? null : pData.clone();
|
||||||
|
|
||||||
return
|
return
|
||||||
new DatagramPacket(
|
new DatagramPacket(
|
||||||
cData,
|
cData, p.getOffset(), p.getLength(),
|
||||||
p.getOffset(),
|
p.getAddress(), p.getPort());
|
||||||
p.getLength(),
|
|
||||||
p.getAddress(),
|
|
||||||
p.getPort());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,12 +210,9 @@ public class MultiplexingDatagramSocket
|
|||||||
= new MultiplexedDatagramSocket[socketCount - 1];
|
= new MultiplexedDatagramSocket[socketCount - 1];
|
||||||
|
|
||||||
System.arraycopy(sockets, 0, newSockets, 0, i);
|
System.arraycopy(sockets, 0, newSockets, 0, i);
|
||||||
System
|
System.arraycopy(
|
||||||
.arraycopy(
|
sockets, i + 1,
|
||||||
sockets,
|
newSockets, i,
|
||||||
i + 1,
|
|
||||||
newSockets,
|
|
||||||
i,
|
|
||||||
newSockets.length - i);
|
newSockets.length - i);
|
||||||
sockets = newSockets;
|
sockets = newSockets;
|
||||||
}
|
}
|
||||||
@ -212,13 +230,44 @@ public class MultiplexingDatagramSocket
|
|||||||
* @param dest the <tt>DatagramPacket</tt> which is to have its properties
|
* @param dest the <tt>DatagramPacket</tt> which is to have its properties
|
||||||
* set to the value of the respective properties of <tt>src</tt>
|
* set to the value of the respective properties of <tt>src</tt>
|
||||||
*/
|
*/
|
||||||
static void copy(DatagramPacket src, DatagramPacket dest)
|
public static void copy(DatagramPacket src, DatagramPacket dest)
|
||||||
{
|
{
|
||||||
synchronized (dest)
|
synchronized (dest)
|
||||||
{
|
{
|
||||||
dest.setAddress(src.getAddress());
|
dest.setAddress(src.getAddress());
|
||||||
dest.setData(src.getData(), src.getOffset(), src.getLength());
|
|
||||||
dest.setPort(src.getPort());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,9 +9,18 @@ package org.ice4j.socket;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.logging.*;
|
||||||
|
|
||||||
|
import org.ice4j.*;
|
||||||
|
import org.ice4j.attribute.*;
|
||||||
|
import org.ice4j.ice.*;
|
||||||
|
import org.ice4j.ice.harvest.*;
|
||||||
|
import org.ice4j.message.*;
|
||||||
|
import org.ice4j.stack.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an application-purposed (as opposed to a ICE-specific)
|
* Represents an application-purposed (as opposed to an ICE-specific)
|
||||||
* <tt>DatagramSocket</tt> for a <tt>RelayedCandidate</tt> harvested by a
|
* <tt>DatagramSocket</tt> for a <tt>RelayedCandidate</tt> harvested by a
|
||||||
* <tt>TurnCandidateHarvest</tt> (and its associated
|
* <tt>TurnCandidateHarvest</tt> (and its associated
|
||||||
* <tt>TurnCandidateHarvester</tt>, of course).
|
* <tt>TurnCandidateHarvester</tt>, of course).
|
||||||
@ -22,18 +31,104 @@ import java.net.*;
|
|||||||
* @author Lubomir Marinov
|
* @author Lubomir Marinov
|
||||||
*/
|
*/
|
||||||
public class RelayedCandidateDatagramSocket
|
public class RelayedCandidateDatagramSocket
|
||||||
extends SafeCloseDatagramSocket
|
extends DatagramSocket
|
||||||
|
implements MessageEventHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>RelayedCandidateDatagramSocket</tt> instance.
|
* The <tt>Logger</tt> used by the <tt>RelayedCandidateDatagramSocket</tt>
|
||||||
|
* class and its instances for logging output.
|
||||||
|
*/
|
||||||
|
private static final Logger logger
|
||||||
|
= Logger.getLogger(RelayedCandidateDatagramSocket.class.getName());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The lifetime in milliseconds of a TURN permission created using a
|
||||||
|
* CreatePermission request.
|
||||||
|
*/
|
||||||
|
private static final long PERMISSION_LIFETIME = 300 /* seconds */ * 1000L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The time in milliseconds before a TURN permission expires that a
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> is to try to reinstall it.
|
||||||
|
*/
|
||||||
|
private static final long PERMISSION_LIFETIME_LEEWAY
|
||||||
|
= 60 /* seconds */ * 1000L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of per-peer <tt>Channel</tt>s through which this
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt>s relays data send to it to
|
||||||
|
* peer <tt>TransportAddress</tt>es.
|
||||||
|
*/
|
||||||
|
private final List<Channel> channels = new LinkedList<Channel>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The indicator which determines whether this instance has started
|
||||||
|
* executing or has executed its {@link #close()} method.
|
||||||
|
*/
|
||||||
|
private boolean closed = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>DatagramPacket</tt>s which are to be received through this
|
||||||
|
* <tt>DatagramSocket</tt> upon calls to its
|
||||||
|
* {@link #receive(DatagramPacket)} method. They have been received from the
|
||||||
|
* TURN server in the form of Data indications.
|
||||||
|
*/
|
||||||
|
private final List<DatagramPacket> packetsToReceive
|
||||||
|
= new LinkedList<DatagramPacket>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>DatagramSocket</tt>s which have been sent through this
|
||||||
|
* <tt>DatagramSocket</tt> using its {@link #send(DatagramPacket)} method
|
||||||
|
* and which are to be relayed through its associated TURN server in the
|
||||||
|
* form of Send indications.
|
||||||
|
*/
|
||||||
|
private final List<DatagramPacket> packetsToSend
|
||||||
|
= new LinkedList<DatagramPacket>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>RelayedCandidate</tt> which uses this instance as the value of
|
||||||
|
* its <tt>socket</tt> property.
|
||||||
|
*/
|
||||||
|
private final RelayedCandidate relayedCandidate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>Thread</tt> which is to send the {@link #packetsToSend} to the
|
||||||
|
* associated TURN server.
|
||||||
|
*/
|
||||||
|
private Thread sendThread;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>TurnCandidateHarvest</tt> which has harvested
|
||||||
|
* {@link #relayedCandidate}.
|
||||||
|
*/
|
||||||
|
private final TurnCandidateHarvest turnCandidateHarvest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>RelayedCandidateDatagramSocket</tt> instance which
|
||||||
|
* is to be the <tt>socket</tt> of a specific <tt>RelayedCandidate</tt>
|
||||||
|
* harvested by a specific <tt>TurnCandidateHarvest</tt>.
|
||||||
*
|
*
|
||||||
|
* @param relayedCandidate the <tt>RelayedCandidate</tt> which is to use the
|
||||||
|
* new instance as the value of its <tt>socket</tt> property
|
||||||
|
* @param turnCandidateHarvest the <tt>TurnCandidateHarvest</tt> which has
|
||||||
|
* harvested <tt>relayedCandidate</tt>
|
||||||
* @throws SocketException if anything goes wrong while initializing the new
|
* @throws SocketException if anything goes wrong while initializing the new
|
||||||
* <tt>RelayedCandidateDatagramSocket</tt> instance
|
* <tt>RelayedCandidateDatagramSocket</tt> instance
|
||||||
*/
|
*/
|
||||||
public RelayedCandidateDatagramSocket()
|
public RelayedCandidateDatagramSocket(
|
||||||
|
RelayedCandidate relayedCandidate,
|
||||||
|
TurnCandidateHarvest turnCandidateHarvest)
|
||||||
throws SocketException
|
throws SocketException
|
||||||
{
|
{
|
||||||
super(null /* bindaddr */);
|
super(/* bindaddr */ (SocketAddress) null);
|
||||||
|
|
||||||
|
this.relayedCandidate = relayedCandidate;
|
||||||
|
this.turnCandidateHarvest = turnCandidateHarvest;
|
||||||
|
|
||||||
|
this.turnCandidateHarvest.harvester.stunStack.addIndicationListener(
|
||||||
|
this.turnCandidateHarvest.hostCandidate.getTransportAddress(),
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -44,7 +139,249 @@ public class RelayedCandidateDatagramSocket
|
|||||||
@Override
|
@Override
|
||||||
public void close()
|
public void close()
|
||||||
{
|
{
|
||||||
// TODO Auto-generated method stub
|
synchronized (this)
|
||||||
|
{
|
||||||
|
if (this.closed)
|
||||||
|
return;
|
||||||
|
else
|
||||||
|
this.closed = true;
|
||||||
|
}
|
||||||
|
synchronized (packetsToReceive)
|
||||||
|
{
|
||||||
|
packetsToReceive.notifyAll();
|
||||||
|
}
|
||||||
|
synchronized (packetsToSend)
|
||||||
|
{
|
||||||
|
packetsToSend.notifyAll();
|
||||||
|
}
|
||||||
|
turnCandidateHarvest.harvester.stunStack.removeIndicationListener(
|
||||||
|
turnCandidateHarvest.hostCandidate.getTransportAddress(),
|
||||||
|
this);
|
||||||
|
turnCandidateHarvest.close(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates {@link #sendThread} which is to send {@link #packetsToSend} to
|
||||||
|
* the associated TURN server.
|
||||||
|
*/
|
||||||
|
private void createSendThread()
|
||||||
|
{
|
||||||
|
synchronized (packetsToSend)
|
||||||
|
{
|
||||||
|
sendThread
|
||||||
|
= new Thread()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
runInSendThread();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
synchronized (packetsToSend)
|
||||||
|
{
|
||||||
|
if (sendThread
|
||||||
|
== Thread.currentThread())
|
||||||
|
sendThread = null;
|
||||||
|
if ((sendThread == null)
|
||||||
|
&& !closed
|
||||||
|
&& !packetsToSend.isEmpty())
|
||||||
|
createSendThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
sendThread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the local address to which the socket is bound.
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> returns the <tt>address</tt> of
|
||||||
|
* its <tt>localSocketAddress</tt>.
|
||||||
|
* <p>
|
||||||
|
* If there is a security manager, its <tt>checkConnect</tt> method is first
|
||||||
|
* called with the host address and <tt>-1</tt> as its arguments to see if
|
||||||
|
* the operation is allowed.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return the local address to which the socket is bound, or an
|
||||||
|
* <tt>InetAddress</tt> representing any local address if either the socket
|
||||||
|
* is not bound, or the security manager <tt>checkConnect</tt> method does
|
||||||
|
* not allow the operation
|
||||||
|
* @see #getLocalSocketAddress()
|
||||||
|
* @see DatagramSocket#getLocalAddress()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public InetAddress getLocalAddress()
|
||||||
|
{
|
||||||
|
return getLocalSocketAddress().getAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the port number on the local host to which this socket is bound.
|
||||||
|
* <tt>RelayedCandidateDatagram</tt> returns the <tt>port</tt> of its
|
||||||
|
* <tt>localSocketAddress</tt>.
|
||||||
|
*
|
||||||
|
* @return the port number on the local host to which this socket is bound
|
||||||
|
* @see #getLocalSocketAddress()
|
||||||
|
* @see DatagramSocket#getLocalPort()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getLocalPort()
|
||||||
|
{
|
||||||
|
return getLocalSocketAddress().getPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the address of the endpoint this socket is bound to, or
|
||||||
|
* <tt>null</tt> if it is not bound yet. Since
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> represents an
|
||||||
|
* application-purposed <tt>DatagramSocket</tt> relaying data to and from a
|
||||||
|
* TURN server, the <tt>localSocketAddress</tt> is the
|
||||||
|
* <tt>transportAddress</tt> of the respective <tt>RelayedCandidate</tt>.
|
||||||
|
*
|
||||||
|
* @return a <tt>SocketAddress</tt> representing the local endpoint of this
|
||||||
|
* socket, or <tt>null</tt> if it is not bound yet
|
||||||
|
* @see DatagramSocket#getLocalSocketAddress()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public InetSocketAddress getLocalSocketAddress()
|
||||||
|
{
|
||||||
|
return getRelayedCandidate().getTransportAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the <tt>RelayedCandidate</tt> which uses this instance as the value
|
||||||
|
* of its <tt>socket</tt> property.
|
||||||
|
*
|
||||||
|
* @return the <tt>RelayedCandidate</tt> which uses this instance as the
|
||||||
|
* value of its <tt>socket</tt> property
|
||||||
|
*/
|
||||||
|
public final RelayedCandidate getRelayedCandidate()
|
||||||
|
{
|
||||||
|
return relayedCandidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies this <tt>MessageEventHandler</tt> that a specific STUN message
|
||||||
|
* has been received, parsed and is ready for delivery.
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> handles STUN indications sent
|
||||||
|
* from the associated TURN server and received at the associated local
|
||||||
|
* <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param e a <tt>StunMessageEvent</tt> which encapsulates the received STUN
|
||||||
|
* message
|
||||||
|
*/
|
||||||
|
public void handleMessageEvent(StunMessageEvent e)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Is it meant for us? (It should be because
|
||||||
|
* RelayedCandidateDatagramSocket registers for STUN indications
|
||||||
|
* received at the associated local TransportAddress only.)
|
||||||
|
*/
|
||||||
|
if (!turnCandidateHarvest.hostCandidate.getTransportAddress().equals(
|
||||||
|
e.getLocalAddress()))
|
||||||
|
return;
|
||||||
|
// Is it from our TURN server?
|
||||||
|
if (!turnCandidateHarvest.harvester.stunServer.equals(
|
||||||
|
e.getRemoteAddress()))
|
||||||
|
return;
|
||||||
|
|
||||||
|
Message message = e.getMessage();
|
||||||
|
char messageType = message.getMessageType();
|
||||||
|
|
||||||
|
if (messageType != Message.DATA_INDICATION)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* RFC 5766: When the client receives a Data indication, it checks that
|
||||||
|
* the Data indication contains both an XOR-PEER-ADDRESS and a DATA
|
||||||
|
* attribute, and discards the indication if it does not.
|
||||||
|
*/
|
||||||
|
XorPeerAddressAttribute peerAddressAttribute
|
||||||
|
= (XorPeerAddressAttribute)
|
||||||
|
message.getAttribute(Attribute.XOR_PEER_ADDRESS);
|
||||||
|
|
||||||
|
if (peerAddressAttribute == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
DataAttribute dataAttribute
|
||||||
|
= (DataAttribute) message.getAttribute(Attribute.DATA);
|
||||||
|
|
||||||
|
if (dataAttribute == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
TransportAddress peerAddress
|
||||||
|
= peerAddressAttribute.getAddress(message.getTransactionID());
|
||||||
|
|
||||||
|
if (peerAddress == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
byte[] data = dataAttribute.getData();
|
||||||
|
|
||||||
|
if (data == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
DatagramPacket packetToReceive;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
packetToReceive
|
||||||
|
= new DatagramPacket(data, 0, data.length, peerAddress);
|
||||||
|
}
|
||||||
|
catch (SocketException sex)
|
||||||
|
{
|
||||||
|
packetToReceive = null;
|
||||||
|
}
|
||||||
|
if (packetToReceive != null)
|
||||||
|
{
|
||||||
|
synchronized (packetsToReceive)
|
||||||
|
{
|
||||||
|
packetsToReceive.add(packetToReceive);
|
||||||
|
System.err.println(
|
||||||
|
getClass().getSimpleName()
|
||||||
|
+ ".handleMessageEvent: packetToReceive.length= "
|
||||||
|
+ packetToReceive.getLength());
|
||||||
|
packetsToReceive.notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies this <tt>RelayedCandidateDatagramSocket</tt> that a specific
|
||||||
|
* <tt>Request</tt> it has sent has either failed or received a STUN error
|
||||||
|
* <tt>Response</tt>.
|
||||||
|
*
|
||||||
|
* @param response the <tt>Response</tt> which responds to <tt>request</tt>
|
||||||
|
* @param request the <tt>Request</tt> sent by this instance to which
|
||||||
|
* <tt>response</tt> responds
|
||||||
|
* @return <tt>true</tt> if the failure or error condition has been handled
|
||||||
|
* and the caller should assume this instance has recovered from it;
|
||||||
|
* otherwise, <tt>false</tt>
|
||||||
|
*/
|
||||||
|
public boolean processErrorOrFailure(Response response, Request request)
|
||||||
|
{
|
||||||
|
if (request.getMessageType() == Message.CREATEPERMISSION_REQUEST)
|
||||||
|
setChannelBound(request, false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies this <tt>RelayedCandidateDatagramSocket</tt> that a specific
|
||||||
|
* <tt>Request</tt> it has sent has received a STUN success
|
||||||
|
* <tt>Response</tt>.
|
||||||
|
*
|
||||||
|
* @param response the <tt>Response</tt> which responds to <tt>request</tt>
|
||||||
|
* @param request the <tt>Request</tt> sent by this instance to which
|
||||||
|
* <tt>response</tt> responds
|
||||||
|
*/
|
||||||
|
public void processSuccess(Response response, Request request)
|
||||||
|
{
|
||||||
|
if (request.getMessageType() == Message.CREATEPERMISSION_REQUEST)
|
||||||
|
setChannelBound(request, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,7 +399,182 @@ public class RelayedCandidateDatagramSocket
|
|||||||
public void receive(DatagramPacket p)
|
public void receive(DatagramPacket p)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
// TODO Auto-generated method stub
|
synchronized (packetsToReceive)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* According to the javadoc of DatagramSocket#close(), any
|
||||||
|
* thread currently blocked in #receive(DatagramPacket) upon
|
||||||
|
* this socket will throw a SocketException.
|
||||||
|
*/
|
||||||
|
if (closed)
|
||||||
|
{
|
||||||
|
throw
|
||||||
|
new SocketException(
|
||||||
|
RelayedCandidateDatagramSocket.class
|
||||||
|
.getSimpleName()
|
||||||
|
+ " has been closed.");
|
||||||
|
}
|
||||||
|
else if (packetsToReceive.isEmpty())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
packetsToReceive.wait();
|
||||||
|
}
|
||||||
|
catch (InterruptedException iex)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DatagramPacket packetToReceive = packetsToReceive.remove(0);
|
||||||
|
|
||||||
|
MultiplexingDatagramSocket.copy(packetToReceive, p);
|
||||||
|
packetsToReceive.notifyAll();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs in {@link #sendThread} to send {@link #packetsToSend} to the
|
||||||
|
* associated TURN server.
|
||||||
|
*/
|
||||||
|
private void runInSendThread()
|
||||||
|
{
|
||||||
|
synchronized (packetsToSend)
|
||||||
|
{
|
||||||
|
while (!closed)
|
||||||
|
{
|
||||||
|
if (packetsToSend.isEmpty())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
packetsToSend.wait();
|
||||||
|
}
|
||||||
|
catch (InterruptedException iex)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int packetToSendCount = packetsToSend.size();
|
||||||
|
|
||||||
|
for (int packetToSendIndex = 0;
|
||||||
|
packetToSendIndex < packetToSendCount;
|
||||||
|
packetToSendIndex++)
|
||||||
|
{
|
||||||
|
DatagramPacket packetToSend
|
||||||
|
= packetsToSend.get(packetToSendIndex);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get a channel to the peer which is to receive the
|
||||||
|
* packetToSend.
|
||||||
|
*/
|
||||||
|
int channelCount = channels.size();
|
||||||
|
TransportAddress peerAddress
|
||||||
|
= new TransportAddress(
|
||||||
|
packetToSend.getAddress(),
|
||||||
|
packetToSend.getPort(),
|
||||||
|
Transport.UDP);
|
||||||
|
Channel channel = null;
|
||||||
|
|
||||||
|
for (int channelIndex = 0;
|
||||||
|
channelIndex < channelCount;
|
||||||
|
channelIndex++)
|
||||||
|
{
|
||||||
|
Channel aChannel = channels.get(channelIndex);
|
||||||
|
|
||||||
|
if (aChannel.peerAddressEquals(peerAddress))
|
||||||
|
{
|
||||||
|
channel = aChannel;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (channel == null)
|
||||||
|
{
|
||||||
|
channel = new Channel(peerAddress);
|
||||||
|
channels.add(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Either bind the channel or send the packetToSend through
|
||||||
|
* it.
|
||||||
|
*/
|
||||||
|
if (channel.isBound())
|
||||||
|
{
|
||||||
|
packetsToSend.remove(packetToSendIndex);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
channel.send(packetToSend, peerAddress);
|
||||||
|
}
|
||||||
|
catch (StunException sex)
|
||||||
|
{
|
||||||
|
if (logger.isLoggable(Level.INFO))
|
||||||
|
{
|
||||||
|
logger.log(
|
||||||
|
Level.INFO,
|
||||||
|
"Failed to send through "
|
||||||
|
+ RelayedCandidateDatagramSocket
|
||||||
|
.class.getSimpleName()
|
||||||
|
+ " channel." ,
|
||||||
|
sex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (!channel.isBinding())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
channel.bind();
|
||||||
|
}
|
||||||
|
catch (StunException sex)
|
||||||
|
{
|
||||||
|
if (logger.isLoggable(Level.INFO))
|
||||||
|
{
|
||||||
|
logger.log(
|
||||||
|
Level.INFO,
|
||||||
|
"Failed to bind "
|
||||||
|
+ RelayedCandidateDatagramSocket
|
||||||
|
.class.getSimpleName()
|
||||||
|
+ " channel." ,
|
||||||
|
sex);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Well, it may not be the fault of the packetToSend
|
||||||
|
* but it happened while we were trying to send it
|
||||||
|
* and we don't have a way to report an error so
|
||||||
|
* just drop packetToSend in order to change
|
||||||
|
* something and not just go again trying the same
|
||||||
|
* thing.
|
||||||
|
*/
|
||||||
|
packetsToSend.remove(packetToSendIndex);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If no packetToSend has been sent by the current iteration,
|
||||||
|
* then we must be waiting for some condition to change in order
|
||||||
|
* to be able to send.
|
||||||
|
*/
|
||||||
|
if (packetsToSend.size() == packetToSendCount)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
packetsToSend.wait();
|
||||||
|
}
|
||||||
|
catch (InterruptedException iex)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,6 +590,248 @@ public class RelayedCandidateDatagramSocket
|
|||||||
public void send(DatagramPacket p)
|
public void send(DatagramPacket p)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
// TODO Auto-generated method stub
|
synchronized (packetsToSend)
|
||||||
|
{
|
||||||
|
if (closed)
|
||||||
|
{
|
||||||
|
throw
|
||||||
|
new IOException(
|
||||||
|
RelayedCandidateDatagramSocket.class.getSimpleName()
|
||||||
|
+ " has been closed.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
packetsToSend.add(MultiplexingDatagramSocket.clone(p));
|
||||||
|
|
||||||
|
if (sendThread == null)
|
||||||
|
createSendThread();
|
||||||
|
else
|
||||||
|
packetsToSend.notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the <tt>bound</tt> property of a <tt>Channel</tt> the installation
|
||||||
|
* of which has been attempted by sending a specific <tt>Request</tt>.
|
||||||
|
*
|
||||||
|
* @param request the <tt>Request</tt> which has been attempted in order to
|
||||||
|
* install a <tt>Channel</tt>
|
||||||
|
* @param bound <tt>true</tt> if the <tt>bound</tt> property of the
|
||||||
|
* <tt>Channel</tt> is to be set to <tt>true</tt>; otherwise, <tt>false</tt>
|
||||||
|
*/
|
||||||
|
private void setChannelBound(Request request, boolean bound)
|
||||||
|
{
|
||||||
|
XorPeerAddressAttribute peerAddressAttribute
|
||||||
|
= (XorPeerAddressAttribute)
|
||||||
|
request.getAttribute(Attribute.XOR_PEER_ADDRESS);
|
||||||
|
byte[] transactionID = request.getTransactionID();
|
||||||
|
TransportAddress peerAddress
|
||||||
|
= peerAddressAttribute.getAddress(transactionID);
|
||||||
|
|
||||||
|
synchronized (packetsToSend)
|
||||||
|
{
|
||||||
|
int channelCount = channels.size();
|
||||||
|
|
||||||
|
for (int channelIndex = 0;
|
||||||
|
channelIndex < channelCount;
|
||||||
|
channelIndex++)
|
||||||
|
{
|
||||||
|
Channel channel = channels.get(channelIndex);
|
||||||
|
|
||||||
|
if (channel.peerAddressEquals(peerAddress))
|
||||||
|
{
|
||||||
|
channel.setBound(bound, transactionID);
|
||||||
|
packetsToSend.notifyAll();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a channel which relays data sent through this
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> to a specific
|
||||||
|
* <tt>TransportAddress</tt> via the TURN server associated with this
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt>.
|
||||||
|
*/
|
||||||
|
private class Channel
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The time stamp in milliseconds at which {@link #bindingTransactionID}
|
||||||
|
* has been used to bind/install this <tt>Channel</tt>.
|
||||||
|
*/
|
||||||
|
private long bindingTimeStamp = -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ID of the transaction with which a CreatePermission
|
||||||
|
* <tt>Request</tt> has been sent to bind/install this <tt>Channel</tt>.
|
||||||
|
*/
|
||||||
|
private byte[] bindingTransactionID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The indication which determines whether a confirmation has been
|
||||||
|
* received that this <tt>Channel</tt> has been bound.
|
||||||
|
*/
|
||||||
|
private boolean bound = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>TransportAddress</tt> of the peer to which this
|
||||||
|
* <tt>Channel</tt> provides a permission of this
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> to send data to.
|
||||||
|
*/
|
||||||
|
private final TransportAddress peerAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>Channel</tt> instance which is to provide this
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> with a permission to send
|
||||||
|
* to a specific peer <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param peerAddress the <tt>TransportAddress</tt> of the peer to which
|
||||||
|
* the new instance is to provide a permission of this
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> to send data to
|
||||||
|
*/
|
||||||
|
public Channel(TransportAddress peerAddress)
|
||||||
|
{
|
||||||
|
this.peerAddress = peerAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds/installs this channel so that it provides this
|
||||||
|
* <tt>RelayedCandidateDatagramSocket</tt> with a permission to send
|
||||||
|
* data to the <tt>TransportAddress</tt> associated with this instance.
|
||||||
|
*
|
||||||
|
* @throws StunException if anything goes wrong while binding/installing
|
||||||
|
* this channel
|
||||||
|
*/
|
||||||
|
public void bind()
|
||||||
|
throws StunException
|
||||||
|
{
|
||||||
|
byte[] transactionID
|
||||||
|
= TransactionID.createNewTransactionID().getBytes();
|
||||||
|
Request createPermissionRequest
|
||||||
|
= MessageFactory.createCreatePermissionRequest(
|
||||||
|
peerAddress,
|
||||||
|
transactionID);
|
||||||
|
|
||||||
|
createPermissionRequest.setTransactionID(transactionID);
|
||||||
|
turnCandidateHarvest.sendRequest(
|
||||||
|
RelayedCandidateDatagramSocket.this,
|
||||||
|
createPermissionRequest);
|
||||||
|
|
||||||
|
bindingTransactionID = transactionID;
|
||||||
|
bindingTimeStamp = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the indicator which determines whether this instance has started
|
||||||
|
* binding/installing itself and has not received a confirmation that it
|
||||||
|
* has succeeded in doing so yet.
|
||||||
|
*
|
||||||
|
* @return <tt>true</tt> if this instance has started binding/installing
|
||||||
|
* itself and has not received a confirmation that it has succeeded in
|
||||||
|
* doing so yet; otherwise, <tt>false</tt>
|
||||||
|
*/
|
||||||
|
public boolean isBinding()
|
||||||
|
{
|
||||||
|
return (bindingTransactionID != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the indication which determines whether this instance is
|
||||||
|
* currently considered bound/installed.
|
||||||
|
*
|
||||||
|
* @return <tt>true</tt> if this instance is currently considered
|
||||||
|
* bound/installed; otherwise, <tt>false</tt>
|
||||||
|
*/
|
||||||
|
public boolean isBound()
|
||||||
|
{
|
||||||
|
if ((bindingTimeStamp == -1)
|
||||||
|
|| (bindingTimeStamp
|
||||||
|
+ PERMISSION_LIFETIME
|
||||||
|
- PERMISSION_LIFETIME_LEEWAY)
|
||||||
|
< System.currentTimeMillis())
|
||||||
|
return false;
|
||||||
|
return (bindingTransactionID == null) && bound;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether the <tt>peerAddress</tt> property of this instance
|
||||||
|
* is considered by this <tt>Channel</tt> to be equal to a specific
|
||||||
|
* <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param peerAddress the <tt>TransportAddress</tt> which is to be
|
||||||
|
* checked for equality (as defined by this <tt>Channel</tt> and not
|
||||||
|
* necessarily by the <tt>TransportAddress</tt> class)
|
||||||
|
* @return <tt>true</tt> if the specified <tt>TransportAddress</tt> is
|
||||||
|
* considered by this <tt>Channel</tt> to be equal to its
|
||||||
|
* <tt>peerAddress</tt> property; otherwise, <tt>false</tt>
|
||||||
|
*/
|
||||||
|
public boolean peerAddressEquals(TransportAddress peerAddress)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
this.peerAddress.getAddress().equals(peerAddress.getAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a specific <tt>DatagramPacket</tt> through this
|
||||||
|
* <tt>Channel</tt> to a specific peer <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param p the <tt>DatagramPacket</tt> to be sent
|
||||||
|
* @param peerAddress the <tt>TransportAddress</tt> of the peer to which
|
||||||
|
* the <tt>DatagramPacket</tt> is to be sent
|
||||||
|
* @throws StunException if anything goes wrong while sending the
|
||||||
|
* specified <tt>DatagramPacket</tt> to the specified peer
|
||||||
|
* <tt>TransportAddress</tt>
|
||||||
|
*/
|
||||||
|
public void send(DatagramPacket p, TransportAddress peerAddress)
|
||||||
|
throws StunException
|
||||||
|
{
|
||||||
|
byte[] pData = p.getData();
|
||||||
|
int pOffset = p.getOffset();
|
||||||
|
int pLength = p.getLength();
|
||||||
|
byte[] data;
|
||||||
|
|
||||||
|
if ((pOffset == 0) && (pLength == pData.length))
|
||||||
|
data = pData;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
data = new byte[pLength];
|
||||||
|
System.arraycopy(pData, pOffset, data, 0, pLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] transactionID
|
||||||
|
= TransactionID.createNewTransactionID().getBytes();
|
||||||
|
Indication sendIndication
|
||||||
|
= MessageFactory.createSendIndication(
|
||||||
|
peerAddress,
|
||||||
|
data,
|
||||||
|
transactionID);
|
||||||
|
|
||||||
|
sendIndication.setTransactionID(transactionID);
|
||||||
|
turnCandidateHarvest.harvester.stunStack.sendIndication(
|
||||||
|
sendIndication,
|
||||||
|
turnCandidateHarvest.harvester.stunServer,
|
||||||
|
turnCandidateHarvest.hostCandidate.getTransportAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the indicator which determines whether this <tt>Channel</tt> is
|
||||||
|
* bound/installed.
|
||||||
|
*
|
||||||
|
* @param bound <tt>true</tt> if this <tt>Channel</tt> is to be marked
|
||||||
|
* as bound/installed; otherwise, <tt>false</tt>
|
||||||
|
* @param boundTransactionID an array of <tt>byte</tt>s which represents
|
||||||
|
* the ID of the transaction with which the confirmation about the
|
||||||
|
* binding/installing has arrived
|
||||||
|
*/
|
||||||
|
public void setBound(boolean bound, byte[] boundTransactionID)
|
||||||
|
{
|
||||||
|
if (bindingTransactionID != null)
|
||||||
|
{
|
||||||
|
bindingTransactionID = null;
|
||||||
|
this.bound = bound;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,7 +21,7 @@ import java.net.*;
|
|||||||
* @author Lubomir Marinov
|
* @author Lubomir Marinov
|
||||||
*/
|
*/
|
||||||
public class SafeCloseDatagramSocket
|
public class SafeCloseDatagramSocket
|
||||||
extends DatagramSocket
|
extends DelegatingDatagramSocket
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,20 +52,19 @@ public class SafeCloseDatagramSocket
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a datagram socket, bound to the specified local socket address.
|
* Initializes a new <tt>SafeCloseDatagramSocket</tt> instance which to
|
||||||
* <p>
|
* implement the <tt>DatagramSocket</tt> functionality by delegating to a
|
||||||
* If, if the address is <tt>null</tt>, creates an unbound socket.
|
* specific <tt>DatagramSocket</tt>.
|
||||||
* </p>
|
|
||||||
*
|
*
|
||||||
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
|
* @param delegate the <tt>DatagramSocket</tt> to which the new instance is
|
||||||
* unbound socket
|
* to delegate
|
||||||
* @throws SocketException if the socket could not be opened, or the socket
|
* @throws SocketException if anything goes wrong while initializing the new
|
||||||
* could not bind to the specified local port
|
* <tt>SafeCloseDatagramSocket</tt> instance
|
||||||
*/
|
*/
|
||||||
public SafeCloseDatagramSocket(SocketAddress bindaddr)
|
public SafeCloseDatagramSocket(DatagramSocket delegate)
|
||||||
throws SocketException
|
throws SocketException
|
||||||
{
|
{
|
||||||
super(bindaddr);
|
super(delegate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,6 +101,23 @@ public class SafeCloseDatagramSocket
|
|||||||
super(port, laddr);
|
super(port, laddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a datagram socket, bound to the specified local socket address.
|
||||||
|
* <p>
|
||||||
|
* If, if the address is <tt>null</tt>, creates an unbound socket.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
|
||||||
|
* unbound socket
|
||||||
|
* @throws SocketException if the socket could not be opened, or the socket
|
||||||
|
* could not bind to the specified local port
|
||||||
|
*/
|
||||||
|
public SafeCloseDatagramSocket(SocketAddress bindaddr)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
super(bindaddr);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes this datagram socket.
|
* Closes this datagram socket.
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
@ -101,6 +101,7 @@ public class TurnDatagramPacketFilter
|
|||||||
{
|
{
|
||||||
case Message.TURN_METHOD_ALLOCATE:
|
case Message.TURN_METHOD_ALLOCATE:
|
||||||
case Message.TURN_METHOD_CHANNELBIND:
|
case Message.TURN_METHOD_CHANNELBIND:
|
||||||
|
case Message.TURN_METHOD_CREATEPERMISSION:
|
||||||
case Message.TURN_METHOD_DATA:
|
case Message.TURN_METHOD_DATA:
|
||||||
case Message.TURN_METHOD_REFRESH:
|
case Message.TURN_METHOD_REFRESH:
|
||||||
case Message.TURN_METHOD_SEND:
|
case Message.TURN_METHOD_SEND:
|
||||||
|
|||||||
@ -48,7 +48,7 @@ class Connector
|
|||||||
/**
|
/**
|
||||||
* A flag that is set to false to exit the message processor.
|
* A flag that is set to false to exit the message processor.
|
||||||
*/
|
*/
|
||||||
private boolean isRunning;
|
private boolean running;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The instance to be notified if errors occur in the network listening
|
* The instance to be notified if errors occur in the network listening
|
||||||
@ -79,13 +79,12 @@ class Connector
|
|||||||
socket.getLocalPort(), Transport.UDP);
|
socket.getLocalPort(), Transport.UDP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the network listening thread.
|
* Start the network listening thread.
|
||||||
*/
|
*/
|
||||||
void start()
|
void start()
|
||||||
{
|
{
|
||||||
this.isRunning = true;
|
this.running = true;
|
||||||
Thread thread = new Thread(this, "IceConnector@"+hashCode());
|
Thread thread = new Thread(this, "IceConnector@"+hashCode());
|
||||||
thread.setDaemon(true);
|
thread.setDaemon(true);
|
||||||
thread.start();
|
thread.start();
|
||||||
@ -107,46 +106,86 @@ class Connector
|
|||||||
*/
|
*/
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
while (this.isRunning)
|
DatagramPacket packet = null;
|
||||||
|
|
||||||
|
while (this.running)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int bufsize = sock.getReceiveBufferSize();
|
|
||||||
byte message[] = new byte[bufsize];
|
|
||||||
DatagramPacket packet = new DatagramPacket(message, bufsize);
|
|
||||||
|
|
||||||
DatagramSocket localSock;
|
DatagramSocket localSock;
|
||||||
|
|
||||||
synchronized (sockLock)
|
synchronized (sockLock)
|
||||||
{
|
{
|
||||||
if (!isRunning)
|
if (!running)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
localSock = this.sock;
|
localSock = this.sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Make sure localSock's receiveBufferSize is taken into
|
||||||
|
* account including after it gets changed.
|
||||||
|
*/
|
||||||
|
int receiveBufferSize = localSock.getReceiveBufferSize();
|
||||||
|
|
||||||
|
if (packet == null)
|
||||||
|
{
|
||||||
|
packet
|
||||||
|
= new DatagramPacket(
|
||||||
|
new byte[receiveBufferSize],
|
||||||
|
receiveBufferSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
byte[] packetData = packet.getData();
|
||||||
|
|
||||||
|
if ((packetData == null)
|
||||||
|
|| (packetData.length < receiveBufferSize))
|
||||||
|
{
|
||||||
|
packet.setData(
|
||||||
|
new byte[receiveBufferSize],
|
||||||
|
0,
|
||||||
|
receiveBufferSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* XXX Tell the packet it is large enough because the
|
||||||
|
* socket will not look at the length of the data array
|
||||||
|
* property and will just respect the length property.
|
||||||
|
*/
|
||||||
|
packet.setLength(receiveBufferSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
localSock.receive(packet);
|
localSock.receive(packet);
|
||||||
|
|
||||||
//get lost if we are no longer running.
|
//get lost if we are no longer running.
|
||||||
if(!isRunning)
|
if(!running)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
logger.finest("received datagram");
|
logger.finest("received datagram");
|
||||||
|
|
||||||
RawMessage rawMessage = new RawMessage( message,
|
RawMessage rawMessage
|
||||||
packet.getLength(),
|
= new RawMessage(
|
||||||
new TransportAddress( packet.getAddress(),
|
packet.getData(),
|
||||||
packet.getPort(), Transport.UDP),
|
packet.getLength(),
|
||||||
listenAddress);
|
new TransportAddress(
|
||||||
|
packet.getAddress(),
|
||||||
|
packet.getPort(),
|
||||||
|
Transport.UDP),
|
||||||
|
listenAddress);
|
||||||
|
|
||||||
messageQueue.add(rawMessage);
|
messageQueue.add(rawMessage);
|
||||||
}
|
}
|
||||||
catch (SocketException ex)
|
catch (SocketException ex)
|
||||||
{
|
{
|
||||||
if (isRunning)
|
if (running)
|
||||||
{
|
{
|
||||||
logger.log(Level.WARNING,
|
logger.log(
|
||||||
"A connector died:", ex);
|
Level.WARNING,
|
||||||
|
"Connector died: " + listenAddress,
|
||||||
|
ex);
|
||||||
|
|
||||||
stop();
|
stop();
|
||||||
//Something wrong has happened
|
//Something wrong has happened
|
||||||
@ -159,7 +198,7 @@ class Connector
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
//The exception was most probably caused by calling
|
//The exception was most probably caused by calling
|
||||||
//this.stop() ....
|
//this.stop().
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
@ -192,7 +231,7 @@ class Connector
|
|||||||
{
|
{
|
||||||
synchronized(sockLock)
|
synchronized(sockLock)
|
||||||
{
|
{
|
||||||
this.isRunning = false;
|
this.running = false;
|
||||||
this.sock = null;
|
this.sock = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -221,11 +260,9 @@ class Connector
|
|||||||
@Override
|
@Override
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
return "ice4j.Connector@"
|
return
|
||||||
+ listenAddress
|
"ice4j.Connector@" + listenAddress
|
||||||
+" status: "
|
+ " status: " + (running ? "not" : "") +" running";
|
||||||
+ (isRunning? "not":"")
|
|
||||||
+" running";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -9,7 +9,7 @@ package org.ice4j.stack;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import org.ice4j.*;
|
import org.ice4j.*;
|
||||||
|
import org.ice4j.message.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a utility class used for dispatching incoming request events. We use
|
* This is a utility class used for dispatching incoming request events. We use
|
||||||
@ -18,44 +18,109 @@ import org.ice4j.*;
|
|||||||
* NetAccessPoint only).
|
* NetAccessPoint only).
|
||||||
*
|
*
|
||||||
* @author Emil Ivov
|
* @author Emil Ivov
|
||||||
|
* @author Lubomir Marinov
|
||||||
*/
|
*/
|
||||||
public class EventDispatcher
|
public class EventDispatcher
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All property change listeners registered so far.
|
* The STUN request and indication listeners registered with this
|
||||||
|
* <tt>EventDispatcher</tt>.
|
||||||
*/
|
*/
|
||||||
private final Vector<RequestListener> requestListeners
|
private final List<MessageTypeEventHandler<?>> messageListeners
|
||||||
= new Vector<RequestListener>();
|
= new Vector<MessageTypeEventHandler<?>>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hashtable for managing property change listeners registered for specific
|
* The <tt>Map</tt> of <tt>EventDispatcher</tt>s which keep the
|
||||||
* properties. Maps property names to PropertyChangeSupport objects.
|
* registrations of STUN request and indication listeners registered for
|
||||||
|
* STUN requests and indications from specific local
|
||||||
|
* <tt>TransportAddress<tt>es.
|
||||||
*/
|
*/
|
||||||
private final Hashtable<TransportAddress, EventDispatcher>
|
private final Map<TransportAddress, EventDispatcher> children
|
||||||
requestListenersChildren
|
= new Hashtable<TransportAddress, EventDispatcher>();
|
||||||
= new Hashtable<TransportAddress, EventDispatcher>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an <tt>EventDispatcher</tt> object.
|
* Initializes a new <tt>EventDispatcher</tt> instance.
|
||||||
*/
|
*/
|
||||||
public EventDispatcher()
|
public EventDispatcher()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a specific <tt>MessageEventHandler</tt> for notifications about
|
||||||
|
* STUN indications received at a specific local <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param localAddr the local <tt>TransportAddress</tt> STUN indications
|
||||||
|
* received at which are to be reported to the specified
|
||||||
|
* <tt>indicationListener</tt>
|
||||||
|
* @param indicationListener the <tt>MessageEventHandler</tt> which is to be
|
||||||
|
* registered for notifications about STUN indications received at the
|
||||||
|
* specified local <tt>TransportAddress</tt>
|
||||||
|
*/
|
||||||
|
public void addIndicationListener(
|
||||||
|
TransportAddress localAddr,
|
||||||
|
MessageEventHandler indicationListener)
|
||||||
|
{
|
||||||
|
addMessageListener(
|
||||||
|
localAddr,
|
||||||
|
new IndicationEventHandler(indicationListener));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a specific <tt>MessageTypeEventHandler</tt> for notifications
|
||||||
|
* about received STUN messages.
|
||||||
|
*
|
||||||
|
* @param messageListener the <tt>MessageTypeEventHandler</tt> which is to
|
||||||
|
* be registered for notifications about received STUN messages
|
||||||
|
*/
|
||||||
|
private synchronized void addMessageListener(
|
||||||
|
MessageTypeEventHandler<?> messageListener)
|
||||||
|
{
|
||||||
|
synchronized(messageListeners)
|
||||||
|
{
|
||||||
|
if(!messageListeners.contains(messageListener))
|
||||||
|
messageListeners.add(messageListener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a specific <tt>MessageTypeEventHandler</tt> for notifications
|
||||||
|
* about STUN messages received at a specific local
|
||||||
|
* <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param localAddr the local <tt>TransportAddress</tt> STUN messages
|
||||||
|
* received at which are to be reported to the specified
|
||||||
|
* <tt>messageListener</tt>
|
||||||
|
* @param messageListener the <tt>MessageTypeEventHandler</tt> which is to
|
||||||
|
* be registered for notifications about STUN messages received at the
|
||||||
|
* specified local <tt>TransportAddress</tt>
|
||||||
|
*/
|
||||||
|
private synchronized void addMessageListener(
|
||||||
|
TransportAddress localAddr,
|
||||||
|
MessageTypeEventHandler<?> messageListener)
|
||||||
|
{
|
||||||
|
synchronized(children)
|
||||||
|
{
|
||||||
|
EventDispatcher child = children.get(localAddr);
|
||||||
|
|
||||||
|
if (child == null)
|
||||||
|
{
|
||||||
|
child = new EventDispatcher();
|
||||||
|
children.put(localAddr, child);
|
||||||
|
}
|
||||||
|
child.addMessageListener(messageListener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a RequestListener to the listener list. The listener is registered
|
* Add a RequestListener to the listener list. The listener is registered
|
||||||
* for requests coming from no matter which NetAccessPoint.
|
* for requests coming from no matter which NetAccessPoint.
|
||||||
*
|
*
|
||||||
* @param listener The ReuqestListener to be added
|
* @param listener The ReuqestListener to be added
|
||||||
*/
|
*/
|
||||||
public synchronized void addRequestListener(RequestListener listener)
|
public void addRequestListener(RequestListener listener)
|
||||||
{
|
{
|
||||||
synchronized(requestListeners)
|
addMessageListener(new RequestListenerMessageEventHandler(listener));
|
||||||
{
|
|
||||||
if(!requestListeners.contains(listener))
|
|
||||||
requestListeners.addElement(listener);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,19 +131,53 @@ public class EventDispatcher
|
|||||||
* @param localAddr The NETAP descriptor that we're interested in.
|
* @param localAddr The NETAP descriptor that we're interested in.
|
||||||
* @param listener The ConfigurationChangeListener to be added
|
* @param listener The ConfigurationChangeListener to be added
|
||||||
*/
|
*/
|
||||||
|
public void addRequestListener( TransportAddress localAddr,
|
||||||
public synchronized void addRequestListener( TransportAddress localAddr,
|
RequestListener listener)
|
||||||
RequestListener listener)
|
|
||||||
{
|
{
|
||||||
synchronized(requestListenersChildren)
|
addMessageListener(
|
||||||
|
localAddr,
|
||||||
|
new RequestListenerMessageEventHandler(listener));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregisters a specific <tt>MessageTypeEventHandler</tt> from
|
||||||
|
* notifications about received STUN messages.
|
||||||
|
*
|
||||||
|
* @param messageListener the <tt>MessageTypeEventHandler</tt> to be
|
||||||
|
* unregistered for notifications about received STUN messages
|
||||||
|
*/
|
||||||
|
private synchronized void removeMessageListener(
|
||||||
|
MessageTypeEventHandler<?> messageListener)
|
||||||
|
{
|
||||||
|
synchronized(messageListeners)
|
||||||
{
|
{
|
||||||
EventDispatcher child = requestListenersChildren.get(localAddr);
|
messageListeners.remove(messageListener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregisters a specific <tt>MessageTypeEventHandler</tt> from
|
||||||
|
* notifications about STUN messages received at a specific local
|
||||||
|
* <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param localAddr the local <tt>TransportAddress</tt> STUN messages
|
||||||
|
* received at which to no longer be reported to the specified
|
||||||
|
* <tt>messageListener</tt>
|
||||||
|
* @param messageListener the <tt>MessageTypeEventHandler</tt> to be
|
||||||
|
* unregistered for notifications about STUN messages received at the
|
||||||
|
* specified local <tt>TransportAddress</tt>
|
||||||
|
*/
|
||||||
|
private synchronized void removeMessageListener(
|
||||||
|
TransportAddress localAddr,
|
||||||
|
MessageTypeEventHandler<?> messageListener)
|
||||||
|
{
|
||||||
|
synchronized(children)
|
||||||
|
{
|
||||||
|
EventDispatcher child = children.get( localAddr );
|
||||||
|
|
||||||
if (child == null)
|
if (child == null)
|
||||||
{
|
return;
|
||||||
child = new EventDispatcher();
|
child.removeMessageListener(messageListener);
|
||||||
requestListenersChildren.put(localAddr, child);
|
|
||||||
}
|
|
||||||
child.addRequestListener(listener);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,13 +189,9 @@ public class EventDispatcher
|
|||||||
*
|
*
|
||||||
* @param listener The RequestListener to be removed
|
* @param listener The RequestListener to be removed
|
||||||
*/
|
*/
|
||||||
public synchronized void removeRequestListener(
|
public void removeRequestListener(RequestListener listener)
|
||||||
RequestListener listener)
|
|
||||||
{
|
{
|
||||||
synchronized(requestListeners)
|
removeMessageListener(new RequestListenerMessageEventHandler(listener));
|
||||||
{
|
|
||||||
requestListeners.removeElement(listener);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,19 +202,12 @@ public class EventDispatcher
|
|||||||
* @param localAddr The NetAPDescriptor that was listened on.
|
* @param localAddr The NetAPDescriptor that was listened on.
|
||||||
* @param listener The RequestListener to be removed
|
* @param listener The RequestListener to be removed
|
||||||
*/
|
*/
|
||||||
public synchronized void removeRequestListener(TransportAddress localAddr,
|
public void removeRequestListener(TransportAddress localAddr,
|
||||||
RequestListener listener)
|
RequestListener listener)
|
||||||
{
|
{
|
||||||
synchronized(requestListenersChildren)
|
removeMessageListener(
|
||||||
{
|
localAddr,
|
||||||
EventDispatcher child = requestListenersChildren.get( localAddr );
|
new RequestListenerMessageEventHandler(listener));
|
||||||
|
|
||||||
if (child == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
child.removeRequestListener(listener);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -130,29 +218,31 @@ public class EventDispatcher
|
|||||||
*/
|
*/
|
||||||
public void fireMessageEvent(StunMessageEvent evt)
|
public void fireMessageEvent(StunMessageEvent evt)
|
||||||
{
|
{
|
||||||
|
|
||||||
TransportAddress localAddr = evt.getLocalAddress();
|
TransportAddress localAddr = evt.getLocalAddress();
|
||||||
List<RequestListener> listenersCopy = null;
|
MessageTypeEventHandler<?>[] messageListenersCopy;
|
||||||
|
|
||||||
synchronized(requestListeners)
|
synchronized(messageListeners)
|
||||||
{
|
{
|
||||||
listenersCopy
|
messageListenersCopy
|
||||||
= new ArrayList<RequestListener>(requestListeners);
|
= messageListeners.toArray(
|
||||||
|
new MessageTypeEventHandler<?>[
|
||||||
|
messageListeners.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (RequestListener target : listenersCopy)
|
char messageType = (char) (evt.getMessage().getMessageType() & 0x0110);
|
||||||
|
|
||||||
|
for (MessageTypeEventHandler<?> messageListener : messageListenersCopy)
|
||||||
{
|
{
|
||||||
target.processRequest(evt);
|
if (messageType == messageListener.messageType)
|
||||||
|
messageListener.handleMessageEvent(evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized(requestListenersChildren)
|
synchronized(children)
|
||||||
{
|
{
|
||||||
EventDispatcher child = requestListenersChildren.get(localAddr);
|
EventDispatcher child = children.get(localAddr);
|
||||||
|
|
||||||
if (child != null)
|
if (child != null)
|
||||||
{
|
|
||||||
child.fireMessageEvent(evt);
|
child.fireMessageEvent(evt);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,24 +256,23 @@ public class EventDispatcher
|
|||||||
*/
|
*/
|
||||||
public boolean hasRequestListeners(TransportAddress localAddr)
|
public boolean hasRequestListeners(TransportAddress localAddr)
|
||||||
{
|
{
|
||||||
synchronized(requestListeners)
|
synchronized(messageListeners)
|
||||||
{
|
{
|
||||||
if(!requestListeners.isEmpty())
|
if(!messageListeners.isEmpty())
|
||||||
{
|
{
|
||||||
// there is a generic listener
|
// there is a generic listener
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized(requestListenersChildren)
|
synchronized(children)
|
||||||
{
|
{
|
||||||
if (!requestListenersChildren.isEmpty())
|
if (!children.isEmpty())
|
||||||
{
|
{
|
||||||
EventDispatcher child = requestListenersChildren.get(localAddr);
|
EventDispatcher child = children.get(localAddr);
|
||||||
if (child != null && child.requestListeners != null)
|
|
||||||
{
|
if (child != null)
|
||||||
return !child.requestListeners.isEmpty();
|
return !child.messageListeners.isEmpty();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,8 +284,143 @@ public class EventDispatcher
|
|||||||
*/
|
*/
|
||||||
public void removeAllListeners()
|
public void removeAllListeners()
|
||||||
{
|
{
|
||||||
if(requestListeners != null)
|
messageListeners.clear();
|
||||||
requestListeners.removeAllElements();
|
children.clear();
|
||||||
requestListenersChildren.clear();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements <tt>MessageEventHandler</tt> for a
|
||||||
|
* <tt>MessageEventHandler</tt> which handles STUN indications.
|
||||||
|
*
|
||||||
|
* @author Lubomir Marinov
|
||||||
|
*/
|
||||||
|
private static class IndicationEventHandler
|
||||||
|
extends MessageTypeEventHandler<MessageEventHandler>
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>IndicationEventHandler</tt> which is to
|
||||||
|
* implement <tt>MessageEventHandler</tt> for a specific
|
||||||
|
* <tt>MessageEventHandler</tt> which handles STUN indications.
|
||||||
|
*
|
||||||
|
* @param requestListener the <tt>RequestListener</tt> for which the new
|
||||||
|
* instance is to implement <tt>MessageEventHandler</tt>
|
||||||
|
*/
|
||||||
|
public IndicationEventHandler(MessageEventHandler indicationListener)
|
||||||
|
{
|
||||||
|
super(Message.STUN_INDICATION, indicationListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies this <tt>MessageEventHandler</tt> that a STUN message has
|
||||||
|
* been received, parsed and is ready for delivery.
|
||||||
|
*
|
||||||
|
* @param e a <tt>StunMessageEvent</tt> which encapsulates the STUN
|
||||||
|
* message to be handled
|
||||||
|
* @see MessageEventHandler#handleMessageEvent(StunMessageEvent)
|
||||||
|
*/
|
||||||
|
public void handleMessageEvent(StunMessageEvent e)
|
||||||
|
{
|
||||||
|
delegate.handleMessageEvent(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the base for providers of <tt>MessageEventHandler</tt>
|
||||||
|
* implementations to specific <tt>Object</tt>s.
|
||||||
|
*
|
||||||
|
* @author Lubomir Marinov
|
||||||
|
*/
|
||||||
|
private static abstract class MessageTypeEventHandler<T>
|
||||||
|
implements MessageEventHandler
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>Object</tt> for which this instance implements
|
||||||
|
* <tt>MessageEventHandler</tt>.
|
||||||
|
*/
|
||||||
|
public final T delegate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of the STUN messages that this <tt>MessageEventHandler</tt>
|
||||||
|
* is interested in.
|
||||||
|
*/
|
||||||
|
public final char messageType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>MessageTypeEventHandler</tt> which is to
|
||||||
|
* forward STUN messages with a specific type to a specific handler.
|
||||||
|
*
|
||||||
|
* @param messageType the type of the STUN messages that the new
|
||||||
|
* instance is to forward to the specified handler <tt>delegate</tt>
|
||||||
|
* @param delegate the handler to which the new instance is to forward
|
||||||
|
* STUN messages with the specified <tt>messageType</tt>
|
||||||
|
*/
|
||||||
|
public MessageTypeEventHandler(char messageType, T delegate)
|
||||||
|
{
|
||||||
|
if (delegate == null)
|
||||||
|
throw new NullPointerException("delegate");
|
||||||
|
|
||||||
|
this.messageType = messageType;
|
||||||
|
this.delegate = delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (!getClass().isInstance(obj))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
MessageTypeEventHandler<?> mteh = (MessageTypeEventHandler<?>) obj;
|
||||||
|
|
||||||
|
return
|
||||||
|
(messageType == mteh.messageType)
|
||||||
|
&& delegate.equals(mteh.delegate);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode()
|
||||||
|
{
|
||||||
|
return (messageType | delegate.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements <tt>MessageEventHandler</tt> for <tt>RequestListener</tt>.
|
||||||
|
*
|
||||||
|
* @author Lubomir Marinov
|
||||||
|
*/
|
||||||
|
private static class RequestListenerMessageEventHandler
|
||||||
|
extends MessageTypeEventHandler<RequestListener>
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>RequestListenerMessageEventHandler</tt> which
|
||||||
|
* is to implement <tt>MessageEventHandler</tt> for a specific
|
||||||
|
* <tt>RequestListener</tt>.
|
||||||
|
*
|
||||||
|
* @param requestListener the <tt>RequestListener</tt> for which the new
|
||||||
|
* instance is to implement <tt>MessageEventHandler</tt>
|
||||||
|
*/
|
||||||
|
public RequestListenerMessageEventHandler(
|
||||||
|
RequestListener requestListener)
|
||||||
|
{
|
||||||
|
super(Message.STUN_REQUEST, requestListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies this <tt>MessageEventHandler</tt> that a STUN message has
|
||||||
|
* been received, parsed and is ready for delivery.
|
||||||
|
*
|
||||||
|
* @param e a <tt>StunMessageEvent</tt> which encapsulates the STUN
|
||||||
|
* message to be handled
|
||||||
|
* @see MessageEventHandler#handleMessageEvent(StunMessageEvent)
|
||||||
|
*/
|
||||||
|
public void handleMessageEvent(StunMessageEvent e)
|
||||||
|
{
|
||||||
|
delegate.processRequest(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -55,12 +55,16 @@ public class RawMessage
|
|||||||
TransportAddress remoteAddress,
|
TransportAddress remoteAddress,
|
||||||
TransportAddress localAddress)
|
TransportAddress localAddress)
|
||||||
{
|
{
|
||||||
//... don't do a null check - let it throw an NP exception
|
/*
|
||||||
|
* Let NullPointerException go out.
|
||||||
this.messageBytes = new byte[messageBytes.length];
|
*
|
||||||
|
* The length of the array messgeBytes may be enormous while
|
||||||
|
* messageLength may be tiny so it does not make sense to clone
|
||||||
|
* messageBytes.
|
||||||
|
*/
|
||||||
|
this.messageBytes = new byte[messageLength];
|
||||||
|
System.arraycopy(messageBytes, 0, this.messageBytes, 0, messageLength);
|
||||||
this.messageLength = messageLength;
|
this.messageLength = messageLength;
|
||||||
System.arraycopy(messageBytes, 0, this.messageBytes,
|
|
||||||
0, messageBytes.length);
|
|
||||||
this.localAddress = localAddress;
|
this.localAddress = localAddress;
|
||||||
this.remoteAddress = remoteAddress;
|
this.remoteAddress = remoteAddress;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,6 +24,7 @@ import org.ice4j.security.*;
|
|||||||
* configure the stack.
|
* configure the stack.
|
||||||
*
|
*
|
||||||
* @author Emil Ivov
|
* @author Emil Ivov
|
||||||
|
* @author Lubomir Marinov
|
||||||
*/
|
*/
|
||||||
public class StunStack
|
public class StunStack
|
||||||
implements MessageEventHandler
|
implements MessageEventHandler
|
||||||
@ -280,6 +281,57 @@ public class StunStack
|
|||||||
return netAccessManager;
|
return netAccessManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a specific STUN <tt>Indication</tt> to a specific destination
|
||||||
|
* <tt>TransportAddress</tt> through a socket registered with this
|
||||||
|
* <tt>StunStack</tt> using a specific <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param indication the STUN <tt>Indication</tt> to be sent to the
|
||||||
|
* specified destination <tt>TransportAddress</tt> through the socket with
|
||||||
|
* the specified <tt>TransportAddress</tt>
|
||||||
|
* @param sendTo the <tt>TransportAddress</tt> of the destination to which
|
||||||
|
* the specified <tt>indication</tt> is to be sent
|
||||||
|
* @param sendThrough the <tt>TransportAddress</tt> of the socket registered
|
||||||
|
* with this <tt>StunStack</tt> through which the specified
|
||||||
|
* <tt>indication</tt> is to be sent
|
||||||
|
* @throws StunException if anything goes wrong while sending the specified
|
||||||
|
* <tt>indication</tt> to the destination <tt>sendTo</tt> through the socket
|
||||||
|
* identified by <tt>sendThrough</tt>
|
||||||
|
*/
|
||||||
|
public void sendIndication(
|
||||||
|
Indication indication,
|
||||||
|
TransportAddress sendTo,
|
||||||
|
TransportAddress sendThrough)
|
||||||
|
throws StunException
|
||||||
|
{
|
||||||
|
if (indication.getTransactionID() == null)
|
||||||
|
{
|
||||||
|
indication.setTransactionID(
|
||||||
|
TransactionID.createNewTransactionID().getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
getNetAccessManager().sendMessage(indication, sendThrough, sendTo);
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException iaex)
|
||||||
|
{
|
||||||
|
throw
|
||||||
|
new StunException(
|
||||||
|
StunException.ILLEGAL_ARGUMENT,
|
||||||
|
"Failed to send STUN indication: " + indication,
|
||||||
|
iaex);
|
||||||
|
}
|
||||||
|
catch (IOException ioex)
|
||||||
|
{
|
||||||
|
throw
|
||||||
|
new StunException(
|
||||||
|
StunException.NETWORK_ERROR,
|
||||||
|
"Failed to send STUN indication: " + indication,
|
||||||
|
ioex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends the specified request through the specified access point, and
|
* Sends the specified request through the specified access point, and
|
||||||
* registers the specified ResponseCollector for later notification.
|
* registers the specified ResponseCollector for later notification.
|
||||||
@ -427,6 +479,24 @@ public class StunStack
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new <tt>MessageEventHandler</tt> which is to be notified about
|
||||||
|
* STUN indications received at a specific local <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param localAddr the <tt>TransportAddress</tt> of the local socket for
|
||||||
|
* which received STUN indications are to be reported to the specified
|
||||||
|
* <tt>MessageEventHandler</tt>
|
||||||
|
* @param indicationListener the <tt>MessageEventHandler</tt> which is to be
|
||||||
|
* registered for notifications about STUN indications received at the
|
||||||
|
* specified local <tt>TransportAddress</tt>
|
||||||
|
*/
|
||||||
|
public void addIndicationListener(
|
||||||
|
TransportAddress localAddr,
|
||||||
|
MessageEventHandler indicationListener)
|
||||||
|
{
|
||||||
|
eventDispatcher.addIndicationListener(localAddr, indicationListener);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the listener that should be notified when a new Request is received.
|
* Sets the listener that should be notified when a new Request is received.
|
||||||
* @param requestListener the listener interested in incoming requests.
|
* @param requestListener the listener interested in incoming requests.
|
||||||
@ -436,6 +506,24 @@ public class StunStack
|
|||||||
this.eventDispatcher.addRequestListener( requestListener );
|
this.eventDispatcher.addRequestListener( requestListener );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes an existing <tt>MessageEventHandler</tt> to no longer be notified
|
||||||
|
* about STUN indications received at a specific local
|
||||||
|
* <tt>TransportAddress</tt>.
|
||||||
|
*
|
||||||
|
* @param localAddr the <tt>TransportAddress</tt> of the local socket for
|
||||||
|
* which received STUN indications are to no longer be reported to the
|
||||||
|
* specified <tt>MessageEventHandler</tt>
|
||||||
|
* @param indicationListener the <tt>MessageEventHandler</tt> which is to be
|
||||||
|
* unregistered for notifications about STUN indications received at the
|
||||||
|
* specified local <tt>TransportAddress</tt>
|
||||||
|
*/
|
||||||
|
public void removeIndicationListener(
|
||||||
|
TransportAddress localAddr,
|
||||||
|
MessageEventHandler indicationListener)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the specified listener from the local listener list. (If any
|
* Removes the specified listener from the local listener list. (If any
|
||||||
* instances of this listener have been registered for a particular
|
* instances of this listener have been registered for a particular
|
||||||
@ -607,6 +695,11 @@ public class StunStack
|
|||||||
logger.fine("all tids in stock were" + clientTransactions);
|
logger.fine("all tids in stock were" + clientTransactions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// indication
|
||||||
|
else if (msg instanceof Indication)
|
||||||
|
{
|
||||||
|
eventDispatcher.fireMessageEvent(event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -13,16 +13,27 @@ import java.util.*;
|
|||||||
import org.ice4j.*;
|
import org.ice4j.*;
|
||||||
import org.ice4j.ice.*;
|
import org.ice4j.ice.*;
|
||||||
import org.ice4j.ice.harvest.*;
|
import org.ice4j.ice.harvest.*;
|
||||||
|
import org.ice4j.security.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple ice4j testing scenario. The sample application would create and start
|
* Simple ice4j testing scenario. The sample application would create and start
|
||||||
* both agents and make one of them and make one of them run checks against the
|
* both agents and make one of them run checks against the other.
|
||||||
* other.
|
|
||||||
*
|
*
|
||||||
* @author Emil Ivov
|
* @author Emil Ivov
|
||||||
|
* @author Lubomir Marinov
|
||||||
*/
|
*/
|
||||||
public class Ice
|
public class Ice
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The indicator which determines whether the <tt>Ice</tt> application (i.e.
|
||||||
|
* the run-sample Ant target) is to start connectivity establishment of the
|
||||||
|
* remote peer (in addition to the connectivity establishment of the local
|
||||||
|
* agent which is always started, of course).
|
||||||
|
*/
|
||||||
|
private static final boolean START_CONNECTIVITY_ESTABLISHMENT_OF_REMOTE_PEER
|
||||||
|
= true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start time for debugging purposes.
|
* Start time for debugging purposes.
|
||||||
*/
|
*/
|
||||||
@ -55,6 +66,9 @@ public class Ice
|
|||||||
localAgent.setRemoteUfrag(remotePeer.getLocalUfrag());
|
localAgent.setRemoteUfrag(remotePeer.getLocalUfrag());
|
||||||
localAgent.setRemotePassword(remotePeer.getLocalPassword());
|
localAgent.setRemotePassword(remotePeer.getLocalPassword());
|
||||||
|
|
||||||
|
if (START_CONNECTIVITY_ESTABLISHMENT_OF_REMOTE_PEER)
|
||||||
|
transferRemoteCandidates(remotePeer, localAgent);
|
||||||
|
|
||||||
remotePeer.setRemoteUfrag(localAgent.getLocalUfrag());
|
remotePeer.setRemoteUfrag(localAgent.getLocalUfrag());
|
||||||
remotePeer.setRemotePassword(localAgent.getLocalPassword());
|
remotePeer.setRemotePassword(localAgent.getLocalPassword());
|
||||||
|
|
||||||
@ -64,6 +78,9 @@ public class Ice
|
|||||||
|
|
||||||
localAgent.startConnectivityEstablishment();
|
localAgent.startConnectivityEstablishment();
|
||||||
|
|
||||||
|
if (START_CONNECTIVITY_ESTABLISHMENT_OF_REMOTE_PEER)
|
||||||
|
remotePeer.startConnectivityEstablishment();
|
||||||
|
|
||||||
System.out.println("Local audio clist:\n"
|
System.out.println("Local audio clist:\n"
|
||||||
+ localAgent.getStream("audio").getCheckList());
|
+ localAgent.getStream("audio").getCheckList());
|
||||||
|
|
||||||
@ -95,9 +112,11 @@ public class Ice
|
|||||||
{
|
{
|
||||||
long processingEndTime = System.currentTimeMillis();
|
long processingEndTime = System.currentTimeMillis();
|
||||||
|
|
||||||
System.out.println("Agent entered the " + evt.getNewValue()
|
Object iceProcessingState = evt.getNewValue();
|
||||||
+ " state.");
|
|
||||||
if(evt.getNewValue() == IceProcessingState.COMPLETED)
|
System.out.println(
|
||||||
|
"Agent entered the " + iceProcessingState + " state.");
|
||||||
|
if(iceProcessingState == IceProcessingState.COMPLETED)
|
||||||
{
|
{
|
||||||
System.out.println("Total ICE processing time: "
|
System.out.println("Total ICE processing time: "
|
||||||
+ (processingEndTime - startTime) + "ms");
|
+ (processingEndTime - startTime) + "ms");
|
||||||
@ -129,8 +148,8 @@ public class Ice
|
|||||||
System.out.println(stream.getCheckList());
|
System.out.println(stream.getCheckList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(evt.getNewValue() == IceProcessingState.TERMINATED
|
else if(iceProcessingState == IceProcessingState.TERMINATED
|
||||||
|| evt.getNewValue() == IceProcessingState.FAILED)
|
|| iceProcessingState == IceProcessingState.FAILED)
|
||||||
{
|
{
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
@ -159,9 +178,7 @@ public class Ice
|
|||||||
if(remoteStream != null)
|
if(remoteStream != null)
|
||||||
transferRemoteCandidates(localStream, remoteStream);
|
transferRemoteCandidates(localStream, remoteStream);
|
||||||
else
|
else
|
||||||
{
|
|
||||||
localAgent.removeStream(localStream);
|
localAgent.removeStream(localStream);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,9 +204,7 @@ public class Ice
|
|||||||
if(remoteComponent != null)
|
if(remoteComponent != null)
|
||||||
transferRemoteCandidates(localComponent, remoteComponent);
|
transferRemoteCandidates(localComponent, remoteComponent);
|
||||||
else
|
else
|
||||||
{
|
|
||||||
localStream.removeComponent(localComponent);
|
localStream.removeComponent(localComponent);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,6 +252,7 @@ public class Ice
|
|||||||
{
|
{
|
||||||
Agent agent = new Agent();
|
Agent agent = new Agent();
|
||||||
|
|
||||||
|
// STUN
|
||||||
StunCandidateHarvester stunHarv = new StunCandidateHarvester(
|
StunCandidateHarvester stunHarv = new StunCandidateHarvester(
|
||||||
new TransportAddress("sip-communicator.net", 3478, Transport.UDP));
|
new TransportAddress("sip-communicator.net", 3478, Transport.UDP));
|
||||||
StunCandidateHarvester stun6Harv = new StunCandidateHarvester(
|
StunCandidateHarvester stun6Harv = new StunCandidateHarvester(
|
||||||
@ -246,8 +262,25 @@ public class Ice
|
|||||||
agent.addCandidateHarvester(stunHarv);
|
agent.addCandidateHarvester(stunHarv);
|
||||||
agent.addCandidateHarvester(stun6Harv);
|
agent.addCandidateHarvester(stun6Harv);
|
||||||
|
|
||||||
|
// TURN
|
||||||
|
String[] hostnames
|
||||||
|
= new String[]
|
||||||
|
{
|
||||||
|
"130.79.90.150",
|
||||||
|
"2001:660:4701:1001:230:5ff:fe1a:805f"
|
||||||
|
};
|
||||||
|
int port = 3478;
|
||||||
|
LongTermCredential longTermCredential
|
||||||
|
= new LongTermCredential("guest", "anonymouspower!!");
|
||||||
|
|
||||||
|
for (String hostname : hostnames)
|
||||||
|
agent.addCandidateHarvester(
|
||||||
|
new TurnCandidateHarvester(
|
||||||
|
new TransportAddress(hostname, port, Transport.UDP),
|
||||||
|
longTermCredential));
|
||||||
|
|
||||||
createStream(rtpPort, "audio", agent);
|
createStream(rtpPort, "audio", agent);
|
||||||
//createStream(rtpPort + 2, "video", agent);
|
// createStream(rtpPort + 2, "video", agent);
|
||||||
|
|
||||||
return agent;
|
return agent;
|
||||||
}
|
}
|
||||||
@ -287,15 +320,13 @@ public class Ice
|
|||||||
+ (endTime - startTime) +" ms");
|
+ (endTime - startTime) +" ms");
|
||||||
startTime = endTime;
|
startTime = endTime;
|
||||||
//rtcpComp
|
//rtcpComp
|
||||||
//agent.createComponent(
|
// agent.createComponent(
|
||||||
// stream, Transport.UDP, rtpPort + 1, rtpPort + 1, rtpPort + 101);
|
// stream, Transport.UDP, rtpPort + 1, rtpPort + 1, rtpPort + 101);
|
||||||
|
|
||||||
endTime = System.currentTimeMillis();
|
// endTime = System.currentTimeMillis();
|
||||||
System.out.println("RTCP Component created in "
|
// System.out.println("RTCP Component created in "
|
||||||
+ (endTime - startTime) +" ms");
|
// + (endTime - startTime) +" ms");
|
||||||
|
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user