Class w3c.jigsaw.http.Client
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class w3c.jigsaw.http.Client

java.lang.Object
   |
   +----w3c.jigsaw.http.Client

public class Client
extends Object
implements EventHandler, Runnable
This class implements the object that handles client connections. One such object exists per open connections at any given time.

The basic architecture is the following: the httpd instance accepts new connections on its port. When such a connection is accepted a Client object is requested to the client pool (which can implement what ever strategy is suitable). Each request is than managed by looking up an resource and invoking the resource methods corresponding to the request.

See Also:
http, Request, Repy

Variable Index

 o input
 o output
 o parser
 o server
 o socket
 o thread

Method Index

 o bind(Socket)
Bind this awaiting client to the provided socket.
 o chunkTransfer(InputStream)
Emit the reply stream, using chunk encoding.
 o emitReply(Reply)
Emit the given reply to the client.
 o error(String)
Emit an error on behalf of this client.
 o flushOutput()
 o getIdentifier()
Get the client identifier.
 o getInetAddress()
Get the IP address of this client.
 o getInputStream()
Get this client input stream.
 o getNextRequest()
Get the next avaiable request for this client.
 o getOutputFileDescriptor()
Get this client output file descriptor.
 o getOutputStream()
get this client output stream.
 o getServer()
Get the client server's
 o getThread()
Get the client thread.
 o handleTimerEvent(Object, long)
Handle timer events.
 o isBound()
Is this client bound to some socket already ?
 o kill(boolean)
Kill this client, freeing all its associated resources.
 o log(Request, Reply, int, long)
Log the given request.
 o loop()
Request get processed in three stages: a) Read and pre-parse the request: in this stage, the parser throws HTTPParserException only.
 o loopForBinding()
Wait for this client to be bind to some socket.
 o processRequest(Request)
Process a request.
 o run()
Starts this client.
 o runConnection()
Run for our newly attached connection.
 o sendContinue()
 o sendReply(Reply)
The public way of emitting replies.
 o setTimeout(int, Object)
Sets this client next timer.
 o terminateConnection()
Terminate the connection that the client is currently handling.
 o toString()
Print a client.
 o trace(String)
Make this client emit a trace.
 o tryKeepConnection(Request, Reply)
Should we keep alive for the given coupl of request and reply.
 o unbind()

Variables

 o server
  protected httpd server
 o parser
  protected MimeParser parser
 o thread
  protected Thread thread
 o input
  protected InputStream input
 o output
  protected DataOutputStream output
 o socket
  protected Socket socket

Methods

 o getIdentifier
  public int getIdentifier()
Get the client identifier.
 o getInputStream
  public InputStream getInputStream()
Get this client input stream.
 o getOutputStream
  public OutputStream getOutputStream()
get this client output stream.
 o getServer
  public final httpd getServer()
Get the client server's
 o getThread
  public Thread getThread()
Get the client thread.
 o getInetAddress
  public InetAddress getInetAddress()
Get the IP address of this client.
Returns:
InetAddress
 o getOutputFileDescriptor
  public FileDescriptor getOutputFileDescriptor() throws IOException
Get this client output file descriptor. This is used by the shuffler, when possible.
 o flushOutput
  public void flushOutput() throws IOException
 o toString
  public String toString()
Print a client. This methods print the best identifier for clients. It includes the client thread (if possible) and its identifier with regard to the server context.

If no thread is attached to the client (which means the client has been * interrupted but has not been killed), than a z for zombie is printed.

Overrides:
toString in class Object
 o trace
  public void trace(String msg)
Make this client emit a trace. This function will take care of preceeding any traces with a client uniq identifier. All traces are managed by the httpd instances that created the client.
Parameters:
msg - The message to emit.
 o error
  public void error(String msg)
Emit an error on behalf of this client. Error reporting is managed by the instance of httpd that created this client.
Parameters:
msg - The error message.
See Also:
httpd
 o log
  public void log(Request request,
                  Reply reply,
                  int nbytes,
                  long duration)
Log the given request. Logging is delegated to the httpd instance that craetd this client.
Parameters:
request - The request to log.
reply - Its corresponding reply.
nbytes - The number of bytes sent to the client.
duration - The time it took to process the request.
 o handleTimerEvent
  public synchronized void handleTimerEvent(Object data,
                                            long time)
