soc.server.genericServer
Class Connection

java.lang.Object
  extended byjava.lang.Thread
      extended bysoc.server.genericServer.Connection
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Runnable, java.io.Serializable, StringConnection

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

A server connection.

Version:
1.0
Author:
Cristian Bogdan Reads from the net, writes atomically to the net and holds the connection data
See Also:
Serialized Form

Nested Class Summary
(package private)  class Connection.Putter
           
 
Field Summary
protected  boolean connected
           
protected  java.util.Date connectTime
           
protected  java.lang.Object data
          the arbitrary app-specific data associated with this connection.
protected  java.lang.Exception error
           
protected  java.lang.String hst
           
(package private)  java.io.DataInputStream in
           
protected  boolean inputConnected
           
(package private)  java.io.DataOutputStream out
           
 java.util.Vector outQueue
           
(package private) static int putters
           
(package private) static java.lang.Object puttersMonitor
           
 java.lang.Thread reader
           
protected  int remoteVersion
           
(package private)  java.net.Socket s
           
(package private)  Server sv
           
protected static int TIMEOUT_VALUE
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
(package private) Connection(java.net.Socket so, Server sve)
          initialize the connection data
 
Method Summary
 boolean connect()
          start reading from the net; called only by the server.
 void disconnect()
          close the socket, stop the reader
 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()
           
 int getVersion()
          Give the version number (if known) of the remote end of this connection.
 java.lang.String host()
           
 boolean isConnected()
          Are we currently connected and active?
 void put(java.lang.String str)
          Send this data over the connection.
 boolean putAux(java.lang.String str)
          put a message on the net
 boolean putForReal(java.lang.String str)
          Data is added aynchronously (sitting in outQueue).
 void run()
          continuously read from the net
 void setData(java.lang.Object dat)
          Set the data for this connection
 void setVersion(int version)
          Set the version number of the remote end of this connection.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
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

data

protected java.lang.Object data
the arbitrary app-specific data associated with this connection. Protected to force callers to use getData() part of StringConnection interface.


in

java.io.DataInputStream in

out

java.io.DataOutputStream out

s

java.net.Socket s

sv

Server sv

reader

public java.lang.Thread reader

hst

protected java.lang.String hst

remoteVersion

protected int remoteVersion

error

protected java.lang.Exception error

connectTime

protected java.util.Date connectTime

connected

protected boolean connected

inputConnected

protected boolean inputConnected
See Also:
disconnectSoft()

outQueue

public java.util.Vector outQueue
Constructor Detail

Connection

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

Method Detail

host

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

connect

public boolean connect()
start reading from the net; called only by the server. If successful, also sets connectTime to now.

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

run

public void run()
continuously read from the net

Specified by:
run in interface java.lang.Runnable

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 interface StringConnection
Parameters:
str - Data to send

putForReal

public boolean putForReal(java.lang.String str)
Data is added aynchronously (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 error)

putAux

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

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

getData

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

setData

public void setData(java.lang.Object dat)
Set the data for this connection

Specified by:
setData in interface StringConnection
Parameters:
dat - The new data, 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()

disconnect

public void disconnect()
close the socket, stop the reader

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

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.