soc.server.genericServer
Class LocalStringConnection

java.lang.Object
  extended by soc.server.genericServer.StringConnection
      extended by soc.server.genericServer.LocalStringConnection
All Implemented Interfaces:
java.lang.Runnable

public class LocalStringConnection
extends StringConnection
implements 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.

As used within JSettlers, the structure of this class has much in common with LocalStringConnection, as they both subclass StringConnection. If you add something to one class, you should probably add it to the other, or to the superclass instead.

  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
  1.0.4 - 2008-09-04 - add appData
  1.0.5 - 2009-05-31 - add isVersionKnown, setVersion(int,bool), setVersionTracking,
                       isInputAvailable, callback to processFirstCommand,
                       wantsHideTimeoutMessage, setHideTimeoutMessage;
                       common constructor code moved to init().
  1.0.5.1- 2009-10-26- javadoc warnings fixed
  1.0.5.2- 2010-04-05- add toString for debugging
  1.2.0 - 2013-09-07 - for I18N, add StringConnection.setI18NStringManager(SOCStringManager, String) and StringConnection.getLocalized(String).
                       StringConnection is now a superclass, not an interface

Version:
1.2.0
Author:
Jeremy D. Monin

Field Summary
protected  boolean accepted
          Active connection, server has called accept, and not disconnected yet
protected static java.lang.String EOF_MARKER
          Unique end-of-file marker object.
protected  java.util.Vector<java.lang.String> in
           
protected  boolean in_reachedEOF
           
private  LocalStringConnection ourPeer
           
protected  java.util.Vector<java.lang.String> out
           
protected  boolean out_setEOF
           
 
Fields inherited from class soc.server.genericServer.StringConnection
appData, connectTime, data, error, hideTimeoutMessage, localeStr, ourServer, remoteVersion, remoteVersionKnown, remoteVersionTrack, stringMgr
 
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.
 LocalStringConnection getPeer()
          Remember, the peer's in is our out, and vice versa.
 Server getServer()
          Server-side: Reference to the server handling this connection.
 java.lang.String host()
          Hostname of the remote side of the connection - Always returns localhost; this method required for StringConnection interface.
private  void init()
          Constructor common-fields initialization
 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 isInputAvailable()
          Is input available now, without blocking? Same idea as FilterInputStream.available().
 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; continuously read and treat input.
 void setAccepted()
          Intended for server to call: Set our accepted flag.
 void setEOF()
          Signal the end of outbound data.
 void setServer(Server srv)
          Server-side: Set the generic server for this connection.
 java.lang.String toString()
          toString includes data.toString for debugging.
 
Methods inherited from class soc.server.genericServer.StringConnection
getAppData, getConnectTime, getData, getError, getI18NLocale, getLocalized, getLocalized, getLocalizedSpecial, getVersion, isVersionKnown, setAppData, setData, setHideTimeoutMessage, setI18NStringManager, setVersion, setVersion, setVersionTracking, wantsHideTimeoutMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EOF_MARKER

protected static java.lang.String EOF_MARKER
Unique end-of-file marker object. Always compare against this with == not string.equals.


in

protected java.util.Vector<java.lang.String> in

out

protected java.util.Vector<java.lang.String> 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
Constructor Detail

LocalStringConnection

public LocalStringConnection()
Create a new, unused LocalStringConnection. After construction, call connect(String) 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.


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

init

private void init()
Constructor common-fields initialization


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 class 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. Called after conn is removed from server structures.

Specified by:
disconnect in class 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 class 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()

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. This is how the code knows it's on the server (not client) side. If a server is set, its removeConnection method is called if our input reaches EOF, and it's notified if our version changes. Call this before calling run().

Parameters:
srv - The new server, or null
See Also:
StringConnection.setVersionTracking(boolean)

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 class 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 class 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 class StringConnection

isInputAvailable

public boolean isInputAvailable()
Is input available now, without blocking? Same idea as FilterInputStream.available().

Specified by:
isInputAvailable in class StringConnection
Since:
1.0.5

run

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

Specified by:
run in interface java.lang.Runnable
Specified by:
run in class StringConnection

toString

public java.lang.String toString()
toString includes data.toString for debugging.

Overrides:
toString in class java.lang.Object
Since:
1.0.5.2