📄 downloadmanagerstateimpl.java
字号:
}finally{
this_mon.exit();
}
// we need to ensure this is persisted now as it has implications regarding crash restarts etc
save();
}
public boolean
isResumeDataComplete()
{
// this is a cache of resume state to speed up startup
long state = getLongAttribute( AT_RESUME_STATE );
if ( state == 0 ){
// don't know
boolean complete = DiskManagerFactory.isTorrentResumeDataComplete( this );
setLongAttribute( AT_RESUME_STATE, complete?2:1 );
return( complete );
}else{
return( state == 2 );
}
}
public TOTorrent
getTorrent()
{
return( torrent );
}
public void
save()
{
boolean do_write;
try {
this_mon.enter();
do_write = write_required;
write_required = false;
} finally {
this_mon.exit();
}
if ( do_write ){
try {
// System.out.println( "writing download state for '" + new String(torrent.getName()));
if (Logger.isEnabled())
Logger.log(new LogEvent(torrent, LOGID, "Saving state for download '"
+ TorrentUtils.getLocalisedName(torrent) + "'"));
torrent.setAdditionalMapProperty( ATTRIBUTE_KEY, attributes );
TorrentUtils.writeToFile(torrent, true);
} catch (Throwable e) {
Logger.log(new LogEvent(torrent, LOGID, "Saving state", e));
}
} else {
// System.out.println( "not writing download state for '" + new String(torrent.getName()));
}
}
public void
delete()
{
try{
class_mon.enter();
HashWrapper wrapper = torrent.getHashWrapper();
state_map.remove( wrapper );
TorrentUtils.delete( torrent );
File dir = new File( ACTIVE_DIR, ByteFormatter.encodeString( wrapper.getBytes()));
if ( dir.exists() && dir.isDirectory()){
FileUtil.recursiveDelete( dir );
}
removeListeners();
}catch( Throwable e ){
Debug.printStackTrace( e );
}finally{
class_mon.exit();
}
}
protected void
mergeTorrentDetails(
TOTorrent other_torrent )
{
try{
boolean write = TorrentUtils.mergeAnnounceURLs( other_torrent, torrent );
// System.out.println( "DownloadManagerState:mergeTorrentDetails -> " + write );
if ( write ){
save();
if ( download_manager != null ){
TRTrackerAnnouncer client = download_manager.getTrackerClient();
if ( client != null ){
// pick up any URL changes
client.resetTrackerUrl( false );
}
}
}
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
public void
setFlag(
long flag,
boolean set )
{
long old_value = getLongAttribute( AT_FLAGS );
long new_value;
if ( set ){
new_value = old_value | flag;
}else{
new_value = old_value & ~flag;
}
if ( old_value != new_value ){
setLongAttribute( AT_FLAGS, new_value );
}
}
public boolean
getFlag(
long flag )
{
long value = getLongAttribute( AT_FLAGS );
return(( value & flag ) != 0 );
}
public boolean parameterExists(String name) {
return parameters.containsKey(name);
}
public void
setParameterDefault(
String name )
{
try{
this_mon.enter();
Object value = parameters.get( name );
if ( value == null ){
return;
}
// gotta clone here otherwise we update the underlying map and the setMapAttribute code
// doesn't think it has changed
parameters = new HashMap( parameters );
parameters.remove( name );
}finally{
this_mon.exit();
}
setMapAttribute( AT_PARAMETERS, parameters );
}
public long
getLongParameter(
String name )
{
try{
this_mon.enter();
Object value = parameters.get( name );
if ( value == null ){
value = default_parameters.get( name );
if ( value == null ){
Debug.out( "Unknown parameter '" + name + "' - must be defined in DownloadManagerState" );
return( 0 );
}else{
// default overrides
// **** note - if you add to these make sure you extend the parameter listeners
// registered as well (see addParameterListeners)
if ( name == PARAM_MAX_UPLOADS_WHEN_SEEDING_ENABLED ){
if ( COConfigurationManager.getBooleanParameter( "enable.seedingonly.maxuploads" )){
value = new Boolean( true );
}
}else if ( name == PARAM_MAX_UPLOADS_WHEN_SEEDING ){
int def = COConfigurationManager.getIntParameter( "Max Uploads Seeding" );
value = new Integer( def );
}else if ( name == PARAM_MAX_UPLOADS ){
int def = COConfigurationManager.getIntParameter("Max Uploads" );
value = new Integer( def );
}else if ( name == PARAM_MAX_PEERS ){
int def = COConfigurationManager.getIntParameter( "Max.Peer.Connections.Per.Torrent" );
value = new Integer( def );
}else if ( name == PARAM_MAX_PEERS_WHEN_SEEDING_ENABLED ){
if ( COConfigurationManager.getBooleanParameter( "Max.Peer.Connections.Per.Torrent.When.Seeding.Enable" )){
value = new Boolean( true );
}
}else if ( name == PARAM_MAX_PEERS_WHEN_SEEDING ){
int def = COConfigurationManager.getIntParameter( "Max.Peer.Connections.Per.Torrent.When.Seeding" );
value = new Integer( def );
}
}
}
if ( value instanceof Boolean ){
return(((Boolean)value).booleanValue()?1:0);
}else if ( value instanceof Integer ){
return( ((Integer)value).longValue());
}else if ( value instanceof Long ){
return( ((Long)value).longValue());
}
Debug.out( "Invalid parameter value for '" + name + "' - " + value );
return( 0 );
}finally{
this_mon.exit();
}
}
public void
setLongParameter(
String name,
long value )
{
Object default_value = default_parameters.get( name );
if ( default_value == null ){
Debug.out( "Unknown parameter '" + name + "' - must be defined in DownloadManagerState" );
}
try{
this_mon.enter();
// gotta clone here otherwise we update the underlying map and the setMapAttribute code
// doesn't think it has changed
parameters = new HashMap( parameters );
parameters.put( name, new Long(value));
}finally{
this_mon.exit();
}
setMapAttribute( AT_PARAMETERS, parameters );
}
public int
getIntParameter(
String name )
{
return( (int)getLongParameter( name ));
}
public void
setIntParameter(
String name,
int value )
{
setLongParameter( name, value );
}
public boolean
getBooleanParameter(
String name )
{
return( getLongParameter( name ) != 0 );
}
public void
setBooleanParameter(
String name,
boolean value )
{
setLongParameter( name, value?1:0 );
}
public void
setAttribute(
String name,
String value )
{
if ( name.equals( AT_CATEGORY )){
if ( value == null ){
setCategory( null );
}else{
Category cat = CategoryManager.getCategory( value );
if ( cat == null ){
cat = CategoryManager.createCategory( value );
}
setCategory( cat );
}
return;
}
if (name.equals(AT_RELATIVE_SAVE_PATH)) {
if (value.length() > 0) {
File relative_path_file = new File(value);
relative_path_file = DownloadManagerDefaultPaths.normaliseRelativePath(relative_path_file);
value = (relative_path_file == null) ? "" : relative_path_file.getPath();
}
}
setStringAttribute( name, value );
}
public String
getAttribute(
String name )
{
if ( name.equals( AT_CATEGORY )){
Category cat = getCategory();
if ( cat == null ){
return( null );
}
if ( cat == CategoryManager.getCategory( Category.TYPE_UNCATEGORIZED )){
return( null );
}
return( cat.getName());
}else{
return( getStringAttribute( name ));
}
}
public
Category
getCategory()
{
return category;
}
public void
setCategory(
Category cat )
{
if ( cat == CategoryManager.getCategory(Category.TYPE_UNCATEGORIZED)){
cat = null;
}
if ( cat == category ){
return;
}
if (cat != null && cat.getType() != Category.TYPE_USER){
cat = null;
}
Category oldCategory = (category == null)?CategoryManager.getCategory(Category.TYPE_UNCATEGORIZED):category;
category = cat;
if (oldCategory != null ){
oldCategory.removeManager( this );
}
if (category != null ){
category.addManager( this );
}
if ( category != null && category.getType() == Category.TYPE_USER ){
setStringAttribute( AT_CATEGORY, category.getName());
}else{
setStringAttribute( AT_CATEGORY, null );
}
}
public String
getTrackerClientExtensions()
{
return( getStringAttribute( AT_TRACKER_CLIENT_EXTENSIONS ));
}
public void
setTrackerClientExtensions(
String value )
{
setStringAttribute( AT_TRACKER_CLIENT_EXTENSIONS, value );
}
public String getDisplayName() {
return this.getStringAttribute(AT_DISPLAY_NAME);
}
public void setDisplayName(String value) {
this.setStringAttribute(AT_DISPLAY_NAME, value);
}
public String getUserComment() {
return this.getStringAttribute(AT_USER_COMMENT);
}
public void setUserComment(String value) {
this.setStringAttribute(AT_USER_COMMENT, value);
}
public String getRelativeSavePath() {
return this.getStringAttribute(AT_RELATIVE_SAVE_PATH);
}
public void setRelativeSavePath(String path) {
this.setStringAttribute(AT_RELATIVE_SAVE_PATH, path);
}
public String getPrimaryFile() {
String sPrimary = this.getStringAttribute(AT_PRIMARY_FILE);
// Only recheck when file doesn't exists if this is the first check
// of the session, because the file may never exist and we don't want
// to continuously go through the fileinfos
if (sPrimary == null
|| sPrimary.length() == 0
|| (firstPrimaryFileRead && !new File(sPrimary).exists()
&& download_manager.getStats().getDownloadCompleted(true) != 0)) {
DiskManagerFileInfo[] fileInfo = download_manager.getDiskManagerFileInfo();
if (fileInfo.length > 0) {
int idxBiggest = -1;
long lBiggest = -1;
for (int i = 0; i < fileInfo.length && i < 10; i++) {
if (!fileInfo[i].isSkipped() && fileInfo[i].getLength() > lBiggest) {
lBiggest = fileInfo[i].getLength();
idxBiggest = i;
}
}
if (idxBiggest >= 0) {
sPrimary = fileInfo[idxBiggest].getFile(true).getPath();
}
}
// System.out.println("calc getPrimaryFile " + sPrimary + ": " + download_manager.getDisplayName());
}
if (sPrimary == null) {
sPrimary = "";
}
if (firstPrimaryFileRead) {
firstPrimaryFileRead = false;
}
setPrimaryFile(sPrimary);
return sPrimary;
}
/**
* @param primary
*/
public void setPrimaryFile(String fileFullPath) {
this.setStringAttribute(AT_PRIMARY_FILE, fileFullPath);
}
public String[]
getNetworks()
{
List values = getListAttributeSupport( AT_NETWORKS );
List res = new ArrayList();
// map back to the constants to allow == comparisons
for (int i=0;i<values.size();i++){
String nw = (String)values.get(i);
for (int j=0;j<AENetworkClassifier.AT_NETWORKS.length;j++){
String nn = AENetworkClassifier.AT_NETWORKS[j];
if ( nn.equals( nw )){
res.add( nn );
}
}
}
String[] x = new String[res.size()];
res.toArray(x);
return( x );
}
public boolean isNetworkEnabled(
String network) {
List values = getListAttributeSupport( AT_NETWORKS );
return values.contains(network);
}
public void
setNetworks(
String[] networks )
{
if ( networks == null ){
networks = new String[0];
}
List l = new ArrayList();
for (int i=0;i<networks.length;i++){
l.add( networks[i]);
}
setListAttribute( AT_NETWORKS, l );
}
public void
setNetworkEnabled(
String network,
boolean enabled) {
List values = getListAttributeSupport( AT_NETWORKS );
boolean alreadyEnabled = values.contains(network);
List l = new ArrayList();
if(enabled && !alreadyEnabled) {
for (int i=0;i<values.size();i++){
l.add(values.get(i));
}
l.add(network);
setListAttribute( AT_NETWORKS, l );
}
if(!enabled && alreadyEnabled) {
for (int i=0;i<values.size();i++){
l.add(values.get(i));
}
l.remove(network);
setListAttribute( AT_NETWORKS, l );
}
}
// peer sources
public String[]
getPeerSources()
{
List values = getListAttributeSupport( AT_PEER_SOURCES );
List res = new ArrayList();
// map back to the constants to allow == comparisons
for (int i=0;i<values.size();i++){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -