org.mortbay.p2psockets.http
Class P2PSocketListener

java.lang.Object
  extended bySocketListener
      extended byorg.mortbay.p2psockets.http.P2PSocketListener

public class P2PSocketListener
extends SocketListener

Socket HTTP Listener modified to use Jxta P2P Sockets. The behaviour of the listener can be controlled with the attributues of the ThreadedServer and ThreadPool from which it is derived. Specifically:

 MinThreads    - Minumum threads waiting to service requests.
 MaxThread     - Maximum thread that will service requests.
 MaxIdleTimeMs - Time for an idle thread to wait for a request or read.
 LowResourcePersistTimeMs - time in ms that connections will persist if listener is
                            low on resources. 
 
We aggregate P2PThreadedServers inside of P2PSocketListeners instead of subclassing from them so that we can retain P2PSocketListener as our superclass, so that the rest of Jetty doesn't have to have know about P2PSocketListener. -- bkn3@columbia.edu

Version:
$Id: P2PSocketListener.java,v 1.3 2004/11/12 10:38:28 bradneuberg Exp $
Author:
Greg Wilkins (gregw)
See Also:
Serialized Form

Constructor Summary
P2PSocketListener()
           
P2PSocketListener(InetAddrPort address)
           
 
Method Summary
 void customizeRequest(HttpConnection connection, HttpRequest request)
          Customize the request from connection.
protected  void customizeRequest(java.net.Socket socket, HttpRequest request)
          Customize request from socket.
 int getBufferReserve()
           
 int getBufferSize()
           
 int getConfidentialPort()
           
 java.lang.String getConfidentialScheme()
           
 java.lang.String getDefaultScheme()
           
 java.lang.String getHost()
           
 HttpServer getHttpServer()
           
 boolean getIdentifyListener()
           
 int getIdleThreads()
          Get the number of idle threads in the pool.
 java.net.InetAddress getInetAddress()
           
 InetAddrPort getInetAddrPort()
           
 int getIntegralPort()
           
 java.lang.String getIntegralScheme()
           
 int getLingerTimeSecs()
           
 int getLowResourcePersistTimeMs()
           
 int getMaxIdleTimeMs()
          Get the maximum thread idle time.
 int getMaxReadTimeMs()
           
 int getMaxThreads()
          Set the maximum number of threads.
 int getMinThreads()
          Get the minimum number of threads.
 java.lang.String getName()
           
 int getPort()
           
 java.net.ServerSocket getServerSocket()
           
 int getThreads()
          Get the number of threads in the pool.
 void handle(java.lang.Object job)
          Handle Job - we modify this to call the handleConnection that is inside of P2PSocketListener.
 void handleConnection(java.net.Socket socket)
          Handle Job.
 boolean isConfidential(HttpConnection connection)
           
 boolean isIntegral(HttpConnection connection)
           
 boolean isLowOnResources()
          Get the lowOnResource state of the listener.
 boolean isOutOfResources()
          Get the outOfResource state of the listener.
 boolean isStarted()
          Is the pool running jobs.
 void join()
           
 void persistConnection(HttpConnection connection)
          Persist the connection.
 void run(java.lang.Object job)
          Run job.
 void setBufferReserve(int size)
           
 void setBufferSize(int size)
           
 void setConfidentialPort(int confidentialPort)
           
 void setConfidentialScheme(java.lang.String confidentialScheme)
           
 void setDefaultScheme(java.lang.String scheme)
           
 void setHost(java.lang.String host)
           
 void setHttpServer(HttpServer server)
           
 void setIdentifyListener(boolean identifyListener)
           
 void setInetAddress(java.net.InetAddress addr)
           
 void setInetAddrPort(InetAddrPort address)
          Set the server InetAddress and port.
 void setIntegralPort(int integralPort)
           
 void setIntegralScheme(java.lang.String integralScheme)
           
 void setLingerTimeSecs(int ls)
           
 void setLowResourcePersistTimeMs(int ms)
          Set the low resource persistace time.
 void setMaxIdleTimeMs(int maxIdleTimeMs)
          Set the maximum thread idle time.
 void setMaxStopTimeMs(int ms)
          Deprecated. maxIdleTime is used instead.
 void setMaxThreads(int maxThreads)
          Set the maximum number of threads.
 void setMinThreads(int minThreads)
          Set the minimum number of threads.
 void setName(java.lang.String name)
           
 void setPort(int port)
           
 void start()
           
 void stop()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

