@Override public void handleEvent(TorrentEvent event) { if (event == TorrentEvent.STOPPED) { if (!finished.get()) { cancel(); } else { remove(); } } else if (event == TorrentEvent.STATUS_CHANGED) { // considered to be finished uploading if seed ratio has been // reached boolean finished = torrent.isFinished(); float seedRatio = torrent.getSeedRatio(); TorrentStatus status = torrent.getStatus(); int seedTime = status != null ? status.getSeedingTime() : 0; float targetSeedRatio = torrentManager.getTorrentManagerSettings().getSeedRatioLimit(); int targetSeedTime = torrentManager.getTorrentManagerSettings().getSeedTimeLimit(); if (finished && (seedRatio >= targetSeedRatio || seedTime >= targetSeedTime)) { this.finished.set(true); torrent.stop(); } } }
@Override public boolean isInactive() { if (torrent.getStatus().isPaused() || torrent.getStatus().isFinished()) { return true; } return false; }
@Override public String getCustomIconDescriptor() { if (torrent.isSingleFileTorrent()) { return null; } return BITTORRENT_UPLOAD; }
@Override public UploadStatus getState() { if (cancelled.get()) { return UploadStatus.CANCELLED; } if (finished.get()) { return UploadStatus.COMPLETE; } TorrentStatus status = torrent.getStatus(); if (status == null) { return UploadStatus.CONNECTING; } if (torrent.isPaused() || (torrent.isFinished() && torrent.getNumUploads() == 0)) { return UploadStatus.QUEUED; } if (status.isError()) { // TODO add retry // TODO custom error state return UploadStatus.UNAVAILABLE_RANGE; } if (status.isPaused()) { return UploadStatus.UPLOADING; } else { TorrentState state = status.getState(); switch (state) { case DOWNLOADING: case FINISHED: case SEEDING: return UploadStatus.UPLOADING; case QUEUED_FOR_CHECKING: case CHECKING_FILES: case DOWNLOADING_METADATA: case ALLOCATING: return UploadStatus.CONNECTING; default: throw new UnsupportedOperationException("Unknown state: " + state); } } }
@Override public URN getUrn() { if (urn == null) { synchronized (this) { if (urn == null) { try { urn = URN.createSha1UrnFromHex(torrent.getSha1()); } catch (IOException e) { throw new RuntimeException(e); } } } } return urn; }
private void remove() { torrent.removeListener(this); torrentUploadManager.removeMemento(torrent); activityCallback.removeUpload(this); };
public void registerTorrentListener() { torrent.addListener(this); }
@Override public float getSeedRatio() { return torrent.getSeedRatio(); }
@Override public int getNumUploadConnections() { return torrent.getNumConnections(); }
@Override public File getFile() { return torrent.getTorrentDataFile(); }
@Override public float getAverageBandwidth() { // Unused return (torrent.getUploadRate() / 1024); }
@Override public float getMeasuredBandwidth() throws InsufficientDataException { return (torrent.getUploadRate() / 1024); }
@Override public long getTotalAmountUploaded() { return torrent.getTotalUploaded(); }
@Override public long getFileSize() { TorrentStatus status = torrent.getStatus(); long fileSize = status != null ? status.getTotalWanted() : -1; return fileSize; }
@Override public String getFileName() { return torrent.getName(); }