com.msi.network.connection
Class TCPConnection

java.lang.Object
  extended bycom.msi.network.connection.TCPConnection
All Implemented Interfaces:
Connection

public class TCPConnection
extends java.lang.Object
implements Connection

TCPConnection - a TCP/IP socket connection.

Supports connection timeouts (with a default value) and reads with individual read timeouts.

Version:
1.0
Author:
Ken Thompson

Field Summary
private  java.net.InetAddress clientAddress
           
private  int clientPort
           
protected  java.net.Socket conn
          conn - Socket that is connected to the client.
private  long connTO
           
protected  java.io.BufferedInputStream instream
          instream -
private  TCPInterface interfaceController
           
private  int serverPort
           
 
Constructor Summary
TCPConnection(java.net.ServerSocket sock, TCPInterface ioif)
          Creates a new TCPConnection instance.
TCPConnection(java.net.ServerSocket sock, TCPInterface ioif, long cTO)
          Creates a new TCPConnection instance.
 
Method Summary
 void close()
          Close the connection freeing any system resources.
private  boolean endsWith(java.lang.String s, java.lang.String suf)
          Check in a case insensitive manner that the supplied string ends with the suffix.
 java.net.InetAddress getClientAddress()
          Describe getClientAddress method here.
 int getClientPort()
          Describe getClientPort method here.
 long getConnTO()
          Get the value of connTO.
 java.lang.String getLine()
          Read what the client has to write, stripping any leading whitespace and dropping the eol marker(s).
 java.lang.String getLine(long TO)
          Read what the client has to write, stripping any leading whitespace and dropping the eol marker(s).
 int getServerPort()
          Describe getServerPort method here.
 void init()
          Create a connection and set up the input stream
 void putLine(java.lang.String line)
          Send information back to the client one line at a time.
 void putLine(java.lang.String ln, java.net.InetAddress cAddress, int cPort)
          This supports external submission of the recipient address, which allows us to send datagrams to multiple clients across a single port.
 int read(byte[] data)
          Reads all the client input regardless of line breaks.
 int read(byte[] data, long TO)
          Reads all the client input regardless of line breaks.
 void reset()
          reset -
 void setConnTO(long v)
          Set the value of connTO.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clientAddress

private java.net.InetAddress clientAddress

clientPort

private int clientPort

serverPort

private int serverPort

connTO

private long connTO

instream

protected java.io.BufferedInputStream instream
instream -


conn

protected java.net.Socket conn
conn - Socket that is connected to the client.


interfaceController

private TCPInterface interfaceController
Constructor Detail

TCPConnection

public TCPConnection(java.net.ServerSocket sock,
                     TCPInterface ioif)
Creates a new TCPConnection instance.

Parameters:
sock - a ServerSocket value
ioif - the TCPInterface that manages this connection.

TCPConnection

public TCPConnection(java.net.ServerSocket sock,
                     TCPInterface ioif,
                     long cTO)
Creates a new TCPConnection instance.

Parameters:
sock - a ServerSocket value
ioif - the TCPInterface that manages this connection.
cTO - the new connection time out value.
Method Detail

getLine

public java.lang.String getLine()
                         throws ConnectionException
Read what the client has to write, stripping any leading whitespace and dropping the eol marker(s).

Specified by:
getLine in interface Connection
Returns:
a String value
Throws:
ConnectionException - if an error occurs

getLine

public java.lang.String getLine(long TO)
                         throws ConnectionException
Read what the client has to write, stripping any leading whitespace and dropping the eol marker(s).

Specified by:
getLine in interface Connection
Parameters:
TO - timeout in milliseconds
Returns:
a String value
Throws:
ConnectionException - if an error occurs

read

public int read(byte[] data,
                long TO)
         throws ConnectionException,
                java.io.IOException
Reads all the client input regardless of line breaks. Timeout granularity is in hundredths of seconds, but the TO parameter is not rounded. Timeout granularity is the sleep-time.

read bytes in a loop until byte buffer is full, or low-level read times out. Any bytes read before the timeout are returned and if it is the end of communications, the caller will find out on the next call. No bytes read indicates the client is gone missing.

Specified by:
read in interface Connection
Parameters:
data - buffer for input.
TO - timeout in milliseconds, 0 is forever
Returns:
number of bytes read, -1 at EOF.
Throws:
ConnectionException - if an error occurs
java.io.IOException

read

public int read(byte[] data)
         throws ConnectionException,
                java.io.IOException
Reads all the client input regardless of line breaks. Timeout granularity is in hundredths of seconds, but the TO parameter is not rounded. Timeout granularity is the sleep-time.

Specified by:
read in interface Connection
Parameters:
data - buffer for input.
Returns:
number of bytes read
Throws:
ConnectionException - if an error occurs
java.io.IOException

endsWith

private boolean endsWith(java.lang.String s,
                         java.lang.String suf)
Check in a case insensitive manner that the supplied string ends with the suffix.

Parameters:
s - string checked for ending
suf - suffix searched for
Returns:
true iff the string ends in the suffix.

putLine

public void putLine(java.lang.String line)
             throws ConnectionException
Send information back to the client one line at a time.

Specified by:
putLine in interface Connection
Parameters:
line - a String value
Throws:
ConnectionException - if an error occurs

putLine

public void putLine(java.lang.String ln,
                    java.net.InetAddress cAddress,
                    int cPort)
             throws ConnectionException
This supports external submission of the recipient address, which allows us to send datagrams to multiple clients across a single port.

Specified by:
putLine in interface Connection
Parameters:
ln - a String value
cAddress - an InetAddress value
cPort - an int value
Throws:
ConnectionException - if an error occurs

init

public void init()
          throws java.io.IOException
Create a connection and set up the input stream

Specified by:
init in interface Connection
Throws:
java.io.IOException - if an error occurs

close

public void close()
Close the connection freeing any system resources.

Specified by:
close in interface Connection

reset

public void reset()
           throws java.io.IOException
reset -

Specified by:
reset in interface Connection
Throws:
java.io.IOException - if an error occurs

getClientAddress

public java.net.InetAddress getClientAddress()
Describe getClientAddress method here.

Specified by:
getClientAddress in interface Connection
Returns:
an InetAddress value

getClientPort

public int getClientPort()
Describe getClientPort method here.

Specified by:
getClientPort in interface Connection
Returns:
an int value

getServerPort

public int getServerPort()
Describe getServerPort method here.

Specified by:
getServerPort in interface Connection
Returns:
an int value

getConnTO

public long getConnTO()
Get the value of connTO.

Returns:
value of connTO.

setConnTO

public void setConnTO(long v)
Set the value of connTO.

Parameters:
v - Value to assign to connTO.


Copyright © 2001-2002 MileStone Solutions, Inc. All Rights Reserved.