com.limegroup.gnutella.uploader
Class HTTPUploader

java.lang.Object
  extended bycom.limegroup.gnutella.uploader.HTTPUploader
All Implemented Interfaces:
BandwidthTracker, Uploader

public final class HTTPUploader
extends java.lang.Object
implements Uploader

Maintains state for an HTTP upload request. This class follows the State Pattern, delegating its writeResponse method to the appropriate state. All states except for CONNECTING, COMPLETE, and INTERRUPTED have an associated state class that implements HTTPMessage. Care must be taken to call closeFileStreams whenever a chunk of the transfer is finished, and to call stop when the entire HTTP/1.1 session is finished. A single HTTPUploader should be reused for multiple chunks of a single file in an HTTP/1.1 session. However, multiple HTTPUploaders should be used for multiple files in a single HTTP/1.1 session.


Field Summary
 
Fields inherited from interface com.limegroup.gnutella.Uploader
BROWSE_HOST, COMPLETE, CONNECTING, FILE_NOT_FOUND, FREELOADER, INTERRUPTED, LIMIT_REACHED, MALFORMED_REQUEST, PUSH_PROXY, QUEUED, UNAVAILABLE_RANGE, UPDATE_FILE, UPLOADING
 
Constructor Summary
HTTPUploader(HTTPRequestMethod method, java.lang.String fileName, java.net.Socket socket, int index, java.util.Map params, StalledUploadWatchdog dog)
          Consructor for a "normal" non-push upload.
 
Method Summary
 int amountUploaded()
          The amount of bytes that this upload has transferred.
 void closeFileStreams()
          Close the file input stream.
 int getAmountRequested()
          returns the length of the requested size for uploading
 float getAverageBandwidth()
          Returns the overall averaged bandwidth between all calls of measureBandwidth
 boolean getCloseConnection()
          Returns whether or not the current state wants to close the connection.
 FileDesc getFileDesc()
          Returns the FileDesc instance for this uploader.
 java.lang.String getFileName()
          returns the name of the file being uploaded.
 int getFileSize()
          returns the length of the file being uploaded.
 int getGnutellaPort()
          return the port of the gnutella-client host (not the HTTP port)
 java.lang.String getHost()
          returns the string representation of the IP Address of the host being uploaded to.
 int getIndex()
          returns the index of the file being uploaded.
 int getLastTransferState()
          Returns the last transfer state of this uploader.
 float getMeasuredBandwidth()
          Returns the throughput of this in kilobytes/sec (KB/s) between the last two calls to measureBandwidth, or 0.0 if unknown.
 HTTPRequestMethod getMethod()
          Returns the current HTTP Request Method.
 java.net.InetAddress getNodeAddress()
           
 int getNodePort()
           
 int getQueuePosition()
          Returns the queued position if queued.
 URN getRequestedURN()
          Returns the content URN that the client asked for.
 int getState()
          Returns the current state of this uploader.
 int getTotalAmountUploaded()
          The total amount of bytes that this upload and all previous uploaders have transferred on this socket in this file-exchange.
 int getUploadBegin()
          The byte offset where we should start the upload.
 int getUploadEnd()
          Returns the offset of the last byte to send PLUS ONE.
 java.lang.String getUserAgent()
          return the userAgent
 void initializeStreams()
          Initializes the OutputStream for this HTTPUploader to use.
 boolean isBrowseHostEnabled()
          returns true if browse host is enabled, false if it is not.
 boolean isChatEnabled()
          returns true if chat for the host is on, false if it is not.
 boolean isHeaderParsed()
          return whether or not the headers have been parsed
 boolean isInactive()
          Returns whether or not this upload is in what is considered an "inactive" state, such as completed or aborted.
 void measureBandwidth()
          Measures the data throughput since the last call to measureBandwidth.
 void readHeader(java.io.InputStream iStream)
          Reads the HTTP header sent by the requesting client -- note that the 'GET' portion of the request header has already been read.
 void reinitialize(HTTPRequestMethod method, java.util.Map params)
          Reinitializes this uploader for a new request method.
 void setFileDesc(FileDesc fd)
          Sets the FileDesc for this HTTPUploader to use.
 void setState(int state)
          This method changes the appropriate state class based on the integer representing the state.
 void stop()
          Closes the outputstream, inputstream, and socket for this upload connection if they are not null.
 boolean supportsQueueing()
           
 java.lang.String toString()
           
 void writeResponse()
          Starts "uploading" the requested file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HTTPUploader

