com.limegroup.gnutella
Class UploadManager

java.lang.Object
  extended bycom.limegroup.gnutella.UploadManager
All Implemented Interfaces:
BandwidthTracker

public final class UploadManager
extends java.lang.Object
implements BandwidthTracker

This class parses HTTP requests and delegates to HTTPUploader to handle individual uploads. The state of HTTPUploader is maintained by this class. HTTPUploader's state follows the following pattern: \ / |->---- UNAVAILABLE_RANGE -->--| |->---- PUSH_PROXY --------->--| /-->---- FILE NOT FOUND ----->--| /--->---- MALFORMED REQUEST -->--| /---->---- BROWSE HOST -------->--| /----->---- UPDATE FILE -------->--| /------>---- QUEUED ------------->--| /------->---- LIMIT REACHED ------>--| /-------->---- UPLOADING ---------->--| -->--CONNECTING-->--/ | | \|/ | | /|\ |--->INTERRUPTED |--------<---COMPLETE-<------<-------<-------<------/ (done) | | (done) The states in the middle (those other than CONNECTING, COMPLETE and INTERRUPTED) are part of the "State Pattern" and have an associated class that implements HTTPMessage. These state pattern classes are ONLY set while a transfer is active. For example, after we determine a request should be 'File Not Found', and send the response back, the state will become COMPLETE (unless there was an IOException while sending the response, in which case the state will become INTERRUPTED). To retrieve the last state that was used for transferring, use HTTPUploader.getLastTransferState(). Of particular note is that Queued uploaders are actually in COMPLETED state for the majority of the time. The QUEUED state is only active when we are actively writing back the 'You are queued' response. COMPLETE uploaders may be using HTTP/1.1, in which case the HTTPUploader recycles back to CONNECTING upon receiving the next GET/HEAD request and repeats. INTERRUPTED HTTPUploaders are never reused. However, it is possible that the socket may be reused. This odd case is ONLY possible when a requester is queued for one file and sends a subsequent request for another file. The first HTTPUploader is set as interrupted and a second one is created for the new file, using the same socket as the first one.

See Also:
HTTPUploader

Field Summary
static int BAD_URN_QUERY_INDEX
          The file index used in this structure to indicate a bad URN query.
static int BROWSE_HOST_FILE_INDEX
          The file index used in this structure to indicate a browse host request
static int MALFORMED_REQUEST_INDEX
          The file index used in this structure to indicate a malformed request.
static int MAX_POLL_TIME
           
static int MIN_POLL_TIME
          The min and max allowed times (in milliseconds) between requests by queued hosts.
static int PUSH_PROXY_FILE_INDEX
          The file index used in this structure to indicate a Push Proxy request.
static boolean RECORD_STATS
          Whether or not to record stats.
static int UPDATE_FILE_INDEX
          The file index used in this structure to indicate an update-file request
 
Constructor Summary
UploadManager()
           
 
Method Summary
 void acceptUpload(HTTPRequestMethod method, java.net.Socket socket, boolean forceAllow)
          Accepts a new upload, creating a new HTTPUploader if it successfully parses the HTTP request.
 int calculateBandwidth()
          calculates the appropriate burst size for the allocating bandwith on the upload.
 void close(java.net.Socket socket)
          closes the passed socket and its corresponding I/O streams
 float getAverageBandwidth()
          returns the summed average of the uploads
 float getMeasuredBandwidth()
          Returns the total upload throughput, i.e., the sum over all uploads.
 int getNumQueuedUploads()
           
 boolean hadSuccesfulUpload()
          Returns true if this has ever successfully uploaded a file during this session.
 boolean isBusy()
          Returns whether or not there are currently upload slots available, not taking the queue into account.
 boolean isQueueFull()
          Returns whether or not the upload queue is full.
 boolean isServiceable()
          Returns whether or not an upload request can be serviced immediately.
 boolean killUploadsForFileDesc(FileDesc fd)
          Kills all uploads that are uploading the given FileDesc.
 void measureBandwidth()
          Calls measureBandwidth on each uploader.
 int measuredUploadSpeed()
          Returns the estimated upload speed in KILOBITS/s [sic] of the next transfer, assuming the client (i.e., downloader) has infinite bandwidth.
 int positionInQueue(java.net.Socket socket)
           
 int uploadsInProgress()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_POLL_TIME

