QJCC homepage

biz.chitec.quarterback.gjsa.server
Class ServerThreadSession

java.lang.Object
  extended bybiz.chitec.quarterback.gjsa.server.ServerThreadSession

public class ServerThreadSession
extends java.lang.Object

Session of a connection in the server. Each client can have multiple session open in its connection. These sessions are started on top of each other. A session is able to answer requests and manage bursted replies. It does not care whether it belongs to a connectionbound or connectionless connection. It does also not care about threading issues. It is more or less an intelligent storage for data to which requests from the socket are delegated. The first session is created by the ServerThreadBase. Other sessions are always created out of already existing sessions by sending the command OPENSESSION. The next mode changing command will open a new session and execute that command in the new session. The ServerThreadSession takes a stack of lists of CommandExecutor objects. The lists are stored in ServerStackEntry objects which are linked in a Deque and form this way a stack.

ServerStackEntries

The server stack is no object in itself, but a deque of ServerStackEntry objects that are supported and handled by some methods of ServerThreadSession.

A new ServerStackEntry is put at top of the server stack in two or three steps: First, startNewState() is called with the state's number. Then, properties can be put into the server stack using putProperty() and at last, the CommandExecutors are set with one of the finishNewState() methods. If no new properties are needed in the new state, steps one and three can be executed together with a newState() method.

Closing mechanism

Bound connections can be closed at any time through the quitAll() method. However, a server stack of a connectionbound ServerThreadBase has the possibility of delaying a shutdown of the whole server. The server stack may contain a "NOKILL" property which prevents the exit mechanism from killing this thread base.

Version:
$Id: 95ca6f1051230eacf807144ef444292bbcd21815 $
Author:
chitec/Dirk Hillbrecht 2002-2003, cantamen/Dirk Hillbrecht 2003-2005. Distributed under the terms of the GNU LGPL.
See Also:
ServerThreadBase, GJSAServer, SessionConnector

Field Summary
private  boolean burstall
          Description of the Field
private  int burstnextcount
          Description of the Field
private  int burstsize
          Description of the Field
private  java.util.Set childsessions
          Sessions which are started from this session on.
private  boolean dirtystack
          Description of the Field
private  ServerThreadSession fathersession
          The session fron which this session was started, null if no father exists.
private  int id
          Number of this session within its threadbase
private  int lastnewchildid
          Storage of the last new child's id after its finalization
private  GJSAServer myserv
          pointer to the Server object that has launched this ServerThreadSession
private  ServerThreadBase mythreadbase
          The ServerThreadBase object that is base for this session object.
private  ServerThreadSession newchild
          Temporary storage for a new child session during its creation
private  ServerStackEntry stackbase
          place for specialized command executors to handle the requests passed by the client
private  ServerStackEntry stacktop
          place for specialized command executors to handle the requests passed by the client
private  boolean syncburstfinished
          Description of the Field
private  BurstMaker syncburstmaker
          Description of the Field
private  java.util.List syncburstreplies
          Description of the Field
private  int syncburststart
          Description of the Field
 
Constructor Summary
ServerThreadSession(ServerThreadBase stbx)
          Constructor for initial session of a server thread.
ServerThreadSession(ServerThreadSession stsx)
          Constructor for derived session.
 
Method Summary
 void closeSession()
           
 void closeSession(boolean quitall)
          Removes the whole server stack.
 ServerReply continueSyncBurst()
          continues a synchronous burst.
 void finishNewState(CommandExecutor commexec)
          finish a new ServerStackEntry with one state specific CommandExecutor.
 void finishNewState(java.util.List commexecs)
          finish a new ServerStackEntry with state specific CommandExecutors only.
 void finishNewState(java.util.List commexecs, java.util.List allcommexecs)
          finish a new server stack entry with state specific CommandExecutors and CommandExecutors that are called also in every further state.
 boolean getBurstAll()
          Get if bursts anything burstable
 boolean getBurstNext()
          Get if bursts next command.
 int getBurstSize()
          Returns size of one burst chunk.
 GJSAServer getGJSAServer()
          Returns the GJSA server to which this session belongs
 int getLastChildSessionID()
          Return last new child's ID.
 java.lang.Object getProperty(java.lang.Object key)
          Gets a property from the server stack.
