QJCC homepage

biz.chitec.quarterback.gjsa.server
Class SocketLayer

java.lang.Object
  extended byjava.lang.Thread
      extended bybiz.chitec.quarterback.gjsa.server.SocketLayer
All Implemented Interfaces:
java.lang.Runnable

public class SocketLayer
extends java.lang.Thread

Lowest-level class in server-socket-communication A SocketLayer object is created by GJSAServer for each incoming connection. It always runs in its own thread. It does always the initial and the low-level stuff on the connection:

SocketLayer has three possibilities what to do with the freshly read first query:
  1. The query contains an ID of an already opened connectionless connection. In this case, the complete query is handed over to the CLBag (via GJSAServer) so that the appropriate handler can cope with it. The delivered answer is written back to the Socket which is closed afterwards.
  2. The query begins with the NEWCONNECTIONLESS command. Then, a new ServerThreadBase object is created, put into CLBag, it is given the rest of the query (if available), the result is appended to the self-generated answer to the NEWCONNECTIONLESS command, and everything is finally written back to the Socket, which is closed afterwards.
  3. The query begins with every other command and is not signed as "connectionless". In this case, a new ServerThreadBase is created, it gets the SocketLayer, and after that, it completely takes over the connection with the SocketLayer being the final instance on top of the Socket.
SocketLayer always survives the complete livetime of the open connection to the client. It is also the instance which incorporates the Thread under which the question of the client is answered (regardless of who actually handles the query).

Version:
$Id: e4e41bd5141a742f2424710024b7e447084a5214 $
Author:
Dirk Hillbrecht 2003. Distributed under the terms of the GNU LGPL.
See Also:
ServerThreadBase, GJSAServer, SessionConnector

Nested Class Summary
private  class SocketLayer.InternalHandlingException
           
 
Field Summary
private  java.lang.String clienthostip
          The client IP address
private  java.lang.String clienthostname
          The (DNS-)name of the client host
private  int connectionencoding
          What is the encoding of the connection?
private  boolean encrypted
          Is this connection encrypted?
static int INITIALTIMEOUT
          Timeout used directly after opening the Socket.
private  boolean internalsocketclosing
          Marker whether the socket is closed internally
private  Logger mess
           
 GJSAServer myserv
          Reference to the Server object that has launched this ServerThreadBase
private  java.net.InetAddress remoteaddress
          The client InetAddress object
private  java.net.Socket socket
          Socket to the client
private  java.io.ObjectInputStream socketinputstream
           
private  java.io.ObjectOutputStream socketoutputstream
           
private  java.io.BufferedReader socketreader
           
private  java.io.BufferedWriter socketwriter
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SocketLayer(GJSAServer gx, java.net.Socket sx, boolean ex, int codex)
          Constructor for the SocketLayer.
 
Method Summary
 boolean clientMatchesPattern(java.lang.String pattern)
          Description of the Method
 void closeSocket()
          Closes the socket externally.
 java.lang.String getClientHostIP()
          Returns the IP address of the connected client.
 java.lang.String getClientHostName()
          Returns the symbolic name of the connected client.
 java.net.InetAddress getClientInetAddr()
          Returns the InetAddress object for the currently connected client This method will also work for connectionless connect.
 boolean isEncrypted()
          Returns whether this SocketLayer is on an encrypted connection or not.
 boolean isInternalSocketClosing()
          Returns whether the socket will be closed internally.
 ServerEnvelope readFromSocket()
          Reads data from socket.
 ServerEnvelope readFromSocket(ServerThreadBase stb)
          Read one query from the socket.
 void run()
          Main method to do something useful with the socket.
 void setInternalSocketClosing(boolean b)
          Toggles internal socket closing.
 void setSoTimeout(int millis)
          Set the socket's read timeout.
 void writeToSocket(ServerEnvelope se)
          Write one answer to the socket.
 
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

INITIALTIMEOUT

public static final int INITIALTIMEOUT
Timeout used directly after opening the Socket. The first query must arrive within this amount of time.

See Also:
Constant Field Values

myserv

public GJSAServer myserv
Reference to the Server object that has launched this ServerThreadBase


socket

private java.net.Socket socket
Socket to the client


encrypted

private boolean encrypted
Is this connection encrypted?


