Search in sources :

Example 21 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class PrivacyView method swt_updateFields.

private void swt_updateFields(DownloadManager old_dm, DownloadManager new_dm) {
    if (cMainComposite == null || cMainComposite.isDisposed()) {
        return;
    }
    byte[] hash = null;
    if (new_dm != null) {
        TOTorrent torrent = new_dm.getTorrent();
        if (torrent != null) {
            try {
                hash = torrent.getHash();
            } catch (Throwable e) {
            }
        }
    }
    i2p_lookup_button.setData("hash", hash);
    updateI2PState();
    Utils.disposeComposite(i2p_lookup_comp, false);
    i2p_result_summary.setText("");
    i2p_result_list.setText("");
    if (old_dm != null) {
        DownloadManagerState state = old_dm.getDownloadState();
        state.removeListener(this, DownloadManagerState.AT_NETWORKS, DownloadManagerStateAttributeListener.WRITTEN);
        state.removeListener(this, DownloadManagerState.AT_PEER_SOURCES, DownloadManagerStateAttributeListener.WRITTEN);
        state.removeListener(this, DownloadManagerState.AT_FLAGS, DownloadManagerStateAttributeListener.WRITTEN);
    }
    if (new_dm != null) {
        DownloadManagerState state = new_dm.getDownloadState();
        state.addListener(this, DownloadManagerState.AT_NETWORKS, DownloadManagerStateAttributeListener.WRITTEN);
        state.addListener(this, DownloadManagerState.AT_PEER_SOURCES, DownloadManagerStateAttributeListener.WRITTEN);
        state.addListener(this, DownloadManagerState.AT_FLAGS, DownloadManagerStateAttributeListener.WRITTEN);
        setupNetworksAndSources(new_dm);
        setupTorrentTracker(new_dm);
    } else {
        setupNetworksAndSources(null);
        setupTorrentTracker(null);
    }
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) DownloadManagerState(com.biglybt.core.download.DownloadManagerState)

Example 22 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class RelatedContentManager method addDownloads.

