private static final void fireCacheCompleted(URI uri) { synchronized (completeListeners) { String hash = DigestUtils.shaHex(uri.toString()); if (completeListeners.containsKey(hash)) { for (CompleteListener listener : completeListeners.get(hash)) { listener.cacheCompleted(uri); } completeListeners.remove(hash); } } }
/** * Called when the PeerCoordinator got the MetaInfo via magnet. CoordinatorListener. Create the * storage, tell SnarkManager, and give the storage back to the coordinator. * * @throws RuntimeException via fatal() * @since 0.8.4 */ public void gotMetaInfo(PeerCoordinator coordinator, MetaInfo metainfo) { try { String base = Storage.filterName(metainfo.getName()); File baseFile; if (_util.getFilesPublic()) baseFile = new File(rootDataDir, base); else baseFile = new SecureFile(rootDataDir, base); // The following two may throw IOE... storage = new Storage(_util, baseFile, metainfo, this, false); storage.check(); // ... so don't set meta until here meta = metainfo; if (completeListener != null) { String newName = completeListener.gotMetaInfo(this); if (newName != null) torrent = newName; // else some horrible problem } coordinator.setStorage(storage); } catch (IOException ioe) { if (storage != null) { try { storage.close(); } catch (IOException ioee) { } } fatal("Could not check or create storage", ioe); } }
public void storageAllChecked(Storage storage) { // if (checking) // System.out.println(); allChecked = true; checking = false; if (storage.isChanged() && completeListener != null) completeListener.updateStatus(this); }
/** Aborts program abnormally. */ private void fatal(String s, Throwable t) { _log.error(s, t); // System.err.println("snark: " + s + ((t == null) ? "" : (": " + t))); // if (debug >= INFO && t != null) // t.printStackTrace(); stopTorrent(); if (t != null) s += ": " + t; if (completeListener != null) completeListener.fatal(this, s); throw new RuntimeException(s, t); }
@Override protected void onPostExecute(String resul) { super.onPostExecute(resul); try { // Toast.makeText(MainActivity.this, "result"+ resul, Toast.LENGTH_SHORT).show(); listener.onReccomendationComplete(resul.split(" ")); } catch (Exception e) { e.printStackTrace(); } }
public void storageChecked(Storage storage, int num, boolean checked) { // allocating = false; if (!allChecked && !checking) { // Use the MetaInfo from the storage since our own might not // yet be setup correctly. // MetaInfo meta = storage.getMetaInfo(); // if (meta != null) // System.out.print("Checking existing " // + meta.getPieces() // + " pieces: "); checking = true; } if (!checking) { if (_log.shouldLog(Log.INFO)) _log.info("Got " + (checked ? "" : "BAD ") + "piece: " + num); if (completeListener != null) completeListener.gotPiece(this); } }
@Override public boolean onTouchEvent(MotionEvent event) { mEvent = event; int action = event.getActionMasked(); float pointX = 0; float pointY = 0; boolean isPointUp = true; switch (action) { case MotionEvent.ACTION_DOWN: isPointUp = false; pointX = event.getX(); pointY = event.getY(); break; case MotionEvent.ACTION_MOVE: isPointUp = false; pointX = event.getX(); pointY = event.getY(); break; case MotionEvent.ACTION_UP: isPointUp = true; break; default: break; } for (int i = 0; i < mCircleGroup.length; i++) { mCircleGroup[i].checkSelectPoint(pointX, pointY); if (!mCircleList.contains(mCircleGroup[i]) && mCircleGroup[i].isCheckingNow()) { mCircleList.add(mCircleGroup[i]); mCircleGroup[i].setId(i); } if (isPointUp) { mCircleGroup[i].setCheckState(CircleView.STATE_UNCHECKOUT); } } if (isPointUp) { if (mCircleList.size() != 0) { mListener.onComplete(genPassword()); } mCircleList.clear(); } invalidate(); return true; }
/** * Stop contacting the tracker and talking with peers * * @param fast if true, limit the life of the unannounce threads * @since 0.9.1 */ public synchronized void stopTorrent(boolean fast) { stopped = true; TrackerClient tc = trackerclient; if (tc != null) tc.halt(fast); PeerCoordinator pc = coordinator; if (pc != null) pc.halt(); Storage st = storage; if (st != null) { // TODO: Cache the config-in-mem to compare vs config-on-disk // (needed for auto-save to not double-save in some cases) // boolean changed = storage.isChanged() || getUploaded() != savedUploaded; boolean changed = true; try { storage.close(); } catch (IOException ioe) { System.out.println("Error closing " + torrent); ioe.printStackTrace(); } if (changed && completeListener != null) completeListener.updateStatus(this); } if (pc != null && _peerCoordinatorSet != null) _peerCoordinatorSet.remove(pc); if (_peerCoordinatorSet == null) _util.disconnect(); }
/** * 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(); }
/** * StorageListener and CoordinatorListener callback * * @since 0.9.2 */ public void addMessage(String message) { if (completeListener != null) completeListener.addMessage(this, message); }
public void storageCompleted(Storage storage) { if (_log.shouldLog(Log.INFO)) _log.info("Completely received " + torrent); // storage.close(); // System.out.println("Completely received: " + torrent); if (completeListener != null) completeListener.torrentComplete(this); }