Search in sources :

Example 1 with DownloadStats

use of com.biglybt.pif.download.DownloadStats in project BiglyBT by BiglySoftware.

the class DefaultRankCalculator method getActivelySeeding.

/**
 * Retrieves whether the torrent is "actively" seeding
 *
 * @return true: actively seeding
 */
public boolean getActivelySeeding() {
    boolean bIsActive = false;
    DownloadStats stats = dl.getStats();
    int state = dl.getState();
    // when bAutoStart0Peers
    if (iRankType == StartStopRulesDefaultPlugin.RANK_TIMED && !isFirstPriority() && !(bAutoStart0Peers && rules.calcPeersNoUs(dl, dl.getAggregatedScrapeResult()) == 0 && lastScrapeResultOk)) {
        bIsActive = (state == Download.ST_SEEDING);
    } else if (state != Download.ST_SEEDING || (bAutoStart0Peers && rules.calcPeersNoUs(dl, dl.getAggregatedScrapeResult()) == 0)) {
        // Not active if we aren't seeding
        // Not active if we are AutoStarting 0 Peers, and peer count == 0
        bIsActive = false;
        staleCDSince = -1;
    } else if (SystemTime.getCurrentTime() - stats.getTimeStarted() <= FORCE_ACTIVE_FOR) {
        bIsActive = true;
        staleCDSince = -1;
    } else {
        bIsActive = (stats.getUploadAverage() >= minSpeedForActiveSeeding);
        if (bActivelySeeding != bIsActive) {
            long now = SystemTime.getCurrentTime();
            // Change
            if (lCDActivelyChangedOn < 0) {
                // Start Timer
                lCDActivelyChangedOn = now;
                bIsActive = !bIsActive;
            } else if (now - lCDActivelyChangedOn < ACTIVE_CHANGE_WAIT) {
                // Continue as old state until timer finishes
                bIsActive = !bIsActive;
            }
            if (bActivelySeeding != bIsActive) {
                if (bIsActive) {
                    staleCDSince = -1;
                    staleCDOffset = 0;
                } else {
                    staleCDSince = System.currentTimeMillis();
                }
            }
        } else {
            // no change, reset timer
            lCDActivelyChangedOn = -1;
        }
    }
    if (bActivelySeeding != bIsActive) {
        bActivelySeeding = bIsActive;
        if (rules != null) {
            rules.requestProcessCycle(null);
            if (rules.bDebugLog)
                rules.log.log(dl.getTorrent(), LoggerChannel.LT_INFORMATION, "somethingChanged: ActivelySeeding changed");
        }
    }
    return bActivelySeeding;
}
Also used : DownloadStats(com.biglybt.pif.download.DownloadStats)

Example 2 with DownloadStats

use of com.biglybt.pif.download.DownloadStats in project BiglyBT by BiglySoftware.

the class DefaultRankCalculator method _recalcSeedingRankSupport.