P2PSocketListener

public P2PSocketListener()

P2PSocketListener

public P2PSocketListener(InetAddrPort address)
                  throws java.io.IOException
Method Detail

setHttpServer

public void setHttpServer(HttpServer server)

getHttpServer

public HttpServer getHttpServer()

getBufferSize

public int getBufferSize()

setBufferSize

public void setBufferSize(int size)

getBufferReserve

public int getBufferReserve()

setBufferReserve

public void setBufferReserve(int size)

getIdentifyListener

public boolean getIdentifyListener()

setIdentifyListener

public void setIdentifyListener(boolean identifyListener)
Parameters:
identifyListener - If true, the listener name is added to all requests as the org.mortbay.http.HttListener attribute

setDefaultScheme

public void setDefaultScheme(java.lang.String scheme)

getDefaultScheme

public java.lang.String getDefaultScheme()

getLowResourcePersistTimeMs

public int getLowResourcePersistTimeMs()
Returns:
time in ms that connections will persist if listener is low on resources.

setLowResourcePersistTimeMs

public void setLowResourcePersistTimeMs(int ms)
Set the low resource persistace time. When the listener is low on resources, this timeout is used for idle persistent connections. It is desirable to have this set to a short period of time so that idle persistent connections do not consume resources on a busy server.

Parameters:
ms - time in ms that connections will persist if listener is low on resources.

start

public void start()
           throws java.lang.Exception
Throws:
java.lang.Exception

stop

public void stop()
          throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException

handleConnection

public void handleConnection(java.net.Socket socket)
                      throws java.io.IOException
Handle Job. Implementation of ThreadPool.handle(), calls handleConnection.

Throws:
java.io.IOException

customizeRequest

public void customizeRequest(HttpConnection connection,
                             HttpRequest request)
Customize the request from connection. This method extracts the socket from the connection and calls the customizeRequest(Socket,HttpRequest) method.

Parameters:
request -

customizeRequest

protected void customizeRequest(java.net.Socket socket,
                                HttpRequest request)
Customize request from socket. Derived versions of P2PSocketListener may specialize this method to customize the request with attributes of the socket used (eg SSL session ids). This version resets the SoTimeout if it has been reduced due to low resources. Derived implementations should call super.customizeRequest(socket,request) unless persistConnection has also been overridden and not called.

Parameters:
request -

persistConnection

public void persistConnection(HttpConnection connection)
Persist the connection. This method is called by the HttpConnection in order to prepare a connection to be persisted. For this implementation, if the listener is low on resources, the connection read timeout is set to lowResourcePersistTimeMs. The customizeRequest method is used to reset this to the normal value after a request has been read.


isLowOnResources

public boolean isLowOnResources()
Get the lowOnResource state of the listener. A P2PSocketListener is considered low on resources if the total number of threads is maxThreads and the number of idle threads is less than minThreads.

Returns:
True if low on idle threads.

isOutOfResources

public boolean isOutOfResources()
Get the outOfResource state of the listener. A P2PSocketListener is considered out of resources if the total number of threads is maxThreads and the number of idle threads is zero.

Returns:
True if out of resources.

isIntegral

public boolean isIntegral(HttpConnection connection)

isConfidential

public boolean isConfidential(HttpConnection connection)

getIntegralScheme

public java.lang.String getIntegralScheme()

setIntegralScheme

public void setIntegralScheme(java.lang.String integralScheme)

getIntegralPort

public int getIntegralPort()

setIntegralPort

public void setIntegralPort(int integralPort)

getConfidentialScheme

public java.lang.String getConfidentialScheme()

setConfidentialScheme

public void setConfidentialScheme(java.lang.String confidentialScheme)

getConfidentialPort

