org.klomp.snark
Class SnarkManager

java.lang.Object
  extended by org.klomp.snark.SnarkManager
All Implemented Interfaces:
CompleteListener

public class SnarkManager
extends Object
implements CompleteListener

Manage multiple snarks


Field Summary
static String CONFIG_DIR_SUFFIX
           
static String DEFAULT_AUTO_START
           
static String DEFAULT_BACKUP_TRACKER
          URL.
static int DEFAULT_MAX_UP_BW
           
static int DEFAULT_REFRESH_DELAY_SECS
           
static int DEFAULT_STARTUP_DELAY
           
static String DEFAULT_THEME
           
static Set<String> DEFAULT_TRACKER_ANNOUNCES
           
static Set<String> KNOWN_OPENTRACKERS
          host names for config form
static int MAX_FILES_PER_TORRENT
          hardcoded for sanity.
static int MIN_UP_BW
           
static String PROP_AUTO_START
           
static String PROP_DIR
           
static String PROP_FILES_PUBLIC
           
static String PROP_I2CP_HOST
           
static String PROP_I2CP_OPTS
           
static String PROP_I2CP_PORT
           
static String PROP_OLD_AUTO_START
           
static String PROP_OPENTRACKERS
           
static String PROP_PAGE_SIZE
           
static String PROP_PRIVATETRACKERS
           
static String PROP_REFRESH_DELAY
           
static String PROP_STARTUP_DELAY
           
static String PROP_THEME
           
static String PROP_TRACKERS
          comma delimited list of name=announceURL=baseURL for the trackers to be displayed
static String PROP_UPBW_MAX
           
static String PROP_UPLOADERS_TOTAL
           
static String RC_PROP_THEME
           
static String RC_PROP_UNIVERSAL_THEMING
           
 
Constructor Summary
SnarkManager(I2PAppContext ctx)
          For embedded.
SnarkManager(I2PAppContext ctx, String ctxPath, String ctxName)
          For webapp.
 
Method Summary
 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(Snark snark, String message)
          A Snark.CompleteListener method.
 void addMessage(String message)
          Use if it does not include a link.
 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 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 mode
 void fatal(Snark snark, String error)
          A Snark.CompleteListener method.
 File getDataDir()
           
 List<String> getMessages()
          newest last
 int getPageSize()
          For GUI
 List<String> getPrivateTrackers()
           
 int getRefreshDelaySeconds()
           
 long[] getSavedAddedAndCompleted(Snark snark)
          Get setting for a torrent 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 copy
 String getTheme()
          Get current theme.
 String[] getThemes()
          Get all themes
 Snark getTorrent(String filename)
          Grab the torrent given the (canonical) filename of the .torrent file
 Snark getTorrentByBaseName(String filename)
          Grab the torrent given the base name of the storage
 Snark getTorrentByInfoHash(byte[] infohash)
          Grab the torrent given the info hash
 Collection<Snark> getTorrents()
          Unmodifiable
 Map<String,Tracker> getTrackerMap()
          Unsorted map of name to Tracker object Modifiable, not a copy
 Collection<Tracker> getTrackers()
          Unsorted, do not modify
 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 isSmartSortEnabled()
           
 boolean isStopping()
           
 Set<String> listTorrentFiles()
          Set of canonical .torrent filenames that we are dealing with.
 void loadConfig(String filename)
          null to set initial defaults
 void loadSavedFilePriorities(Snark snark)
          Get the saved priorities for a torrent from the config file.
 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()
           
 boolean shouldAutoStart()
           
 void start()
          Caller _must_ call loadConfig(file) before this if setting new values for i2cp host/port or i2psnark.dir
 void startAllTorrents()
          If not connected, thread it, otherwise inline
 void startTorrent(byte[] infoHash)
          If not connected, thread it, otherwise inline
 void startTorrent(Snark snark)
          If not connected, thread it, otherwise inline
 void stop()
           
 void stopAllTorrents(boolean finalShutdown)
          Stop all running torrents, and close the tunnel after a delay to allow for announces.
 void stopTorrent(Snark torrent, boolean shouldRemove)
          Stop the torrent, leaving it on the list of torrents unless told to remove it.
 Snark stopTorrent(String filename, 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)
          all params may be null or need trimming
 void updateStatus(Snark snark)
          A Snark.CompleteListener method.
 I2PSnarkUtil util()
          hook to I2PSnarkUtil for the servlet
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROP_I2CP_HOST

public static final String PROP_I2CP_HOST
See Also:
Constant Field Values

PROP_I2CP_PORT

public static final String PROP_I2CP_PORT
See Also:
Constant Field Values

PROP_I2CP_OPTS

public static final String PROP_I2CP_OPTS
See Also:
Constant Field Values

PROP_UPLOADERS_TOTAL

