p2ps.imp.endpoint.TCP
Class TCPEndpoint

java.lang.Object
  extended byp2ps.imp.endpoint.TCP.TCPEndpoint
All Implemented Interfaces:
Endpoint, StreamEndpoint

public class TCPEndpoint
extends java.lang.Object
implements StreamEndpoint

A generic TCP socket class

Version:
$Revision: 295 $
Author:
Ian Wang

Field Summary
static int READ_BUFFER_SIZE
           
static int RECEIVE_BUFFER_SIZE
           
static int SEND_BUFFER_SIZE
           
 
Constructor Summary
TCPEndpoint(java.lang.String scheme, MessageProtocolHandler messhandler)
          Creates a TCP endpoint
TCPEndpoint(java.lang.String scheme, MessageProtocolHandler messhandler, java.util.Map properties)
          Creates a TCP endpoint
TCPEndpoint(java.lang.String scheme, Socket socket, MessageProtocolHandler messhandler)
          Creates a TCP endpoint with the specified socket handler.
 
Method Summary
 void addEndpointMessageListener(EndpointMessageListener listener)
          Adds a listener to receive data from this socket
 void close()
          Closes the endpoint.
 void connect(java.lang.String address, int port)
          Connects the socket to a specified address and port
protected  Socket createSocket(java.net.SocketAddress address, java.util.Map properties)
          Creates the socket
 java.net.URI getEndpointURI()
           
protected  java.io.InputStream getInputStream()
           
protected  MessageProtocolHandler getMessageProtocolHandler()
           
protected  java.io.OutputStream getOutputStream()
           
 java.net.URI getSendURI()
           
protected  Socket getSocket()
           
 void init()
          initializes the endpoint
 void initConnection()
          Connects the endpoint
protected  void initMessageHandling()
          Initializes the message handler utilities
 boolean isBlock()
           
 boolean isClose()
           
 boolean isClosed()
           
 boolean isInputEndpoint()
           
protected  boolean isMessageHandlingEnabled()
           
 boolean isOutputEndpoint()
           
 void receiveMessage(DataMessage mess)
          handle the specified Data Message received on the specified endpoint
 void removeEndpointMessageListener(EndpointMessageListener listener)
          Removes a listener from this socket
 java.io.InputStream retrieveInputStream()
          An InputStream for the endpoint.
 java.io.OutputStream retrieveOutputStream()
          An OutputStream for the endpoint.
 void send(byte[] message)
          Sends a message from the endpoint to the address the endpoint is bound to, with no sendid.
 void send(java.lang.String sendid, byte[] message)
          Send a message from the socket
 void setBlock(boolean block)
          Sets whether blocking sends are used
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

RECEIVE_BUFFER_SIZE

public static int RECEIVE_BUFFER_SIZE

SEND_BUFFER_SIZE

public static int SEND_BUFFER_SIZE

READ_BUFFER_SIZE

public static int READ_BUFFER_SIZE
Constructor Detail

TCPEndpoint

public TCPEndpoint(java.lang.String scheme,
                   Socket socket,
                   MessageProtocolHandler messhandler)
            throws java.io.IOException
Creates a TCP endpoint with the specified socket handler. Init connection should be called to initialize the endpoint.


TCPEndpoint

public TCPEndpoint(java.lang.String scheme,
                   MessageProtocolHandler messhandler)
            throws java.io.IOException
Creates a TCP endpoint


TCPEndpoint

public TCPEndpoint(java.lang.String scheme,
                   MessageProtocolHandler messhandler,
                   java.util.Map properties)
            throws java.io.IOException
Creates a TCP endpoint

Method Detail

createSocket

protected Socket createSocket(java.net.SocketAddress address,
                              java.util.Map properties)
                       throws java.io.IOException
Creates the socket

Throws:
java.io.IOException

addEndpointMessageListener

