Removes code duplication.

This commit is contained in:
boris 2015-02-27 16:42:05 +00:00
parent 5e489a8f05
commit 6999ebc19d

View File

@ -263,7 +263,7 @@ class NetAccessManager
mp = new MessageProcessor(this); mp = new MessageProcessor(this);
mp.start(); mp.start();
logger.fine("A message processor has been relaunched because " logger.fine("A message processor has been relaunched because "
+"of an error."); + "of an error.");
} }
} }
@ -408,6 +408,35 @@ class NetAccessManager
} }
} }
/**
* Returns the <tt>Connector</tt> responsible for a particular source
* address and a particular destination address.
*
* @param src the source address.
* @param dst the destination address.
* Returns the <tt>Connector</tt> responsible for a particular source
* address and a particular destination address, or <tt>null</tt> if there's
* none.
*/
private Connector getConnector(TransportAddress src, TransportAddress dst)
{
synchronized (connectorsSyncRoot)
{
switch (src.getTransport())
{
case UDP:
return netUDPAccessPoints.get(src);
case TCP:
List<Connector> connectors = netTCPAccessPoints.get(src);
if (connectors != null)
return findTCPConnectorByRemoteAddress(connectors, dst);
}
}
return null;
}
//---------------thread pool implementation -------------------------------- //---------------thread pool implementation --------------------------------
/** /**
* Adjusts the number of concurrently running MessageProcessors. * Adjusts the number of concurrently running MessageProcessors.
@ -485,35 +514,6 @@ class NetAccessManager
} }
} }
/**
* Returns the <tt>Connector</tt> responsible for a particular source
* address and a particular destination address.
*
* @param src the source address.
* @param dst the destination address.
* Returns the <tt>Connector</tt> responsible for a particular source
* address and a particular destination address, or <tt>null</tt> if there's
* none.
*/
private Connector getConnector(TransportAddress src, TransportAddress dst)
{
synchronized (connectorsSyncRoot)
{
switch (src.getTransport())
{
case UDP:
return netUDPAccessPoints.get(src);
case TCP:
List<Connector> connectors = netTCPAccessPoints.get(src);
if (connectors != null)
return findTCPConnectorByRemoteAddress(connectors, dst);
}
}
return null;
}
//--------------- SENDING MESSAGES ----------------------------------------- //--------------- SENDING MESSAGES -----------------------------------------
/** /**
* Sends the specified stun message through the specified access point. * Sends the specified stun message through the specified access point.
@ -534,18 +534,7 @@ class NetAccessManager
throws IllegalArgumentException, throws IllegalArgumentException,
IOException IOException
{ {
byte[] bytes = stunMessage.encode(stunStack); sendMessage(stunMessage.encode(stunStack), srcAddr, remoteAddr);
Connector ap = getConnector(srcAddr, remoteAddr);
if (ap == null)
{
throw new IllegalArgumentException(
"No socket has been added for: " + srcAddr + " <-> "
+ remoteAddr);
}
ap.sendMessage(bytes, remoteAddr);
} }
/** /**
@ -568,17 +557,7 @@ class NetAccessManager
throws IllegalArgumentException, throws IllegalArgumentException,
IOException, StunException IOException, StunException
{ {
byte[] bytes = channelData.encode(); sendMessage(channelData.encode(), srcAddr, remoteAddr);
Connector ap = getConnector(srcAddr, remoteAddr);
if (ap == null)
{
throw new IllegalArgumentException(
"No socket has been added for source address: " + srcAddr);
}
ap.sendMessage(bytes, remoteAddr);
} }
/** /**
@ -592,21 +571,19 @@ class NetAccessManager
* access point that had not been installed, * access point that had not been installed,
* @throws IOException if an error occurs while sending message bytes * @throws IOException if an error occurs while sending message bytes
* through the network socket. * through the network socket.
* @throws StunException
*/ */
void sendMessage( void sendMessage(
byte[] bytes, byte[] bytes,
TransportAddress srcAddr, TransportAddress srcAddr,
TransportAddress remoteAddr) TransportAddress remoteAddr)
throws IllegalArgumentException, throws IllegalArgumentException,
IOException, StunException IOException
{ {
Connector ap = getConnector(srcAddr, remoteAddr); Connector ap = getConnector(srcAddr, remoteAddr);
if (ap == null) if (ap == null)
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"No socket has been added for source address: " + srcAddr); "No socket found for " + srcAddr + "->" + remoteAddr);
} }
ap.sendMessage(bytes, remoteAddr); ap.sendMessage(bytes, remoteAddr);