@@ -1084,6 +1084,8 @@ public String getTaggableName(){
1084
1084
controller .setDownloadManagerState ( download_manager_state );
1085
1085
1086
1086
readParameters ();
1087
+
1088
+ readFilePriorityConfig ( true , false );
1087
1089
1088
1090
// establish any file links
1089
1091
@@ -1137,7 +1139,7 @@ protected Boolean initialValue(){
1137
1139
}
1138
1140
}else if ( attribute_name .equals ( DownloadManagerState .AT_SET_FILE_PRIORITY_REM_PIECE )){
1139
1141
1140
- readParameters ( );
1142
+ readFilePriorityConfig ( false , false );
1141
1143
}
1142
1144
}
1143
1145
};
@@ -1652,6 +1654,13 @@ public void perform(TimerEvent event){
1652
1654
1653
1655
}
1654
1656
1657
+ @ Override
1658
+ public void
1659
+ syncGlobalConfig ()
1660
+ {
1661
+ readFilePriorityConfig ( false , true );
1662
+ }
1663
+
1655
1664
protected void
1656
1665
readParameters ()
1657
1666
{
@@ -1670,10 +1679,31 @@ public void perform(TimerEvent event){
1670
1679
max_uploads_when_seeding = Math .max ( max_uploads_when_seeding , DownloadManagerState .MIN_MAX_UPLOADS );
1671
1680
1672
1681
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 );
1675
1682
}
1676
1683
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
+
1677
1707
protected int []
1678
1708
getMaxConnections ( boolean mixed )
1679
1709
{
@@ -4047,6 +4077,59 @@ public boolean isDownloadComplete(boolean bIncludeDND) {
4047
4077
}
4048
4078
}
4049
4079
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
+
4050
4133
/**
4051
4134
* Doesn't not inform if state didn't change from last inform call
4052
4135
*/
@@ -4078,6 +4161,10 @@ public boolean isDownloadComplete(boolean bIncludeDND) {
4078
4161
resume_time = 0 ;
4079
4162
}
4080
4163
4164
+ if ( new_state == DownloadManager .STATE_DOWNLOADING ){
4165
+
4166
+ checkFilePriorities ( false );
4167
+ }
4081
4168
4082
4169
listeners .dispatch ( LDT_STATECHANGED , new Object []{ this , new Integer ( new_state )});
4083
4170
}
@@ -4165,46 +4252,29 @@ public boolean isDownloadComplete(boolean bIncludeDND) {
4165
4252
informPieceDoneChanged (
4166
4253
DiskManagerPiece piece )
4167
4254
{
4168
- int num = set_file_priority_high_pieces_rem ;
4255
+ int sfp_pieces = set_file_priority_high_pieces_rem ;
4169
4256
4170
- if ( num > 0 ){
4257
+ if ( sfp_pieces > 0 ){
4171
4258
4172
4259
DiskManager dm = getDiskManager ();
4173
4260
4174
4261
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
+
4178
4265
DMPieceList list = piece .getPieceList ();
4179
4266
4180
- for ( int i =0 ;i <list .size ();i ++) {
4267
+ for ( int i =0 ; i <list .size (); i ++){
4181
4268
4182
4269
DMPieceMapEntry entry = list .get ( i );
4183
4270
4184
- DiskManagerFileInfo info = entry .getFile ();
4185
-
4186
- if ( info .getPriority () != 0 ){
4187
-
4188
- continue ;
4189
- }
4271
+ DiskManagerFileInfo file = entry .getFile ();
4190
4272
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 );
4208
4278
}
4209
4279
}
4210
4280
}
0 commit comments