private  java.lang.Object getPropertyRecursive(java.lang.Object key)
          Internal recursive body of getProperty().
 ServerThreadBase getServerThreadBase()
          Returns the ServerThreadBase to which this session belongs
 int getSessionID()
          Return my own session id
 int getState()
          returns the state this session is just in
 boolean hasChildren()
          Returns whether this session has any children
 ServerReply initBurstableReply(BurstMaker bm)
          initiates a bursted reply.
protected  void internalOneSyncBurstStep()
          Loads one sync burst result into the cache vector.
 boolean isComplex()
          Returns whether this session has either more than one entry in the stack or a subsession.
 void leaveState()
          Leaves the state the session is working in.
 boolean mayBeClosed()
          returns whether the thread base may be closed or not.
 void newState(int newstate, CommandExecutor commexec)
          establish a new state with one state specific CommandExecutor.
 void newState(int newstate, java.util.List commexecs)
          establish a new state with state specific CommandExecutors only.
 void newState(int newstate, java.util.List commexecs, java.util.List allcommexecs)
          establish a new state with state specific CommandExecutors and CommandExecutors that are called also in every further state.
 int openSession()
          Initiate creation of a new session.
 java.lang.Object putProperty(java.lang.Object key, java.lang.Object value)
          puts a property into the server stack.
 void quitAll()
           
 void registerNewChild()
          Adds the newchild to the children of this session
 void releaseChild(ServerThreadSession sts)
          Removes a child thread session from the children.
 java.lang.Object removeProperty(java.lang.Object key)
          Removes a property from the server stack.
 ServerEnvelope reply(ServerEnvelope se)
          handles a ServerEnvelope.
 ServerReply replyOneQuestion(ServerRequest sq)
           
 ServerReply replyOneQuestion(ServerRequest sq, boolean allonly)
          handles one request and returns a reply
 void retractNewState()
          retract a new state-to-be between startNewState and finishNewState
 void setBurstAll(boolean bx)
          Set if shound burst anything burstable
 void setBurstNext(boolean bx)
          Set if should burst next command.
 void setBurstSize(int s)
          Sets size of one burst chunk.
 void startNewState(int newstate)
          start a new state putting a new ServerStackEntry on top of the server stack.
 java.lang.String toString()
          Description of the Method
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

myserv

private GJSAServer myserv
pointer to the Server object that has launched this ServerThreadSession


mythreadbase

private ServerThreadBase mythreadbase
The ServerThreadBase object that is base for this session object.


fathersession

private ServerThreadSession fathersession
The session fron which this session was started, null if no father exists.


childsessions

private java.util.Set childsessions
Sessions which are started from this session on.


id

private int id
Number of this session within its threadbase


stackbase

private ServerStackEntry stackbase
place for specialized command executors to handle the requests passed by the client


stacktop

private ServerStackEntry stacktop
place for specialized command executors to handle the requests passed by the client


dirtystack

private boolean dirtystack
Description of the Field


burstall

private boolean burstall
Description of the Field


burstnextcount

private int burstnextcount
Description of the Field


burstsize

private int burstsize
Description of the Field


syncburstmaker

private BurstMaker syncburstmaker
Description of the Field


syncburststart

private int syncburststart
Description of the Field


syncburstreplies

private java.util.List syncburstreplies
Description of the Field


syncburstfinished

private boolean syncburstfinished
Description of the Field


newchild

private ServerThreadSession newchild
Temporary storage for a new child session during its creation


lastnewchildid

private int lastnewchildid
Storage of the last new child's id after its finalization

