jist.swans.trans
Class TcpSocket

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

public class TcpSocket
extends java.lang.Object
implements SocketInterface.TcpSocketInterface

SWANS Implementation of Socket entity.

Since:
SWANS1.0

Nested Class Summary
static class TcpSocket.TcpSocketCallback
          Implementation of Socket Callback for TcpSocket.
 
Nested classes/interfaces inherited from interface jist.swans.trans.SocketInterface
SocketInterface.TcpServerSocketInterface, SocketInterface.TcpSocketInterface
 
Field Summary
static long DELAYED_ACK_TIME
          Number of milliseconds to wait before sending an ACK.
static int FULL_DEBUG
          Debug printing flag.
static int INFO
          Debug printing flag.
static short INIT_WINDOW_SIZE
          Initial window size.
static int MSL
          Maximum Segment Lifetime.
static int MSS
          Maximum Segment Size.
static int OFF
          Debug printing flag.
static long PERSIST_TIMER
          Number of seconds to wait before sending probe packets.
static int PRINTOUT
          Indicator for printing debug outputs.
static long RETRANSMIT_TIMEOUT
          Number of seconds to wait for first retransmission timer.
static long RETRANSMIT_TIMEOUT_FINAL
          Number of seconds to wait for second retransmission timer.
static int TCP_DEBUG
          Debug printing flag.
 
Constructor Summary
  TcpSocket()
          Creates an unconnected socket, with the system-default type of SocketImpl.
  TcpSocket(java.net.InetAddress address, int port)
          Creates a stream socket and connects it to the specified port number at the specified IP address.
  TcpSocket(java.net.InetAddress host, int port, boolean stream)
          Deprecated.
  TcpSocket(java.net.InetAddress address, int port, java.net.InetAddress localAddr, int localPort)
          Creates a socket and connects it to the specified remote address on the specified remote port.
protected TcpSocket(java.net.SocketImpl impl)
          Creates an unconnected Socket with a user-specified SocketImpl.
  TcpSocket(java.lang.String host, int port)
          Creates a stream socket and connects it to the specified port number on the named host.
  TcpSocket(java.lang.String host, int port, boolean stream)
          Deprecated.
  TcpSocket(java.lang.String host, int port, java.net.InetAddress localAddr, int localPort)
          Creates a socket and connects it to the specified remote host on the specified remote port.
protected TcpSocket(TransInterface.TransTcpInterface tcpEntity, java.net.InetAddress rAddr, short rPort, java.net.InetAddress lAddr, short lPort, int initialAckNum, short winSize)
          Creates an unconnected Socket with a user-specified TransInterface Entity and SYN packet received by TcpServerSocket.
 
Method Summary
 void _jistPostInit()
          Post-constructor call.
 void bind(java.net.SocketAddress bindpoint)
          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 connect(java.net.SocketAddress endpoint)
          Connects this socket to the server.
 void connect(java.net.SocketAddress endpoint, int timeout)
          Connects this socket to the server with a specified timeout value.
 void constructPackets()
          Creates packets to be sent based on the receiver's advertised window (managing flow control).
 void createProxy()
          Create an entity reference to itself.
protected  void establishingConnection()
          this method is used to wait for incoming ACK packet for connection establishment.
 byte[] getBytesFromSocket(int length)
          This method is called by the input stream to retrieve data from the transport layer.
 java.nio.channels.SocketChannel getChannel()
          Returns the unique SocketChannel object associated with this socket, if any.
 java.net.InetAddress getInetAddress()
          Returns the local address of this server socket/socket.
 InputStream getInputStream()
          Returns an input stream for this socket.
 boolean getKeepAlive()
          Tests if SO_KEEPALIVE is enabled.
 java.net.InetAddress getLocalAddress()
          Gets the local address to which the socket is bound.
 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.
 boolean getOOBInline()
          Tests if OOBINLINE is enabled.
 OutputStream getOutputStream()
          Returns an output stream for this socket.
 int getPort()
          Returns the remote port to which this socket is connected.
 SocketInterface.TcpSocketInterface getProxy()
          Returns the entity reference to the socket itself.
 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.
 java.net.SocketAddress getRemoteSocketAddress()
          Returns the address of the endpoint this socket is connected to, or null if it is unconnected.
 boolean getReuseAddress()
          Tests if SO_REUSEADDR is enabled.
 int getSendBufferSize()
          Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket.
 int getSoLinger()
          Returns setting for SO_LINGER.
 int getSoTimeout()
          Retrieve setting for SO_TIMEOUT.
 boolean getTcpNoDelay()
          Tests if TCP_NODELAY is enabled.
 int getTrafficClass()
          Gets traffic class or type-of-service in the IP header for packets sent from this Socket.
 boolean isBound()
          Returns the binding state of the ServerSocket.
 boolean isClosed()
          Returns the closed state of the ServerSocket.
 boolean isConnected()
          Returns the connection state of the socket.
 boolean isInputShutdown()
          Returns wether the read-half of the socket connection is closed.
 boolean isOutputShutdown()
          Returns wether the write-half of the socket connection is closed.
 void persistTimerTimeout(int timerId, int seqNum)
          Execution when persist timer times out (sending probe message).
