Jitsi: the OpenSource Java VoIP and Instant Messaging client.

net.java.sip.communicator.impl.netaddr
Class NetworkAddressManagerServiceImpl

java.lang.Object
  extended by net.java.sip.communicator.impl.netaddr.NetworkAddressManagerServiceImpl
All Implemented Interfaces:
NetworkAddressManagerService

public class NetworkAddressManagerServiceImpl
extends Object
implements NetworkAddressManagerService

This implementation of the Network Address Manager allows you to intelligently retrieve the address of your localhost according to the destinations that you will be trying to reach. It also provides an interface to the ICE implementation in ice4j.

Author:
Emil Ivov

Field Summary
static String BIND_RETRIES_PROPERTY_NAME
          The name of the property containing the number of binds that we should should execute in case a port is already bound to (each retry would be on a new random port).
static int DEFAULT_STUN_SERVER_PORT
          Default STUN server port.
(package private)  DatagramSocket localHostFinderSocket
          The socket that we use for dummy connections during selection of a local address that has to be used when communicating with a specific location.
static String STUN_SRV_NAME
          The service name to use when discovering STUN servers through DNS using SRV requests as per RFC 5389.
static String TURN_SRV_NAME
          The service name to use when discovering TURN servers through DNS using SRV requests as per RFC 5766.
 
Fields inherited from interface net.java.sip.communicator.service.netaddr.NetworkAddressManagerService
BIND_RETRIES_DEFAULT_VALUE
 
