Class SnarkManager

java.lang.Object
org.klomp.snark.SnarkManager
All Implemented Interfaces:
ClientApp, I2PSocketManager.DisconnectListener, CompleteListener

public class SnarkManager extends Object implements CompleteListener, ClientApp, I2PSocketManager.DisconnectListener
Manage multiple snarks
  • Field Details

  • Constructor Details

    • 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 Details

    • start

      public void start()
      Caller _must_ call loadConfig(file) before this if setting new values for i2cp host/port or i2psnark.dir
    • sessionDisconnected

      public void sessionDisconnected()
      DisconnectListener interface
      Specified by:
      sessionDisconnected in interface I2PSocketManager.DisconnectListener
      Since:
      0.9.53
    • 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.
      Specified by:
      startup in interface ClientApp
      Since:
      0.9.30
    • shutdown

      public void shutdown(String[] args)
      ClientApp method. Does nothing. Doesn't matter, we are only registering.
      Specified by:
      shutdown in interface ClientApp
      Parameters:
      args - generally null but could be stopArgs from clients.config
      Since:
      0.9.30
    • getState

      public ClientAppState getState()
      ClientApp method. Doesn't matter, we are only registering.
      Specified by:
      getState in interface ClientApp
      Returns:
      INITIALIZED always.
      Since:
      0.9.30
    • getName

      public String getName()
      ClientApp method.
      Specified by:
      getName in interface ClientApp
      Returns:
      non-null
      Since:
      0.9.30
    • getDisplayName

      public String getDisplayName()
      ClientApp method.
      Specified by:
      getDisplayName in interface ClientApp
      Returns:
      non-null
      Since:
      0.9.30
    • 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<UIMessages.Message> 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 interface CompleteListener
    • isSmartSortEnabled

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

      public boolean isCollapsePanelsEnabled()
      Returns:
      default true
      Since:
      0.9.32
    • 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()
    • getConfigDir

      public File getConfigDir()
      For RPC
      Since:
      0.9.30
    • getSavedComments

      public CommentSet getSavedComments(Snark snark)
      The conmments for a torrent
      Specified by:
      getSavedComments in interface CompleteListener
      Returns:
      null if none
      Since:
      0.9.31
    • locked_saveComments

      public void locked_saveComments(Snark snark, CommentSet comments)
      Save the conmments for a torrent Caller must synchronize on comments.
      Specified by:
      locked_saveComments in interface CompleteListener
      Parameters:
      comments - non-null
      Since:
      0.9.31
    • loadConfig

      public void loadConfig(String filename)
      Parameters:
      filename - null to set initial defaults
    • getUniversalTheming

      public boolean getUniversalTheming()
      Since:
      0.9.31
    • getTheme

      public String getTheme()
      Get current theme.
      Returns:
      String -- the current theme, untranslated
    • getThemeIconSet

      public String getThemeIconSet()
      Get the path to the preferred embedded icons for toImg, "solid/" for dark and light, "" for everything else. If you add a theme with a new icon set, then you need to add a corresponding condition here.
      Returns:
      String "solid/" or ""
      Since:
      0.9.48
    • getThemes

      public static String[] getThemes()
      Get all themes
      Returns:
      Array of all the themes found, non-null, unsorted, untranslated. Not a copy, do not modify.
    • 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)
      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
      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

      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, or null
      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. 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

      public boolean 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
      Returns:
      success
      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
    • getSavedCommentsEnabled

      public boolean getSavedCommentsEnabled(Snark snark)
      Get setting for comments enabled from the config file. Caller must first check global I2PSnarkUtil.commentsEnabled() Default true.
      Since:
      0.9.31
    • setSavedCommentsEnabled

      public void setSavedCommentsEnabled(Snark snark, boolean yes)
      Set setting for comments enabled in the config file.
      Since:
      0.9.31
    • 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
    • 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

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

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