/** @since 0.8.5 */ public static void main(String[] args) { if (args.length <= 0) { System.err.println("Usage: MetaInfo files..."); return; } for (int i = 0; i < args.length; i++) { InputStream in = null; try { in = new FileInputStream(args[i]); MetaInfo meta = new MetaInfo(in); System.out.println(args[i] + " InfoHash: " + I2PSnarkUtil.toHex(meta.getInfoHash())); } catch (IOException ioe) { System.err.println("Error in file " + args[i] + ": " + ioe); } finally { try { if (in != null) in.close(); } catch (IOException ioe) { } } } }
/** * @return always will be valid even in magnet mode * @since 0.8.4 */ public byte[] getInfoHash() { // should always be the same if (meta != null) return meta.getInfoHash(); return infoHash; }
public void run() { // XXX - Support other IPs String announce = meta.getAnnounce(); String infoHash = urlencode(meta.getInfoHash()); String peerID = urlencode(coordinator.getID()); long uploaded = coordinator.getUploaded(); long downloaded = coordinator.getDownloaded(); long left = coordinator.getLeft(); boolean completed = (left == 0); try { boolean started = false; while (!started) { try { // Send start. TrackerInfo info = doRequest(announce, infoHash, peerID, uploaded, downloaded, left, STARTED_EVENT); Iterator it = info.getPeers().iterator(); while (it.hasNext()) coordinator.addPeer((Peer) it.next()); started = true; } catch (IOException ioe) { // Probably not fatal (if it doesn't last to long...) Snark.debug( "WARNING: Could not contact tracker at '" + announce + "': " + ioe, Snark.WARNING); } if (!started && !stop) { Snark.debug(" Retrying in one minute...", Snark.DEBUG); try { // Sleep one minutes... Thread.sleep(60 * 1000); } catch (InterruptedException interrupt) { // ignore } } } while (!stop) { try { // Sleep some minutes... Thread.sleep(SLEEP * 60 * 1000); } catch (InterruptedException interrupt) { // ignore } if (stop) break; uploaded = coordinator.getUploaded(); downloaded = coordinator.getDownloaded(); left = coordinator.getLeft(); // First time we got a complete download? String event; if (!completed && left == 0) { completed = true; event = COMPLETED_EVENT; } else event = NO_EVENT; // Only do a request when necessary. if (event == COMPLETED_EVENT || coordinator.needPeers() || System.currentTimeMillis() > lastRequestTime + interval) { try { TrackerInfo info = doRequest(announce, infoHash, peerID, uploaded, downloaded, left, event); Iterator it = info.getPeers().iterator(); while (it.hasNext()) coordinator.addPeer((Peer) it.next()); } catch (IOException ioe) { // Probably not fatal (if it doesn't last to long...) Snark.debug( "WARNING: Could not contact tracker at '" + announce + "': " + ioe, Snark.WARNING); } } } } catch (Throwable t) { Snark.debug("TrackerClient: " + t, Snark.ERROR); t.printStackTrace(); } finally { try { TrackerInfo info = doRequest(announce, infoHash, peerID, uploaded, downloaded, left, STOPPED_EVENT); } catch (IOException ioe) { /* ignored */ } } }
/** * multitorrent * * @param baseFile if null, use rootDir/torrentName; if non-null, use it instead * @throws RuntimeException via fatal() * @since 0.9.11 */ public Snark( I2PSnarkUtil util, String torrent, String ip, int user_port, StorageListener slistener, CoordinatorListener clistener, CompleteListener complistener, PeerCoordinatorSet peerCoordinatorSet, ConnectionAcceptor connectionAcceptor, boolean start, String rootDir, File baseFile) { if (slistener == null) slistener = this; completeListener = complistener; _util = util; _log = util.getContext().logManager().getLog(Snark.class); _peerCoordinatorSet = peerCoordinatorSet; acceptor = connectionAcceptor; this.torrent = torrent; this.rootDataDir = new File(rootDir); stopped = true; activity = "Network setup"; id = generateID(); if (_log.shouldLog(Log.INFO)) _log.info("My peer id: " + PeerID.idencode(id)); /* * Don't start a tunnel if the torrent isn't going to be started. * If we are starting, * startTorrent() will force a connect. * boolean ok = util.connect(); if (!ok) fatal("Unable to connect to I2P"); I2PServerSocket serversocket = util.getServerSocket(); if (serversocket == null) fatal("Unable to listen for I2P connections"); else { Destination d = serversocket.getManager().getSession().getMyDestination(); debug("Listening on I2P destination " + d.toBase64() + " / " + d.calculateHash().toBase64(), NOTICE); } */ // Figure out what the torrent argument represents. File f = null; InputStream in = null; byte[] x_infoHash = null; try { f = new File(torrent); if (f.exists()) in = new FileInputStream(f); else { /** * ** No, we don't ever fetch a torrent file this way and we don't want to block in the * constructor activity = "Getting torrent"; File torrentFile = _util.get(torrent, 3); if * (torrentFile == null) { fatal("Unable to fetch " + torrent); if (false) return; // never * reached - fatal(..) throws } else { torrentFile.deleteOnExit(); in = new * FileInputStream(torrentFile); } *** */ throw new IOException("not found"); } meta = new MetaInfo(in); x_infoHash = meta.getInfoHash(); } catch (IOException ioe) { // OK, so it wasn't a torrent metainfo file. if (f != null && f.exists()) if (ip == null) fatal( "'" + torrent + "' exists," + " but is not a valid torrent metainfo file." + System.getProperty("line.separator"), ioe); else fatal("I2PSnark does not support creating and tracking a torrent at the moment"); /* { // Try to create a new metainfo file debug ("Trying to create metainfo torrent for '" + torrent + "'", NOTICE); try { activity = "Creating torrent"; storage = new Storage (f, "http://" + ip + ":" + port + "/announce", slistener); storage.create(); meta = storage.getMetaInfo(); } catch (IOException ioe2) { fatal("Could not create torrent for '" + torrent + "'", ioe2); } } */ else fatal("Cannot open '" + torrent + "'", ioe); } catch (OutOfMemoryError oom) { fatal("ERROR - Out of memory, cannot create torrent " + torrent + ": " + oom.getMessage()); } finally { if (in != null) try { in.close(); } catch (IOException ioe) { } } infoHash = x_infoHash; // final if (_log.shouldLog(Log.INFO)) _log.info(meta.toString()); // When the metainfo torrent was created from an existing file/dir // it already exists. if (storage == null) { try { activity = "Checking storage"; boolean shouldPreserve = completeListener != null && completeListener.getSavedPreserveNamesSetting(this); if (baseFile == null) { String base = meta.getName(); if (!shouldPreserve) base = Storage.filterName(base); if (_util.getFilesPublic()) baseFile = new File(rootDataDir, base); else baseFile = new SecureFile(rootDataDir, base); } storage = new Storage(_util, baseFile, meta, slistener, shouldPreserve); if (completeListener != null) { storage.check( completeListener.getSavedTorrentTime(this), completeListener.getSavedTorrentBitField(this)); } else { storage.check(); } // have to figure out when to reopen // if (!start) // storage.close(); } catch (IOException ioe) { try { storage.close(); } catch (IOException ioee) { ioee.printStackTrace(); } fatal("Could not check or create storage", ioe); } } /* * see comment above * activity = "Collecting pieces"; coordinator = new PeerCoordinator(id, meta, storage, clistener, this); PeerCoordinatorSet set = PeerCoordinatorSet.instance(); set.add(coordinator); ConnectionAcceptor acceptor = ConnectionAcceptor.instance(); acceptor.startAccepting(set, serversocket); trackerclient = new TrackerClient(meta, coordinator); */ savedUploaded = (completeListener != null) ? completeListener.getSavedUploaded(this) : 0; if (start) startTorrent(); }