protected  void printMessage(TransTcp.TcpMessage msg, boolean isReceive)
          Prints out the message header and payload.
 int queueBytes(byte[] data)
          This method is called to send bytes to the other side.
 void resetTimerTimeout(int timerId)
          Execution when reset timer times out (closing the socket).
 void retransmitTimerTimeout(int seqNum, long time)
          Attempt to retransmit because the timer times out.
protected  TransTcp.TcpMessage sendDataPacket(byte[] data)
          Send a packet to the remote socket.
protected  void sendFINPacket()
          A method to send FIN packet.
protected  void sendFirstACKPacket()
          A method to send ACK packet.
protected  void sendRSTPacket(int seqNum, int ackNum)
          A method to send RST packet.
protected  void sendSYNACKPacket()
          A method to send SYNACK packet.
protected  void sendSYNPacket()
          A method to send SYN packet.
 void sendUrgentData(int data)
          Send one byte of urgent data on the socket.
 void setKeepAlive(boolean on)
          Enable/disable SO_KEEPALIVE.
 void setOOBInline(boolean on)
          Enable/disable OOBINLINE (receipt of TCP urgent data) By default, this option is disabled and TCP urgent data received on a socket is silently discarded.
 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.
 void setSendBufferSize(int size)
          Sets the SO_SNDBUF option to the specified value for this Socket.
static void setSocketImplFactory(java.net.SocketImplFactory fac)
          Sets the client socket implementation factory for the application.
 void setSoLinger(boolean on, int linger)
          Enable/disable SO_LINGER with the specified linger time in seconds.
 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.
 void setTcpNoDelay(boolean on)
          Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
 void setTrafficClass(int tc)
          Sets traffic class or type-of-service octet in the IP header for packets sent from this Socket.
 void shutdownInput()
          Places the input stream for this socket at "end of stream".
 void shutdownOutput()
          Disables the output stream for this socket.
 void startPersistTimer(int seqNum)
          Methods for persist timer (sender sending packets to probe receiver window).
 void startResetTimer()
          Methods for reset timer (this timer is used when establishing or closing connection --> If no reply is received, connection is reset).
 void startRetransmitTimer(int seqNum, long time)
          Schedule a retransmission for a message.
 void startTimeWaitTimer()
          Methods for timer during TIME_WAIT state.
 void timeWaitTimerTimeout()
          Execution when time wait timer times out (closing the socket).
 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

OFF

public static final int OFF
Debug printing flag. OFF = no debug output

See Also:
Constant Field Values

INFO

public static final int INFO
Debug printing flag. INFO = minimum debug output

See Also:
Constant Field Values

TCP_DEBUG

public static final int TCP_DEBUG
Debug printing flag. TCP_DEBUG = debug output, showing only packet traffic

See Also:
Constant Field Values

FULL_DEBUG

public static final int FULL_DEBUG
Debug printing flag. FULL_DEBUG = full debug output

See Also:
Constant Field Values

PRINTOUT

public static final int PRINTOUT
Indicator for printing debug outputs.

See Also:
Constant Field Values

MSS

public static final int MSS
Maximum Segment Size. Number of bytes to transfer in one packet.

See Also:
Constant Field Values

MSL

public static final int MSL
Maximum Segment Lifetime. Maximum amount of time any segment can exist in the network before being discarded. (Can be 30 seconds, 1 minute, or 2 minutes) (TCP has to wait for 2*MSL before the socket pair (client IP address, client port number, server IP address, and server port number) can be reused)

See Also:
Constant Field Values

DELAYED_ACK_TIME

public static final long DELAYED_ACK_TIME
Number of milliseconds to wait before sending an ACK.

See Also:
Constant Field Values

RETRANSMIT_TIMEOUT

public static final long RETRANSMIT_TIMEOUT
Number of seconds to wait for first retransmission timer.

See Also:
Constant Field Values

RETRANSMIT_TIMEOUT_FINAL

public static final long RETRANSMIT_TIMEOUT_FINAL
Number of seconds to wait for second retransmission timer.

See Also:
Constant Field Values

PERSIST_TIMER

public static final long PERSIST_TIMER
Number of seconds to wait before sending probe packets. This happens when receiver is advertising zero window.

See Also:
Constant Field Values

INIT_WINDOW_SIZE

public static final short INIT_WINDOW_SIZE
Initial window size.

See Also:
Constant Field Values
Constructor Detail

TcpSocket

public TcpSocket()
Creates an unconnected socket, with the system-default type of SocketImpl.


TcpSocket

public TcpSocket(java.net.InetAddress address,
                 int port)
Creates a stream socket and connects it to the specified port number at the specified IP address.

Parameters:
address - the IP address
port - the port number

TcpSocket

public TcpSocket(java.net.InetAddress host,
                 int port,
                 boolean stream)
Deprecated. Use DatagramSocket instead for UDP transport.

Parameters:
host - the IP address
port - the port number
stream - if true, create a stream socket; otherwise, create a datagram socket.

TcpSocket

public TcpSocket(java.net.InetAddress address,
                 int port,
                 java.net.InetAddress localAddr,
                 int localPort)
Creates a socket and connects it to the specified remote address on the specified remote port.

Parameters:
address - the remote address
port - the remote port
localAddr - the local address the socket is bound to
localPort - the local port the socket is bound to

TcpSocket

protected TcpSocket(java.net.SocketImpl impl)
Creates an unconnected Socket with a user-specified SocketImpl.

Parameters:
impl - an instance of a SocketImpl the subclass wishes to use on the Socket

TcpSocket

protected TcpSocket(TransInterface.TransTcpInterface tcpEntity,
                    java.net.InetAddress rAddr,
                    short rPort,
                    java.net.InetAddress lAddr,
                    short lPort,
                    int initialAckNum,
                    short winSize)
Creates an unconnected Socket with a user-specified TransInterface Entity and SYN packet received by TcpServerSocket.

Parameters:
tcpEntity - entity reference to transport layer
rAddr - remote address
rPort - remote port number
lAddr - local address
lPort - local port number
initialAckNum - initial acknowledgement number
winSize - the other side's receiver window size

TcpSocket

public TcpSocket(java.lang.String host,
                 int port)
Creates a stream socket and connects it to the specified port number on the named host.

Parameters:
host - the host name, or null for the loopback address.
port - the port number.

TcpSocket

public TcpSocket(java.lang.String host,
                 int port,
                 boolean stream)
Deprecated. Use DatagramSocket instead for UDP transport.

Parameters:
host - the host name, or null for the loopback address.
port - the port number.
stream - a boolean indicating whether this is a stream socket or a datagram socket.

TcpSocket

public TcpSocket(java.lang.String host,
                 int port,
                 java.net.InetAddress localAddr,
                 int localPort)
Creates a socket and connects it to the specified remote host on the specified remote port.

Parameters:
host - the name of the remote host, or null for the loopback address.
port - the remote port
localAddr - the local address the socket is bound to
localPort - the local port the socket is bound to
Method Detail

createProxy

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


getProxy

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

Returns:
Entity reference to TcpSocket

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

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.

close

public void close()
Closes this socket.

Specified by:
close in interface SocketInterface

connect

public void connect(java.net.SocketAddress endpoint)
Connects this socket to the server.

Specified by:
connect in interface SocketInterface.TcpSocketInterface
Parameters:
endpoint - the SocketAddress

connect

public void connect(java.net.SocketAddress endpoint,
                    int timeout)
Connects this socket to the server with a specified timeout value.

Specified by:
connect in interface SocketInterface.TcpSocketInterface
Parameters:
endpoint - the SocketAddress
timeout - the timeout value to be used in milliseconds

getChannel

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

Specified by:
getChannel in interface SocketInterface.TcpSocketInterface
Returns:
the 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.

getInputStream

public InputStream getInputStream()
Returns an input stream for this socket.

Specified by:
getInputStream in interface SocketInterface.TcpSocketInterface
Returns:
an input stream for reading bytes from this socket.

getKeepAlive

public boolean getKeepAlive()
Tests if SO_KEEPALIVE is enabled.

Specified by:
getKeepAlive in interface SocketInterface.TcpSocketInterface
Returns:
a boolean indicating whether or not SO_KEEPALIVE is enabled.

getLocalAddress

public java.net.InetAddress getLocalAddress()
Gets the local address to which the socket is bound.

Specified by:
getLocalAddress in interface SocketInterface.TcpSocketInterface
Returns:
the local address to which the socket is bound or InetAddress.anyLocalAddress() if the socket is not bound yet.

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.

getOOBInline

public boolean getOOBInline()
Tests if OOBINLINE is enabled.

Specified by:
getOOBInline in interface SocketInterface.TcpSocketInterface
Returns:
a boolean indicating whether or not OOBINLINE is enabled.

getOutputStream

public OutputStream getOutputStream()
Returns an output stream for this socket.

Specified by:
getOutputStream in interface SocketInterface.TcpSocketInterface
Returns:
an output stream for writing bytes to this socket.

getPort

public int getPort()
Returns the remote port to which this socket is connected.

Specified by:
getPort in interface SocketInterface.TcpSocketInterface
Returns:
the remote port number to which this socket is connected, or 0 if the socket is not connected 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.

getRemoteSocketAddress

public java.net.SocketAddress getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

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

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.

getSendBufferSize

public int getSendBufferSize()
Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket.

Specified by:
getSendBufferSize in interface SocketInterface.TcpSocketInterface
Returns:
the value of the SO_SNDBUF option for this Socket.

getSoLinger

public int getSoLinger()
Returns setting for SO_LINGER.

Specified by:
getSoLinger in interface SocketInterface.TcpSocketInterface
Returns:
the setting for SO_LINGER.

getSoTimeout

public int getSoTimeout()
Retrieve setting for SO_TIMEOUT.

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

getTcpNoDelay

public boolean getTcpNoDelay()
Tests if TCP_NODELAY is enabled.

Specified by:
getTcpNoDelay in interface SocketInterface.TcpSocketInterface
Returns:
a boolean indicating whether or not TCP_NODELAY is enabled.

getTrafficClass

public int getTrafficClass()
Gets traffic class or type-of-service in the IP header for packets sent from this Socket.

Specified by:
getTrafficClass in interface SocketInterface.TcpSocketInterface
Returns:
the traffic class or type-of-service already set

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

isConnected

public boolean isConnected()
Returns the connection state of the socket.

Specified by:
isConnected in interface SocketInterface.TcpSocketInterface
Returns:
true if the socket successfuly connected to a server

isInputShutdown

public boolean isInputShutdown()
Returns wether the read-half of the socket connection is closed.

Specified by:
isInputShutdown in interface SocketInterface.TcpSocketInterface
Returns:
true if the input of the socket has been shutdown

isOutputShutdown

public boolean isOutputShutdown()
Returns wether the write-half of the socket connection is closed.

Specified by:
isOutputShutdown in interface SocketInterface.TcpSocketInterface
Returns:
true if the output of the socket has been shutdown

sendUrgentData

public void sendUrgentData(int data)
Send one byte of urgent data on the socket.

Specified by:
sendUrgentData in interface SocketInterface.TcpSocketInterface
Parameters:
data - The byte of data to send

setKeepAlive

public void setKeepAlive(boolean on)
Enable/disable SO_KEEPALIVE.

Specified by:
setKeepAlive in interface SocketInterface.TcpSocketInterface
Parameters:
on - whether or not to have socket keep alive turned on

setOOBInline

public void setOOBInline(boolean on)
Enable/disable OOBINLINE (receipt of TCP urgent data) By default, this option is disabled and TCP urgent data received on a socket is silently discarded.

Specified by:
setOOBInline in interface SocketInterface.TcpSocketInterface
Parameters:
on - true to enable OOBINLINE, false to disable.

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

setSendBufferSize

public void setSendBufferSize(int size)
Sets the SO_SNDBUF option to the specified value for this Socket.

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

setSocketImplFactory

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

Parameters:
fac - the desired factory

setSoLinger

public void setSoLinger(boolean on,
                        int linger)
Enable/disable SO_LINGER with the specified linger time in seconds.

Specified by:
setSoLinger in interface SocketInterface.TcpSocketInterface
Parameters:
on - whether or not to linger on.
linger - how long to linger for, if on is true.

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

setTcpNoDelay

public void setTcpNoDelay(boolean on)
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).

Specified by:
setTcpNoDelay in interface SocketInterface.TcpSocketInterface
Parameters:
on - true to enable TCP_NODELAY, false to disable.

setTrafficClass

public void setTrafficClass(int tc)
Sets traffic class or type-of-service octet in the IP header for packets sent from this Socket.

Specified by:
setTrafficClass in interface SocketInterface.TcpSocketInterface
Parameters:
tc - an int value for the bitset.

shutdownInput

