us.ihmc.mockets
Class StreamMocket

java.lang.Object
  extended by us.ihmc.mockets.StreamMocket
All Implemented Interfaces:
java.io.Serializable

public class StreamMocket
extends java.lang.Object
implements java.io.Serializable

The Mocket class represents an endpoint of a mobile socket connection. The class is designed to be a drop-in replacement for the Socket class in the Java Platform API.

See Also:
Serialized Form

Nested Class Summary
 class StreamMocket.Statistics
          The Statistics class is used to retrieve statistics about the current mocket connection.
 
Constructor Summary
StreamMocket()
          Create a new, unconnected, endpoint for a mocket connection.
 
Method Summary
 void bind(java.net.SocketAddress localSocketAddress)
          Binds a specific socket address (IP and port) to the local endpoint.
 void close()
          Closes the connection.
 void connect(java.net.InetAddress remoteAddress, int remotePort)
          Opens a connection to the specified remote address and port.
 void connect(java.net.InetAddress remoteAddress, int remotePort, int connectTimeout)
          Opens a connection to the specified remote address and port.
protected  void finalize()
          Removes StreamMocket object.
 int getDataBufferingTime()
          Returns the maximum time for buffering outgoing data before transmitting data.
 java.io.InputStream getInputStream()
          Returns the input stream for this connection.
 java.net.SocketAddress getLocalSocketAddress()
          Returns the local address as a socket address.
 java.io.OutputStream getOutputStream()
          Returns the output stream for this connection.
 java.net.SocketAddress getRemoteSocketAddress()
          Returns the address of the remote peer as a socket address.
 StreamMocket.Statistics getStatistics()
          Returns the statistics associated with this mocket.
 boolean isConnected()
           
 void setDataBufferingTime(int ms)
          Sets the maximum time for buffering outgoing data before transmitting data.
 void setStatusListener(MocketStatusListener msl)
          Register a callback function to be invoked when no data (or keepalive) has been received from the peer mocket.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StreamMocket

public StreamMocket()
             throws java.net.SocketException
Create a new, unconnected, endpoint for a mocket connection.

Throws:
java.net.SocketException
Method Detail

setStatusListener

public void setStatusListener(MocketStatusListener msl)
Register a callback function to be invoked when no data (or keepalive) has been received from the peer mocket. The callback will indicate the time (in milliseconds) since last contact. If the callback returns true, the mocket connection will be closed. The peer is declared unreachable and the callback is invoked if no messages are received from the peer for more than 2 seconds. This waiting time is twice the default interval between keep-alive messages.

Parameters:
msl - callback function to register.

connect

public void connect(java.net.InetAddress remoteAddress,
                    int remotePort)
             throws java.io.IOException
Opens a connection to the specified remote address and port.

Parameters:
remoteAddress - the remote address to connect to. Specified as an IP address.
remotePort - the remote port to connect to.
Throws:
java.io.IOException

connect

public void connect(java.net.InetAddress remoteAddress,
                    int remotePort,
                    int connectTimeout)
             throws java.io.IOException
Opens a connection to the specified remote address and port. Allows to choose the connection attempt timeout.

Parameters:
remoteAddress - the remote address to connect to. Specified as an IP address.
remotePort - the remote port to connect to.
Throws:
java.io.IOException - if there was a problem in opening the connection

getLocalSocketAddress

public java.net.SocketAddress getLocalSocketAddress()
Returns the local address as a socket address.

Returns:
local address.

getRemoteSocketAddress

public java.net.SocketAddress getRemoteSocketAddress()
Returns the address of the remote peer as a socket address.

Returns:
address of the remote peer.

bind

public void bind(java.net.SocketAddress localSocketAddress)
          throws java.io.IOException
Binds a specific socket address (IP and port) to the local endpoint. NOTE: This address will not be retained if the mocket is serialized.

Parameters:
localSocketAddress - the socket address to which the local endpoint should be bound
Throws:
java.io.IOException - in case there is a problem with binding to the specified address

getStatistics

public StreamMocket.Statistics getStatistics()
Returns the statistics associated with this mocket.

Returns:
the Statistics object for this mocket connection.

setDataBufferingTime

public void setDataBufferingTime(int ms)
Sets the maximum time for buffering outgoing data before transmitting data. Equivalent to TCP_NDELAY socket option. Note that setting this to 0 generates a packet each time send() is called. Accuracy of this setting depends on the period of the run() loop in the transmitter - currently 100ms.

Parameters:
ms - milliseconds data will wait in the buffer before transmission.

getDataBufferingTime

public int getDataBufferingTime()
Returns the maximum time for buffering outgoing data before transmitting data.

Returns:
data buffering time in milliseconds.
See Also:
setDataBufferingTime(int)

isConnected

public boolean isConnected()
Returns:

finalize

protected void finalize()
Removes StreamMocket object.

Overrides:
finalize in class java.lang.Object

close

public void close()
Closes the connection.


getInputStream

public java.io.InputStream getInputStream()
Returns the input stream for this connection.

Returns:
the input stream for the connection.

getOutputStream

public java.io.OutputStream getOutputStream()
Returns the output stream for this connection.

Returns:
the output stream for the connection.