simnet
Class ServerSocket

java.lang.Object
  extended bysimnet.ServerSocket
All Implemented Interfaces:
PlugoutListener

public class ServerSocket
extends java.lang.Object
implements PlugoutListener

A TCP Server Socket. Need to use statistics.


Field Summary
protected  int backlog
          Maximum number of unaccepted connections (size of incomplete and complete queues cannot exceed this).
(package private)  java.util.LinkedList completeCQ
          The list of connections ready to be accepted.
protected  int connectionTimeout
          The timeout when trying accepting a connection (0=wait forever).
protected  java.lang.Object consumer
          The consumer that owns this socket.
static int CONSUMER_ID
          The notification code when the consumer gets plugged out (randomly chosen to avoid conflicts)
static int DEFAULT_QUEUE_SIZE
          The default size of the send and receive queues.
(package private)  java.util.HashSet incompleteCQ
          The set of half-open connections that have not been accepted.
protected  int port
          The port this socket is listening on (-1 for unbound).
protected  Simulator sim
          For convienence, keep a reference to the simulator around.
protected  int state
          The TCP state of this socket
protected  ServerSocketStatistics stats
          Keep track of our statistics.
protected  TCP tcp
          A pointer to a TCP object
 
Constructor Summary
(package private) ServerSocket(TCP tcp_transport, java.lang.Object c)
          Constructs a newly allocated ServerSocket object using the specified transport, with the specified consumer.
 
Method Summary
 Socket accept()
          Accept completed connections from this socket if one is available within connectionTimeout ms.
 void bind(int port)
          Bind this socket to the specified port.
 void close()
          Close this server socket.
 void finalize()
           
 int getBacklog()
          Return the number of unaccepted connections.
 int getBacklogLimit()
          Returns the maximum number of unaccepted connections.
 boolean isBound()
          Returns if this socket is bound to a local port.
 boolean isClosed()
          Returns if this socket has been closed (after being used).
 void listen(int backlog)
          Get a socket ready to accept connections supporting the specified number of unaccepted connections.
 void plugoutNotification(int id, java.lang.Object replacement, java.lang.Object[] args)
          When the consumer is getting plugged out, close this socket.
 void setConnectionTimeout(int cto)
          Sets the connection timeout to the specified value (0=wait forever) in ms.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CONSUMER_ID

public static final int CONSUMER_ID
The notification code when the consumer gets plugged out (randomly chosen to avoid conflicts)

See Also:
Constant Field Values

DEFAULT_QUEUE_SIZE

public static final int DEFAULT_QUEUE_SIZE
The default size of the send and receive queues.

See Also:
Constant Field Values

sim

protected Simulator sim
For convienence, keep a reference to the simulator around.


state

protected int state
The TCP state of this socket


port

protected int port
The port this socket is listening on (-1 for unbound).


connectionTimeout

protected int connectionTimeout
The timeout when trying accepting a connection (0=wait forever).


tcp

protected TCP tcp
A pointer to a TCP object


consumer

protected java.lang.Object consumer
The consumer that owns this socket.


backlog

protected int backlog
Maximum number of unaccepted connections (size of incomplete and complete queues cannot exceed this).


incompleteCQ

java.util.HashSet incompleteCQ
The set of half-open connections that have not been accepted.


completeCQ

java.util.LinkedList completeCQ
The list of connections ready to be accepted.


stats

protected ServerSocketStatistics stats
Keep track of our statistics.

Constructor Detail

ServerSocket

ServerSocket(TCP tcp_transport,
             java.lang.Object c)
Constructs a newly allocated ServerSocket object using the specified transport, with the specified consumer. Should not be called directly, use TCP.createServerSocket.

Method Detail

isClosed

public boolean isClosed()
Returns if this socket has been closed (after being used).


isBound

public boolean isBound()
Returns if this socket is bound to a local port.


setConnectionTimeout

public void setConnectionTimeout(int cto)
                          throws SimnetSocketException
Sets the connection timeout to the specified value (0=wait forever) in ms. May be called at any time, but will not affect calls to accept which already started.

Throws:
SimnetSocketException - if the parameter is negative.

bind

public void bind(int port)
          throws SimnetSocketException
Bind this socket to the specified port. If -1 is specified, the socket will be bound to any free local port. Note that using -1 does not make much sense for a server socket, but is supported anyway.

Parameters:
port - The port number to bind to
Throws:
SimnetSocketException - if this socket is already bound, is an invalid port or the port is already in use.

listen

public void listen(int backlog)
            throws SimnetSocketException
Get a socket ready to accept connections supporting the specified number of unaccepted connections.

Throws:
SimnetSocketException - if the socket is already used, or is not bound

getBacklog

public int getBacklog()
Return the number of unaccepted connections.


getBacklogLimit

public int getBacklogLimit()
Returns the maximum number of unaccepted connections.


accept

public Socket accept()
              throws SimnetSocketException,
                     java.lang.InterruptedException
Accept completed connections from this socket if one is available within connectionTimeout ms.

Throws:
SimnetSocketException - if this socket is not listening, gets closed, or no complete connection becomes avaialble.
java.lang.InterruptedException - if the call to wait gets interrupted.

close

public void close()
Close this server socket.


toString

public java.lang.String toString()

plugoutNotification

public void plugoutNotification(int id,
                                java.lang.Object replacement,
                                java.lang.Object[] args)
When the consumer is getting plugged out, close this socket.

Specified by:
plugoutNotification in interface PlugoutListener
Parameters:
id - The id passed to registerPlugoutNotification
replacement - the object that will be replacing the object that is being plugged out. It will be null if the object is getting plugged out, rather then being replaced with a new plugin.
args - were the args passed to registerPlugoutNotification

finalize

public void finalize()