Package org.klomp.snark.web
Class FetchAndAdd
- java.lang.Object
- 
- org.klomp.snark.Snark
- 
- org.klomp.snark.web.FetchAndAdd
 
 
- 
- All Implemented Interfaces:
- Runnable,- EepGet.StatusListener,- CoordinatorListener,- ShutdownListener,- StorageListener
 
 public class FetchAndAdd extends Snark implements EepGet.StatusListener, Runnable A cancellable torrent file downloader. We extend Snark so its status may be easily listed in the web table without adding a lot of code there. Upon successful download, this Snark will be deleted and a "real" Snark created. The methods return values similar to a Snark in magnet mode. A fake info hash, which is the SHA1 of the URL, is returned to prevent duplicates. This Snark may be stopped and restarted, although a partially downloaded file is discarded.- Since:
- 0.9.1 Moved from I2PSnarkUtil
 
- 
- 
Field Summary- 
Fields inherited from class org.klomp.snark.Snark_util, PROP_MAX_CONNECTIONS
 
- 
 - 
Constructor SummaryConstructors Constructor Description FetchAndAdd(I2PAppContext ctx, SnarkManager mgr, String url, File dataDir)Caller should call _mgr.addDownloader(this), which will start things off.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidattemptFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt, int numRetries, Exception cause)voidattempting(String url)voidbytesTransferred(long alreadyTransferred, int currentWrite, long bytesTransferred, long bytesRemaining, String url)Total length should be == alreadyTransferred + currentWrite + bytesRemaining for all callsStringgetBaseName()longgetDownloaded()longgetDownloadRate()byte[]getInfoHash()StringgetName()longgetNeededLength()Bytes still wanted.intgetPeerCount()longgetRemainingLength()Bytes not yet in storage.longgetTotalLength()intgetTrackerSeenPeers()voidheaderReceived(String url, int attemptNum, String key, String val)Note: Headers are not processed, and this is not called, for most error response codes, unless setWriteErrorToOutput() is called before fetch().booleanisStopped()voidrun()Set off by startTorrent()voidstartTorrent()Start up contacting peers and querying the tracker.voidstopTorrent()Stop contacting the tracker and talking with peersvoidtransferComplete(long alreadyTransferred, long bytesTransferred, long bytesRemaining, String url, String outputFile, boolean notModified)voidtransferFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt)- 
Methods inherited from class org.klomp.snark.SnarkaddComments, addMessage, getCheckingProgress, getComments, getID, getMetaInfo, getNeeded, getPeerList, getPieceLength, getPieces, getRPCID, getSkippedLength, getStartedTime, getStorage, getTrackerProblems, getTrackerURL, getUploaded, getUploadRate, gotMetaInfo, isAllocating, isAutoStoppable, isChecking, isStarting, overUpBWLimit, overUpBWLimit, overUploadLimit, peerChange, restartAcceptor, setAutoStoppable, setStarting, setTrackerProblems, setTrackerSeenPeers, setWantedPieces, shutdown, stopTorrent, storageAllChecked, storageAllocated, storageChecked, storageCompleted, storageCreateFile, updatePiecePriorities
 
- 
 
- 
- 
- 
Constructor Detail- 
FetchAndAddpublic FetchAndAdd(I2PAppContext ctx, SnarkManager mgr, String url, File dataDir) Caller should call _mgr.addDownloader(this), which will start things off.- Parameters:
- dataDir- null to default to snark data directory
 
 
- 
 - 
Method Detail- 
startTorrentpublic void startTorrent() Description copied from class:SnarkStart up contacting peers and querying the tracker. Blocks if tunnel is not yet open.- Overrides:
- startTorrentin class- Snark
 
 - 
stopTorrentpublic void stopTorrent() Description copied from class:SnarkStop contacting the tracker and talking with peers- Overrides:
- stopTorrentin class- Snark
 
 - 
getNamepublic String getName() 
 - 
