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