soc.server.genericServer
Class LocalStringConnection

java.lang.Object
  extended bysoc.server.genericServer.LocalStringConnection
All Implemented Interfaces:
java.lang.Runnable, StringConnection

public class LocalStringConnection
extends java.lang.Object
implements StringConnection, java.lang.Runnable

Symmetric buffered connection sending strings between two local peers. Uses vectors and thread synchronization, no actual network traffic. This class has a run method, but you must start the thread yourself. Constructors will not create or start a thread.

Author:
Jeremy D. Monin
  1.0.0 - 2007-11-18 - initial release
  1.0.1 - 2008-06-28 - add getConnectTime
  1.0.2 - 2008-07-30 - check if s already null in disconnect
  1.0.3 - 2008-08-08 - add disconnectSoft, getVersion, setVersion  

Field Summary
protected  boolean accepted
          Active connection, server has called accept, and not disconnected yet
protected  java.util.Date connectTime
           
protected  java.lang.Object data
          the abritrary app-specific data associated with this connection
protected static java.lang.Object EOF_MARKER
           
protected  java.lang.Exception error
           
protected  java.util.Vector in
           
protected  boolean in_reachedEOF
           
private  LocalStringConnection ourPeer
           
protected  Server ourServer
           
protected  java.util.Vector out
           
protected  boolean out_setEOF
           
protected  int remoteVersion
           
 
Constructor Summary
LocalStringConnection()
          Create a new, unused LocalStringConnection.
LocalStringConnection(LocalStringConnection peer)
          Constructor for an existing peer; we'll share two Vectors for in/out queues.
 
Method Summary
 boolean connect()
          Local version; nothing special to do to start reading messages.
 void connect(java.lang.String serverSocketName)
          Connect to specified stringport.
 void disconnect()
          close the socket, discard pending buffered data, set EOF.
 void disconnectSoft()
          Accept no further input, allow output to drain, don't immediately close the socket.
 java.util.Date getConnectTime()
           
 java.lang.Object getData()
           
 java.lang.Exception getError()
           
 LocalStringConnection getPeer()
          Remember, the peer's in is our out, and vice versa.
 Server getServer()
          Server-side: Reference to the server handling this connection.
 int getVersion()
          Give the version number (if known) of the remote end of this connection.
 java.lang.String host()
          Hostname of the remote side of the connection - Always returns localhost; this method required for StringConnection interface.
 boolean isAccepted()
          Is currently accepted by a server
 boolean isConnected()
          Are we currently connected and active?
 boolean isInEOF()
          Have we received an EOF marker inbound?
 boolean isOutEOF()
          Have we closed our outbound side?
 void put(java.lang.String dat)
          Send data over the connection.
 java.lang.String readNext()
          Read the next string sent from the remote end, blocking if necessary to wait.
 void run()
          For server-side (ourServer != null); continuously read and treat input.
 void setAccepted()
          Intended for server to call: Set our accepted flag.
 void setData(java.lang.Object dat)
          Set the app-specific data for this connection.
 void setEOF()
          Signal the end of outbound data.
 void setServer(Server srv)
          Server-side: Set the generic server for this connection.
 void setVersion(int version)
          Set the version number of the remote end of this connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOF_MARKER

protected static java.lang.Object EOF_MARKER

in

protected java.util.Vector in

out

protected java.util.Vector out

in_reachedEOF

protected boolean in_reachedEOF

out_setEOF

protected boolean out_setEOF

accepted

protected boolean accepted
Active connection, server has called accept, and not disconnected yet


ourPeer

private LocalStringConnection ourPeer

ourServer

protected Server ourServer

error

protected java.lang.Exception error

connectTime

protected java.util.Date connectTime

remoteVersion

protected int remoteVersion

data

protected java.lang.Object data
the abritrary app-specific data associated with this connection

Constructor Detail

LocalStringConnection

public LocalStringConnection()
Create a new, unused LocalStringConnection. After construction, call connect to use this object. This class has a run method, but you must start the thread yourself. Constructors will not create or start a thread.

See Also:
connect(String)

LocalStringConnection

public LocalStringConnection(LocalStringConnection peer)
                      throws java.io.EOFException
Constructor for an existing peer; we'll share two Vectors for in/out queues. This class has a run method, but you must start the thread yourself. Constructors will not create or start a thread.

Parameters:
peer - The peer to use.
Throws:
java.io.EOFException - If peer is at EOF already
java.lang.IllegalArgumentException - if peer is null, or already has a peer.
Method Detail

readNext

public java.lang.String readNext()
                          throws java.io.EOFException,
                                 java.lang.IllegalStateException
