org.klomp.snark

## Class SnarkManager

- java.lang.Object
- - org.klomp.snark.SnarkManager

- All Implemented Interfaces:
    Snark.CompleteListener

      public class SnarkManager
      extends Object
      implements Snark.CompleteListener

  Manage multiple snarks

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `class` | `SnarkManager.SnarkManagerShutdown`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `DEFAULT_AUTO_START`
    `static String` | `DEFAULT_LINK_PREFIX`
    `static int` | `DEFAULT_MAX_UP_BW`
    `static int` | `DEFAULT_STARTUP_DELAY`
    `static String` | `DEFAULT_THEME`
    `static int` | `MIN_UP_BW`
    `static String` | `PROP_AUTO_START`
    `static String` | `PROP_DIR`
    `static String` | `PROP_I2CP_HOST`
    `static String` | `PROP_I2CP_OPTS`
    `static String` | `PROP_I2CP_PORT`
    `static String` | `PROP_LINK_PREFIX`
    `static String` | `PROP_META_BITFIELD_SUFFIX`
    `static String` | `PROP_META_MAGNET_PREFIX`
    `static String` | `PROP_META_PREFIX`
    `static String` | `PROP_META_PRIORITY_SUFFIX`
    `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`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addMagnet(String name, byte[] ih, String trackerURL, boolean updateStatus)` Add a torrent with the info hash alone (magnet / maggot)
    `void` | `addMessage(String message)`
    `void` | `addTorrent(MetaInfo metainfo, BitField bitfield, String filename, boolean dontAutoStart)` Add a torrent from a MetaInfo.
    `void` | `copyAndAddTorrent(File fromfile, String filename)` Add a torrent from a file not in the torrent directory.
    `void` | `deleteMagnet(Snark snark)` Stop and delete a torrent running in magnet mode
    `Properties` | `getConfig()`
    `File` | `getDataDir()`
    `List` | `getMessages()` newest last
    `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.
    `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
    `Map<String,String>` | `getTrackers()` sorted map of name to announceURL=baseURL
    `String` | `gotMetaInfo(Snark snark)` We transitioned from magnet mode, we have now initialized our metainfo and storage.
    `static SnarkManager` | `instance()`
    `String` | `linkPrefix()`
    `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` | `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` | `removeTorrentStatus(MetaInfo metainfo)` Remove the status of a torrent from the config file.
    `void` | `saveConfig()`
    `void` | `saveMagnetStatus(byte[] ih)` Just remember we have it
    `void` | `saveTorrentStatus(MetaInfo metainfo, BitField bitfield, int[] priorities)` Save the completion status of a torrent and the current time in the config file in the form "i2psnark.zmeta.$base64infohash=$time,$base64bitfield".
    `boolean` | `shouldAutoStart()`
    `void` | `start()` Caller _must_ call loadConfig(file) before this if setting new values for i2cp host/port or i2psnark.dir
    `void` | `stop()`
    `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 autoStart, String startDelay, String seedPct, String eepHost, String eepPort, String i2cpHost, String i2cpPort, String i2cpOpts, String upLimit, String upBW, boolean useOpenTrackers, String openTrackers, String theme)`
    `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_META_PREFIX

          public static final String PROP_META_PREFIX

      See Also:
        Constant Field Values

    - #### PROP_META_BITFIELD_SUFFIX

          public static final String PROP_META_BITFIELD_SUFFIX

      See Also:
        Constant Field Values

    - #### PROP_META_PRIORITY_SUFFIX

          public static final String PROP_META_PRIORITY_SUFFIX

      See Also:
        Constant Field Values

    - #### PROP_META_MAGNET_PREFIX

          public static final String PROP_META_MAGNET_PREFIX

      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_LINK_PREFIX

          public static final String PROP_LINK_PREFIX

      See Also:
        Constant Field Values

    - #### DEFAULT_LINK_PREFIX

          public static final String DEFAULT_LINK_PREFIX

      See Also:
        Constant Field Values

    - #### PROP_STARTUP_DELAY

          public static final String PROP_STARTUP_DELAY

      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

    - #### 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

    - #### 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

  - ### Method Detail

    - #### instance

          public static SnarkManager instance()

    - #### 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()

    - #### util

          public I2PSnarkUtil util()

      hook to I2PSnarkUtil for the servlet

    - #### addMessage

          public void addMessage(String message)

    - #### getMessages

          public List getMessages()

      newest last

    - #### shouldAutoStart

          public boolean shouldAutoStart()

    - #### linkPrefix

          public String linkPrefix()

    - #### 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.

    - #### updateConfig

          public void updateConfig(String dataDir,
                          boolean autoStart,
                          String startDelay,
                          String seedPct,
                          String eepHost,
                          String eepPort,
                          String i2cpHost,
                          String i2cpPort,
                          String i2cpOpts,
                          String upLimit,
                          String upBW,
                          boolean useOpenTrackers,
                          String openTrackers,
                          String theme)

    - #### saveConfig

          public void saveConfig()

    - #### getConfig

          public Properties getConfig()

    - #### 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

    - #### 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

    - #### 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

    - #### addTorrent

          public void addTorrent(MetaInfo metainfo,
                        BitField bitfield,
                        String filename,
                        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.
      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.
      Throws:
        `RuntimeException` - via Snark.fatal()
        `IOException`
      Since:
        0.8.4

    - #### copyAndAddTorrent

          public void copyAndAddTorrent(File fromfile,
                               String filename)
                                 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.
      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 `Snark.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 `Snark.CompleteListener`

    - #### loadSavedFilePriorities

          public void loadSavedFilePriorities(Snark snark)

      Get the saved priorities for a torrent from the config file.
      Since:
        0.8.1

    - #### saveTorrentStatus

          public void saveTorrentStatus(MetaInfo metainfo,
                               BitField bitfield,
                               int[] priorities)

      Save the completion status of a torrent and the current time in the config file in the form "i2psnark.zmeta.$base64infohash=$time,$base64bitfield". The config file property key is appended with the Base64 of the infohash, with the '=' changed to '$' since a key can't contain '='. The time is a standard long converted to string. The status is either a bitfield converted to Base64 or "." for a completed torrent to save space in the config file and in memory.
      Parameters:
        `bitfield` - non-null
        `priorities` - may be null

    - #### removeTorrentStatus

          public void removeTorrentStatus(MetaInfo metainfo)

      Remove the status of a torrent from the config file. This may help the config file from growing too big.

    - #### saveMagnetStatus

          public void saveMagnetStatus(byte[] ih)

      Just remember we have it
      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

    - #### stopTorrent

          public void stopTorrent(Snark torrent,
                         boolean shouldRemove)

      Stop the torrent, leaving it on the list of torrents unless told to remove it
      Since:
        0.8.4

    - #### removeTorrent

          public void removeTorrent(String filename)

      Stop the torrent and delete the torrent file itself, but leaving the data behind. 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 `Snark.CompleteListener`

    - #### updateStatus

          public void updateStatus(Snark snark)

      A Snark.CompleteListener method.
      Specified by:
        `updateStatus` in interface `Snark.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 `Snark.CompleteListener`
      Returns:
        the new name for the torrent or null on error
      Since:
        0.8.4

    - #### getTrackers

          public Map<String,String> getTrackers()

      sorted map of name to announceURL=baseURL