soc.server.genericServer
Class Connection

java.lang.Object
  extended by soc.server.genericServer.StringConnection
      extended by soc.server.genericServer.Connection
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Runnable

public final class Connection
extends StringConnection
implements java.lang.Runnable, java.io.Serializable, java.lang.Cloneable

A client's connection at a server.

Version:
1.2.0
Author:
Cristian Bogdan Reads from the net, writes atomically to the net and holds the connection data

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.

Refactored in v1.2.0 to extend StringConnection instead of Thread.

See Also:
Serialized Form

Nested Class Summary
(package private)  class Connection.Putter
          Connection inner class thread to send outQueue messages to the net.
 
Field Summary
protected  boolean connected
           
protected  java.lang.String hst
          Hostname of the remote end of the connection, for host()
(package private)  java.io.DataInputStream in
           
protected  boolean inputConnected
           
(package private)  java.io.DataOutputStream out
           
private  java.util.Vector<java.lang.String> outQueue
           
(package private) static int putters
           
(package private) static java.lang.Object puttersMonitor
           
(package private)  java.net.Socket s
           
protected static int TIMEOUT_VALUE
           
 
Fields inherited from class soc.server.genericServer.StringConnection
appData, connectTime, data, error, hideTimeoutMessage, localeStr, ourServer, remoteVersion, remoteVersionKnown, remoteVersionTrack, stringMgr
 
Constructor Summary
Connection(java.net.Socket so, Server sve)
          initialize the connection data
 
Method Summary
 boolean connect()
          Set up to reading from the net, start a new Putter thread to send to the net; called only by the server.
 void disconnect()
          close the socket, stop the reader; called after conn is removed from server structures
 void disconnectSoft()
          Accept no further input, allow output to drain, don't immediately close the socket.
 java.lang.String getName()
          Get our connection thread name for debugging.
 java.lang.String host()
           
 boolean isConnected()
          Are we currently connected and active?
 boolean isInputAvailable()
          Is input available now, without blocking? Same idea as FilterInputStream.available().
 void put(java.lang.String str)
          Send this data over the connection.
private  boolean putAux(java.lang.String str)
          put a message on the net
private  boolean putForReal(java.lang.String str)
          Data is added asynchronously (sitting in outQueue).
 void run()
          continuously read from the net
 java.lang.String toString()
          toString includes data.toString for debugging, and getName().
 
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

putters

static int putters

puttersMonitor

static java.lang.Object puttersMonitor

TIMEOUT_VALUE

protected static final int TIMEOUT_VALUE
See Also:
Constant Field Values

in

java.io.DataInputStream in

out

java.io.DataOutputStream out

s

java.net.Socket s

hst

protected java.lang.String hst
Hostname of the remote end of the connection, for host()


connected

protected boolean connected

inputConnected

protected boolean inputConnected
See Also:
disconnectSoft()

outQueue

private java.util.Vector<java.lang.String> outQueue
Constructor Detail

Connection

Connection(java.net.Socket so,
           Server sve)
initialize the connection data

Method Detail

getName

public java.lang.String getName()
Get our connection thread name for debugging. Also used by toString().

Returns:
"connection-"remotehostname-portnumber, or "connection-(null)-"hashCode
Since:
1.2.0

host

public java.lang.String host()
Specified by:
host in class StringConnection
Returns:
Hostname of the remote end of the connection

connect

public boolean connect()
Set up to reading from the net, start a new Putter thread to send to the net; called only by the server. If successful, also sets connectTime to now. Before calling connect(), be sure to call run() to start the inbound reading thread.

Specified by:
connect in class StringConnection
Returns:
true if thread start was successful, false if an error occurred.

isInputAvailable

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

Specified by:
isInputAvailable in class StringConnection

run

public void run()
continuously read from the net

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

put

public final void put(java.lang.String str)
Send this data over the connection. Adds it to the outQueue to be sent by the Putter thread.

Specified by:
put in class StringConnection
Parameters:
str - Data to send

putForReal

private boolean putForReal(java.lang.String str)
Data is added asynchronously (sitting in outQueue). This method is called when it's dequeued and sent over the connection to the remote end.

Parameters:
str - Data to send
Returns:
True if sent, false if error (and sets StringConnection.error)

putAux

private final boolean putAux(java.lang.String str)
put a message on the net

Returns:
true for success, false and disconnects on failure (and sets StringConnection.error)

disconnect

public void disconnect()
close the socket, stop the reader; 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

isConnected

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

Specified by:
isConnected in class StringConnection

toString

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

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