public HTTPUploader(HTTPRequestMethod method,
                    java.lang.String fileName,
                    java.net.Socket socket,
                    int index,
                    java.util.Map params,
                    StalledUploadWatchdog dog)
Consructor for a "normal" non-push upload. Note that this can be a URN get request.

Parameters:
method - the HTTPRequestMethod for the request
fileName - the name of the file
socket - the Socket instance to serve the upload over
index - the index of the file in the set of shared files
params - the map of parameters in the http request.
dog - the StalledUploadWatchdog to use for monitor stalls. to initialize this' bandwidth tracker so we have history
Method Detail

reinitialize

public void reinitialize(HTTPRequestMethod method,
                         java.util.Map params)
Reinitializes this uploader for a new request method.

Parameters:
method - the HTTPRequestMethod to change to.
params - the parameter list to change to.

setFileDesc

public void setFileDesc(FileDesc fd)
                 throws java.io.IOException
Sets the FileDesc for this HTTPUploader to use. Throws IOException if the file cannot be read from the disk.

Throws:
java.io.IOException

initializeStreams

public void initializeStreams()
                       throws java.io.IOException
Initializes the OutputStream for this HTTPUploader to use. Throws IOException if the connection was closed.

Throws:
java.io.IOException

writeResponse

public void writeResponse()
                   throws java.io.IOException
Starts "uploading" the requested file. The behavior of the upload, however, depends on the current upload state. If the file was not found, for example, the upload sends a 404 Not Found message, whereas in the case of a normal upload, the file is transferred as expected.

This method also handles storing any newly discovered alternate locations for this file in the corresponding FileDesc. The new alternate locations are discovered through the requesting client's HTTP headers.

Implements the Uploader interface.

Specified by:
writeResponse in interface Uploader
Throws:
java.io.IOException

stop

public void stop()
Closes the outputstream, inputstream, and socket for this upload connection if they are not null. Implements the Uploader interface.

Specified by:
stop in interface Uploader

closeFileStreams

public void closeFileStreams()
Close the file input stream.


setState

public void setState(int state)
This method changes the appropriate state class based on the integer representing the state. I'm not sure if this is a good idea, since it results in a case statement, that i was trying to avoid with. Implements the Uploader interface.

Specified by:
setState in interface Uploader

getCloseConnection

public boolean getCloseConnection()
Returns whether or not the current state wants to close the connection.


getMethod

public HTTPRequestMethod getMethod()
Returns the current HTTP Request Method.

Specified by:
getMethod in interface Uploader

getQueuePosition

public int getQueuePosition()
Returns the queued position if queued.

Specified by:
getQueuePosition in interface Uploader

isInactive

public boolean isInactive()
Returns whether or not this upload is in what is considered an "inactive" state, such as completed or aborted.

Specified by:
isInactive in interface Uploader
Returns:
true if this upload is in an inactive state, false otherwise

getUploadBegin

public int getUploadBegin()
The byte offset where we should start the upload.


getUploadEnd

public int getUploadEnd()
Returns the offset of the last byte to send PLUS ONE.


getFileSize

public int getFileSize()
Description copied from interface: Uploader
returns the length of the file being uploaded.

Specified by:
getFileSize in interface Uploader

getAmountRequested

public int getAmountRequested()
Description copied from interface: Uploader
returns the length of the requested size for uploading

Specified by:
getAmountRequested in interface Uploader

getIndex

public int getIndex()
Description copied from interface: Uploader
returns the index of the file being uploaded.

Specified by:
getIndex in interface Uploader