private int _recalcSeedingRankSupport(int oldSR) {
    sExplainSR = "";
    DownloadStats stats = dl.getStats();
    int newSR = 0;
    // make undownloaded sort to top so they can start first.
    if (!dl.isComplete()) {
        newSR = SR_COMPLETE_STARTS_AT + (10000 - dl.getPosition());
        // make sure we capture FP being turned off when torrent does from
        // complete to incomplete
        isFirstPriority();
        if (rules.bDebugLog) {
            sExplainSR += "  not complete. SetSR " + newSR + "\n";
        }
        return newSR;
    }
    // here we are seeding
    lastModifiedShareRatio = stats.getShareRatio();
    DownloadScrapeResult sr = dl.getAggregatedScrapeResult();
    lastModifiedScrapeResultPeers = rules.calcPeersNoUs(dl, sr);
    lastModifiedScrapeResultSeeds = rules.calcSeedsNoUs(dl, sr);
    boolean bScrapeResultsOk = (lastModifiedScrapeResultPeers > 0 || lastModifiedScrapeResultSeeds > 0 || lastScrapeResultOk) && (lastModifiedScrapeResultPeers >= 0 && lastModifiedScrapeResultSeeds >= 0);
    if (!isFirstPriority()) {
        // Check Ignore Rules
        // never apply ignore rules to First Priority Matches
        // (we don't want leechers circumventing the 0.5 rule)
        // 0 means unlimited
        int activeMaxSR = dlSpecificMaxShareRatio;
        if (activeMaxSR <= 0) {
            activeMaxSR = iIgnoreShareRatio;
        }
        if (activeMaxSR != 0 && lastModifiedShareRatio >= activeMaxSR && (lastModifiedScrapeResultSeeds >= iIgnoreShareRatio_SeedStart || !bScrapeResultsOk) && lastModifiedShareRatio != -1) {
            if (rules.bDebugLog)
                sExplainSR += "  shareratio met: shareRatio(" + lastModifiedShareRatio + ") >= " + activeMaxSR + "\n";
            return SR_SHARERATIOMET;
        } else if (rules.bDebugLog && activeMaxSR != 0 && lastModifiedShareRatio >= activeMaxSR) {
            sExplainSR += "  shareratio NOT met: ";
            if (lastModifiedScrapeResultSeeds >= iIgnoreShareRatio_SeedStart)
                sExplainSR += lastModifiedScrapeResultSeeds + " below seed threshold of " + iIgnoreShareRatio_SeedStart;
            sExplainSR += "\n";
        }
        if (lastModifiedScrapeResultPeers == 0 && bScrapeResultsOk) {
            // We have to use the normal SR_0PEERS in case it isn't FP
            if (bIgnore0Peers) {
                if (rules.bDebugLog)
                    sExplainSR += "  Ignore 0 Peers criteria met\n";
                return SR_0PEERS;
            }
        // if (bFirstPriorityIgnore0Peer) {
        // if (rules.bDebugLog)
        // sExplainSR += "  Ignore 0 Peers criteria for FP met\n";
        // 
        // return SR_FP0PEERS;
        // }
        } else if (rules.bDebugLog && lastModifiedScrapeResultPeers == 0) {
            sExplainSR += "  0 Peer Ignore rule NOT applied: Scrape invalid\n";
        }
        // 0 means disabled
        if ((iIgnoreSeedCount != 0) && (lastModifiedScrapeResultSeeds >= iIgnoreSeedCount)) {
            if (rules.bDebugLog)
                sExplainSR += "  SeedCount Ignore rule met.  numSeeds(" + lastModifiedScrapeResultSeeds + " >= iIgnoreSeedCount(" + iIgnoreSeedCount + ")\n";
            return SR_NUMSEEDSMET;
        }
        // 0 means never stop
        if (iIgnoreRatioPeers != 0 && lastModifiedScrapeResultSeeds != 0) {
            float ratio = (float) lastModifiedScrapeResultPeers / lastModifiedScrapeResultSeeds;
            if (ratio <= iIgnoreRatioPeers && lastModifiedScrapeResultSeeds >= iIgnoreRatioPeers_SeedStart) {
                if (rules.bDebugLog)
                    sExplainSR += "  P:S Ignore rule met.  ratio(" + ratio + " <= threshold(" + iIgnoreRatioPeers_SeedStart + ")\n";
                return SR_RATIOMET;
            }
        }
    }
    // Never do anything with rank type of none
    if (iRankType == StartStopRulesDefaultPlugin.RANK_NONE) {
        if (rules.bDebugLog)
            sExplainSR += "  Ranking Type set to none.. blanking seeding rank\n";
        // everythink ok!
        return newSR;
    }
    if (iRankType == StartStopRulesDefaultPlugin.RANK_TIMED) {
        if (bIsFirstPriority) {
            newSR += SR_TIMED_QUEUED_ENDS_AT + 1;
            return newSR;
        }
        int state = dl.getState();
        if (state == Download.ST_STOPPING || state == Download.ST_STOPPED || state == Download.ST_ERROR) {
            if (rules.bDebugLog)
                sExplainSR += "  Download stopping, stopped or in error\n";
            return SR_NOTQUEUED;
        } else if (state == Download.ST_SEEDING || state == Download.ST_READY || state == Download.ST_WAITING || state == Download.ST_PREPARING) {
            // force sort to top
            long lMsElapsed = 0;
            long lMsTimeToSeedFor = minTimeAlive;
            if (state == Download.ST_SEEDING && !dl.isForceStart()) {
                lMsElapsed = (SystemTime.getCurrentTime() - stats.getTimeStartedSeeding());
                if (iTimed_MinSeedingTimeWithPeers > 0) {
                    PeerManager peerManager = dl.getPeerManager();
                    if (peerManager != null) {
                        int connectedLeechers = peerManager.getStats().getConnectedLeechers();
                        if (connectedLeechers > 0) {
                            lMsTimeToSeedFor = iTimed_MinSeedingTimeWithPeers;
                        }
                    }
                }
            }
            if (lMsElapsed >= lMsTimeToSeedFor) {
                newSR = 1;
                if (oldSR > SR_TIMED_QUEUED_ENDS_AT) {
                    rules.requestProcessCycle(null);
                    if (rules.bDebugLog)
                        rules.log.log(dl.getTorrent(), LoggerChannel.LT_INFORMATION, "somethingChanged: TimeUp");
                }
            } else {
                newSR = SR_TIMED_QUEUED_ENDS_AT + 1 + (int) (lMsElapsed / 1000);
                if (oldSR <= SR_TIMED_QUEUED_ENDS_AT) {
                    rules.requestProcessCycle(null);
                    if (rules.bDebugLog)
                        rules.log.log(dl.getTorrent(), LoggerChannel.LT_INFORMATION, "somethingChanged: strange timer change");
                }
            }
            return newSR;
        } else {
            // ST_QUEUED
            // priority goes to ones who haven't been seeded for long
            // maybe share ratio might work well too
            long diff;
            if (dlLastActiveTime == 0) {
                diff = dl.getStats().getSecondsOnlySeeding();
                if (diff > SR_TIMED_QUEUED_ENDS_AT - 100000) {
                    // close to overrunning.. so base off position
                    diff = SR_TIMED_QUEUED_ENDS_AT - 100000 + dl.getPosition();
                }
                newSR = SR_TIMED_QUEUED_ENDS_AT - (int) diff;
            } else {
                diff = ((System.currentTimeMillis() / 1000) - (dlLastActiveTime / 1000));
                if (diff >= SR_TIMED_QUEUED_ENDS_AT) {
                    newSR = SR_TIMED_QUEUED_ENDS_AT - 1;
                } else {
                    newSR = (int) diff;
                }
            }
            return newSR;
        }
    }
    // SeedCount and SPRatio require Scrape Results..
    if (bScrapeResultsOk) {
        if (iRankType == StartStopRulesDefaultPlugin.RANK_PEERCOUNT) {
            if (lastModifiedScrapeResultPeers > lastModifiedScrapeResultSeeds * 10)
                newSR = 100 * lastModifiedScrapeResultPeers * 10;
            else
                newSR = (int) ((long) 100 * lastModifiedScrapeResultPeers * lastModifiedScrapeResultPeers / (lastModifiedScrapeResultSeeds + 1));
        } else if ((iRankType == StartStopRulesDefaultPlugin.RANK_SEEDCOUNT) && (iRankTypeSeedFallback == 0 || iRankTypeSeedFallback > lastModifiedScrapeResultSeeds)) {
            if (lastModifiedScrapeResultSeeds < 10000)
                newSR = 10000 - lastModifiedScrapeResultSeeds;
            else
                newSR = 1;
            // shift over to make way for fallback
            newSR *= SEEDONLY_SHIFT;
        } else {
            // iRankType == RANK_SPRATIO or we are falling back
            if (lastModifiedScrapeResultPeers != 0) {
                if (lastModifiedScrapeResultSeeds == 0) {
                    if (lastModifiedScrapeResultPeers >= minPeersToBoostNoSeeds)
                        newSR += SPRATIO_BASE_LIMIT;
                } else {
                    // numSeeds != 0 && numPeers != 0
                    float x = (float) lastModifiedScrapeResultSeeds / lastModifiedScrapeResultPeers;
                    newSR += SPRATIO_BASE_LIMIT / ((x + 1) * (x + 1));
                }
            }
        }
    } else {
        if (rules.bDebugLog)
            sExplainSR += "  Can't calculate SR, no scrape results\n";
    }
    if (staleCDOffset > 0) {
        // every 10 minutes of not being active, subtract one SR
        if (newSR > staleCDOffset) {
            newSR -= staleCDOffset;
            sExplainSR += "  subtracted " + staleCDOffset + " due to non-activeness\n";
        } else {
            staleCDOffset = 0;
        }
    }
    if (newSR < 0)
        newSR = 1;
    return newSR;
}
Also used : PeerManager(com.biglybt.pif.peers.PeerManager) DownloadStats(com.biglybt.pif.download.DownloadStats) DownloadScrapeResult(com.biglybt.pif.download.DownloadScrapeResult)

