org.vrspace.server
Class Session

java.lang.Object
  extended byjava.util.Observable
      extended byorg.vrspace.server.Session
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
BinarySession, PipedSession

public class Session
extends java.util.Observable
implements java.lang.Runnable

A Session is constructed and started when a client connects to the server.
If client authenticates passes, session calls Dispatcher.login()
Should it be a OwnedDBObject? Could we store session state then?

See Also:
Dispatcher

Nested Class Summary
 class Session.Status
          Session status info
 
Field Summary
protected  boolean active
           
protected  boolean cacheRequests
           
protected  Client client
           
protected  boolean daemon
           
protected  int debugLevel
           
protected  Dispatcher dispatcher
           
protected  long lastTime
           
protected  java.lang.String lf
           
 boolean preserveSocket
           
protected  long receivedChars
           
protected  long receivedRequests
           
protected  boolean relogin
           
static java.lang.String RESPONSE_ERR
           
static java.lang.String RESPONSE_NONE
           
static java.lang.String RESPONSE_OK
           
protected  long sentChars
           
protected  long sentRequests
           
protected  Server server
           
protected  java.net.Socket socket
           
protected  long startTime
           
protected  long stopTime
           
static char TERMINATOR
           
protected  java.io.PrintWriter writer
           
 
Constructor Summary
protected Session()
           
  Session(Server server, java.net.Socket s, Dispatcher d)
          Constructor, called by Server
 
Method Summary
protected  void close()
          Session cleanup.
protected  void debug(int level, java.lang.String message)
           
 void flushRequests()
          flush cached requests
 java.lang.String getAddress()
           
 java.lang.String getId()
          Returns the session id in format remoteHost:remotePort
 java.net.Socket getSocket()
          Get the underlying socket.
 Session.Status getStatus()
          Returns session status
 boolean isActive()
          active?
protected  java.lang.String read()
          Returns one line read from the network.
protected  void readln()
          Reads lines from the socket and calls Client.request().
 void run()
          Main loop, started from Server.
 void sendError(Request r)
          write( "-"+ r ) - ERROR response to a request
 void sendRequest(Request r)
          Sends a request to the client
 void sendResponse(Request r)
          write( "+"+ r ) - OK response to a request
 void terminate()
          Terminates the session.
 void terminate(boolean relogin)
          Terminates the session.
protected  void write(java.lang.String type, Message r)
          Sends response r of type type to the client, appending TERMINATOR as necessary.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RESPONSE_OK

public static final java.lang.String RESPONSE_OK
See Also:
Constant Field Values

RESPONSE_ERR

public static final java.lang.String RESPONSE_ERR
See Also:
Constant Field Values

RESPONSE_NONE

public static final java.lang.String RESPONSE_NONE
See Also:
Constant Field Values

socket

protected java.net.Socket socket

dispatcher

protected Dispatcher dispatcher

client

protected Client client

writer

protected java.io.PrintWriter writer

TERMINATOR

public static final char TERMINATOR
See Also:
Constant Field Values

active

protected boolean active

cacheRequests

protected boolean cacheRequests

lf

protected java.lang.String lf

sentChars

protected long sentChars

sentRequests

protected long sentRequests

receivedChars

protected long receivedChars

receivedRequests

protected long receivedRequests

startTime

protected long startTime

stopTime

protected long stopTime

lastTime

protected long lastTime

daemon

protected boolean daemon

server

protected Server server

relogin

protected boolean relogin

preserveSocket

public boolean preserveSocket

debugLevel

protected int debugLevel
Constructor Detail

Session

protected Session()

Session

public Session(Server server,
               java.net.Socket s,
               Dispatcher d)
        throws java.lang.Exception
Constructor, called by Server

Method Detail

getId

public java.lang.String getId()
Returns the session id in format remoteHost:remotePort


isActive

public boolean isActive()
active?


getSocket

public java.net.Socket getSocket()
Get the underlying socket.


run

public void run()
Main loop, started from Server. After authentication, calls readln() method which actualy reads the data during the session.

Specified by:
run in interface java.lang.Runnable

readln

protected void readln()
Reads lines from the socket and calls Client.request(). Uses read() method to read one line from the client.

See Also:
Client

read

protected java.lang.String read()
                         throws java.io.IOException,
                                ConnectionException
Returns one line read from the network.

Throws:
java.io.IOException
ConnectionException

write

protected void write(java.lang.String type,
                     Message r)
Sends response r of type type to the client, appending TERMINATOR as necessary.


sendResponse

public void sendResponse(Request r)
write( "+"+ r ) - OK response to a request


sendError

public void sendError(Request r)
write( "-"+ r ) - ERROR response to a request


sendRequest

public void sendRequest(Request r)
Sends a request to the client


flushRequests

public void flushRequests()
flush cached requests


terminate

public void terminate()
Terminates the session. This method will cause the objects main thread to finish processing.


terminate

public void terminate(boolean relogin)
Terminates the session. This method will cause the objects main thread to finish processing.


close

protected void close()
Session cleanup. This method is declared protected as it should never be called from any class than this one. Instead call terminate. It will always be called in the finally block of the Session's run method.


debug

protected void debug(int level,
                     java.lang.String message)

getStatus

public Session.Status getStatus()
Returns session status


getAddress

public java.lang.String getAddress()