Skip to content

Commit eab1947

Browse files
committed
Added global setting for file priority pieces remaining
1 parent 6a37795 commit eab1947

10 files changed

+201
-60
lines changed

core/src/com/biglybt/core/config/ConfigKeys.java

+1
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ public static class Transfer {
430430
public static final String ICFG_PRIORITIZE_FIRST_MB = "Prioritize First MB";
431431
public static final String BCFG_PRIORITIZE_FIRST_PIECE_FORCE = "Prioritize First Piece Force";
432432
public static final String BCFG_PRIORITIZE_MOST_COMPLETED_FILES = "Prioritize Most Completed Files";
433+
public static final String ICFG_SET_FILE_PRIORITY_REM_PIECE = "Set File Priority Remaining Pieces";
433434
public static final String SCFG_IGNORE_PEER_PORTS = "Ignore.peer.ports";
434435
public static final String BCFG_LAN_SPEED_ENABLED = "LAN Speed Enabled";
435436
public static final String ICFG_MAX_LAN_UPLOAD_SPEED_K_BS = "Max LAN Upload Speed KBs";

core/src/com/biglybt/core/config/impl/ConfigurationDefaults.java

+1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ public class ConfigurationDefaults {
421421
def.put( ConfigKeys.Transfer.ICFG_PRIORITIZE_FIRST_MB, 0 );
422422
def.put( ConfigKeys.Transfer.BCFG_PRIORITIZE_FIRST_PIECE_FORCE, FALSE );
423423
def.put( "Prioritize Most Completed Files", FALSE );
424+
def.put( ConfigKeys.Transfer.ICFG_SET_FILE_PRIORITY_REM_PIECE, ZERO );
424425
def.put( "Piece Picker Request Hint Enabled", TRUE );
425426
def.put( "Use Lazy Bitfield", FALSE );
426427
def.put( "Zero New", FALSE );

core/src/com/biglybt/core/disk/DiskManagerFileInfoSet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public interface
5151
/**
5252
* Sets the priorities of all files
5353
*
54-
* @param newPriorities array size must be # of files in set
54+
* @param newPriorities array size must be # of files in set. Use Integer.MIN_VALUE to signify "no change"
5555
*/
5656
public void setPriority(int[] newPriorities);
5757

core/src/com/biglybt/core/disk/impl/DiskManagerFileInfoSetImpl.java

+30-12
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,48 @@ public int nbFiles() {
5555
}
5656

5757
@Override
58-
public void setPriority(int[] newPriorities) {
59-
if(newPriorities.length != files.length)
58+
public void
59+
setPriority(
60+
int[] newPriorities)
61+
{
62+
if ( newPriorities.length != files.length ){
63+
6064
throw new IllegalArgumentException("array length mismatches the number of files");
61-
65+
}
66+
6267
DownloadManagerState dmState = diskManager.getDownloadState();
6368

64-
try {
69+
try{
6570
dmState.suppressStateSave(true);
6671

67-
68-
for(int i=0;i<files.length;i++)
69-
if(newPriorities[i] != 0){
70-
files[i].setPriority(newPriorities[i]);
72+
for ( int i=0;i<files.length;i++ ){
73+
74+
// existing code ignored 0, dunno why, removed it. anyway I've added Integer.MIN_VALUE to signify no change
75+
76+
int np = newPriorities[i];
77+
78+
if ( np != Integer.MIN_VALUE ){
79+
80+
files[i].setPriority( np );
7181
}
72-
} finally {
82+
}
83+
}finally{
84+
7385
dmState.suppressStateSave(false);
7486
}
7587
}
7688

7789
@Override
78-
public void setSkipped(boolean[] toChange, boolean setSkipped) {
79-
if(toChange.length != files.length)
90+
public void
91+
setSkipped(
92+
boolean[] toChange,
93+
boolean setSkipped)
94+
{
95+
if (toChange.length != files.length ){
96+
8097
throw new IllegalArgumentException("array length mismatches the number of files");
81-
98+
}
99+
82100
DownloadManagerState dmState = diskManager.getDownloadState();
83101

84102
try {

core/src/com/biglybt/core/disk/impl/DiskManagerUtil.java

+34-13
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,12 @@ static abstract class FileSkeleton implements DiskManagerFileInfoHelper {
462462
final DiskManagerFileInfoSet fileSetSkeleton = new DiskManagerFileInfoSet() {
463463

464464
@Override
465-
public void load(int[] priorities, boolean[] skipped){
465+
public void
466+
load(
467+
int[] priorities, boolean[] skipped)
468+
{
466469
for ( int i=0;i<priorities.length;i++){
470+
467471
res[i].load(priorities[i],skipped[i]);
468472
}
469473
}
@@ -485,39 +489,56 @@ public int nbFiles() {
485489
}
486490

487491
@Override
488-
public void setPriority(int[] newPriorities) {
489-
if(newPriorities.length != res.length){
492+
public void
493+
setPriority(
494+
int[] newPriorities )
495+
{
496+
if ( newPriorities.length != res.length){
497+
490498
throw new IllegalArgumentException("array length mismatches the number of files");
491499
}
492500

493501
List<DiskManagerFileInfo> priorityChanged = new ArrayList<>( res.length );
494502

495-
for(int i=0;i<res.length;i++){
503+
for ( int i=0; i<res.length; i++){
496504

497-
int np = newPriorities[i];
505+
FileSkeleton file = res[i];
498506

499-
res[i].priority = np;
507+
int np = newPriorities[i];
500508

501-
if ( np != 0 ){
502-
priorityChanged.add( res[i] );
509+
if ( np != Integer.MIN_VALUE ){
510+
511+
if ( file.priority != np ){
512+
513+
file.priority = np;
514+
515+
priorityChanged.add( file );
516+
}
503517
}
504518
}
505519
if ( !loading[0] ){
506520

507-
DiskManagerImpl.storeFilePriorities( download_manager, res);
521+
DiskManagerImpl.storeFilePriorities( download_manager, res );
508522
}
509523

510524
if ( !priorityChanged.isEmpty()){
525+
511526
listener.filePriorityChanged(getDiskManager(),priorityChanged);
512527
}
513528
}
514529

515530
@Override
516-
public void setSkipped(boolean[] toChange, boolean setSkipped) {
517-
if(toChange.length != res.length)
531+
public void
532+
setSkipped(
533+
boolean[] toChange, boolean setSkipped)
534+
{
535+
if ( toChange.length != res.length ){
536+
518537
throw new IllegalArgumentException("array length mismatches the number of files");
519-
520-
if (!setSkipped ){
538+
}
539+
540+
if ( !setSkipped ){
541+
521542
String[] types = DiskManagerImpl.getStorageTypes(download_manager);
522543

523544
boolean[] toLinear = new boolean[toChange.length];

core/src/com/biglybt/core/download/DownloadManager.java

+7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@
134134

135135
public void
136136
forceRecheck();
137+
138+
/**
139+
* Called for certain config change events so that individual managers don't have to separately listener for changes
140+
*/
141+
142+
public void
143+
syncGlobalConfig();
137144

138145
/**
139146
* Reset the file download state to totally undownloaded. Download must be stopped

core/src/com/biglybt/core/download/impl/DownloadManagerImpl.java

+102-32
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,8 @@ public String getTaggableName(){
10841084
controller.setDownloadManagerState( download_manager_state );
10851085

10861086
readParameters();
1087+
1088+
readFilePriorityConfig( true, false );
10871089

10881090
// establish any file links
10891091

@@ -1137,7 +1139,7 @@ protected Boolean initialValue(){
11371139
}
11381140
}else if ( attribute_name.equals( DownloadManagerState.AT_SET_FILE_PRIORITY_REM_PIECE )){
11391141

1140-
readParameters();
1142+
readFilePriorityConfig( false, false );
11411143
}
11421144
}
11431145
};
@@ -1652,6 +1654,13 @@ public void perform(TimerEvent event){
16521654

16531655
}
16541656

1657+
@Override
1658+
public void
1659+
syncGlobalConfig()
1660+
{
1661+
readFilePriorityConfig( false, true );
1662+
}
1663+
16551664
protected void
16561665
readParameters()
16571666
{
@@ -1670,10 +1679,31 @@ public void perform(TimerEvent event){
16701679
max_uploads_when_seeding = Math.max( max_uploads_when_seeding, DownloadManagerState.MIN_MAX_UPLOADS );
16711680

16721681
upload_priority_manual = state.getIntParameter( DownloadManagerState.PARAM_UPLOAD_PRIORITY );
1673-
1674-
set_file_priority_high_pieces_rem = state.getIntAttribute( DownloadManagerState.AT_SET_FILE_PRIORITY_REM_PIECE );
16751682
}
16761683

1684+
private void
1685+
readFilePriorityConfig(
1686+
boolean init,
1687+
boolean global_change )
1688+
{
1689+
int sfp = getDownloadState().getIntAttribute( DownloadManagerState.AT_SET_FILE_PRIORITY_REM_PIECE );
1690+
1691+
if ( sfp == 0 ){
1692+
1693+
sfp = COConfigurationManager.getIntParameter( ConfigKeys.Transfer.ICFG_SET_FILE_PRIORITY_REM_PIECE );
1694+
}
1695+
1696+
if ( sfp != set_file_priority_high_pieces_rem ){
1697+
1698+
set_file_priority_high_pieces_rem = sfp;
1699+
1700+
if ( !init ){
1701+
1702+
checkFilePriorities( global_change );
1703+
}
1704+
}
1705+
}
1706+
16771707
protected int[]
16781708
getMaxConnections( boolean mixed )
16791709
{
@@ -4047,6 +4077,59 @@ public boolean isDownloadComplete(boolean bIncludeDND) {
40474077
}
40484078
}
40494079

4080+
private void
4081+
checkFilePriorities(
4082+
boolean global_change )
4083+
{
4084+
// on global change to zero we reset file priorities as user seems to want to disable the default and manually clearing priorities across
4085+
// all downloads is a pain
4086+
4087+
int sfp_pieces = set_file_priority_high_pieces_rem;
4088+
4089+
if ( global_change || sfp_pieces > 0 ){
4090+
4091+
DiskManager dm = getDiskManager();
4092+
4093+
if ( dm != null ){
4094+
4095+
long sfp_bytes = dm.getPieceLength() * sfp_pieces;
4096+
4097+
DiskManagerFileInfoSet set = getDiskManagerFileInfoSet();
4098+
4099+
DiskManagerFileInfo[] files = set.getFiles();
4100+
4101+
int[] priorities = null;
4102+
4103+
for ( DiskManagerFileInfo file: files ){
4104+
4105+
long rem = file.getLength() - file.getDownloaded();
4106+
4107+
int expected = sfp_bytes==0?0:( rem <= sfp_bytes?1:0);
4108+
4109+
int existing = file.getPriority();
4110+
4111+
if ( expected != existing && ( existing == 0 || existing == 1 )){
4112+
4113+
if ( priorities == null ){
4114+
4115+
priorities = new int[files.length];
4116+
4117+
Arrays.fill( priorities, Integer.MIN_VALUE );
4118+
}
4119+
4120+
priorities[file.getIndex()] = expected;
4121+
}
4122+
}
4123+
4124+
if ( priorities != null ){
4125+
4126+
set.setPriority( priorities );
4127+
}
4128+
}
4129+
4130+
}
4131+
}
4132+
40504133
/**
40514134
* Doesn't not inform if state didn't change from last inform call
40524135
*/
@@ -4078,6 +4161,10 @@ public boolean isDownloadComplete(boolean bIncludeDND) {
40784161
resume_time = 0;
40794162
}
40804163

4164+
if ( new_state == DownloadManager.STATE_DOWNLOADING ){
4165+
4166+
checkFilePriorities( false );
4167+
}
40814168

40824169
listeners.dispatch( LDT_STATECHANGED, new Object[]{ this, new Integer( new_state )});
40834170
}
@@ -4165,46 +4252,29 @@ public boolean isDownloadComplete(boolean bIncludeDND) {
41654252
informPieceDoneChanged(
41664253
DiskManagerPiece piece )
41674254
{
4168-
int num = set_file_priority_high_pieces_rem;
4255+
int sfp_pieces = set_file_priority_high_pieces_rem;
41694256

4170-
if ( num > 0 ){
4257+
if ( sfp_pieces > 0 ){
41714258

41724259
DiskManager dm = getDiskManager();
41734260

41744261
if ( dm != null ){
4175-
4176-
DiskManagerPiece[] pieces = dm.getPieces();
4177-
4262+
4263+
long sfp_bytes = dm.getPieceLength() * sfp_pieces; // easier to work with bytes and close enough
4264+
41784265
DMPieceList list = piece.getPieceList();
41794266

4180-
for ( int i=0;i<list.size();i++) {
4267+
for ( int i=0; i<list.size(); i++){
41814268

41824269
DMPieceMapEntry entry = list.get( i );
41834270

4184-
DiskManagerFileInfo info = entry.getFile();
4185-
4186-
if ( info.getPriority() != 0 ){
4187-
4188-
continue;
4189-
}
4271+
DiskManagerFileInfo file = entry.getFile();
41904272

4191-
int start = info.getFirstPieceNumber();
4192-
4193-
int end = start + info.getNbPieces();
4194-
4195-
int remaining = 0;
4196-
4197-
for ( int j = start; j < end; j++ ){
4198-
4199-
if ( !pieces[ j ].isDone() ){
4200-
4201-
remaining++;
4202-
}
4203-
}
4204-
4205-
if ( remaining <= num ){
4206-
4207-
info.setPriority( 1 );
4273+
long rem = file.getLength() - file.getDownloaded();
4274+
4275+
if ( rem <= sfp_bytes ){
4276+
4277+
file.setPriority( 1 );
42084278
}
42094279
}
42104280
}

0 commit comments

Comments
 (0)