Example 3 with DownloadStats

use of com.biglybt.pif.download.DownloadStats in project BiglyBT by BiglySoftware.

the class DefaultRankCalculator method getActivelyDownloading.

/**
 * Retrieves whether the torrent is "actively" downloading
 *
 * @return true: actively downloading
 */
public boolean getActivelyDownloading() {
    boolean bIsActive = false;
    DownloadStats stats = dl.getStats();
    int state = dl.getState();
    // - Must be above speed threshold, or started less than 30s ago
    if (state != Download.ST_DOWNLOADING) {
        bIsActive = false;
    } else if (SystemTime.getCurrentTime() - stats.getTimeStarted() <= FORCE_ACTIVE_FOR) {
        bIsActive = true;
    } else {
        // activity based on DL Average
        bIsActive = (stats.getDownloadAverage() >= minSpeedForActiveDL);
        if (bActivelyDownloading != bIsActive) {
            long now = SystemTime.getCurrentTime();
            // Change
            if (lDLActivelyChangedOn == -1) {
                // Start Timer
                lDLActivelyChangedOn = now;
                bIsActive = !bIsActive;
            } else if (now - lDLActivelyChangedOn < ACTIVE_CHANGE_WAIT) {
                // Continue as old state until timer finishes
                bIsActive = !bIsActive;
            }
        } else {
            // no change, reset timer
            lDLActivelyChangedOn = -1;
        }
    }
    if (bActivelyDownloading != bIsActive) {
        bActivelyDownloading = bIsActive;
        if (rules != null) {
            rules.requestProcessCycle(null);
            if (rules.bDebugLog)
                rules.log.log(dl.getTorrent(), LoggerChannel.LT_INFORMATION, "somethingChanged: ActivelyDownloading changed");
        }
    }
    return bActivelyDownloading;
}
Also used : DownloadStats(com.biglybt.pif.download.DownloadStats)

Aggregations

DownloadStats (com.biglybt.pif.download.DownloadStats)3 DownloadScrapeResult (com.biglybt.pif.download.DownloadScrapeResult)1 PeerManager (com.biglybt.pif.peers.PeerManager)1