Constructor Detail

ServerThreadSession

public ServerThreadSession(ServerThreadBase stbx)
Constructor for initial session of a server thread. Does only get the thread base. Has no father session.

Parameters:
stbx - The ServerThreadBase which initiates the session.

ServerThreadSession

public ServerThreadSession(ServerThreadSession stsx)
Constructor for derived session. Gets it father session as parameter, initializes itself from it.

Parameters:
stsx - The father session.
Method Detail

leaveState

public void leaveState()
Leaves the state the session is working in. The dropMe() method of the ServerStackEntry cares about correct termination of the services offered by this state (e.g. database access or any preliminary stuff). The initial stack entry cannot be discarded this way, if the session should be closed completely, use quitAll().


closeSession

public void closeSession(boolean quitall)
Removes the whole server stack. From top to bottom, every entry's dropMe() method is called. This method cleans the complete server stack. Note that this method does NOT quit any child sessions. Use the close() method for cleaning up everything!


closeSession

public void closeSession()

quitAll

public void quitAll()

putProperty

public java.lang.Object putProperty(java.lang.Object key,
                                    java.lang.Object value)
puts a property into the server stack. This method does nothing more as calling the method of the same name of the uppermost stack entry

Parameters:
key - Description of the Parameter
value - Description of the Parameter
Returns:
Description of the Return Value

getProperty

public java.lang.Object getProperty(java.lang.Object key)
Gets a property from the server stack. This method does nothing more as calling the method of the same name of the uppermost stack entry. If the server stack of the current session does not contain the property, the father session is queried (in case it exists). This method itself only checks for the existance of a newchild object. If it exists, its getProperty()-body is called, otherwise this' method is invoked.

Parameters:
key - The key of the property
Returns:
The property's value

getPropertyRecursive

private java.lang.Object getPropertyRecursive(java.lang.Object key)
Internal recursive body of getProperty(). Needs to be seperated as checking new newchild must only happen in the very first stage.

Parameters:
key - The key of the property
Returns:
The propertie's value

removeProperty

public java.lang.Object removeProperty(java.lang.Object key)
Removes a property from the server stack. This method does nothing more as calling the method of the same name of the uppermost stack entry. Note that never properties of the father session(s) are touched.

Parameters:
key - The key to search for
Returns:
The (just removed) property's value

getState

public int getState()
returns the state this session is just in

Returns:
The state value

getGJSAServer

public GJSAServer getGJSAServer()
Returns the GJSA server to which this session belongs


getServerThreadBase

public ServerThreadBase getServerThreadBase()
Returns the ServerThreadBase to which this session belongs


getSessionID

public int getSessionID()
Return my own session id


registerNewChild

public void registerNewChild()
Adds the newchild to the children of this session


releaseChild

public void releaseChild(ServerThreadSession sts)
Removes a child thread session from the children. Called by the child if it is about to be deleted.


openSession

public int openSession()
Initiate creation of a new session. On invocation, create a blank new ServerThreadSession object which will be used for the next state change.

Returns:
The ID of the next created session.

getLastChildSessionID

public int getLastChildSessionID()
Return last new child's ID. Returns the number of the last new child session that has been created by this session. This function can be used to determine whether the last child session creation has been successful: It must return the same number as the openSession command before.

Returns:
The session ID of the last successfully created child.

newState

public void newState(int newstate,
                     java.util.List commexecs,
                     java.util.List allcommexecs)
establish a new state with state specific CommandExecutors and CommandExecutors that are called also in every further state.

Parameters:
newstate - Description of the Parameter
commexecs - Description of the Parameter
allcommexecs - Description of the Parameter

newState

public void newState(int newstate,
                     java.util.List commexecs)
establish a new state with state specific CommandExecutors only.

Parameters:
newstate - Description of the Parameter
commexecs - Description of the Parameter

newState

public void newState(int newstate,
                     CommandExecutor commexec)
