@Override public boolean isInactive() { if (torrent.getStatus().isPaused() || torrent.getStatus().isFinished()) { return true; } return false; }
@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 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 long getFileSize() { TorrentStatus status = torrent.getStatus(); long fileSize = status != null ? status.getTotalWanted() : -1; return fileSize; }