public static final String PROP_UPLOADERS_TOTAL
See Also:
Constant Field Values

PROP_UPBW_MAX

public static final String PROP_UPBW_MAX
See Also:
Constant Field Values

PROP_DIR

public static final String PROP_DIR
See Also:
Constant Field Values

PROP_FILES_PUBLIC

public static final String PROP_FILES_PUBLIC
See Also:
Constant Field Values

PROP_OLD_AUTO_START

public static final String PROP_OLD_AUTO_START
See Also:
Constant Field Values

PROP_AUTO_START

public static final String PROP_AUTO_START
See Also:
Constant Field Values

DEFAULT_AUTO_START

public static final String DEFAULT_AUTO_START
See Also:
Constant Field Values

PROP_STARTUP_DELAY

public static final String PROP_STARTUP_DELAY
See Also:
Constant Field Values

PROP_REFRESH_DELAY

public static final String PROP_REFRESH_DELAY
See Also:
Constant Field Values

PROP_PAGE_SIZE

public static final String PROP_PAGE_SIZE
See Also:
Constant Field Values

RC_PROP_THEME

public static final String RC_PROP_THEME
See Also:
Constant Field Values

RC_PROP_UNIVERSAL_THEMING

public static final String RC_PROP_UNIVERSAL_THEMING
See Also:
Constant Field Values

PROP_THEME

public static final String PROP_THEME
See Also:
Constant Field Values

DEFAULT_THEME

public static final String DEFAULT_THEME
See Also:
Constant Field Values

PROP_OPENTRACKERS

public static final String PROP_OPENTRACKERS
See Also:
Constant Field Values

PROP_PRIVATETRACKERS

public static final String 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

CONFIG_DIR_SUFFIX

public static final String CONFIG_DIR_SUFFIX
See Also:
Constant Field Values

DEFAULT_BACKUP_TRACKER

public static final String DEFAULT_BACKUP_TRACKER
URL. This is our equivalent to router.utorrent.com for bootstrap

See Also:
Constant Field Values

DEFAULT_TRACKER_ANNOUNCES

public static final Set<String> DEFAULT_TRACKER_ANNOUNCES

KNOWN_OPENTRACKERS

public static final Set<String> KNOWN_OPENTRACKERS
host names for config form


PROP_TRACKERS

public static final String 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_TORRENT
hardcoded for sanity. perhaps this should be customizable, for people who increase their ulimit, etc.

See Also:
Constant Field Values
Constructor Detail

SnarkManager

public SnarkManager(I2PAppContext ctx)
For embedded.


SnarkManager

public SnarkManager(I2PAppContext ctx,
                    String ctxPath,
                    String ctxName)
For webapp.

Parameters:
ctxPath - generally "/i2psnark"
ctxName - generally "i2psnark"
Since:
0.9.6
Method Detail

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

util

public I2PSnarkUtil util()
hook to I2PSnarkUtil for the servlet


addMessage

public void addMessage(String message)
Use if it does not include a link. Escapes '<' and '>' before queueing


addMessageNoEscape

public void addMessageNoEscape(String message)
Use if it includes a link. Does not escape '<' and '>' before queueing

Since:
0.9.14.1

getMessages

public List<String> getMessages()
newest last


clearMessages

public void clearMessages()
Since:
0.9

areFilesPublic

public boolean areFilesPublic()
Returns:
default false
Since:
0.8.9

shouldAutoStart

public boolean shouldAutoStart()

isSmartSortEnabled

public boolean isSmartSortEnabled()
Returns:
default true
Since:
0.9.23

getRefreshDelaySeconds

public int getRefreshDelaySeconds()
Returns:
-1 for never
Since:
0.8.9

getPageSize

public int getPageSize()
For GUI

Since:
0.9.6

getDataDir

public File getDataDir()

loadConfig

public void loadConfig(String filename)
null to set initial defaults


getTheme

public String getTheme()
Get current theme.

Returns:
String -- the current theme

getThemes

public String[] 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)
all params may be null or need trimming


getPrivateTrackers

public List<String> getPrivateTrackers()
Returns:
non-null, fixed size, may be empty or unmodifiable
Since:
0.9.1

saveOpenTrackers

public void saveOpenTrackers(List<String> ot)
Parameters:
ot - null to restore default
Since:
0.9.1

savePrivateTrackers

public void savePrivateTrackers(List<String> pt)
Parameters:
pt - null ok, default is none
Since:
0.9.1

saveConfig

public void saveConfig()

listTorrentFiles

public Set<String> listTorrentFiles()
Set of canonical .torrent filenames that we are dealing with. An unsynchronized copy.


getTorrent

public Snark getTorrent(String filename)
Grab the torrent given the (canonical) filename of the .torrent file

Returns:
Snark or null