Handle timer events. For the time being, timer events are only used to detect an overrunning request, so this handler just kills the correponding client.

The timers package was written by Jonathan Payne.

Parameters:
data - The timer closure.
time - The absoliute time at which the event was triggered.
See Also:
EventManager, EventHandler
 o setTimeout
  public synchronized void setTimeout(int ms,
                                      Object data)
Sets this client next timer. A client can only handle a single pending timeout, so setting a new timeout to some client cancels any previous pending timeout.
Parameters:
ms - The number of milliseconds after which the timer should expire.
data - The call data for the tevent timer handler.
 o getNextRequest
  protected Request getNextRequest() throws ClientException
Get the next avaiable request for this client. This is the first stage in actually processing a client connection. This method sets a client timeout to detect idle clients connection. When trigered this timeout will break the client's connection, which will make the client thread exits from this method with a ClientException.
Returns:
A Request instance, or null if the connection was closed prematurely.
Throws: ClientException
If the request couldn't be parsed, or if the input stream was faulty.
 o processRequest
  protected Reply processRequest(Request request) throws ClientException
Process a request. This methods processs the request to the point that a reply is available. This methods sets a timeout, to limit the duration of this request processing. This timeout overrides the getRequest timeout.
Parameters:
request - The request to process.
Throws: ClientException
If either the timeout expires or the entity was unable to handle the request.
 o tryKeepConnection
  protected boolean tryKeepConnection(Request request,
                                      Reply reply)
Should we keep alive for the given coupl of request and reply. Test wether we can keep the connection alive, after the given reply has been emited. This method distinguishes wether we are acting as a proxy, or as a simple client, and calls the appropriate method to finish the job.
Parameters:
request - The request to examine.
reply - Its computed reply.
 o chunkTransfer
  protected int chunkTransfer(InputStream is) throws IOException
Emit the reply stream, using chunk encoding. The client buffer should be allocated at this point.
Parameters:
is - The stream to emit until EOF
 o emitReply
  protected int emitReply(Reply reply) throws IOException
Emit the given reply to the client.
Parameters:
reply - The reply to be emited.
Returns:
The number of body bytes emited.
 o sendReply
  public int sendReply(Reply reply) throws IOException
The public way of emitting replies. Normally, a resource shouldn't access any client data, however support for status code in the 100 range requires that a resource be able to emit a Continue reply before successfully processing a two phase method (see section 8.2 of HTTP/1.1 specification).

This method allows a resource to send a reply straight to the client. If the Client protocol version doesn't allow for such asynchronous replies, this method will return without emitting anything.

Parameters:
reply - The reply to emit.
Returns:
The number of bytes of content emited, or -1 if the client protocol version doesn't allow for such nifty things.
Throws: IOException
If emitting the reply failed.
 o sendContinue
  public int sendContinue() throws IOException
 o loop
  protected void loop() throws ClientException
Request get processed in three stages: a) Read and pre-parse the request: in this stage, the parser throws HTTPParserException only. b) Got the request, now handle it. In this stage only HTTPException can occur. c) Send back output, in this stage IOException only can occur. After this, we log the request and see if the client supports keep connection.
 o terminateConnection
  protected synchronized void terminateConnection()
Terminate the connection that the client is currently handling. We clear up all our socket related resources, and hope that the client thread will get some exception, if it is blocked on some input/outputs.
 o run
  public void run()
Starts this client. Any running client waits until it is bound to a socket. Once this happen it runs the connection and return to its waiting state.
 o runConnection
  protected boolean runConnection()
Run for our newly attached connection.
 o bind
  public synchronized void bind(Socket s)
Bind this awaiting client to the provided socket.
Parameters:
s - The socket this client should now handle.
 o isBound
  public synchronized boolean isBound()
Is this client bound to some socket already ?
 o loopForBinding
  public synchronized void loopForBinding()
Wait for this client to be bind to some socket.
 o unbind
  public synchronized void unbind()
 o kill
  public synchronized void kill(boolean force)
Kill this client, freeing all its associated resources.
Parameters:
force - If true, kill the client even if it is handling a connection, otherwise, notify the client to terminate.

All Packages  Class Hierarchy  This Package  Previous  Next  Index