Package org.ow2.clif.util
Class NetConfHelper
- java.lang.Object
-
- org.ow2.clif.util.NetConfHelper
-
- All Implemented Interfaces:
java.lang.Runnable
public class NetConfHelper extends java.lang.Object implements java.lang.RunnableUtility class for automatic IP address selection. Contains both a server part and a client part, to implement a bidirectional TCP-based communication for selecting an IP address that is reachable from the server part:- the server must be running and listening to an address that is reachable from the client;
- the client opens a TCP connection to the server, and successively sends the IP addresses of all its locally configured network interfaces (but localhost);
- for each received address, the server tries to open a new socket towards the client towards the received address, and sends the actual client address advertised by the socket previously opened by the client;
- if the client receives the address sent by the server (within a given time-out), and if this address equals the candidate address that was first sent by the client, then this address is validated. Otherwise, the candidate address is rejected. Most probably, the client went through a proxy or Network Address Translation (NAT).
- Author:
- Bruno Dillenseger
-
-
Constructor Summary
Constructors Constructor Description NetConfHelper()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidmain(java.lang.String[] args)Runs either the server part or the client part.voidrun()Server part activityvoidstartServer(java.net.InetSocketAddress localAddr)Start the server part.voidstopServer()Stop the server partstatic java.net.InetAddresstryAddresses(java.lang.String ip, int port, int timeout_ms)Submit all IP addresses of locally configured network interfaces to the server part.
-
-
-
Method Detail
-
main
public static void main(java.lang.String[] args) throws java.lang.ExceptionRuns either the server part or the client part.- Parameters:
args- two forms:- server server_IP_address [server_port] - runs the server part, opening a TCP server socket on the given IP address and port number. When the port number is not given, it defaults to the CLIF registry's default port number + 1.
- client server_IP_address server_port timeout_ms - runs the client part, trying all available local IP addresses.
- Throws:
java.lang.Exception- either the provided arguments are not correct, or the server part could not be started because of a network-related problem (e.g. it can't bind to the provided IP address/port).
-
tryAddresses
public static java.net.InetAddress tryAddresses(java.lang.String ip, int port, int timeout_ms)Submit all IP addresses of locally configured network interfaces to the server part.- Parameters:
ip- the IP address of the configuration helper's server partport- the port number of the configuration helper's server parttimeout_ms- the time-out value waiting for a reply from the server after having submitted an IP address.- Returns:
- a valid IP address, or null if none passed the selection.
-
startServer
public void startServer(java.net.InetSocketAddress localAddr) throws ClifExceptionStart the server part.- Parameters:
localAddr- the local IP address and port to bind to.- Throws:
ClifException- the server part of the configuration helper could not be started.
-
stopServer
public void stopServer() throws ClifExceptionStop the server part- Throws:
ClifException- problem while trying to close the server socket
-
run
public void run()
Server part activity- Specified by:
runin interfacejava.lang.Runnable
-
-