Package org.klomp.snark
Class SnarkManager
java.lang.Object
org.klomp.snark.SnarkManager
- All Implemented Interfaces:
ClientApp
,CompleteListener
public class SnarkManager extends Object implements CompleteListener, ClientApp
Manage multiple snarks
-
Field Summary
-
Constructor Summary
Constructors Constructor Description SnarkManager(I2PAppContext ctx)
For embedded.SnarkManager(I2PAppContext ctx, String ctxPath, String ctxName)
For webapp. -
Method Summary
Modifier and Type Method Description void
addDownloader(Snark torrent)
Add and start a FetchAndAdd task.void
addMagnet(String name, byte[] ih, String trackerURL, boolean updateStatus)
Add a torrent with the info hash alone (magnet / maggot)Snark
addMagnet(String name, byte[] ih, String trackerURL, boolean updateStatus, boolean autoStart, File dataDir, CompleteListener listener)
Add a torrent with the info hash alone (magnet / maggot) External use is for UpdateRunner.void
addMagnet(String name, byte[] ih, String trackerURL, boolean updateStatus, File dataDir)
Add a torrent with the info hash alone (magnet / maggot)void
addMessage(String message)
Use if it does not include a link.void
addMessage(Snark snark, String message)
A Snark.CompleteListener method.void
addMessageNoEscape(String message)
Use if it includes a link.boolean
addTorrent(MetaInfo metainfo, BitField bitfield, String filename, File baseFile, boolean dontAutoStart)
Add a torrent from a MetaInfo.boolean
areFilesPublic()
void
clearMessages()
void
clearMessages(int id)
Clear through this idboolean
copyAndAddTorrent(File fromfile, String filename, File dataDir)
Add a torrent from a file not in the torrent directory.void
deleteMagnet(Snark snark)
Stop and delete a torrent running in magnet modevoid
fatal(Snark snark, String error)
A Snark.CompleteListener method.File
getConfigDir()
For RPCFile
getDataDir()
String
getDisplayName()
ClientApp method.int
getMaxFilesPerTorrent()
List<UIMessages.Message>
getMessages()
newest lastString
getName()
ClientApp method.int
getPageSize()
For GUIList<String>
getPrivateTrackers()
int
getRefreshDelaySeconds()
long[]
getSavedAddedAndCompleted(Snark snark)
Get setting for a torrent from the config file.CommentSet
getSavedComments(Snark snark)
The comments for a torrentboolean
getSavedCommentsEnabled(Snark snark)
Get setting for comments enabled from the config file.boolean
getSavedPreserveNamesSetting(Snark snark)
Get setting for a torrent from the config file.BitField
getSavedTorrentBitField(Snark snark)
Get the saved bitfield for a torrent from the config file.long
getSavedTorrentTime(Snark snark)
Get the timestamp for a torrent from the config file.long
getSavedUploaded(Snark snark)
Get setting for a torrent from the config file.List<Tracker>
getSortedTrackers()
Sorted copyClientAppState
getState()
ClientApp method.String
getTheme()
Get current theme.String[]
getThemes()
Get all themesSnark
getTorrent(String filename)
Grab the torrent given the (canonical) filename of the .torrent fileSnark
getTorrentByBaseName(String filename)
Grab the torrent given the base name of the storageSnark
getTorrentByInfoHash(byte[] infohash)
Grab the torrent given the info hashCollection<Snark>
getTorrents()
UnmodifiableMap<String,Tracker>
getTrackerMap()
Unsorted map of name to Tracker object Modifiable, not a copyCollection<Tracker>
getTrackers()
Unsorted, do not modifyboolean
getUniversalTheming()
String
gotMetaInfo(Snark snark)
We transitioned from magnet mode, we have now initialized our metainfo and storage.void
gotPiece(Snark snark)
A Snark.CompleteListener method.boolean
hasModifiedTrackers()
Has the default tracker list been modified?boolean
isCollapsePanelsEnabled()
boolean
isEnableLightbox()
boolean
isShowStatusFilter()
boolean
isSmartSortEnabled()
boolean
isStopping()
Set<String>
listTorrentFiles()
Set of canonical .torrent filenames that we are dealing with.void
loadConfig(String filename)
void
loadSavedFilePriorities(Snark snark)
Get the saved priorities for a torrent from the config file.void
locked_saveComments(Snark snark, CommentSet comments)
Save the comments for a torrent Caller must synchronize on comments.void
recheckTorrent(Snark snark)
Threaded.void
removeMagnetStatus(byte[] ih)
Remove the magnet marker from the config file.void
removeTorrent(String filename)
Stop the torrent and delete the torrent file itself, but leaving the data behind.void
saveConfig()
void
saveMagnetStatus(byte[] ih, String dir, String trackerURL, String dn)
Just remember we have it.void
saveOpenTrackers(List<String> ot)
void
savePrivateTrackers(List<String> pt)
void
saveTorrentStatus(Snark snark)
Save the completion status of a torrent and other data in the config file for that torrent.void
saveTrackerMap()
void
setDefaultTrackerMap()
void
setSavedCommentsEnabled(Snark snark, boolean yes)
Set setting for comments enabled in the config file.boolean
shouldAutoStart()
void
shutdown(String[] args)
ClientApp method.void
start()
Caller _must_ call loadConfig(file) before this if setting new values for i2cp host/port or i2psnark.dirvoid
startAllTorrents()
If not connected, thread it, otherwise inlinevoid
startTorrent(byte[] infoHash)
If not connected, thread it, otherwise inlinevoid
startTorrent(Snark snark)
If not connected, thread it, otherwise inlinevoid
startup()
ClientApp method.void
stop()
void
stopAllTorrents(boolean finalShutdown)
Stop all running torrents, and close the tunnel after a delay to allow for announces.Snark
stopTorrent(String filename, boolean shouldRemove)
Stop the torrent, leaving it on the list of torrents unless told to remove it.void
stopTorrent(Snark torrent, boolean shouldRemove)
Stop the torrent, leaving it on the list of torrents unless told to remove it.void
torrentComplete(Snark snark)
A Snark.CompleteListener method.void
updateConfig(String dataDir, boolean filesPublic, boolean autoStart, boolean smartSort, String refreshDelay, String startDelay, String pageSize, String seedPct, String eepHost, String eepPort, String i2cpHost, String i2cpPort, String i2cpOpts, String upLimit, String upBW, boolean useOpenTrackers, boolean useDHT, String theme, String lang, boolean enableRatings, boolean enableComments, String commentName, boolean collapsePanels, boolean showStatusFilter, boolean enableLightbox, boolean enableAddCreate)
all params may be null or need trimmingvoid
updateStatus(Snark snark)
A Snark.CompleteListener method.I2PSnarkUtil
util()
hook to I2PSnarkUtil for the servlet
-
Field Details
-
PROP_I2CP_HOST
- See Also:
- Constant Field Values
-
PROP_I2CP_PORT
- See Also:
- Constant Field Values
-
PROP_I2CP_OPTS
- See Also:
- Constant Field Values
-
PROP_UPLOADERS_TOTAL
- See Also:
- Constant Field Values
-
PROP_UPBW_MAX
- See Also:
- Constant Field Values
-
PROP_DIR
- See Also:
- Constant Field Values
-
PROP_FILES_PUBLIC
- See Also:
- Constant Field Values
-
PROP_OLD_AUTO_START
- See Also:
- Constant Field Values
-
PROP_AUTO_START
- See Also:
- Constant Field Values
-
DEFAULT_AUTO_START
- See Also:
- Constant Field Values
-
PROP_STARTUP_DELAY
- See Also:
- Constant Field Values
-
PROP_REFRESH_DELAY
- See Also:
- Constant Field Values
-
PROP_PAGE_SIZE
- See Also:
- Constant Field Values
-
RC_PROP_THEME
- See Also:
- Constant Field Values
-
RC_PROP_UNIVERSAL_THEMING
- See Also:
- Constant Field Values
-
PROP_THEME
- See Also:
- Constant Field Values
-
DEFAULT_THEME
- See Also:
- Constant Field Values
-
PROP_COLLAPSE_PANELS
- Since:
- 0.9.32
- See Also:
- Constant Field Values
-
PROP_SHOW_STATUSFILTER
- Since:
- 0.9.34
- See Also:
- Constant Field Values
-
DEFAULT_SHOW_STATUSFILTER
- See Also:
- Constant Field Values
-
PROP_ENABLE_LIGHTBOX
- Since:
- 0.9.34
- See Also:
- Constant Field Values
-
DEFAULT_ENABLE_LIGHTBOX
- See Also:
- Constant Field Values
-
PROP_ENABLE_ADDCREATE
- Since:
- 0.9.38
- See Also:
- Constant Field Values
-
DEFAULT_ENABLE_ADDCREATE
- See Also:
- Constant Field Values
-
PROP_OPENTRACKERS
- See Also:
- Constant Field Values
-
PROP_PRIVATETRACKERS
- See Also:
- Constant Field Values
-
MIN_UP_BW
public static final int MIN_UP_BW- See Also:
- Constant Field Values
-
DEFAULT_MAX_UP_BW
public static final int DEFAULT_MAX_UP_BW- See Also:
- Constant Field Values
-
DEFAULT_STARTUP_DELAY
public static final int DEFAULT_STARTUP_DELAY- See Also:
- Constant Field Values
-
DEFAULT_REFRESH_DELAY_SECS
public static final int DEFAULT_REFRESH_DELAY_SECS- See Also:
- Constant Field Values
-
DEFAULT_TUNNEL_QUANTITY
public static final int DEFAULT_TUNNEL_QUANTITY- See Also:
- Constant Field Values
-
CONFIG_DIR_SUFFIX
- See Also:
- Constant Field Values
-
DEFAULT_BACKUP_TRACKER
URL. This is our equivalent to router.utorrent.com for bootstrap- See Also:
- Constant Field Values
-
DEFAULT_TRACKER_ANNOUNCES
-
KNOWN_OPENTRACKERS
host names for config form -
PROP_TRACKERS
comma delimited list of name=announceURL=baseURL for the trackers to be displayed- See Also:
- Constant Field Values
-
MAX_FILES_PER_TORRENT
public static final int MAX_FILES_PER_TORRENThardcoded for sanity. perhaps this should be customizable, for people who increase their ulimit, etc.- See Also:
- Constant Field Values
-
PROP_MAX_FILES_PER_TORRENT
- See Also:
- Constant Field Values
-
-
Constructor Details
-
SnarkManager
For embedded. -
SnarkManager
For webapp.- Parameters:
ctxPath
- generally "/i2psnark"ctxName
- generally "i2psnark"- Since:
- 0.9.6
-
-
Method Details
-
start
public void start()Caller _must_ call loadConfig(file) before this if setting new values for i2cp host/port or i2psnark.dir -
stop
public void stop() -
isStopping
public boolean isStopping()- Since:
- 0.9.1
-
startup
public void startup()ClientApp method. Does nothing. Doesn't matter, we are only registering. -
shutdown
ClientApp method. Does nothing. Doesn't matter, we are only registering. -
getState
ClientApp method. Doesn't matter, we are only registering. -
getName
ClientApp method. -
getDisplayName
ClientApp method.- Specified by:
getDisplayName
in interfaceClientApp
- Returns:
- non-null
- Since:
- 0.9.30
-
util
hook to I2PSnarkUtil for the servlet -
addMessage
Use if it does not include a link. Escapes '<' and '>' before queueing -
addMessageNoEscape
Use if it includes a link. Does not escape '<' and '>' before queueing- Since:
- 0.9.14.1
-
getMessages
newest last -
clearMessages
public void clearMessages()- Since:
- 0.9
-
clearMessages
public void clearMessages(int id)Clear through this id- Since:
- 0.9.33
-
areFilesPublic
public boolean areFilesPublic()- Returns:
- default false
- Since:
- 0.8.9
-
shouldAutoStart
public boolean shouldAutoStart()- Specified by:
shouldAutoStart
in interfaceCompleteListener
-
isSmartSortEnabled
public boolean isSmartSortEnabled()- Returns:
- default true
- Since:
- 0.9.23
-
isCollapsePanelsEnabled
public boolean isCollapsePanelsEnabled()- Returns:
- default true
- Since:
- 0.9.32
-
isShowStatusFilter
public boolean isShowStatusFilter()- Returns:
- default false
- Since:
- 0.9.34
-
isEnableLightbox
public boolean isEnableLightbox()- Returns:
- default true
- Since:
- 0.9.34
-
getRefreshDelaySeconds
public int getRefreshDelaySeconds()- Returns:
- -1 for never
- Since:
- 0.8.9
-
getMaxFilesPerTorrent
public int getMaxFilesPerTorrent()- Since:
- 0.9.46 (I2P+)
-
getPageSize
public int getPageSize()For GUI- Since:
- 0.9.6
-
getDataDir
-
getConfigDir
For RPC- Since:
- 0.9.30
-
getSavedComments
The comments for a torrent- Specified by:
getSavedComments
in interfaceCompleteListener
- Returns:
- null if none
- Since:
- 0.9.31
-
locked_saveComments
Save the comments for a torrent Caller must synchronize on comments.- Specified by:
locked_saveComments
in interfaceCompleteListener
- Parameters:
comments
- non-null- Since:
- 0.9.31
-
loadConfig
- Parameters:
filename
- null to set initial defaults
-
getUniversalTheming
public boolean getUniversalTheming()- Since:
- 0.9.31
-
getTheme
Get current theme.- Returns:
- String -- the current theme
-
getThemes
Get all themes- Returns:
- String[] -- Array of all the themes found, non-null, unsorted
-
updateConfig
public void updateConfig(String dataDir, boolean filesPublic, boolean autoStart, boolean smartSort, String refreshDelay, String startDelay, String pageSize, String seedPct, String eepHost, String eepPort, String i2cpHost, String i2cpPort, String i2cpOpts, String upLimit, String upBW, boolean useOpenTrackers, boolean useDHT, String theme, String lang, boolean enableRatings, boolean enableComments, String commentName, boolean collapsePanels, boolean showStatusFilter, boolean enableLightbox, boolean enableAddCreate)all params may be null or need trimming -
getPrivateTrackers
- Returns:
- non-null, fixed size, may be empty or unmodifiable
- Since:
- 0.9.1
-
saveOpenTrackers
- Parameters:
ot
- null to restore default- Since:
- 0.9.1
-
savePrivateTrackers
- Parameters:
pt
- null ok, default is none- Since:
- 0.9.1
-
saveConfig
public void saveConfig() -
listTorrentFiles
Set of canonical .torrent filenames that we are dealing with. An unsynchronized copy. -
getTorrent
Grab the torrent given the (canonical) filename of the .torrent file- Returns:
- Snark or null
-
getTorrents
Unmodifiable- Since:
- 0.9.4
-
getTorrentByBaseName
Grab the torrent given the base name of the storage- Parameters:
filename
- must be the filtered name, which may be different than the metainfo's name- Returns:
- Snark or null
- Since:
- 0.7.14
-
getTorrentByInfoHash
Grab the torrent given the info hash- Returns:
- Snark or null
- Since:
- 0.8.4
-
addMagnet
Add a torrent with the info hash alone (magnet / maggot)- Parameters:
name
- hex or b32 name from the magnet linkih
- 20 byte info hashtrackerURL
- may be nullupdateStatus
- should we add this magnet to the config file, to save it across restarts, in case we don't get the metadata before shutdown?- Throws:
RuntimeException
- via Snark.fatal()- Since:
- 0.8.4
-
addMagnet
public void addMagnet(String name, byte[] ih, String trackerURL, boolean updateStatus, File dataDir)Add a torrent with the info hash alone (magnet / maggot)- Parameters:
name
- hex or b32 name from the magnet linkih
- 20 byte info hashtrackerURL
- may be nullupdateStatus
- should we add this magnet to the config file, to save it across restarts, in case we don't get the metadata before shutdown?dataDir
- must exist, or null to default to snark data directory- Throws:
RuntimeException
- via Snark.fatal()- Since:
- 0.9.17
-
addMagnet
public Snark addMagnet(String name, byte[] ih, String trackerURL, boolean updateStatus, boolean autoStart, File dataDir, CompleteListener listener)Add a torrent with the info hash alone (magnet / maggot) External use is for UpdateRunner.- Parameters:
name
- hex or b32 name from the magnet linkih
- 20 byte info hashtrackerURL
- may be nullupdateStatus
- should we add this magnet to the config file, to save it across restarts, in case we don't get the metadata before shutdown?dataDir
- must exist, or null to default to snark data directorylistener
- to intercept callbacks, should pass through to this- Returns:
- the new Snark or null on failure
- Throws:
RuntimeException
- via Snark.fatal()- Since:
- 0.9.4
-
deleteMagnet
Stop and delete a torrent running in magnet mode- Parameters:
snark
- a torrent with a fake file name ("Magnet xxxx")- Since:
- 0.8.4
-
addDownloader
Add and start a FetchAndAdd task. Remove it with deleteMagnet().- Parameters:
torrent
- must be instanceof FetchAndAdd- Throws:
RuntimeException
- via Snark.fatal()?- Since:
- 0.9.1
-
addTorrent
public boolean addTorrent(MetaInfo metainfo, BitField bitfield, String filename, File baseFile, boolean dontAutoStart) throws IOExceptionAdd a torrent from a MetaInfo. Save the MetaInfo data to filename. Holds the snarks lock to prevent interference from the DirMonitor. This verifies that a torrent with this infohash is not already added. This may take a LONG time to create or check the storage. Called from servlet. This is only for the 'create torrent' form.- Parameters:
metainfo
- the metainfo for the torrentbitfield
- the current completion status of the torrent, or nullfilename
- the absolute path to save the metainfo to, generally ending in ".torrent", which is also the name of the torrent Must be a filesystem-safe name. If null, will generate a name from the metainfo.baseFile
- may be null, if so look in rootDataDir- Returns:
- success
- Throws:
RuntimeException
- via Snark.fatal()IOException
- Since:
- 0.8.4
-
copyAndAddTorrent
Add a torrent from a file not in the torrent directory. Copy the file to filename. Holds the snarks lock to prevent interference from the DirMonitor. Caller must verify this torrent is not already added. This may take a LONG time to create or check the storage.- Parameters:
fromfile
- where the file is now, presumably in a temp directory somewherefilename
- the absolute path to save the metainfo to, generally ending in ".torrent", which is also the name of the torrent Must be a filesystem-safe name.dataDir
- must exist, or null to default to snark data directory- Returns:
- success
- Throws:
RuntimeException
- via Snark.fatal()IOException
- Since:
- 0.8.4
-
getSavedTorrentTime
Get the timestamp for a torrent from the config file. A Snark.CompleteListener method.- Specified by:
getSavedTorrentTime
in interfaceCompleteListener
-
getSavedTorrentBitField
Get the saved bitfield for a torrent from the config file. Convert "." to a full bitfield. A Snark.CompleteListener method.- Specified by:
getSavedTorrentBitField
in interfaceCompleteListener
-
loadSavedFilePriorities
Get the saved priorities for a torrent from the config file.- Since:
- 0.8.1
-
getSavedPreserveNamesSetting
Get setting for a torrent from the config file.- Specified by:
getSavedPreserveNamesSetting
in interfaceCompleteListener
- Returns:
- setting, false if not found
- Since:
- 0.9.15
-
getSavedUploaded
Get setting for a torrent from the config file.- Specified by:
getSavedUploaded
in interfaceCompleteListener
- Returns:
- setting, 0 if not found
- Since:
- 0.9.15
-
getSavedAddedAndCompleted
Get setting for a torrent from the config file.- Returns:
- non-null, rv[0] is added time or 0; rv[1] is completed time or 0
- Since:
- 0.9.23
-
getSavedCommentsEnabled
Get setting for comments enabled from the config file. Caller must first check global I2PSnarkUtil.commentsEnabled() Default true.- Since:
- 0.9.31
-
setSavedCommentsEnabled
Set setting for comments enabled in the config file.- Since:
- 0.9.31
-
saveTorrentStatus
Save the completion status of a torrent and other data in the config file for that torrent. Does nothing for magnets.- Since:
- 0.9.15
-
saveMagnetStatus
Just remember we have it. This used to simply store a line in the config file, but now we also save it in its own config file, just like other torrents, so we can remember the directory, tracker, etc.- Parameters:
dir
- may be nulltrackerURL
- may be nulldn
- may be null- Since:
- 0.8.4
-
removeMagnetStatus
public void removeMagnetStatus(byte[] ih)Remove the magnet marker from the config file.- Since:
- 0.8.4
-
stopTorrent
Stop the torrent, leaving it on the list of torrents unless told to remove it. If shouldRemove is true, removes the config file also. -
stopTorrent
Stop the torrent, leaving it on the list of torrents unless told to remove it. If shouldRemove is true, removes the config file also.- Since:
- 0.8.4
-
removeTorrent
Stop the torrent and delete the torrent file itself, but leaving the data behind. Removes saved config file also. Holds the snarks lock to prevent interference from the DirMonitor. -
torrentComplete
A Snark.CompleteListener method.- Specified by:
torrentComplete
in interfaceCompleteListener
-
updateStatus
A Snark.CompleteListener method.- Specified by:
updateStatus
in interfaceCompleteListener
-
gotMetaInfo
We transitioned from magnet mode, we have now initialized our metainfo and storage. The listener should now call getMetaInfo() and save the data to disk. A Snark.CompleteListener method.- Specified by:
gotMetaInfo
in interfaceCompleteListener
- Returns:
- the new name for the torrent or null on error
- Since:
- 0.8.4
-
fatal
A Snark.CompleteListener method.- Specified by:
fatal
in interfaceCompleteListener
- Since:
- 0.9
-
addMessage
A Snark.CompleteListener method.- Specified by:
addMessage
in interfaceCompleteListener
- Since:
- 0.9.2
-
gotPiece
A Snark.CompleteListener method.- Specified by:
gotPiece
in interfaceCompleteListener
- Since:
- 0.9.4
-
getTrackerMap
Unsorted map of name to Tracker object Modifiable, not a copy- Since:
- 0.9.1
-
getTrackers
Unsorted, do not modify -
getSortedTrackers
Sorted copy- Since:
- 0.9.1
-
hasModifiedTrackers
public boolean hasModifiedTrackers()Has the default tracker list been modified?- Since:
- 0.9.35
-
setDefaultTrackerMap
public void setDefaultTrackerMap()- Since:
- 0.9
-
saveTrackerMap
public void saveTrackerMap()- Since:
- 0.9
-
startTorrent
public void startTorrent(byte[] infoHash)If not connected, thread it, otherwise inline- Throws:
RuntimeException
- via Snark.fatal()- Since:
- 0.9.1
-
startTorrent
If not connected, thread it, otherwise inline- Throws:
RuntimeException
- via Snark.fatal()- Since:
- 0.9.23
-
startAllTorrents
public void startAllTorrents()If not connected, thread it, otherwise inline- Since:
- 0.9.1
-
stopAllTorrents
public void stopAllTorrents(boolean finalShutdown)Stop all running torrents, and close the tunnel after a delay to allow for announces. If called at router shutdown via Jetty shutdown hook -> webapp destroy() -> stop(), the tunnel won't actually be closed as the SimpleTimer2 is already shutdown or will be soon, so we delay a few seconds inline.- Parameters:
finalShutdown
- if true, sleep at the end if any torrents were running- Since:
- 0.9.1
-
recheckTorrent
Threaded. Torrent must be stopped.- Since:
- 0.9.23
-