LimeWire Consolidated API

com.limegroup.gnutella
Class HTTPUploadManager

java.lang.Object
  extended by com.limegroup.gnutella.HTTPUploadManager
All Implemented Interfaces:
BandwidthTracker, HTTPUploadSessionManager, UploadManager, Service, FileLocker

public class HTTPUploadManager
extends Object
implements FileLocker, BandwidthTracker, UploadManager, HTTPUploadSessionManager, Service

Manages HTTPUploader objects that are created by HttpRequestHandlers through the HTTPUploadSessionManager interface. Since HTTP 1.1 allows multiple requests for a single connection an HTTPUploadSession is created for each connection. It keeps track of queuing (which is per connection) and bandwidth and has a reference to the HTTPUploader that represents the current request.

The state of HTTPUploader follows this pattern:

                             |->---- THEX_REQUEST ------->--|
                             |->---- UNAVAILABLE_RANGE -->--|
                             |->---- PUSH_PROXY --------->--|
                            /-->---- FILE NOT FOUND ----->--|
                           /--->---- MALFORMED REQUEST -->--|
                          /---->---- BROWSE HOST -------->--|
                         /----->---- UPDATE FILE -------->--|
                        /------>---- QUEUED ------------->--|
                       /------->---- LIMIT REACHED ------>--|
                      /-------->---- UPLOADING ---------->--|
 -->--CONNECTING-->--/                                      |
        |                                                  \|/
        |                                                   |
       /|\                                                  |--->INTERRUPTED
        |--------<---COMPLETE-<------<-------<-------<------/      (done)
                        |
                        |
                      (done)
 
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 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 connection as the first one.

To initialize the upload manager #start(HTTPAcceptor) needs to be invoked which registers handlers with an HTTPAcceptor.

See Also:
HTTPUploader, HTTPAcceptor

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.limegroup.gnutella.uploader.HTTPUploadSessionManager
HTTPUploadSessionManager.QueueStatus
 
Field Summary
static int TRANSFER_SOCKET_TIMEOUT
           
 
Constructor Summary
HTTPUploadManager(UploadSlotManager slotManager, HttpRequestHandlerFactory httpRequestHandlerFactory, com.google.inject.Provider<HTTPAcceptor> httpAcceptor, com.google.inject.Provider<FileManager> fileManager, com.google.inject.Provider<ActivityCallback> activityCallback, TcpBandwidthStatistics tcpBandwidthStatistics, com.google.inject.Provider<GnutellaUploadFileListProvider> gnutellaFileListProvider, com.google.inject.Provider<GnutellaBrowseFileListProvider> gnutellaBrowseFileListProvider, UrnValidator urnValidator)
           
 
Method Summary
 void addAcceptedUploader(HTTPUploader uploader, org.apache.http.protocol.HttpContext context)
          Adds an accepted HTTPUploader to the internal list of active downloads.
 int calculateBandwidth()
          Calculates the appropriate burst size for the allocating bandwidth on the upload.
 void cleanup()
          For testing: removes all uploaders and clears the request cache.
 void cleanupFinishedUploader(HTTPUploader uploader)
          Cleans up a finished uploader.
 HTTPUploadSessionManager.QueueStatus enqueue(org.apache.http.protocol.HttpContext context, org.apache.http.HttpRequest request)
          Add request to the queue of uploaders.
 float getAverageBandwidth()
          Returns the overall averaged bandwidth between all calls of measureBandwidth.
 float getLastMeasuredBandwidth()
          Return the last value that was measured by BandwidthTracker.getMeasuredBandwidth().
 float getMeasuredBandwidth()
          Returns the throughput of this in kilobytes/sec (KB/s) between the last two calls to measureBandwidth, or 0.0 if unknown.
 int getNumQueuedUploads()
          Returns the number of queued uploads.
 HTTPUploadSession getOrCreateSession(org.apache.http.protocol.HttpContext context)
           
 HTTPUploader getOrCreateUploader(org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context, UploadType type, String filename)
          Returns an uploader for request.
 String getServiceName()
           
 HTTPUploadSession getSession(org.apache.http.protocol.HttpContext context)
          Returns the session stored in context.
 UploadSlotManager getSlotManager()
           
 HTTPUploader getUploader(org.apache.http.protocol.HttpContext context)
           
 boolean hadSuccesfulUpload()
          Returns true if this has ever successfully uploaded a file during this session.
 void handleFreeLoader(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response, org.apache.http.protocol.HttpContext context, HTTPUploader uploader)
          Sets a response code and entity on response for handling requests from unsupported clients.
 void initialize()
           
 boolean isConnectedTo(InetAddress addr)
          Returns true if any uploader is conneected to addr.
 boolean isServiceable()
          Returns whether or not an upload request can be serviced immediately.
 boolean killUploadsForFileDesc(FileDesc fd)
          Stops all uploads that are uploading fd.
 boolean mayBeServiceable()
          Returns if an incoming query (not actual upload request) may be serviceable.
 void measureBandwidth()
          Measures the data throughput since the last call to measureBandwidth.
 int measuredUploadSpeed()
          Returns the estimated upload speed in KILOBITS/s [sic] of the next transfer, assuming the client (i.e., downloader) has infinite
 boolean releaseLock(File file)
          Returns true if the lock was released on the file.
 void sendResponse(HTTPUploader uploader, org.apache.http.HttpResponse response)
          Adds uploader to the GUI if it is not visible, yet, and increments the attempted uploads.
 void start()
          Registers the upload manager at acceptor.
 void stop()
          Unregisters the upload manager at acceptor.
 int uploadsInProgress()
          Returns the number of uploads excluding forced uploads.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TRANSFER_SOCKET_TIMEOUT

public static final int TRANSFER_SOCKET_TIMEOUT
See Also:
Constant Field Values
Constructor Detail

HTTPUploadManager

@Inject
public HTTPUploadManager(UploadSlotManager slotManager,
                                HttpRequestHandlerFactory httpRequestHandlerFactory,
                                com.google.inject.Provider<HTTPAcceptor> httpAcceptor,
                                com.google.inject.Provider<FileManager> fileManager,
                                com.google.inject.Provider<ActivityCallback> activityCallback,
                                TcpBandwidthStatistics tcpBandwidthStatistics,
                                com.google.inject.Provider<GnutellaUploadFileListProvider> gnutellaFileListProvider,
                                com.google.inject.Provider<GnutellaBrowseFileListProvider> gnutellaBrowseFileListProvider,
                                UrnValidator urnValidator)
Method Detail

getServiceName

public String getServiceName()
Specified by:
getServiceName in interface Service

initialize

public void initialize()
Specified by:
initialize in interface Service

start

public void start()
Registers the upload manager at acceptor.

Specified by:
start in interface UploadManager
Specified by:
start in interface Service
Throws:
IllegalStateException - if uploadmanager was already started
See Also:
#stop(HTTPAcceptor)

stop

public void stop()
Unregisters the upload manager at acceptor.

Specified by:
stop in interface UploadManager
Specified by:
stop in interface Service
See Also:
#start(HTTPAcceptor)

handleFreeLoader

public void handleFreeLoader(org.apache.http.HttpRequest request,
                             org.apache.http.HttpResponse response,
                             org.apache.http.protocol.HttpContext context,
                             HTTPUploader uploader)
                      throws org.apache.http.HttpException,
                             IOException
Description copied from interface: HTTPUploadSessionManager
Sets a response code and entity on response for handling requests from unsupported clients.

Specified by:
handleFreeLoader in interface HTTPUploadSessionManager
Throws:
org.apache.http.HttpException
IOException

cleanupFinishedUploader

public void cleanupFinishedUploader(HTTPUploader uploader)
Cleans up a finished uploader. This does the following:
  1. Reports the speed at which this upload occured.
  2. Removes the uploader from the active upload list
  3. Increments the completed uploads in the FileDesc
  4. Removes the uploader from the GUI


addAcceptedUploader

public void addAcceptedUploader(HTTPUploader uploader,
                                org.apache.http.protocol.HttpContext context)
Description copied from interface: HTTPUploadSessionManager
Adds an accepted HTTPUploader to the internal list of active downloads.

Specified by:
addAcceptedUploader in interface HTTPUploadSessionManager

sendResponse

public void sendResponse(HTTPUploader uploader,
                         org.apache.http.HttpResponse response)
Description copied from interface: HTTPUploadSessionManager
Adds uploader to the GUI if it is not visible, yet, and increments the attempted uploads.

Specified by:
sendResponse in interface HTTPUploadSessionManager

isServiceable

public boolean isServiceable()
Description copied from interface: UploadManager
Returns whether or not an upload request can be serviced immediately. In particular, if there are more available upload slots than queued uploads this will return true.

Specified by:
isServiceable in interface UploadManager

mayBeServiceable

public boolean mayBeServiceable()
Description copied from interface: UploadManager
Returns if an incoming query (not actual upload request) may be serviceable.

Specified by:
mayBeServiceable in interface UploadManager

uploadsInProgress

public int uploadsInProgress()
Description copied from interface: UploadManager
Returns the number of uploads excluding forced uploads.

Specified by:
uploadsInProgress in interface UploadManager

getNumQueuedUploads

public int getNumQueuedUploads()
Description copied from interface: UploadManager
Returns the number of queued uploads.

Specified by:
getNumQueuedUploads in interface UploadManager

hadSuccesfulUpload

public boolean hadSuccesfulUpload()
Description copied from interface: UploadManager
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.

Specified by:
hadSuccesfulUpload in interface UploadManager

isConnectedTo

public boolean isConnectedTo(InetAddress addr)
Description copied from interface: UploadManager
Returns true if any uploader is conneected to addr.

Specified by:
isConnectedTo in interface UploadManager

releaseLock

public boolean releaseLock(File file)
Description copied from interface: FileLocker
Returns true if the lock was released on the file.

Specified by:
releaseLock in interface FileLocker

killUploadsForFileDesc

public boolean killUploadsForFileDesc(FileDesc fd)
Description copied from interface: UploadManager
Stops all uploads that are uploading fd.

Specified by:
killUploadsForFileDesc in interface UploadManager

calculateBandwidth

public int calculateBandwidth()
Calculates the appropriate burst size for the allocating bandwidth 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()
Description copied from interface: UploadManager
Returns the estimated upload speed in KILOBITS/s [sic] of the next transfer, assuming the client (i.e., downloader) has infinite

Specified by:
measuredUploadSpeed in interface UploadManager

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

getLastMeasuredBandwidth

public float getLastMeasuredBandwidth()
Description copied from interface: UploadManager
Return the last value that was measured by BandwidthTracker.getMeasuredBandwidth().

Specified by:
getLastMeasuredBandwidth in interface UploadManager
Returns:
bandwidth in KB / s

getSlotManager

public UploadSlotManager getSlotManager()

getOrCreateSession

public HTTPUploadSession getOrCreateSession(org.apache.http.protocol.HttpContext context)

getSession

public HTTPUploadSession getSession(org.apache.http.protocol.HttpContext context)
Returns the session stored in context.

Returns:
null, if no session exists

getOrCreateUploader

public HTTPUploader getOrCreateUploader(org.apache.http.HttpRequest request,
                                        org.apache.http.protocol.HttpContext context,
                                        UploadType type,
                                        String filename)
Description copied from interface: HTTPUploadSessionManager
Returns an uploader for request. If the previous request was for filename as well an existing uploader is returned; otherwise a new uploader is created.

Specified by:
getOrCreateUploader in interface HTTPUploadSessionManager

getUploader

public HTTPUploader getUploader(org.apache.http.protocol.HttpContext context)

enqueue

public HTTPUploadSessionManager.QueueStatus enqueue(org.apache.http.protocol.HttpContext context,
                                                    org.apache.http.HttpRequest request)
Description copied from interface: HTTPUploadSessionManager
Add request to the queue of uploaders.

Specified by:
enqueue in interface HTTPUploadSessionManager
Returns:
the queue status
See Also:
UploadSlotManager

cleanup

public void cleanup()
For testing: removes all uploaders and clears the request cache.


LimeWire Consolidated API

Copyright © 2009. All Rights Reserved.