## org.klomp.snark
Class Snark

    java.lang.Object
      org.klomp.snark.Snark

All Implemented Interfaces:
  CoordinatorListener, ShutdownListener, StorageListener

    public class Snark

extends Object
implements StorageListener, CoordinatorListener, ShutdownListener

Main Snark program startup class.

Author:
  Mark Wielaard (mark@klomp.org)

Nested Class Summary
`static interface` `Snark.CompleteListener`

Field Summary
`(package private) String` `activity`
`static int` `ALL`
`static int` `DEBUG`
`static int` `ERROR`
`static int` `INFO`
`(package private) static int` `MAX_TOTAL_UPLOADERS`
`(package private) static int` `MIN_TOTAL_UPLOADERS` Maintain a configurable total uploader cap coordinatorListener
`static int` `NOTICE`

`static String`

`PROP_MAX_CONNECTIONS`
No, not maintaining a command-line client public static void main(String[] args) { System.out.println(copyright); System.out.println(); if ( (args.length > 0) && ("--config".equals(args[0])) ) { I2PThread.addOOMEventListener(new OOMListener()); SnarkManager sm = SnarkManager.instance(); if (args.length > 1) sm.loadConfig(args[1]); System.out.println("Running in multitorrent mode"); while (true) { try { synchronized (sm) { sm.wait(); } } catch (InterruptedException ie) {} } } // Parse debug, share/ip and torrent file options.

`static int` `WARNING`

Constructor Summary
`Snark(I2PAppContext ctx, Properties opts, String torrent, StorageListener slistener, boolean start, String rootDir)` single torrent - via router
`Snark(I2PSnarkUtil util, String torrent, byte[] ih, String trackerURL, Snark.CompleteListener complistener, PeerCoordinatorSet peerCoordinatorSet, ConnectionAcceptor connectionAcceptor, boolean start, String rootDir)` multitorrent, magnet
`Snark(I2PSnarkUtil util, String torrent, String ip, int user_port, StorageListener slistener, CoordinatorListener clistener)` from main() via parseArguments() single torrent
`Snark(I2PSnarkUtil util, String torrent, String ip, int user_port, StorageListener slistener, CoordinatorListener clistener, Snark.CompleteListener complistener, PeerCoordinatorSet peerCoordinatorSet, ConnectionAcceptor connectionAcceptor, boolean start, String rootDir)` multitorrent

Method Summary
`String` `getBaseName()`
`long` `getDownloaded()`
`long` `getDownloadRate()`
`byte[]` `getID()`
`byte[]` `getInfoHash()`
`MetaInfo` `getMetaInfo()`
`String` `getName()`
`long` `getNeeded()`
`int` `getPeerCount()`
`List<Peer>` `getPeerList()`
`int` `getPieceLength(int p)`
`int` `getPieces()`
`Storage` `getStorage()`
`long` `getTotalLength()`
`String` `getTrackerProblems()`
`int` `getTrackerSeenPeers()`
`String` `getTrackerURL()`
`long` `getUploaded()`
`long` `getUploadRate()`
`void` `gotMetaInfo(PeerCoordinator coordinator, MetaInfo metainfo)` Called when the PeerCoordinator got the MetaInfo via magnet.
`boolean` `isStopped()`
`boolean` `overUpBWLimit()`
`boolean` `overUpBWLimit(long total)`
`boolean` `overUploadLimit(int uploaders)`
`void` `peerChange(PeerCoordinator coordinator, Peer peer)` CoordinatorListener - this does nothing
`boolean` `restartAcceptor()`
`void` `setTrackerProblems(String p)`
`void` `setTrackerSeenPeers(int p)`
`void` `setWantedPieces(Storage storage)` Reset the peer's wanted pieces table Call after the storage double-check fails
`void` `shutdown()` SnarkSnutdown callback unused
`void` `startTorrent()` Start up contacting peers and querying the tracker
`void` `stopTorrent()` Stop contacting the tracker and talking with peers
`void` `storageAllChecked(Storage storage)` Called when all pieces in the storage have been checked.
`void` `storageAllocated(Storage storage, long length)` Called to indicate that length bytes have been allocated.
`void` `storageChecked(Storage storage, int num, boolean checked)` Called when storage is being checked and the num piece of that total pieces has been checked.
`void` `storageCompleted(Storage storage)` Called the one time when the data is completely received and checked.
`void` `storageCreateFile(Storage storage, String name, long length)` Called when the storage creates a new file of a given length.
`void` `updatePiecePriorities()`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### ERROR

    public static final int ERROR

See Also:
  Constant Field Values

### WARNING

    public static final int WARNING

See Also:
  Constant Field Values

### NOTICE

    public static final int NOTICE

See Also:
  Constant Field Values

### INFO

    public static final int INFO

See Also:
  Constant Field Values

### DEBUG

    public static final int DEBUG

See Also:
  Constant Field Values

### ALL

    public static final int ALL

See Also:
  Constant Field Values

### activity

    String activity

### PROP_MAX_CONNECTIONS

    public static final String PROP_MAX_CONNECTIONS

  No, not maintaining a command-line client public static void main(String[] args) { System.out.println(copyright); System.out.println(); if ( (args.length > 0) && ("--config".equals(args[0])) ) { I2PThread.addOOMEventListener(new OOMListener()); SnarkManager sm = SnarkManager.instance(); if (args.length > 1) sm.loadConfig(args[1]); System.out.println("Running in multitorrent mode"); while (true) { try { synchronized (sm) { sm.wait(); } } catch (InterruptedException ie) {} } } // Parse debug, share/ip and torrent file options. Snark snark = parseArguments(args); SnarkShutdown snarkhook = new SnarkShutdown(snark.storage, snark.coordinator, snark.acceptor, snark.trackerclient, snark); //Runtime.getRuntime().addShutdownHook(snarkhook); Timer timer = new Timer(true); TimerTask monitor = new PeerMonitorTask(snark.coordinator); timer.schedule(monitor, PeerMonitorTask.MONITOR_PERIOD, PeerMonitorTask.MONITOR_PERIOD); // Start command interpreter if (Snark.command_interpreter) { boolean quit = false; System.out.println(); System.out.println(usage); System.out.println(); try { BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); String line = br.readLine(); while(!quit && line != null) { line = line.toLowerCase(); if ("quit".equals(line)) quit = true; else if ("list".equals(line)) { synchronized(snark.coordinator.peers) { System.out.println(snark.coordinator.peers.size() + " peers -" + " (i)nterested," + " (I)nteresting," + " (c)hoking," + " (C)hoked:"); Iterator it = snark.coordinator.peers.iterator(); while (it.hasNext()) { Peer peer = (Peer)it.next(); System.out.println(peer); System.out.println("\ti: " + peer.isInterested() + " I: " + peer.isInteresting() + " c: " + peer.isChoking() + " C: " + peer.isChoked()); } } } else if ("info".equals(line)) { System.out.println("Name: " + snark.meta.getName()); System.out.println("Torrent: " + snark.torrent); System.out.println("Tracker: " + snark.meta.getAnnounce()); List files = snark.meta.getFiles(); System.out.println("Files: " + ((files == null) ? 1 : files.size())); System.out.println("Pieces: " + snark.meta.getPieces()); System.out.println("Piece size: " + snark.meta.getPieceLength(0) / 1024 + " KB"); System.out.println("Total size: " + snark.meta.getTotalLength() / (1024 * 1024) + " MB"); } else if ("".equals(line) || "help".equals(line)) { System.out.println(usage); System.out.println(help); } else { System.out.println("Unknown command: " + line); System.out.println(usage); } if (!quit) { System.out.println(); line = br.readLine(); } } } catch(IOException ioe) { System.out.println("ERROR while reading stdin: " + ioe); } // Explicit shutdown. //Runtime.getRuntime().removeShutdownHook(snarkhook); snarkhook.start(); } }

  See Also:
    Constant Field Values

### MIN_TOTAL_UPLOADERS

    static final int MIN_TOTAL_UPLOADERS

  Maintain a configurable total uploader cap coordinatorListener

  See Also:
    Constant Field Values

### MAX_TOTAL_UPLOADERS

    static final int MAX_TOTAL_UPLOADERS

See Also:
  Constant Field Values

Constructor Detail

### Snark

    Snark(I2PSnarkUtil util,
          String torrent,
          String ip,
          int user_port,
          StorageListener slistener,
          CoordinatorListener clistener)

  from main() via parseArguments() single torrent

### Snark

    public Snark(I2PAppContext ctx,
                 Properties opts,
                 String torrent,
                 StorageListener slistener,
                 boolean start,
                 String rootDir)

  single torrent - via router

### Snark

    public Snark(I2PSnarkUtil util,
                 String torrent,
                 String ip,
                 int user_port,
                 StorageListener slistener,
                 CoordinatorListener clistener,
                 Snark.CompleteListener complistener,
                 PeerCoordinatorSet peerCoordinatorSet,
                 ConnectionAcceptor connectionAcceptor,
                 boolean start,
                 String rootDir)

  multitorrent

### Snark

    public Snark(I2PSnarkUtil util,
                 String torrent,
                 byte[] ih,
                 String trackerURL,
                 Snark.CompleteListener complistener,
                 PeerCoordinatorSet peerCoordinatorSet,
                 ConnectionAcceptor connectionAcceptor,
                 boolean start,
                 String rootDir)

  multitorrent, magnet

  Parameters:
    `torrent` - a fake name for now (not a file name)
    `ih` - 20-byte info hash
    `trackerURL` - may be null
  Since:
    0.8.4

Method Detail

### startTorrent

    public void startTorrent()

  Start up contacting peers and querying the tracker

### stopTorrent

    public void stopTorrent()

  Stop contacting the tracker and talking with peers

### getName

    public String getName()

  Returns:
    file name of .torrent file (should be full absolute path), or a fake name if in magnet mode.
  Since:
    0.8.4

### getBaseName

    public String getBaseName()

  Returns:
    base name of torrent [filtered version of getMetaInfo.getName()], or a fake name if in magnet mode
  Since:
    0.8.4

### getID

    public byte[] getID()

  Returns:
    always will be valid even in magnet mode
  Since:
    0.8.4

### getInfoHash

    public byte[] getInfoHash()

  Returns:
    always will be valid even in magnet mode
  Since:
    0.8.4

### getMetaInfo

    public MetaInfo getMetaInfo()

  Returns:
    may be null if in magnet mode
  Since:
    0.8.4

### getStorage

    public Storage getStorage()

  Returns:
    may be null if in magnet mode
  Since:
    0.8.4

### isStopped

    public boolean isStopped()

  Since:
    0.8.4

### getDownloadRate

    public long getDownloadRate()

  Since:
    0.8.4

### getUploadRate

    public long getUploadRate()

  Since:
    0.8.4

### getDownloaded

    public long getDownloaded()

  Since:
    0.8.4

### getUploaded

    public long getUploaded()

  Since:
    0.8.4

### getPeerCount

    public int getPeerCount()

  Since:
    0.8.4

### getPeerList

    public List<Peer> getPeerList()

  Since:
    0.8.4

### getTrackerProblems

    public String getTrackerProblems()

  Returns:
    String returned from tracker, or null if no error
  Since:
    0.8.4

### setTrackerProblems

    public void setTrackerProblems(String p)

  Parameters:
    `p` - tracker error string or null
  Since:
    0.8.4

### getTrackerSeenPeers

    public int getTrackerSeenPeers()

  Returns:
    count returned from tracker
  Since:
    0.8.4

### setTrackerSeenPeers

    public void setTrackerSeenPeers(int p)

  Since:
    0.8.4

### updatePiecePriorities

    public void updatePiecePriorities()

  Since:
    0.8.4

### getTotalLength

    public long getTotalLength()

  Returns:
    total of all torrent files, or total of metainfo file if fetching magnet, or -1
  Since:
    0.8.4

### getNeeded

    public long getNeeded()

  Returns:
    number of pieces still needed (magnet mode or not), or -1 if unknown
  Since:
    0.8.4

### getPieceLength

    public int getPieceLength(int p)

  Parameters:
    `p` - the piece number
  Returns:
    metainfo piece length or 16K if fetching magnet
  Since:
    0.8.4

### getPieces

    public int getPieces()

  Returns:
    number of pieces
  Since:
    0.8.4

### restartAcceptor

    public boolean restartAcceptor()

  Returns:
    true if restarted
  Since:
    0.8.4

### getTrackerURL

    public String getTrackerURL()

  Returns:
    trackerURL string from magnet-mode constructor, may be null
  Since:
    0.8.4

### peerChange

    public void peerChange(PeerCoordinator coordinator,
                           Peer peer)

  CoordinatorListener - this does nothing

  Specified by:
    `peerChange` in interface `CoordinatorListener`

### gotMetaInfo

    public void gotMetaInfo(PeerCoordinator coordinator,
                            MetaInfo metainfo)

  Called when the PeerCoordinator got the MetaInfo via magnet. CoordinatorListener. Create the storage, tell SnarkManager, and give the storage back to the coordinator.

  Specified by:
    `gotMetaInfo` in interface `CoordinatorListener`
  Throws:
    `RuntimeException` - via fatal()
  Since:
    0.8.4

### storageCreateFile

    public void storageCreateFile(Storage storage,
                                  String name,
                                  long length)

  Description copied from interface: `StorageListener`
  Called when the storage creates a new file of a given length.

  Specified by:
    `storageCreateFile` in interface `StorageListener`

### storageAllocated

    public void storageAllocated(Storage storage,
                                 long length)

  Description copied from interface: `StorageListener`
  Called to indicate that length bytes have been allocated.

  Specified by:
    `storageAllocated` in interface `StorageListener`

### storageChecked

    public void storageChecked(Storage storage,
                               int num,
                               boolean checked)

  Description copied from interface: `StorageListener`
  Called when storage is being checked and the num piece of that total pieces has been checked. When the piece hash matches the expected piece hash checked will be true, otherwise it will be false.

  Specified by:
    `storageChecked` in interface `StorageListener`

### storageAllChecked

    public void storageAllChecked(Storage storage)

  Description copied from interface: `StorageListener`
  Called when all pieces in the storage have been checked. Does not mean that the storage is complete, just that the state of the storage is known.

  Specified by:
    `storageAllChecked` in interface `StorageListener`

### storageCompleted

    public void storageCompleted(Storage storage)

  Description copied from interface: `StorageListener`
  Called the one time when the data is completely received and checked.

  Specified by:
    `storageCompleted` in interface `StorageListener`

### setWantedPieces

    public void setWantedPieces(Storage storage)

  Description copied from interface: `StorageListener`
  Reset the peer's wanted pieces table Call after the storage double-check fails

  Specified by:
    `setWantedPieces` in interface `StorageListener`

### shutdown

    public void shutdown()

  SnarkSnutdown callback unused

  Specified by:
    `shutdown` in interface `ShutdownListener`

### overUploadLimit

    public boolean overUploadLimit(int uploaders)

  Specified by:
    `overUploadLimit` in interface `CoordinatorListener`

### overUpBWLimit

    public boolean overUpBWLimit()

  Specified by:
    `overUpBWLimit` in interface `CoordinatorListener`

### overUpBWLimit

    public boolean overUpBWLimit(long total)

  Specified by:
    `overUpBWLimit` in interface `CoordinatorListener`