public int getConfidentialPort()

setConfidentialPort

public void setConfidentialPort(int confidentialPort)

getServerSocket

public java.net.ServerSocket getServerSocket()
Returns:
The ServerSocket

setInetAddrPort

public void setInetAddrPort(InetAddrPort address)
Set the server InetAddress and port.

Parameters:
address - The Address to listen on, or 0.0.0.0:port for all interfaces.

getInetAddrPort

public InetAddrPort getInetAddrPort()
Returns:
IP Address and port in a new Instance of InetAddrPort.

setHost

public void setHost(java.lang.String host)
             throws java.net.UnknownHostException
Parameters:
host -
Throws:
java.net.UnknownHostException

getHost

public java.lang.String getHost()
Returns:
Host name

setInetAddress

public void setInetAddress(java.net.InetAddress addr)
Parameters:
addr -

getInetAddress

public java.net.InetAddress getInetAddress()
Returns:
IP Address

setPort

public void setPort(int port)
Parameters:
port -

getPort

public int getPort()
Returns:
port number

getMaxReadTimeMs

public int getMaxReadTimeMs()
Returns:
milliseconds

setLingerTimeSecs

public void setLingerTimeSecs(int ls)
Parameters:
ls - seconds to linger or -1 to disable linger.

getLingerTimeSecs

public int getLingerTimeSecs()
Returns:
seconds.

toString

public java.lang.String toString()

handle

public void handle(java.lang.Object job)
Handle Job - we modify this to call the handleConnection that is inside of P2PSocketListener. Implementation of ThreadPool.handle(), calls handleConnection.

Parameters:
job - A Connection.

getName

public java.lang.String getName()
Returns:
The name of the ThreadPool.

setName

public void setName(java.lang.String name)
Parameters:
name - Name of the ThreadPool to use when naming Threads.

isStarted

public boolean isStarted()
Is the pool running jobs.

Returns:
True if start() has been called.

getThreads

public int getThreads()
Get the number of threads in the pool. Delegated to the named or anonymous Pool.

Returns:
Number of threads
See Also:
getIdleThreads()

getIdleThreads

public int getIdleThreads()
Get the number of idle threads in the pool. Delegated to the named or anonymous Pool.

Returns:
Number of threads
See Also:
getThreads()

getMinThreads

public int getMinThreads()
Get the minimum number of threads. Delegated to the named or anonymous Pool.

Returns:
minimum number of threads.
See Also:
setMinThreads(int)

setMinThreads

public void setMinThreads(int minThreads)
Set the minimum number of threads. Delegated to the named or anonymous Pool.

Parameters:
minThreads - minimum number of threads
See Also:
getMinThreads()

getMaxThreads

public int getMaxThreads()
Set the maximum number of threads. Delegated to the named or anonymous Pool.

Returns:
maximum number of threads.
See Also:
setMaxThreads(int)

setMaxThreads

public void setMaxThreads(int maxThreads)
Set the maximum number of threads. Delegated to the named or anonymous Pool.

Parameters:
maxThreads - maximum number of threads.
See Also:
getMaxThreads()

getMaxIdleTimeMs

public int getMaxIdleTimeMs()
Get the maximum thread idle time. Delegated to the named or anonymous Pool.

Returns:
Max idle time in ms.
See Also:
setMaxIdleTimeMs(int)

setMaxIdleTimeMs

public void setMaxIdleTimeMs(int maxIdleTimeMs)
Set the maximum thread idle time. Threads that are idle for longer than this period may be stopped. Delegated to the named or anonymous Pool.

Parameters:
maxIdleTimeMs - Max idle time in ms.
See Also:
getMaxIdleTimeMs()

setMaxStopTimeMs

public void setMaxStopTimeMs(int ms)
Deprecated. maxIdleTime is used instead.

Set Max Read Time.


join

public void join()

run

public void run(java.lang.Object job)
         throws java.lang.InterruptedException
Run job. Give a job to the pool.

Parameters:
job - If the job is derived from Runnable, the run method is called, otherwise it is passed as the argument to the handle method.
Throws:
java.lang.InterruptedException