public void shutdownInput()
                   throws java.io.IOException
Places the input stream for this socket at "end of stream".

Specified by:
shutdownInput in interface SocketInterface.TcpSocketInterface
Throws:
java.io.IOException - if an I/O error occurs when shutting down this socket.

shutdownOutput

public void shutdownOutput()
                    throws java.io.IOException
Disables the output stream for this socket.

Specified by:
shutdownOutput in interface SocketInterface.TcpSocketInterface
Throws:
java.io.IOException - if an I/O error occurs when shutting down this socket.

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.

establishingConnection

protected void establishingConnection()
this method is used to wait for incoming ACK packet for connection establishment.


printMessage

protected void printMessage(TransTcp.TcpMessage msg,
                            boolean isReceive)
Prints out the message header and payload.

Parameters:
msg - TCP message to print out
isReceive - true if printing for receiving side

sendSYNPacket

protected void sendSYNPacket()
A method to send SYN packet.


sendSYNACKPacket

protected void sendSYNACKPacket()
A method to send SYNACK packet.


sendFirstACKPacket

protected void sendFirstACKPacket()
A method to send ACK packet.


sendFINPacket

protected void sendFINPacket()
A method to send FIN packet.


sendRSTPacket

protected void sendRSTPacket(int seqNum,
                             int ackNum)
A method to send RST packet.

Parameters:
seqNum - sequence number
ackNum - acknowledgement number

sendDataPacket

protected TransTcp.TcpMessage sendDataPacket(byte[] data)
Send a packet to the remote socket. To send an ACK packet, set data to null.

Parameters:
data - data to send
Returns:
sent packet

constructPackets

public void constructPackets()
Creates packets to be sent based on the receiver's advertised window (managing flow control).

Specified by:
constructPackets in interface SocketInterface.TcpSocketInterface

queueBytes

public int queueBytes(byte[] data)
This method is called to send bytes to the other side. What this method does is storing the bytes in the send buffer and then call sendPackets method to send the bytes.

Specified by:
queueBytes in interface SocketInterface.TcpSocketInterface
Parameters:
data - bytes to be stored in the send buffer
Returns:
the number of bytes actually stored

getBytesFromSocket

public byte[] getBytesFromSocket(int length)
This method is called by the input stream to retrieve data from the transport layer.

Specified by:
getBytesFromSocket in interface SocketInterface.TcpSocketInterface
Parameters:
length - number of bytes to retrieve
Returns:
byte array containing data from socket

startRetransmitTimer

public void startRetransmitTimer(int seqNum,
                                 long time)
Schedule a retransmission for a message.

Specified by:
startRetransmitTimer in interface SocketInterface.TcpSocketInterface
Parameters:
seqNum - sequence number of message to retransmit
time - wait time before attempting to retransmit

retransmitTimerTimeout

public void retransmitTimerTimeout(int seqNum,
                                   long time)
Attempt to retransmit because the timer times out.

Specified by:
retransmitTimerTimeout in interface SocketInterface.TcpSocketInterface
Parameters:
seqNum - sequence number of message to retransmit
time - wait time before attempting to retransmit

startPersistTimer

public void startPersistTimer(int seqNum)
Methods for persist timer (sender sending packets to probe receiver window).

Specified by:
startPersistTimer in interface SocketInterface.TcpSocketInterface
Parameters:
seqNum - number of the probe message

persistTimerTimeout

public void persistTimerTimeout(int timerId,
                                int seqNum)
Execution when persist timer times out (sending probe message).

Specified by:
persistTimerTimeout in interface SocketInterface.TcpSocketInterface
Parameters:
timerId - ID of the persist timer
seqNum - number of the probe message

startResetTimer

public void startResetTimer()
Methods for reset timer (this timer is used when establishing or closing connection --> If no reply is received, connection is reset).

Specified by:
startResetTimer in interface SocketInterface.TcpSocketInterface

resetTimerTimeout

public void resetTimerTimeout(int timerId)
Execution when reset timer times out (closing the socket).

Specified by:
resetTimerTimeout in interface SocketInterface.TcpSocketInterface
Parameters:
timerId - ID of the reset timer

startTimeWaitTimer

public void startTimeWaitTimer()
Methods for timer during TIME_WAIT state.

Specified by:
startTimeWaitTimer in interface SocketInterface.TcpSocketInterface

timeWaitTimerTimeout

public void timeWaitTimerTimeout()
Execution when time wait timer times out (closing the socket).

Specified by:
timeWaitTimerTimeout in interface SocketInterface.TcpSocketInterface

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