Read the next string sent from the remote end, blocking if necessary to wait. Synchronized on in-buffer.

Returns:
Next string in the in-buffer
Throws:
java.io.EOFException - Our input buffer has reached EOF
java.lang.IllegalStateException - Server has not yet accepted our connection

put

public void put(java.lang.String dat)
         throws java.lang.IllegalStateException
Send data over the connection. Does not block. Ignored if setEOF() has been called.

Specified by:
put in interface StringConnection
Parameters:
dat - Data to send
Throws:
java.lang.IllegalStateException - if not yet accepted by server

disconnect

public void disconnect()
close the socket, discard pending buffered data, set EOF.

Specified by:
disconnect in interface StringConnection

disconnectSoft

public void disconnectSoft()
Accept no further input, allow output to drain, don't immediately close the socket. Once called, isConnected() will return false, even if output is still being sent to the other side.

Specified by:
disconnectSoft in interface StringConnection

connect

public void connect(java.lang.String serverSocketName)
             throws java.net.ConnectException,
                    java.lang.IllegalStateException
Connect to specified stringport. Calling thread waits until accepted.

Parameters:
serverSocketName - stringport name to connect to
Throws:
java.net.ConnectException - If stringport name is not found, or is EOF, or if its connect/accept queue is full.
java.lang.IllegalStateException - If this object is already connected

getPeer

public LocalStringConnection getPeer()
Remember, the peer's in is our out, and vice versa.

Returns:
Returns our peer, or null if not yet connected.

isAccepted

public boolean isAccepted()
Is currently accepted by a server

Returns:
Are we currently connected, accepted, and ready to send/receive data?

setAccepted

public void setAccepted()
                 throws java.lang.IllegalStateException
Intended for server to call: Set our accepted flag. Peer must be non-null to set accepted. If our EOF is set, will not set accepted, but will not throw exception. (This happens if the server socket closes while we're in its accept queue.)

Throws:
java.lang.IllegalStateException - If we can't be, or already are, accepted

setEOF

public void setEOF()
Signal the end of outbound data. Not the same as closing, because we don't terminate the inbound side. Synchronizes on out-buffer.


isInEOF

public boolean isInEOF()
Have we received an EOF marker inbound?


isOutEOF

public boolean isOutEOF()
Have we closed our outbound side?

See Also:
setEOF()

getData

public java.lang.Object getData()
Specified by:
getData in interface StringConnection
Returns:
The app-specific data for this generic connection
See Also:
setData(Object)

setData

public void setData(java.lang.Object dat)
Set the app-specific data for this connection. This is anything your application wants to associate with the connection. The StringConnection system itself does not reference or use this data.

Specified by:
setData in interface StringConnection
Parameters:
dat - The new data, or null

getServer

public Server getServer()
Server-side: Reference to the server handling this connection.

Returns:
The generic server (optional) for this connection

setServer

public void setServer(Server srv)
Server-side: Set the generic server for this connection. If a server is set, its removeConnection method is called if our input reaches EOF. Call this before calling run().

Parameters:
srv - The new server, or null

getError

public java.lang.Exception getError()
Specified by:
getError in interface StringConnection
Returns:
Any error encountered, or null

getConnectTime

public java.util.Date getConnectTime()
Specified by:
getConnectTime in interface StringConnection
Returns:
Time of connection to server, or of object creation if that time's not available
See Also:
StringConnection.connect()

host

public java.lang.String host()
Hostname of the remote side of the connection - Always returns localhost; this method required for StringConnection interface.

Specified by:
host in interface StringConnection
Returns:
Hostname of the remote end of the connection

connect

public boolean connect()
Local version; nothing special to do to start reading messages. Call connect(serverSocketName) instead of this method.

Specified by:
connect in interface StringConnection
Returns:
Whether we've connected and been accepted by a StringServerSocket.
See Also:
connect(String)

isConnected

public boolean isConnected()
Are we currently connected and active?

Specified by:
isConnected in interface StringConnection

getVersion

public int getVersion()
Give the version number (if known) of the remote end of this connection. The meaning of this number is application-defined.

Specified by:
getVersion in interface StringConnection
Returns:
Version number, or 0 if unknown.

setVersion

public void setVersion(int version)
Set the version number of the remote end of this connection. The meaning of this number is application-defined.

Specified by:
setVersion in interface StringConnection
Parameters:
version - Version number, or 0 if unknown.

run

public void run()
For server-side (ourServer != null); continuously read and treat input. You must create and start the thread.

Specified by:
run in interface StringConnection