Package adams.core.net
Class PortManager
- java.lang.Object
-
- adams.core.net.PortManager
-
- All Implemented Interfaces:
Serializable
public class PortManager extends Object implements Serializable
Keeps track of used ports.
NB: Using this class is no guarantee that the port isn't being used already, as not all code fragments might use this class!- Version:
- $Revision$
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected Map<Class,HashSet<Integer>>m_ClassPortsthe class-ports association.protected Map<Integer,Class>m_PortClassthe port-class association.protected List<Integer>m_Portsthe list of ports.protected static PortManagerm_Singletonthe singleton.static intMAX_PORTthe maximum port.static intMIN_PORTthe minimum (non-privileged) port.static intNO_PORTno port available.
-
Constructor Summary
Constructors Modifier Constructor Description protectedPortManager()Initializes the port manager.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbind(Class cls, int port)Sets a port as used.voidbind(Object obj, int port)Sets a port as used.voidclear()Removes all ports.static PortManagergetSingleton()Returns the singleton instance of the port manager.protected voidinitialize()Initializes the members.booleanisAvailable(int port)Checks whether the specified port is already in use.Integernext(Class cls)Returns the next available port.Integernext(Class cls, int start)Returns the next available port, starting with the given starting port.Integernext(Object obj)Returns the next available port.Integernext(Object obj, int start)Returns the next available port, starting with the given starting port.voidrelease(int port)Removes the specified port from the internal list of used ports.intsize()Returns the number of ports used.ClassusedBy(int port)Returns the associated class for the port.List<Integer>uses(Class cls)Returns the ports associated with a class.
-
-
-
Field Detail
-
MIN_PORT
public static final int MIN_PORT
the minimum (non-privileged) port.- See Also:
- Constant Field Values
-
MAX_PORT
public static final int MAX_PORT
the maximum port.- See Also:
- Constant Field Values
-
NO_PORT
public static final int NO_PORT
no port available.- See Also:
- Constant Field Values
-
m_Singleton
protected static PortManager m_Singleton
the singleton.
-
-
Method Detail
-
initialize
protected void initialize()
Initializes the members.
-
size
public int size()
Returns the number of ports used.- Returns:
- the number of ports
-
clear
public void clear()
Removes all ports.
-
next
public Integer next(Object obj)
Returns the next available port.- Parameters:
obj- the object requesting the port- Returns:
- the next available port
- Throws:
IllegalArgumentException- if object is null
-
next
public Integer next(Class cls)
Returns the next available port.- Parameters:
cls- the class requesting the port- Returns:
- the next available port
- Throws:
IllegalArgumentException- if class is null
-
bind
public void bind(Object obj, int port)
Sets a port as used.- Parameters:
obj- the object binding the portport- the port in use- Throws:
IllegalArgumentException- if object is null
-
bind
public void bind(Class cls, int port)
Sets a port as used.- Parameters:
cls- the class binding the portport- the port in use- Throws:
IllegalArgumentException- if object is null
-
next
public Integer next(Object obj, int start)
Returns the next available port, starting with the given starting port.- Parameters:
obj- the object requesting the portstart- the first port to start with- Returns:
- the next available port,
- Throws:
IllegalArgumentException- if object is null
-
next
public Integer next(Class cls, int start)
Returns the next available port, starting with the given starting port.- Parameters:
cls- the class requesting the portstart- the first port to start with- Returns:
- the next available port,
- Throws:
IllegalArgumentException- if class is null
-
isAvailable
public boolean isAvailable(int port)
Checks whether the specified port is already in use.- Parameters:
port- the port to check
-
release
public void release(int port)
Removes the specified port from the internal list of used ports.- Parameters:
port- the port to free
-
usedBy
public Class usedBy(int port)
Returns the associated class for the port.- Parameters:
port- the port to get the class for- Returns:
- the class, null if not available
-
uses
public List<Integer> uses(Class cls)
Returns the ports associated with a class.- Parameters:
cls- the class to get the used ports for- Returns:
- the ports
-
getSingleton
public static PortManager getSingleton()
Returns the singleton instance of the port manager.- Returns:
- the port manager
-
-