getFileName

public java.lang.String getFileName()
Description copied from interface: Uploader
returns the name of the file being uploaded.

Specified by:
getFileName in interface Uploader

getState

public int getState()
Description copied from interface: Uploader
Returns the current state of this uploader.

Specified by:
getState in interface Uploader

getLastTransferState

public int getLastTransferState()
Description copied from interface: Uploader
Returns the last transfer state of this uploader. Transfers states are all states except INTERRUPTED, COMPLETE, and CONNECTING.

Specified by:
getLastTransferState in interface Uploader

getHost

public java.lang.String getHost()
Description copied from interface: Uploader
returns the string representation of the IP Address of the host being uploaded to.

Specified by:
getHost in interface Uploader

isChatEnabled

public boolean isChatEnabled()
Description copied from interface: Uploader
returns true if chat for the host is on, false if it is not.

Specified by:
isChatEnabled in interface Uploader

isBrowseHostEnabled

public boolean isBrowseHostEnabled()
Description copied from interface: Uploader
returns true if browse host is enabled, false if it is not.

Specified by:
isBrowseHostEnabled in interface Uploader

getGnutellaPort

public int getGnutellaPort()
Description copied from interface: Uploader
return the port of the gnutella-client host (not the HTTP port)

Specified by:
getGnutellaPort in interface Uploader

getUserAgent

public java.lang.String getUserAgent()
Description copied from interface: Uploader
return the userAgent

Specified by:
getUserAgent in interface Uploader

isHeaderParsed

public boolean isHeaderParsed()
Description copied from interface: Uploader
return whether or not the headers have been parsed

Specified by:
isHeaderParsed in interface Uploader

supportsQueueing

public boolean supportsQueueing()
Specified by:
supportsQueueing in interface Uploader

getNodeAddress

public java.net.InetAddress getNodeAddress()

getNodePort

public int getNodePort()

amountUploaded

public int amountUploaded()
The amount of bytes that this upload has transferred. For HTTP/1.1 transfers, this number is the amount uploaded for this specific chunk only. Uses getTotalAmountUploaded for the entire amount uploaded. Implements the Uploader interface.

Specified by:
amountUploaded in interface Uploader

getTotalAmountUploaded

public int getTotalAmountUploaded()
The total amount of bytes that this upload and all previous uploaders have transferred on this socket in this file-exchange. Implements the Uploader interface.

Specified by:
getTotalAmountUploaded in interface Uploader

getFileDesc

public FileDesc getFileDesc()
Returns the FileDesc instance for this uploader.

Specified by:
getFileDesc in interface Uploader
Returns:
the FileDesc instance for this uploader, or null if the FileDesc could not be assigned from the shared files

getRequestedURN

public URN getRequestedURN()
Returns the content URN that the client asked for.


readHeader

public void readHeader(java.io.InputStream iStream)
                throws java.io.IOException
Reads the HTTP header sent by the requesting client -- note that the 'GET' portion of the request header has already been read.

Parameters:
iStream - the input stream to read the headers from.
Throws:
IOException - if the connection closes while reading
ProblemReadingHeaderException - if any header is invalid
java.io.IOException

measureBandwidth

public void measureBandwidth()
Description copied from interface: BandwidthTracker
Measures the data throughput since the last call to measureBandwidth. This value can be read by calling getMeasuredBandwidth.

Specified by:
measureBandwidth in interface BandwidthTracker

getMeasuredBandwidth

public float getMeasuredBandwidth()
Description copied from interface: BandwidthTracker
Returns the throughput of this in kilobytes/sec (KB/s) between the last two calls to measureBandwidth, or 0.0 if unknown.

Specified by:
getMeasuredBandwidth in interface BandwidthTracker

getAverageBandwidth

public float getAverageBandwidth()
Description copied from interface: BandwidthTracker
Returns the overall averaged bandwidth between all calls of measureBandwidth

Specified by:
getAverageBandwidth in interface BandwidthTracker

toString

public java.lang.String toString()