connectionencoding

private int connectionencoding
What is the encoding of the connection?


socketreader

private java.io.BufferedReader socketreader

socketwriter

private java.io.BufferedWriter socketwriter

socketinputstream

private java.io.ObjectInputStream socketinputstream

socketoutputstream

private java.io.ObjectOutputStream socketoutputstream

mess

private Logger mess

clienthostname

private java.lang.String clienthostname
The (DNS-)name of the client host


clienthostip

private java.lang.String clienthostip
The client IP address


remoteaddress

private java.net.InetAddress remoteaddress
The client InetAddress object


internalsocketclosing

private boolean internalsocketclosing
Marker whether the socket is closed internally

Constructor Detail

SocketLayer

public SocketLayer(GJSAServer gx,
                   java.net.Socket sx,
                   boolean ex,
                   int codex)
Constructor for the SocketLayer. Does only create the object. Everything else is done in the detached run() method.

Parameters:
gx - The GJSA server
sx - The socket to listen on (and write to)
ex - Encryption state of the connection (encrypted or not?)
codex - Encoding mode of the connection.
Method Detail

readFromSocket

public ServerEnvelope readFromSocket(ServerThreadBase stb)
                              throws java.io.IOException,
                                     java.lang.ClassNotFoundException
Read one query from the socket. Reads exactly one query from the socket. Handles differences between String and Object-based communication. This method blocks until something to be read is available (or the connection times out). It should be called by a caller who takes care of this behaviour.

Returns:
ServerEnvelope object containing the query.
Throws:
java.io.IOException
java.lang.ClassNotFoundException

readFromSocket

public ServerEnvelope readFromSocket()
                              throws java.io.IOException,
                                     java.lang.ClassNotFoundException
Reads data from socket.

Throws:
java.io.IOException
java.lang.ClassNotFoundException

writeToSocket

public void writeToSocket(ServerEnvelope se)
                   throws java.io.IOException
Write one answer to the socket. Writes exactly one answer to the socket. Handles differences between String and Object-based communication. This method blocks until the recipient has read the stuff (or the connection times out). It should be called by a caller who takes care of this behaviour.

Parameters:
se - The ServerEnvelope to write to the socket
Throws:
java.io.IOException

setSoTimeout

public void setSoTimeout(int millis)
                  throws java.net.SocketException
Set the socket's read timeout.

Throws:
java.net.SocketException

clientMatchesPattern

public boolean clientMatchesPattern(java.lang.String pattern)
Description of the Method

Parameters:
pattern - Description of the Parameter
Returns:
Description of the Return Value

getClientInetAddr

public java.net.InetAddress getClientInetAddr()
Returns the InetAddress object for the currently connected client This method will also work for connectionless connect.

Returns:
Info about the currently connected client

getClientHostName

public java.lang.String getClientHostName()
Returns the symbolic name of the connected client. May be null.

Returns:
The clientHostName value

getClientHostIP

public java.lang.String getClientHostIP()
Returns the IP address of the connected client. May be null though this is rather unlikely.

Returns:
The clientHostIP value

isEncrypted

public boolean isEncrypted()
Returns whether this SocketLayer is on an encrypted connection or not.


setInternalSocketClosing

public void setInternalSocketClosing(boolean b)
Toggles internal socket closing. Normally, SocketLayer closes the socket automatically if the connection is gone. However, if an asynchronous writing thread is operating on the socket, it must be ensured that everything is written to the socket in advance to closing it. Therefore, socket closing control can be passed to that writing thread. To activate this pass-over, internal socket closing must be disabled by calling this method. Warning: Socket closing should only be disabled if the external socket closing mechanism will close the socket properly. Under all circumstances no orphaned opened sockets should linger around and eat up resources.

Parameters:
b - Flag whether sockets should be closed internally

isInternalSocketClosing

public boolean isInternalSocketClosing()
Returns whether the socket will be closed internally.


closeSocket

public void closeSocket()
Closes the socket externally. Must be called by any accordingly qualified entity of the program to close the socket if this is not done internally. All exceptions are caught internally. If internal socket closing is enabled, this method does nothing.


run

public void run()
Main method to do something useful with the socket. The main method initializes the Socket appropriately, then reads the first query and does whatever is needed.


QJCC homepage