establish a new state with one state specific CommandExecutor.

Parameters:
newstate - Description of the Parameter
commexec - Description of the Parameter

startNewState

public void startNewState(int newstate)
start a new state putting a new ServerStackEntry on top of the server stack. The entry has no CommandExecutors yet.

Parameters:
newstate - Description of the Parameter

retractNewState

public void retractNewState()
retract a new state-to-be between startNewState and finishNewState


finishNewState

public void finishNewState(java.util.List commexecs,
                           java.util.List allcommexecs)
finish a new server stack entry with state specific CommandExecutors and CommandExecutors that are called also in every further state.

Parameters:
commexecs - Description of the Parameter
allcommexecs - Description of the Parameter

finishNewState

public void finishNewState(java.util.List commexecs)
finish a new ServerStackEntry with state specific CommandExecutors only.

Parameters:
commexecs - Description of the Parameter

finishNewState

public void finishNewState(CommandExecutor commexec)
finish a new ServerStackEntry with one state specific CommandExecutor.

Parameters:
commexec - Description of the Parameter

hasChildren

public boolean hasChildren()
Returns whether this session has any children


mayBeClosed

public boolean mayBeClosed()
returns whether the thread base may be closed or not. This is determined by the existance of a "NOKILL" property on the stack

Returns:
Description of the Return Value

setBurstAll

public void setBurstAll(boolean bx)
Set if shound burst anything burstable

Parameters:
bx - The new burstAll value

getBurstAll

public boolean getBurstAll()
Get if bursts anything burstable

Returns:
The burstAll value

setBurstNext

public void setBurstNext(boolean bx)
Set if should burst next command.

Parameters:
bx - The new burstNext value

getBurstNext

public boolean getBurstNext()
Get if bursts next command.

Returns:
The burstNext value

setBurstSize

public void setBurstSize(int s)
Sets size of one burst chunk.

Parameters:
s - The new burstSize value

getBurstSize

public int getBurstSize()
Returns size of one burst chunk.

Returns:
The burstSize value

isComplex

public boolean isComplex()
Returns whether this session has either more than one entry in the stack or a subsession. Needed for connectionless connections where the encryption state has to be checked each time a connection is established. If the initial session fulfills one of the two above conditions, the encryption state is important for checking.


internalOneSyncBurstStep

protected void internalOneSyncBurstStep()
Loads one sync burst result into the cache vector. If no sync burst is available, the method does nothing.


continueSyncBurst

public ServerReply continueSyncBurst()
continues a synchronous burst.

Returns:
Description of the Return Value

initBurstableReply

public ServerReply initBurstableReply(BurstMaker bm)
initiates a bursted reply. This method works independent of the selected burst type. In case of asynchronous burst, nothing else has to be done.

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

replyOneQuestion

public ServerReply replyOneQuestion(ServerRequest sq,
                                    boolean allonly)
                             throws java.lang.Throwable
handles one request and returns a reply

Parameters:
sq - Description of the Parameter
Returns:
Description of the Return Value
Throws:
java.lang.Throwable - Description of the Exception

replyOneQuestion

public ServerReply replyOneQuestion(ServerRequest sq)
                             throws java.lang.Throwable
Throws:
java.lang.Throwable

reply

public ServerEnvelope reply(ServerEnvelope se)
handles a ServerEnvelope. All requests in the ServerEnvelope are handled and the replies are collected. Handling is stopped if either a command returns ERROR or CLOSING (which in fact means that the QUIT command has been given. In these cases, the reply vector is shorter than the request vector.

Note that this methods handles the complete encryption, e.g. it takes encrypted envelopes, decrypts them internally, and reencryts the result.

Parameters:
se - Envelope containing all queries
Returns:
Given ServerEnvelope object, queries removed, replies added

toString

public java.lang.String toString()
Description of the Method

Returns:
Description of the Return Value

QJCC homepage