public static final int MIN_POLL_TIME
The min and max allowed times (in milliseconds) between requests by queued hosts.

See Also:
Constant Field Values

MAX_POLL_TIME

public static final int MAX_POLL_TIME
See Also:
Constant Field Values

BROWSE_HOST_FILE_INDEX

public static final int BROWSE_HOST_FILE_INDEX
The file index used in this structure to indicate a browse host request

See Also:
Constant Field Values

UPDATE_FILE_INDEX

public static final int UPDATE_FILE_INDEX
The file index used in this structure to indicate an update-file request

See Also:
Constant Field Values

BAD_URN_QUERY_INDEX

public static final int BAD_URN_QUERY_INDEX
The file index used in this structure to indicate a bad URN query.

See Also:
Constant Field Values

MALFORMED_REQUEST_INDEX

public static final int MALFORMED_REQUEST_INDEX
The file index used in this structure to indicate a malformed request.

See Also:
Constant Field Values

PUSH_PROXY_FILE_INDEX

public static final int PUSH_PROXY_FILE_INDEX
The file index used in this structure to indicate a Push Proxy request.

See Also:
Constant Field Values

RECORD_STATS

public static final boolean RECORD_STATS
Whether or not to record stats.

Constructor Detail

UploadManager

public UploadManager()
Method Detail

acceptUpload

public void acceptUpload(HTTPRequestMethod method,
                         java.net.Socket socket,
                         boolean forceAllow)
Accepts a new upload, creating a new HTTPUploader if it successfully parses the HTTP request. BLOCKING.

Parameters:
method - the initial request type to use, e.g., GET or HEAD
socket - the Socket that will be used for the new upload. It is assumed that the initial word of the request (e.g., "GET") has been consumed (e.g., by Acceptor)
forceAllow - forces the UploadManager to allow all requests on this socket to take place.

close

public void close(java.net.Socket socket)
closes the passed socket and its corresponding I/O streams


isServiceable

public boolean isServiceable()
Returns whether or not an upload request can be serviced immediately. In particular, if there are no upload slots AND no queue slots are taken this will return true. Under any other condition this will return false.


isBusy

public boolean isBusy()
Returns whether or not there are currently upload slots available, not taking the queue into account. In particular, if there are no upload slots, but there are queue slots, this will still return true.

Returns:
true if there are no upload slots available

isQueueFull

public boolean isQueueFull()
Returns whether or not the upload queue is full.

Returns:
true if the upload queue is full, otherwise false

uploadsInProgress

public int uploadsInProgress()

getNumQueuedUploads

public int getNumQueuedUploads()

hadSuccesfulUpload

public boolean hadSuccesfulUpload()
Returns true if this has ever successfully uploaded a file during this session.

This method was added to adopt more of the BearShare QHD standard.


killUploadsForFileDesc

public boolean killUploadsForFileDesc(FileDesc fd)
Kills all uploads that are uploading the given FileDesc.


positionInQueue

public int positionInQueue(java.net.Socket socket)
Returns:
the index of the uploader in the queue, -1 if not in queue

calculateBandwidth

public int calculateBandwidth()
calculates the appropriate burst size for the allocating bandwith on the upload.

Returns:
burstSize. if it is the special case, in which we want to upload as quickly as possible.

measuredUploadSpeed

public int measuredUploadSpeed()
Returns the estimated upload speed in KILOBITS/s [sic] of the next transfer, assuming the client (i.e., downloader) has infinite bandwidth. Returns -1 if not enough data is available for an accurate estimate.


measureBandwidth

public void measureBandwidth()
Calls measureBandwidth on each uploader.

Specified by:
measureBandwidth in interface BandwidthTracker

getMeasuredBandwidth

public float getMeasuredBandwidth()
Returns the total upload throughput, i.e., the sum over all uploads.

Specified by:
getMeasuredBandwidth in interface BandwidthTracker

getAverageBandwidth

public float getAverageBandwidth()
returns the summed average of the uploads

Specified by:
getAverageBandwidth in interface BandwidthTracker