getBaseNamepublic String getBaseName() - Overrides:
- getBaseNamein class- Snark
- Returns:
- base name of torrent [filtered version of getMetaInfo.getName()], or a fake name if in magnet mode
 
 - 
getInfoHashpublic byte[] getInfoHash() - Overrides:
- getInfoHashin class- Snark
- Returns:
- always will be valid even in magnet mode
 
 - 
getTotalLengthpublic long getTotalLength() - Overrides:
- getTotalLengthin class- Snark
- Returns:
- torrent file size or -1
 
 - 
getRemainingLengthpublic long getRemainingLength() Description copied from class:SnarkBytes not yet in storage. Does NOT account for skipped files.- Overrides:
- getRemainingLengthin class- Snark
- Returns:
- -1 when done so the web will list us as "complete" instead of "seeding"
 
 - 
getNeededLengthpublic long getNeededLength() Description copied from class:SnarkBytes still wanted. DOES account for (i.e. does not include) skipped files. FIXME -1 when not running.- Overrides:
- getNeededLengthin class- Snark
- Returns:
- torrent file bytes remaining or -1
 
 - 
getDownloadRatepublic long getDownloadRate() - Overrides:
- getDownloadRatein class- Snark
 
 - 
getDownloadedpublic long getDownloaded() - Overrides:
- getDownloadedin class- Snark
 
 - 
getPeerCountpublic int getPeerCount() - Overrides:
- getPeerCountin class- Snark
 
 - 
getTrackerSeenPeerspublic int getTrackerSeenPeers() - Overrides:
- getTrackerSeenPeersin class- Snark
- Returns:
- count returned from tracker
 
 - 
attemptFailedpublic void attemptFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt, int numRetries, Exception cause) - Specified by:
- attemptFailedin interface- EepGet.StatusListener
- See Also:
- EepGet.StatusListener.bytesTransferred(long, int, long, long, java.lang.String)
 
 - 
bytesTransferredpublic void bytesTransferred(long alreadyTransferred, int currentWrite, long bytesTransferred, long bytesRemaining, String url)Description copied from interface:EepGet.StatusListenerTotal length should be == alreadyTransferred + currentWrite + bytesRemaining for all calls- Specified by:
- bytesTransferredin interface- EepGet.StatusListener
- Parameters:
- alreadyTransferred- total of all attempts, not including currentWrite If nonzero on the first call, a partial file of that length was found, _and_ the server supports resume. If zero on a subsequent call after some bytes are transferred (and presumably after an attemptFailed), the server does _not_ support resume and we had to start over. To track _actual_ transfer if the output file could already exist, the listener should keep its own counter, or subtract the initial alreadyTransferred value. And watch out for alreadyTransferred resetting if a resume failed...
- currentWrite- since last call to the listener
- bytesTransferred- includes headers, retries, redirects, discarded partial downloads, ...
- bytesRemaining- on this attempt only, currentWrite already subtracted - or -1 if chunked encoding or server does not return a length
 
 - 
transferCompletepublic void transferComplete(long alreadyTransferred, long bytesTransferred, long bytesRemaining, String url, String outputFile, boolean notModified)- Specified by:
- transferCompletein interface- EepGet.StatusListener
- outputFile- null if unknown (output stream constructor)
- See Also:
- EepGet.StatusListener.bytesTransferred(long, int, long, long, java.lang.String)
 
 - 
transferFailedpublic void transferFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt) - Specified by:
- transferFailedin interface- EepGet.StatusListener
- See Also:
- EepGet.StatusListener.bytesTransferred(long, int, long, long, java.lang.String)
 
 - 
headerReceivedpublic void headerReceived(String url, int attemptNum, String key, String val) Description copied from interface:EepGet.StatusListenerNote: Headers are not processed, and this is not called, for most error response codes, unless setWriteErrorToOutput() is called before fetch(). To be changed?- Specified by:
- headerReceivedin interface- EepGet.StatusListener
 
 - 
attemptingpublic void attempting(String url) - Specified by:
- attemptingin interface- EepGet.StatusListener
 
 
- 
 
-