jist.swans.trans
Class TcpServerSocket

java.lang.Object
  extended by jist.swans.trans.TcpServerSocket
All Implemented Interfaces:
JistAPI.Proxiable, JistAPI.Timeless, SocketInterface, SocketInterface.TcpServerSocketInterface

public class TcpServerSocket
extends java.lang.Object
implements SocketInterface.TcpServerSocketInterface

SWANS Implementation of Server Socket entity.

Since:
SWANS1.0
Version:
$Id: TcpServerSocket.java,v 1.11 2004/04/06 16:07:51 barr Exp $
Author:
Kelwin Tamtoro <kt222@cs.cornell.edu>, Rimon Barr <barr+jist@cs.cornell.edu>

Nested Class Summary
static class TcpServerSocket.TcpServerSocketCallback
          Implementation of Socket Callback for TcpServerSocket.
 
Nested classes/interfaces inherited from interface jist.swans.trans.SocketInterface
SocketInterface.TcpServerSocketInterface, SocketInterface.TcpSocketInterface
 
Field Summary
private  int backlog
          Backlog.
private  boolean bindInConstructor
          Indicator if socket is to be bound when created.
private  TransInterface.SocketHandler.TcpHandler callback
          Reference to the server socket's callback.
private  Channel channel
          Channel (for blocking implementation).
private  int currentState
          Current state of the socket.
private  boolean isBound
          The bind state of the server socket.
private  boolean isClosed
          The state of the socket (true if socket is closed).
private  java.net.InetAddress laddr
          Local address.
private  int lport
          Local port.
private  SocketInterface.TcpServerSocketInterface self
          Entity reference to TcpServerSocket.
private  TransInterface.TransTcpInterface tcpEntity
          Entity reference to transport layer.
 
Constructor Summary
TcpServerSocket()
          Creates an unbound server socket.
TcpServerSocket(int port)
          Creates a server socket, bound to the specified port.
TcpServerSocket(int port, int backlog)
          Creates a server socket and binds it to the specified local port number, with the specified backlog.
TcpServerSocket(int port, int backlog, java.net.InetAddress bindAddr)
          Creates a server with the specified port, listen backlog, and local IP address to bind to.
 
Method Summary
 void _jistPostInit()
          Post-constructor call.
 TcpSocket accept()
          Listens for a connection to be made to this socket and accepts it.
 void bind(java.net.SocketAddress bindpoint)
          Binds the ServerSocket to a specific address (IP address and port number).
 void bind(java.net.SocketAddress endpoint, int backlog)
          Binds the ServerSocket to a specific address (IP address and port number).
 void checkPacketandState(TransTcp.TcpMessage msg, NetAddress src)
          Check and process the incoming packet depending on current state of the socket.
 void close()
          Closes this socket.
 void createProxy()
          Create an entity reference to itself.
 java.nio.channels.ServerSocketChannel getChannel()
          Returns the unique ServerSocketChannel object associated with this socket, if any.
 java.net.InetAddress getInetAddress()
          Returns the local address of this server socket/socket.
 int getLocalPort()
          Returns the port on which this socket is listening.
 java.net.SocketAddress getLocalSocketAddress()
          Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.
 SocketInterface.TcpServerSocketInterface getProxy()
          Returns the entity reference to the server socket itself.
static int getRandomSequenceNumber()
          Method to get a random sequence number.
 int getReceiveBufferSize()
          Gets the value of the SO_RCVBUF option for this ServerSocket, that is the proposed buffer size that will be used for Sockets accepted from this ServerSocket.
 boolean getReuseAddress()
          Tests if SO_REUSEADDR is enabled.
 int getSoTimeout()
          Retrieve setting for SO_TIMEOUT.
protected  void implAccept(java.net.Socket s)
          Subclasses of ServerSocket use this method to override accept() to return their own subclass of socket.