public void addEndpointMessageListener(EndpointMessageListener listener)
Adds a listener to receive data from this socket

Specified by:
addEndpointMessageListener in interface Endpoint

removeEndpointMessageListener

public void removeEndpointMessageListener(EndpointMessageListener listener)
Removes a listener from this socket

Specified by:
removeEndpointMessageListener in interface Endpoint

init

public void init()
          throws java.io.IOException
Description copied from interface: Endpoint
initializes the endpoint

Specified by:
init in interface Endpoint
Throws:
java.io.IOException

initConnection

public void initConnection()
                    throws java.io.IOException
Connects the endpoint

Throws:
java.io.IOException

initMessageHandling

protected void initMessageHandling()
                            throws java.io.IOException
Initializes the message handler utilities

Throws:
java.io.IOException

getEndpointURI

public java.net.URI getEndpointURI()
Specified by:
getEndpointURI in interface Endpoint
Returns:
the address of this endpoint

getSendURI

public java.net.URI getSendURI()
Specified by:
getSendURI in interface Endpoint
Returns:
the address being sent to by this endpoint (if known)

isInputEndpoint

public boolean isInputEndpoint()
Specified by:
isInputEndpoint in interface Endpoint
Returns:
true if this endpoint is input enabled

isOutputEndpoint

public boolean isOutputEndpoint()
Specified by:
isOutputEndpoint in interface Endpoint
Returns:
true if this endpoint is output enabled

isBlock

public boolean isBlock()
Returns:
true if blocking sends are used

setBlock

public void setBlock(boolean block)
Sets whether blocking sends are used


connect

public void connect(java.lang.String address,
                    int port)
             throws java.io.IOException
Connects the socket to a specified address and port

Throws:
java.io.IOException

getSocket

protected Socket getSocket()
Returns:
the socket for this TCPEndpoint

retrieveInputStream

public java.io.InputStream retrieveInputStream()
                                        throws java.io.IOException
An InputStream for the endpoint. Note that calling this method must be called before the init() method on the endpoint and causes the message listener interface for the endpoint to be premantly disabled.

Specified by:
retrieveInputStream in interface StreamEndpoint
Throws:
java.io.IOException

retrieveOutputStream

public java.io.OutputStream retrieveOutputStream()
                                          throws java.io.IOException
An OutputStream for the endpoint. Unlike retrieveInputStream this does not disable the message listener interface, however it can be used to bypass this interface.

Specified by:
retrieveOutputStream in interface StreamEndpoint
Throws:
java.io.IOException

getInputStream

protected java.io.InputStream getInputStream()
Returns:
the input stream for this TCPEndpoint

getOutputStream

protected java.io.OutputStream getOutputStream()
Returns:
the output stream for this TCPEndpoint

isMessageHandlingEnabled

protected boolean isMessageHandlingEnabled()
Returns:
true if message handling is enabled for this endpoint

getMessageProtocolHandler

protected MessageProtocolHandler getMessageProtocolHandler()
Returns:
the MessageProtocolHandler for this TCPEndpoint

receiveMessage

public void receiveMessage(DataMessage mess)
handle the specified Data Message received on the specified endpoint


send

public void send(byte[] message)
          throws java.io.IOException
Sends a message from the endpoint to the address the endpoint is bound to, with no sendid.

Specified by:
send in interface Endpoint
Throws:
java.io.IOException

send

public void send(java.lang.String sendid,
                 byte[] message)
          throws java.io.IOException
Send a message from the socket

Specified by:
send in interface Endpoint
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes the endpoint. Note that isClosed should be called to determine when the endpoint is fully closed.

Specified by:
close in interface Endpoint
Throws:
java.io.IOException

isClose

public boolean isClose()
Returns:
true is the underlying socket should be closed

isClosed

public boolean isClosed()
Specified by:
isClosed in interface Endpoint
Returns:
true is the endpoint is fully closed

toString

public java.lang.String toString()