HTTPClient
Class StreamDemultiplexor

java.lang.Object
  |
  +--HTTPClient.StreamDemultiplexor
All Implemented Interfaces:
GlobalConstants

class StreamDemultiplexor
extends java.lang.Object
implements GlobalConstants

This class handles the demultiplexing of input stream. This is needed for things like keep-alive in HTTP/1.0, persist in HTTP/1.1 and in HTTP-NG.


Fields inherited from interface HTTPClient.GlobalConstants
CD_0, CD_CHUNKED, CD_CLOSE, CD_CONTLEN, CD_HDRS, CD_MP_BR, CD_NONE, HTTP, HTTP_1_0, HTTP_1_1, HTTP_NG, HTTPS, SHTTP
 
Constructor Summary
(package private) StreamDemultiplexor(int protocol, java.net.Socket sock, HTTPConnection connection)
          a simple contructor.
 
Method Summary
TypeMethod
(package private) static void ()
           
(package private)  void abort()
          Emergency stop.
(package private)  int available(ResponseHandler resph)
          Determines the number of available bytes.
(package private)  void close(java.io.IOException exception, boolean was_reset)
          Closes the socket and all associated streams.
(package private)  void closeSocketIfAllStreamsClosed()
          Close the socket if all the streams have been closed.
protected  void finalize()
          A safety net to close the connection.
(package private)  java.net.Socket getSocket()
          returns the socket associated with this demux
(package private)  RespInputStream getStream(Response resp)
          creates an input stream for the response.
(package private)  void markForClose(Response resp)
          Mark this demux to not accept any more request and to close the stream after this response or all requests have been processed, or close immediately if no requests are registered.
(package private)  int read(byte[] b, int off, int len, ResponseHandler resph, int timeout)
          reads an array of bytes from the master stream.
(package private)  void register(Response resp_handler, Request req)
          Each Response must register with us.
(package private)  void restartTimer()
          Restarts the timer thread that will close an unused socket after its idle time has expired.
(package private)  void setIdle(int idle)
          Sets the idle time for the restart timer.
(package private)  long skip(long num, ResponseHandler resph)
          skips a number of bytes in the master stream.
 java.lang.String toString()
          produces a string.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StreamDemultiplexor

StreamDemultiplexor(int protocol,
                    java.net.Socket sock,
                    HTTPConnection connection)
              throws java.io.IOException
a simple contructor.
Parameters:
protocol - the protocol used on this stream.
sock - the socket which we're to demux.
connection - the http-connection this socket belongs to.
Method Detail

static void ()

register

void register(Response resp_handler,
              Request req)
        throws RetryException
Each Response must register with us.

getStream

RespInputStream getStream(Response resp)
creates an input stream for the response.
Parameters:
resp - the response structure requesting the stream
Returns:
an InputStream

restartTimer

void restartTimer()
Restarts the timer thread that will close an unused socket after its idle time has expired.

setIdle

void setIdle(int idle)
Sets the idle time for the restart timer.

read

int read(byte[] b,
         int off,
         int len,
         ResponseHandler resph,
         int timeout)
   throws java.io.IOException
reads an array of bytes from the master stream.

skip

long skip(long num,
          ResponseHandler resph)
    throws java.io.IOException
skips a number of bytes in the master stream. This is done via a dummy read, as the socket input stream doesn't like skip()'s.

available

int available(ResponseHandler resph)
        throws java.io.IOException
Determines the number of available bytes. If resph is null, return available bytes on the socket stream itself (used by HTTPConnection).

close

void close(java.io.IOException exception,
           boolean was_reset)
Closes the socket and all associated streams. If exception is not null then all active requests are retried.

There are five ways this method may be activated. 1) if an exception occurs during read or write. 2) if the stream is marked for close but no responses are outstanding (e.g. due to a timeout). 3) when the markedForClose response is closed. 4) if all response streams up until and including the markedForClose response have been closed. 5) if this demux is finalized.

Parameters:
exception - the IOException to be sent to the streams.
was_reset - if true then the exception is due to a connection reset; otherwise it means we generated the exception ourselves and this is a "normal" close.

closeSocketIfAllStreamsClosed

void closeSocketIfAllStreamsClosed()
Close the socket if all the streams have been closed.

When a stream reaches eof it is removed from the response handler list, but when somebody close()'s the response stream it is just marked as such. This means that all responses in the list have either not been read at all or only partially read, but they might have been close()'d meaning that nobody is interested in the data. So If all the response streams up till and including the one markedForClose have been close()'d then we can remove them from our list and close the socket.

Note: if the response list is emtpy or if no response is markedForClose then this method does nothing. Specifically it does not close the socket. We only want to close the socket if we've been told to do so.

Also note that there might still be responses in the list after the markedForClose one. These are due to us having pipelined more requests to the server than it's willing to serve on a single connection. These requests will be retried if possible.


getSocket

java.net.Socket getSocket()
returns the socket associated with this demux

markForClose

void markForClose(Response resp)
Mark this demux to not accept any more request and to close the stream after this response or all requests have been processed, or close immediately if no requests are registered.
Parameters:
response - the Response after which the connection should be closed.

abort

void abort()
Emergency stop. Closes the socket and notifies the responses that the requests are aborted.
Since:
V0.3

finalize

protected void finalize()
                 throws java.lang.Throwable
A safety net to close the connection.
Overrides:
finalize in class java.lang.Object

toString

public java.lang.String toString()
produces a string.
Overrides:
toString in class java.lang.Object
Returns:
a string containing the class name and protocol number