private  void initializeAll(java.net.InetAddress lAddr, int lPort, int backlog, boolean doBind)
          Initializes all of the server socket variables.
 boolean isBound()
          Returns the binding state of the ServerSocket.
 boolean isClosed()
          Returns the closed state of the ServerSocket.
 void setReceiveBufferSize(int size)
          Sets a default proposed value for the SO_RCVBUF option for sockets accepted from this ServerSocket.
 void setReuseAddress(boolean on)
          Enable/disable the SO_REUSEADDR socket option.
static void setSocketFactory(java.net.SocketImplFactory fac)
          Sets the server socket implementation factory for the application.
 void setSoTimeout(int timeout)
          Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
 void setTcpEntity(TransInterface.TransTcpInterface tcpEntity)
          Sets the reference to the network layer.
 java.lang.String toString()
          Returns the implementation address and implementation port of this socket as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

self

private SocketInterface.TcpServerSocketInterface self
Entity reference to TcpServerSocket.


lport

private int lport
Local port.


laddr

private java.net.InetAddress laddr
Local address.


channel

private Channel channel
Channel (for blocking implementation).


callback

private TransInterface.SocketHandler.TcpHandler callback
Reference to the server socket's callback.


tcpEntity

private TransInterface.TransTcpInterface tcpEntity
Entity reference to transport layer.


isBound

private boolean isBound
The bind state of the server socket.


isClosed

private boolean isClosed
The state of the socket (true if socket is closed).


backlog

private int backlog
Backlog.


bindInConstructor

private boolean bindInConstructor
Indicator if socket is to be bound when created.


currentState

private int currentState
Current state of the socket.

Constructor Detail

TcpServerSocket

public TcpServerSocket()
Creates an unbound server socket.


TcpServerSocket

public TcpServerSocket(int port)
Creates a server socket, bound to the specified port.

Parameters:
port - the port number, or 0 to use any free port.

TcpServerSocket

public TcpServerSocket(int port,
                       int backlog)
Creates a server socket and binds it to the specified local port number, with the specified backlog.

Parameters:
port - the specified port, or 0 to use any free port.
backlog - the maximum length of the queue.

TcpServerSocket

public TcpServerSocket(int port,
                       int backlog,
                       java.net.InetAddress bindAddr)
Creates a server with the specified port, listen backlog, and local IP address to bind to.

Parameters:
port - the local TCP port
backlog - the listen backlog
bindAddr - the local InetAddress the server will bind to
Method Detail

createProxy

public void createProxy()
Create an entity reference to itself.


getProxy

public SocketInterface.TcpServerSocketInterface getProxy()
Returns the entity reference to the server socket itself.

Returns:
Entity reference to TcpServerSocket

setTcpEntity

public void setTcpEntity(TransInterface.TransTcpInterface tcpEntity)
Sets the reference to the network layer.

Specified by:
setTcpEntity in interface SocketInterface
Parameters:
tcpEntity - entity reference to transport layer

_jistPostInit

public void _jistPostInit()
Post-constructor call. Since constructors can not be blocking (JiST/Java limitation), we rewrite a regular socket constructor to two calls. This is the second one, and it can be blocking, because it is a regular method.

Specified by:
_jistPostInit in interface SocketInterface

accept

public TcpSocket accept()
Listens for a connection to be made to this socket and accepts it.

Specified by:
accept in interface SocketInterface.TcpServerSocketInterface
Returns:
the new TcpSocket

bind

public void bind(java.net.SocketAddress bindpoint)
Binds the ServerSocket to a specific address (IP address and port number).

Specified by:
bind in interface SocketInterface
Parameters:
bindpoint - The IP address & port number to bind to.

bind

public void bind(java.net.SocketAddress endpoint,
                 int backlog)
Binds the ServerSocket to a specific address (IP address and port number).

Specified by:
bind in interface SocketInterface.TcpServerSocketInterface
Parameters:
endpoint - The IP address & port number to bind to.
backlog - The listen backlog length.

close

public void close()
Closes this socket.

Specified by:
close in interface SocketInterface

getChannel

public java.nio.channels.ServerSocketChannel getChannel()
Returns the unique ServerSocketChannel object associated with this socket, if any.