protected void addDownloads(Download[] downloads, boolean initialising) {
    synchronized (rcm_lock) {
        List<DownloadInfo> new_info = new ArrayList<>(downloads.length);
        for (Download download : downloads) {
            try {
                if (!download.isPersistent()) {
                    continue;
                }
                Torrent torrent = download.getTorrent();
                if (torrent == null) {
                    continue;
                }
                byte[] hash = torrent.getHash();
                if (download_info_map.containsKey(hash)) {
                    continue;
                }
                byte nets = getNetworks(download);
                if (nets == NET_NONE) {
                    continue;
                }
                TOTorrent to_torrent = PluginCoreUtils.unwrap(torrent);
                if (!(TorrentUtils.isReallyPrivate(to_torrent) || TorrentUtils.getFlag(to_torrent, TorrentUtils.TORRENT_FLAG_DISABLE_RCM))) {
                    DownloadManagerState state = PluginCoreUtils.unwrap(download).getDownloadState();
                    if (state.getFlag(DownloadManagerState.FLAG_LOW_NOISE) || state.getFlag(DownloadManagerState.FLAG_METADATA_DOWNLOAD)) {
                        continue;
                    }
                    LinkedList<DownloadInfo> download_infos1;
                    LinkedList<DownloadInfo> download_infos2;
                    if ((nets & NET_PUBLIC) != 0) {
                        download_infos1 = pub_download_infos1;
                        download_infos2 = pub_download_infos2;
                    } else {
                        download_infos1 = non_pub_download_infos1;
                        download_infos2 = non_pub_download_infos2;
                    }
                    int version = RelatedContent.VERSION_INITIAL;
                    long rand = global_random_id ^ state.getLongParameter(DownloadManagerState.PARAM_RANDOM_SEED);
                    int seeds_leechers;
                    int[] aggregate_seeds_leechers = getAggregateSeedsLeechers(state);
                    if (aggregate_seeds_leechers == null) {
                        long cache = state.getLongAttribute(DownloadManagerState.AT_SCRAPE_CACHE);
                        if (cache == -1) {
                            seeds_leechers = -1;
                        } else {
                            int seeds = (int) ((cache >> 32) & 0x00ffffff);
                            int leechers = (int) (cache & 0x00ffffff);
                            seeds_leechers = (int) ((seeds << 16) | (leechers & 0xffff));
                        }
                    } else {
                        version = RelatedContent.VERSION_BETTER_SCRAPE;
                        int seeds = aggregate_seeds_leechers[0];
                        int leechers = aggregate_seeds_leechers[1];
                        seeds_leechers = (int) ((seeds << 16) | (leechers & 0xffff));
                    }
                    byte[][] keys = getKeys(download);
                    int first_seen = (int) (state.getLongParameter(DownloadManagerState.PARAM_DOWNLOAD_ADDED_TIME) / 1000);
                    DownloadInfo info = new DownloadInfo(version, hash, hash, download.getName(), (int) rand, torrent.isPrivate() ? StringInterner.intern(torrent.getAnnounceURL().getHost()) : null, keys[0], keys[1], getTags(download), nets, first_seen, 0, false, torrent.getSize(), (int) (to_torrent.getCreationDate() / (60 * 60)), seeds_leechers);
                    new_info.add(info);
                    if (initialising || download_infos1.size() == 0) {
                        download_infos1.add(info);
                    } else {
                        download_infos1.add(RandomUtils.nextInt(download_infos1.size()), info);
                    }
                    download_infos2.add(info);
                    download_info_map.put(hash, info);
                    if (info.getTracker() != null) {
                        download_priv_set.add(getPrivateInfoKey(info));
                    }
                }
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
        List<Map<String, Object>> history = (List<Map<String, Object>>) COConfigurationManager.getListParameter("rcm.dlinfo.history.privx", new ArrayList<Map<String, Object>>());
        if (initialising) {
            int padd = MAX_HISTORY - download_info_map.size();
            for (int i = 0; i < history.size() && padd > 0; i++) {
                try {
                    DownloadInfo info = deserialiseDI((Map<String, Object>) history.get(i), null);
                    if (info != null && !download_info_map.containsKey(info.getHash())) {
                        download_info_map.put(info.getHash(), info);
                        if (info.getTracker() != null) {
                            download_priv_set.add(getPrivateInfoKey(info));
                        }
                        byte nets = info.getNetworksInternal();
                        if (nets != NET_NONE) {
                            if ((nets & NET_PUBLIC) != 0) {
                                pub_download_infos1.add(info);
                                pub_download_infos2.add(info);
                            } else {
                                non_pub_download_infos1.add(info);
                                non_pub_download_infos2.add(info);
                            }
                            padd--;
                        }
                    }
                } catch (Throwable e) {
                }
            }
            Collections.shuffle(pub_download_infos1);
            Collections.shuffle(non_pub_download_infos1);
        } else {
            if (new_info.size() > 0) {
                final List<String> base32_hashes = new ArrayList<>();
                for (DownloadInfo info : new_info) {
                    byte[] hash = info.getHash();
                    if (hash != null) {
                        base32_hashes.add(Base32.encode(hash));
                    }
                    Map<String, Object> map = serialiseDI(info, null);
                    if (map != null) {
                        history.add(map);
                    }
                }
                while (history.size() > MAX_HISTORY) {
                    history.remove(0);
                }
                COConfigurationManager.setParameter("rcm.dlinfo.history.privx", history);
                if (base32_hashes.size() > 0) {
                    content_change_dispatcher.dispatch(new AERunnable() {

                        @Override
                        public void runSupport() {
                            List<RelatedContent> to_remove = new ArrayList<>();
                            synchronized (rcm_lock) {
                                ContentCache content_cache = loadRelatedContent();
                                for (String h : base32_hashes) {
                                    DownloadInfo di = content_cache.related_content.get(h);
                                    if (di != null) {
                                        to_remove.add(di);
                                    }
                                }
                            }
                            if (to_remove.size() > 0) {
                                delete(to_remove.toArray(new RelatedContent[to_remove.size()]));
                            }
                        }
                    });
                }
            }
        }
    }
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) Torrent(com.biglybt.pif.torrent.Torrent) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) Download(com.biglybt.pif.download.Download) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 23 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class DownloadManagerDefaultPaths method getDownloadOrTagMovementInformation.

static MovementInformation getDownloadOrTagMovementInformation(DownloadManager dm, MovementInformation def_mi) {
    boolean move_data = true;
    boolean move_torrent = false;
    File move_to_target = null;
    String context_str = "";
    String mi_str = "";
    DownloadManagerState state = dm.getDownloadState();
    String explicit_target = state.getAttribute(DownloadManagerState.AT_MOVE_ON_COMPLETE_DIR);
    if (explicit_target != null && explicit_target.length() > 0) {
        File move_to = new File(explicit_target);
        if (!move_to.exists()) {
            move_to.mkdirs();
        }
        if (move_to.isDirectory() && move_to.canWrite()) {
            move_to_target = move_to;
            context_str = "Download-specific move-on-complete directory";
            mi_str = "Download-specific Move on Completion";
        } else {
            logInfo("Ignoring invalid download move-to location: " + move_to, dm);
        }
    }
    if (move_to_target == null) {
        List<Tag> dm_tags = TagManagerFactory.getTagManager().getTagsForTaggable(dm);
        if (dm_tags != null) {
            List<Tag> applicable_tags = new ArrayList<>();
            for (Tag tag : dm_tags) {
                if (tag.getTagType().hasTagTypeFeature(TagFeature.TF_FILE_LOCATION)) {
                    TagFeatureFileLocation fl = (TagFeatureFileLocation) tag;
                    if (fl.supportsTagMoveOnComplete()) {
                        File move_to = fl.getTagMoveOnCompleteFolder();
                        if (move_to != null) {
                            if (!move_to.exists()) {
                                move_to.mkdirs();
                            }
                            if (move_to.isDirectory() && move_to.canWrite()) {
                                applicable_tags.add(tag);
                            } else {
                                logInfo("Ignoring invalid tag move-to location: " + move_to, dm);
                            }
                        }
                    }
                }
            }
            if (!applicable_tags.isEmpty()) {
                if (applicable_tags.size() > 1) {
                    Collections.sort(applicable_tags, new Comparator<Tag>() {

                        @Override
                        public int compare(Tag o1, Tag o2) {
                            return (o1.getTagID() - o2.getTagID());
                        }
                    });
                    String str = "";
                    for (Tag tag : applicable_tags) {
                        str += (str.length() == 0 ? "" : ", ") + tag.getTagName(true);
                    }
                    logInfo("Multiple applicable tags found: " + str + " - selecting first", dm);
                }
                Tag tag_target = applicable_tags.get(0);
                TagFeatureFileLocation fl = (TagFeatureFileLocation) tag_target;
                move_to_target = fl.getTagMoveOnCompleteFolder();
                long options = fl.getTagMoveOnCompleteOptions();
                move_data = (options & TagFeatureFileLocation.FL_DATA) != 0;
                move_torrent = (options & TagFeatureFileLocation.FL_TORRENT) != 0;
                context_str = "Tag '" + tag_target.getTagName(true) + "' move-on-complete directory";
                mi_str = "Tag Move on Completion";
            }
        }
    }
    if (move_to_target != null) {
        SourceSpecification source = new SourceSpecification();
        if (def_mi.target.getBoolean("enabled", false)) {
            source.setBoolean("default dir", "Move Only When In Default Save Dir");
            source.setBoolean("default subdir", SUBDIR_PARAM);
        } else {
            source.setBoolean("default dir", false);
        }
        source.setBoolean("incomplete dl", false);
        TargetSpecification dest = new TargetSpecification();
        if (move_data) {
            dest.setBoolean("enabled", true);
            dest.setString("target_raw", move_to_target.getAbsolutePath());
        } else {
            dest.setBoolean("enabled", def_mi.target.getBoolean("enabled", false));
        }
        dest.setContext(context_str);
        if (move_torrent) {
            dest.setBoolean("torrent", true);
            dest.setString("torrent_path_raw", move_to_target.getAbsolutePath());
        } else {
            dest.setBoolean("torrent", "Move Torrent When Done");
            dest.setString("torrent_path", "Move Torrent When Done Directory");
        }
        TransferSpecification trans = new TransferSpecification();
        MovementInformation mi = new MovementInformation(source, dest, trans, mi_str);
        return (mi);
    } else {
        return (def_mi);
    }
}
Also used : DownloadManagerState(com.biglybt.core.download.DownloadManagerState) Tag(com.biglybt.core.tag.Tag) TagFeatureFileLocation(com.biglybt.core.tag.TagFeatureFileLocation) File(java.io.File)

Example 24 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class DownloadManagerStatsImpl method getAvailWentBadTime.

@Override
public long getAvailWentBadTime() {
    PEPeerManager pm = download_manager.getPeerManager();
    if (pm != null) {
        long bad_time = pm.getAvailWentBadTime();
        if (bad_time > 0) {
            return (bad_time);
        }
        if (pm.getMinAvailability() >= 1.0) {
            return (0);
        }
    }
    DownloadManagerState state = download_manager.getDownloadState();
    return (state.getLongAttribute(DownloadManagerState.AT_AVAIL_BAD_TIME));
}
Also used : PEPeerManager(com.biglybt.core.peer.PEPeerManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState)

Example 25 with DownloadManagerState

use of com.biglybt.core.download.DownloadManagerState in project BiglyBT by BiglySoftware.

the class DiskManagerUtil method setFileLink.

static boolean setFileLink(DownloadManager download_manager, DiskManagerFileInfo[] info, DiskManagerFileInfo file_info, File from_file, File to_link, FileUtil.ProgressListener pl) {
    if (to_link != null) {
        File existing_file = file_info.getFile(true);
        if (to_link.equals(existing_file)) {
            return (true);
        }
        for (int i = 0; i < info.length; i++) {
            if (to_link.equals(info[i].getFile(true))) {
                Logger.log(new LogAlert(download_manager, LogAlert.REPEATABLE, LogAlert.AT_ERROR, "Attempt to link to existing file '" + info[i].getFile(true) + "'"));
                return (false);
            }
        }
        if (to_link.exists()) {
            if (!existing_file.exists()) {
                // using a new file, make sure we recheck
                download_manager.recheckFile(file_info);
            } else {
                if (to_link.getParent().equals(existing_file.getParent()) && to_link.getName().equalsIgnoreCase(existing_file.getName())) {
                    if (!FileUtil.renameFile(existing_file, to_link)) {
                        Logger.log(new LogAlert(download_manager, LogAlert.REPEATABLE, LogAlert.AT_ERROR, "Failed to rename '" + existing_file.toString() + "'"));
                        return (false);
                    }
                } else {
                    Object skip_delete = download_manager.getUserData("set_link_dont_delete_existing");
                    if ((skip_delete instanceof Boolean) && (Boolean) skip_delete) {
                        download_manager.recheckFile(file_info);
                    } else {
                        if (FileUtil.deleteWithRecycle(existing_file, download_manager.getDownloadState().getFlag(DownloadManagerState.FLAG_LOW_NOISE))) {
                            // new file, recheck
                            download_manager.recheckFile(file_info);
                        } else {
                            Logger.log(new LogAlert(download_manager, LogAlert.REPEATABLE, LogAlert.AT_ERROR, "Failed to delete '" + existing_file.toString() + "'"));
                            return (false);
                        }
                    }
                }
            }
        } else {
            if (existing_file.exists()) {
                if (!FileUtil.renameFile(existing_file, to_link, pl)) {
                    Logger.log(new LogAlert(download_manager, LogAlert.REPEATABLE, LogAlert.AT_ERROR, "Failed to rename '" + existing_file.toString() + "'"));
                    return (false);
                }
            }
        }
    }
    DownloadManagerState state = download_manager.getDownloadState();
    state.setFileLink(file_info.getIndex(), from_file, to_link);
    state.save();
    return (true);
}
Also used : TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) CacheFile(com.biglybt.core.diskmanager.cache.CacheFile) File(java.io.File) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) LogAlert(com.biglybt.core.logging.LogAlert)

Aggregations

DownloadManagerState (com.biglybt.core.download.DownloadManagerState)38 File (java.io.File)14 DownloadManager (com.biglybt.core.download.DownloadManager)11 TOTorrent (com.biglybt.core.torrent.TOTorrent)9 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)8 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)6 CacheFile (com.biglybt.core.diskmanager.cache.CacheFile)6 PEPeerManager (com.biglybt.core.peer.PEPeerManager)5 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)5 Download (com.biglybt.pif.download.Download)4 UIInputReceiverListener (com.biglybt.pif.ui.UIInputReceiverListener)4 IOException (java.io.IOException)4 Map (java.util.Map)4 CoreRunningListener (com.biglybt.core.CoreRunningListener)3 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)3 Tag (com.biglybt.core.tag.Tag)3 UIFunctions (com.biglybt.ui.UIFunctions)3 List (java.util.List)3 Core (com.biglybt.core.Core)2 ParameterListener (com.biglybt.core.config.ParameterListener)2