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_ClassPorts
the class-ports association.protected Map<Integer,Class>
m_PortClass
the port-class association.protected List<Integer>
m_Ports
the list of ports.protected static PortManager
m_Singleton
the singleton.static int
MAX_PORT
the maximum port.static int
MIN_PORT
the minimum (non-privileged) port.static int
NO_PORT
no port available.
-
Constructor Summary
Constructors Modifier Constructor Description protected
PortManager()
Initializes the port manager.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
bind(Class cls, int port)
Sets a port as used.void
bind(Object obj, int port)
Sets a port as used.void
clear()
Removes all ports.static PortManager
getSingleton()
Returns the singleton instance of the port manager.protected void
initialize()
Initializes the members.boolean
isAvailable(int port)
Checks whether the specified port is already in use.Integer
next(Class cls)
Returns the next available port.Integer
next(Class cls, int start)
Returns the next available port, starting with the given starting port.Integer
next(Object obj)
Returns the next available port.Integer
next(Object obj, int start)
Returns the next available port, starting with the given starting port.void
release(int port)
Removes the specified port from the internal list of used ports.int
size()
Returns the number of ports used.Class
usedBy(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
-
-