Specified by:
getChannel in interface SocketInterface.TcpServerSocketInterface
Returns:
the server-socket channel associated with this socket, or null if this socket was not created for a channel

getInetAddress

public java.net.InetAddress getInetAddress()
Returns the local address of this server socket/socket.

Specified by:
getInetAddress in interface SocketInterface
Returns:
the address to which this socket is bound, or null if the socket is unbound.

getLocalPort

public int getLocalPort()
Returns the port on which this socket is listening.

Specified by:
getLocalPort in interface SocketInterface
Returns:
the port number to which this socket is listening or -1 if the socket is not bound yet.

getLocalSocketAddress

public java.net.SocketAddress getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.

Specified by:
getLocalSocketAddress in interface SocketInterface
Returns:
a SocketAddress representing the local endpoint of this socket, or null if it is not bound yet.

getReceiveBufferSize

public int getReceiveBufferSize()
Gets the value of the SO_RCVBUF option for this ServerSocket, that is the proposed buffer size that will be used for Sockets accepted from this ServerSocket.

Specified by:
getReceiveBufferSize in interface SocketInterface
Returns:
the value of the SO_RCVBUF option for this Socket.

getReuseAddress

public boolean getReuseAddress()
Tests if SO_REUSEADDR is enabled.

Specified by:
getReuseAddress in interface SocketInterface
Returns:
a boolean indicating whether or not SO_REUSEADDR is enabled.

getSoTimeout

public int getSoTimeout()
Retrieve setting for SO_TIMEOUT.

Specified by:
getSoTimeout in interface SocketInterface
Returns:
the SO_TIMEOUT value

implAccept

protected void implAccept(java.net.Socket s)
Subclasses of ServerSocket use this method to override accept() to return their own subclass of socket.

Parameters:
s - the Socket

isBound

public boolean isBound()
Returns the binding state of the ServerSocket.

Specified by:
isBound in interface SocketInterface
Returns:
true if the ServerSocket succesfuly bound to an address

isClosed

public boolean isClosed()
Returns the closed state of the ServerSocket.

Specified by:
isClosed in interface SocketInterface
Returns:
true if the socket has been closed

setReceiveBufferSize

public void setReceiveBufferSize(int size)
Sets a default proposed value for the SO_RCVBUF option for sockets accepted from this ServerSocket.

Specified by:
setReceiveBufferSize in interface SocketInterface
Parameters:
size - the size to which to set the receive buffer size. This value must be greater than 0.

setReuseAddress

public void setReuseAddress(boolean on)
Enable/disable the SO_REUSEADDR socket option.

Specified by:
setReuseAddress in interface SocketInterface
Parameters:
on - whether to enable or disable the socket option

setSocketFactory

public static void setSocketFactory(java.net.SocketImplFactory fac)
Sets the server socket implementation factory for the application.

Parameters:
fac - the desired factory.

setSoTimeout

public void setSoTimeout(int timeout)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.

Specified by:
setSoTimeout in interface SocketInterface
Parameters:
timeout - the specified timeout, in milliseconds

toString

public java.lang.String toString()
Returns the implementation address and implementation port of this socket as a String.

Specified by:
toString in interface SocketInterface
Overrides:
toString in class java.lang.Object
Returns:
a string representation of this socket.

initializeAll

private void initializeAll(java.net.InetAddress lAddr,
                           int lPort,
                           int backlog,
                           boolean doBind)
Initializes all of the server socket variables.

Parameters:
lAddr - local address
lPort - local port
backlog - listen backlog length
doBind - true if socket is to be bound when created

checkPacketandState

public void checkPacketandState(TransTcp.TcpMessage msg,
                                NetAddress src)
Check and process the incoming packet depending on current state of the socket.

Specified by:
checkPacketandState in interface SocketInterface
Parameters:
msg - the incoming TCP message
src - source of packet

getRandomSequenceNumber

public static int getRandomSequenceNumber()
Method to get a random sequence number.

Returns:
random number less than 1000000