Constructor Summary
NetworkAddressManagerServiceImpl()
           
 
Method Summary
 void addNetworkConfigurationChangeListener(NetworkConfigurationChangeListener listener)
          Adds new NetworkConfigurationChangeListener which will be informed for network configuration changes.
 DatagramSocket createDatagramSocket(InetAddress laddr, int preferredPort, int minPort, int maxPort)
          Creates a DatagramSocket and binds it to the specified localAddress and a port in the range specified by the minPort and maxPort parameters.
 org.ice4j.ice.Agent createIceAgent()
          Creates and returns an ICE agent that a protocol could use for the negotiation of media transport addresses.
 org.ice4j.ice.IceMediaStream createIceStream(int rtpPort, String streamName, org.ice4j.ice.Agent agent)
          Creates an IceMediaStrean and adds to it an RTP and and RTCP component, which also implies running the currently installed harvesters so that they would.
 org.ice4j.ice.harvest.StunCandidateHarvester discoverStunServer(String domainName, byte[] userName, byte[] password)
          Tries to discover a TURN or a STUN server for the specified domainName.
 byte[] getHardwareAddress(NetworkInterface iface)
          Returns the hardware address (i.e.
 InetAddress getLocalHost(InetAddress intendedDestination)
          Returns an InetAddress instance that represents the localhost, and that a socket can bind upon or distribute to peers as a contact address.
 InetSocketAddress getPublicAddressFor(InetAddress dst, int port)
          Tries to obtain an for the specified port.
 void propertyChange(PropertyChangeEvent evt)
          This method gets called when a bound property is changed.
 void removeNetworkConfigurationChangeListener(NetworkConfigurationChangeListener listener)
          Remove NetworkConfigurationChangeListener.
 void start()
          Initializes this network address manager service implementation.
 void stop()
          Kills all threads/processes launched by this thread (if any) and prepares it for shutdown.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

localHostFinderSocket

DatagramSocket localHostFinderSocket
The socket that we use for dummy connections during selection of a local address that has to be used when communicating with a specific location.


BIND_RETRIES_PROPERTY_NAME

public static final String BIND_RETRIES_PROPERTY_NAME
The name of the property containing the number of binds that we should should execute in case a port is already bound to (each retry would be on a new random port).

See Also:
Constant Field Values

DEFAULT_STUN_SERVER_PORT

public static final int DEFAULT_STUN_SERVER_PORT
Default STUN server port.

See Also:
Constant Field Values

TURN_SRV_NAME

public static final String TURN_SRV_NAME
The service name to use when discovering TURN servers through DNS using SRV requests as per RFC 5766.

See Also:
Constant Field Values

STUN_SRV_NAME

public static final String STUN_SRV_NAME
The service name to use when discovering STUN servers through DNS using SRV requests as per RFC 5389.

See Also:
Constant Field Values
Constructor Detail

NetworkAddressManagerServiceImpl

public NetworkAddressManagerServiceImpl()
Method Detail

start

public void start()
Initializes this network address manager service implementation.


stop

public void stop()
Kills all threads/processes launched by this thread (if any) and prepares it for shutdown. You may use this method as a reinitialization technique (you'll have to call start afterwards)


getLocalHost

public InetAddress getLocalHost(InetAddress intendedDestination)
Returns an InetAddress instance that represents the localhost, and that a socket can bind upon or distribute to peers as a contact address.

Specified by:
getLocalHost in interface NetworkAddressManagerService
Parameters:
intendedDestination - the destination that we'd like to use the localhost address with.
Returns:
an InetAddress instance representing the local host, and that a socket can bind upon or distribute to peers as a contact address.

getHardwareAddress

public byte[] getHardwareAddress(NetworkInterface iface)
Returns the hardware address (i.e. MAC address) of the specified interface name.

Specified by:
getHardwareAddress in interface NetworkAddressManagerService
Parameters:
iface - the NetworkInterface
Returns:
array of bytes representing the layer 2 address or null if interface does not exist

getPublicAddressFor

public InetSocketAddress getPublicAddressFor(InetAddress dst,
                                             int port)
                                      throws IOException,
                                             BindException
Tries to obtain an for the specified port.

Specified by:
getPublicAddressFor in interface NetworkAddressManagerService
Parameters:
dst - the destination that we'd like to use this address with.
port - the port whose mapping we are interested in.
Returns:
a public address corresponding to the specified port or null if all attempts to retrieve such an address have failed.
Throws:
IOException - if an error occurs while creating the socket.
BindException - if the port is already in use.

propertyChange

public void propertyChange(PropertyChangeEvent evt)
This method gets called when a bound property is changed.

Parameters:
evt - A PropertyChangeEvent object describing the event source and the property that has changed.

createDatagramSocket

public DatagramSocket createDatagramSocket(InetAddress laddr,
                                           int preferredPort,
                                           int minPort,
                                           int maxPort)
                                    throws IllegalArgumentException,
                                           IOException,
                                           BindException
Creates a DatagramSocket and binds it to the specified localAddress and a port in the range specified by the minPort and maxPort parameters. We first try to bind the newly created socket on the preferredPort port number (unless it is outside the [minPort, maxPort] range in which case we first try the minPort) and then proceed incrementally upwards until we succeed or reach the bind retries limit. If we reach the maxPort port number before the bind retries limit, we will then start over again at minPort and keep going until we run out of retries.

Specified by:
createDatagramSocket in interface NetworkAddressManagerService
Parameters:
laddr - the address that we'd like to bind the socket on.
preferredPort - the port number that we should try to bind to first.
minPort - the port number where we should first try to bind before moving to the next one (i.e. minPort + 1)
maxPort - the maximum port number where we should try binding before giving up and throwinG an exception.
Returns:
the newly created DatagramSocket.
Throws:
IllegalArgumentException - if either minPort or maxPort is not a valid port number or if minPort > maxPort.
IOException - if an error occurs while the underlying resolver lib is using sockets.
BindException - if we couldn't find a free port between minPort and maxPort before reaching the maximum allowed number of retries.

addNetworkConfigurationChangeListener

public void addNetworkConfigurationChangeListener(NetworkConfigurationChangeListener listener)
Adds new NetworkConfigurationChangeListener which will be informed for network configuration changes.

Specified by:
addNetworkConfigurationChangeListener in interface NetworkAddressManagerService
Parameters:
listener - the listener.

removeNetworkConfigurationChangeListener

public void removeNetworkConfigurationChangeListener(NetworkConfigurationChangeListener listener)
Remove NetworkConfigurationChangeListener.

Specified by:
removeNetworkConfigurationChangeListener in interface NetworkAddressManagerService
Parameters:
listener - the listener.

createIceAgent

public org.ice4j.ice.Agent createIceAgent()
Creates and returns an ICE agent that a protocol could use for the negotiation of media transport addresses. One ICE agent should only be used for a single session negotiation.

Specified by:
createIceAgent in interface NetworkAddressManagerService
Returns:
the newly created ICE Agent.

discoverStunServer

public org.ice4j.ice.harvest.StunCandidateHarvester discoverStunServer(String domainName,
                                                                       byte[] userName,
                                                                       byte[] password)
Tries to discover a TURN or a STUN server for the specified domainName. The method would first try to discover a TURN server and then fall back to STUN only. In both cases we would only care about a UDP transport.

Specified by:
discoverStunServer in interface NetworkAddressManagerService
Parameters:
domainName - the domain name that we are trying to discover a TURN server for.
userName - the name of the user we'd like to use when connecting to a TURN server (we won't be using credentials in case we only have a STUN server).
password - the password that we'd like to try when connecting to a TURN server (we won't be using credentials in case we only have a STUN server).
Returns:
A StunCandidateHarvester corresponding to the TURN or STUN server we discovered or null if there were no such records for the specified domainName

createIceStream

public org.ice4j.ice.IceMediaStream createIceStream(int rtpPort,
                                                    String streamName,
                                                    org.ice4j.ice.Agent agent)
                                             throws IllegalArgumentException,
                                                    IOException,
                                                    BindException
Creates an IceMediaStrean and adds to it an RTP and and RTCP component, which also implies running the currently installed harvesters so that they would.

Specified by:
createIceStream in interface NetworkAddressManagerService
Parameters:
rtpPort - the port that we should try to bind the RTP component on (the RTCP one would automatically go to rtpPort + 1)
streamName - the name of the stream to create
agent - the Agent that should create the stream.
Returns:
the newly created IceMediaStream.
Throws:
IllegalArgumentException - if rtpPort is not a valid port number.
IOException - if an error occurs while the underlying resolver is using sockets.
BindException - if we couldn't find a free port between within the default number of retries.

Jitsi: the OpenSource Java VoIP and Instant Messaging client.

Jitsi, the OpenSource Java VoIP and Instant Messaging client.
Distributable under LGPL license.