getTorrents

public Collection<Snark> getTorrents()
Unmodifiable

Since:
0.9.4

getTorrentByBaseName

public Snark getTorrentByBaseName(String filename)
Grab the torrent given the base name of the storage

Returns:
Snark or null
Since:
0.7.14

getTorrentByInfoHash

public Snark getTorrentByInfoHash(byte[] infohash)
Grab the torrent given the info hash

Returns:
Snark or null
Since:
0.8.4

addMagnet

public void addMagnet(String name,
                      byte[] ih,
                      String trackerURL,
                      boolean updateStatus)
Add a torrent with the info hash alone (magnet / maggot)

Parameters:
name - hex or b32 name from the magnet link
ih - 20 byte info hash
trackerURL - may be null
updateStatus - 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 link
ih - 20 byte info hash
trackerURL - may be null
updateStatus - 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 link
ih - 20 byte info hash
trackerURL - may be null
updateStatus - 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
listener - 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

public void deleteMagnet(Snark snark)
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

public void addDownloader(Snark torrent)
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 IOException
Add 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 torrent
bitfield - the current completion status of the torrent
filename - 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.
baseFile - may be null, if so look in rootDataDir
Returns:
success
Throws:
RuntimeException - via Snark.fatal()
IOException
Since:
0.8.4

copyAndAddTorrent

public void copyAndAddTorrent(File fromfile,
                              String filename,
                              File dataDir)
                       throws IOException
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 somewhere
filename - 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
Throws:
RuntimeException - via Snark.fatal()
IOException
Since:
0.8.4

getSavedTorrentTime

public long getSavedTorrentTime(Snark snark)
Get the timestamp for a torrent from the config file. A Snark.CompleteListener method.

Specified by:
getSavedTorrentTime in interface CompleteListener

getSavedTorrentBitField

public BitField getSavedTorrentBitField(Snark snark)
Get the saved bitfield for a torrent from the config file. Convert "." to a full bitfield. A Snark.CompleteListener method.

Specified by:
getSavedTorrentBitField in interface CompleteListener

loadSavedFilePriorities

public void loadSavedFilePriorities(Snark snark)
Get the saved priorities for a torrent from the config file.

Since:
0.8.1

getSavedPreserveNamesSetting

public boolean getSavedPreserveNamesSetting(Snark snark)
Get setting for a torrent from the config file.

Specified by:
getSavedPreserveNamesSetting in interface CompleteListener
Returns:
setting, false if not found
Since:
0.9.15

getSavedUploaded

public long getSavedUploaded(Snark snark)
Get setting for a torrent from the config file.

Specified by:
getSavedUploaded in interface CompleteListener
Returns:
setting, 0 if not found
Since:
0.9.15

getSavedAddedAndCompleted

public long[] getSavedAddedAndCompleted(Snark snark)
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

saveTorrentStatus

public void saveTorrentStatus(Snark snark)
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

public void saveMagnetStatus(byte[] ih,
                             String dir,
                             String trackerURL,
                             String dn)
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 null
trackerURL - may be null
dn - 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

public Snark stopTorrent(String filename,
                         boolean shouldRemove)
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

public void stopTorrent(Snark torrent,
                        boolean shouldRemove)
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

public void removeTorrent(String filename)
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

public void torrentComplete(Snark snark)
A Snark.CompleteListener method.

Specified by:
torrentComplete in interface CompleteListener

updateStatus

public void updateStatus(Snark snark)
A Snark.CompleteListener method.

Specified by:
updateStatus in interface CompleteListener

gotMetaInfo

public String gotMetaInfo(Snark snark)
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 interface CompleteListener
Returns:
the new name for the torrent or null on error
Since:
0.8.4

fatal

public void fatal(Snark snark,
                  String error)
A Snark.CompleteListener method.

Specified by:
fatal in interface CompleteListener
Since:
0.9

addMessage

public void addMessage(Snark snark,
                       String message)
A Snark.CompleteListener method.

Specified by:
addMessage in interface CompleteListener
Since:
0.9.2

gotPiece

public void gotPiece(Snark snark)
A Snark.CompleteListener method.

Specified by:
gotPiece in interface CompleteListener
Since:
0.9.4

getTrackerMap

public Map<String,Tracker> getTrackerMap()
Unsorted map of name to Tracker object Modifiable, not a copy

Since:
0.9.1

getTrackers

public Collection<Tracker> getTrackers()
Unsorted, do not modify


getSortedTrackers

public List<Tracker> getSortedTrackers()
Sorted copy

Since:
0.9.1

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

Since:
0.9.1

startTorrent

public void startTorrent(Snark snark)
If not connected, thread it, otherwise inline

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

public void recheckTorrent(Snark snark)
Threaded. Torrent must be stopped.

Since:
0.9.23