Search in sources :

Example 6 with DownloadManagerState

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

the class TorrentImpl method setComplete.

@Override
public void setComplete(File data_dir) throws TorrentException {
    try {
        LocaleTorrentUtil.setDefaultTorrentEncoding(torrent);
        DownloadManagerState download_manager_state = DownloadManagerStateFactory.getDownloadState(torrent);
        TorrentUtils.setResumeDataCompletelyValid(download_manager_state);
        download_manager_state.save();
        complete = true;
    } catch (Throwable e) {
        throw (new TorrentException("encoding selection fails", e));
    }
}
Also used : DownloadManagerState(com.biglybt.core.download.DownloadManagerState) TOTorrentException(com.biglybt.core.torrent.TOTorrentException)

Example 7 with DownloadManagerState

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

the class MainWindowImpl method downloadAdded.

private static void downloadAdded(final DownloadManager[] dms, boolean allowDonationCheck) {
    boolean oneIsNotLowNoiseOrPersistent = false;
    for (final DownloadManager dm : dms) {
        if (dm == null) {
            continue;
        }
        DownloadManagerState dmState = dm.getDownloadState();
        final TOTorrent torrent = dm.getTorrent();
        if (torrent == null) {
            continue;
        }
        int pfi = PlatformTorrentUtils.getContentPrimaryFileIndex(torrent);
        if (pfi >= 0) {
            dmState.setIntAttribute(DownloadManagerState.AT_PRIMARY_FILE_IDX, pfi);
        }
        if (!oneIsNotLowNoiseOrPersistent && !dmState.getFlag(DownloadManagerState.FLAG_LOW_NOISE) && dm.isPersistent()) {
            oneIsNotLowNoiseOrPersistent = true;
        }
    }
    if (oneIsNotLowNoiseOrPersistent && allowDonationCheck) {
        DonationWindow.checkForDonationPopup();
    }
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState)

Example 8 with DownloadManagerState

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

the class RelatedContentManager method publish.

private boolean publish(final DownloadInfo from_info, final DownloadInfo to_info) throws Exception {
    final DHTPluginInterface dht_plugin = selectDHT(from_info.getNetworksInternal());
    if (dht_plugin == null) {
        return (false);
    }
    final String from_hash = ByteFormatter.encodeString(from_info.getHash());
    final String to_hash = ByteFormatter.encodeString(to_info.getHash());
    final byte[] key_bytes = ("az:rcm:assoc:" + from_hash).getBytes("UTF-8");
    String title = to_info.getTitle();
    if (title.length() > MAX_TITLE_LENGTH) {
        title = title.substring(0, MAX_TITLE_LENGTH);
    }
    Map<String, Object> map = new HashMap<>();
    map.put("d", title);
    map.put("r", new Long(Math.abs(to_info.getRand() % 1000)));
    String tracker = to_info.getTracker();
    if (tracker == null) {
        map.put("h", to_info.getHash());
    } else {
        map.put("t", tracker);
    }
    if (to_info.getLevel() == 0) {
        try {
            Download d = to_info.getRelatedToDownload();
            if (d != null) {
                int version = RelatedContent.VERSION_INITIAL;
                Torrent torrent = d.getTorrent();
                if (torrent != null) {
                    long secs = torrent.getCreationDate();
                    long hours = secs / (60 * 60);
                    if (hours > 0) {
                        map.put("p", new Long(hours));
                    }
                }
                DownloadManagerState state = PluginCoreUtils.unwrap(d).getDownloadState();
                int leechers = -1;
                int seeds = -1;
                int[] aggregate_seeds_leechers = getAggregateSeedsLeechers(state);
                if (aggregate_seeds_leechers == null) {
                    long cache = state.getLongAttribute(DownloadManagerState.AT_SCRAPE_CACHE);
                    if (cache != -1) {
                        seeds = (int) ((cache >> 32) & 0x00ffffff);
                        leechers = (int) (cache & 0x00ffffff);
                    }
                } else {
                    seeds = aggregate_seeds_leechers[0];
                    leechers = aggregate_seeds_leechers[1];
                    version = RelatedContent.VERSION_BETTER_SCRAPE;
                }
                if (version > 0) {
                    map.put("v", new Long(version));
                }
                if (leechers > 0) {
                    map.put("l", new Long(leechers));
                }
                if (seeds > 0) {
                    map.put("z", new Long(seeds));
                }
                byte[][] keys = getKeys(d);
                if (keys[0] != null) {
                    map.put("k", keys[0]);
                }
                if (keys[1] != null) {
                    map.put("w", keys[1]);
                }
                String[] _tags = getTags(d);
                if (_tags != null) {
                    map.put("g", encodeTags(_tags));
                }
                byte nets = getNetworks(d);
                if (nets != NET_PUBLIC) {
                    map.put("o", new Long(nets & 0xff));
                }
            }
        } catch (Throwable e) {
        }
    }
    final Set<String> my_tags = new HashSet<>();
    try {
        Download d = from_info.getRelatedToDownload();
        if (d != null) {
            String[] _tags = getTags(d);
            if (_tags != null) {
                map.put("b", from_info.getRand() % 100);
                map.put("m", encodeTags(_tags));
                Collections.addAll(my_tags, _tags);
            }
        }
    } catch (Throwable e) {
    }
    long size = to_info.getSize();
    if (size != 0) {
        map.put("s", new Long(size));
    }
    final byte[] map_bytes = BEncoder.encode(map);
    // System.out.println( "rcmsize=" + map_bytes.length );
    final int max_hits = 30;
    dht_plugin.get(key_bytes, "Content rel test: " + from_hash.substring(0, 16), DHTPlugin.FLAG_SINGLE_VALUE, max_hits, 30 * 1000, false, false, new DHTPluginOperationListener() {

        private boolean diversified;

        private int hits;

        private Set<String> entries = new HashSet<>();

        @Override
        public void starts(byte[] key) {
        }

        @Override
        public boolean diversified() {
            diversified = true;
            return (false);
        }

        @Override
        public void valueRead(DHTPluginContact originator, DHTPluginValue value) {
            try {
                Map<String, Object> map = (Map<String, Object>) BDecoder.decode(value.getValue());
                DownloadInfo info = decodeInfo(map, from_info.getHash(), 1, false, entries);
                try {
                    String[] r_tags = decodeTags((byte[]) map.get("m"));
                    if (r_tags != null) {
                        Long b = (Long) map.get("b");
                        if (b == null || from_info.getRand() % 100 != b % 100) {
                            for (String tag : r_tags) {
                                synchronized (my_tags) {
                                    my_tags.remove(tag);
                                }
                            }
                        }
                    }
                } catch (Throwable e) {
                }
                if (info != null) {
                    analyseResponse(info, null);
                }
            } catch (Throwable e) {
            }
            hits++;
        }

        @Override
        public void valueWritten(DHTPluginContact target, DHTPluginValue value) {
        }

        @Override
        public void complete(byte[] key, boolean timeout_occurred) {
            // if we have something to say prioritise it somewhat
            int f_cutoff = my_tags.size() > 0 ? 20 : 10;
            try {
                boolean do_it;
                if (diversified || hits >= f_cutoff) {
                    do_it = false;
                } else if (hits <= f_cutoff / 2) {
                    do_it = true;
                } else {
                    do_it = RandomUtils.nextInt(hits - (f_cutoff / 2) + 1) == 0;
                }
                if (do_it) {
                    try {
                        dht_plugin.put(key_bytes, "Content rel: " + from_hash.substring(0, 16) + " -> " + to_hash.substring(0, 16), map_bytes, DHTPlugin.FLAG_ANON, new DHTPluginOperationListener() {

                            @Override
                            public boolean diversified() {
                                return (true);
                            }

                            @Override
                            public void starts(byte[] key) {
                            }

                            @Override
                            public void valueRead(DHTPluginContact originator, DHTPluginValue value) {
                            }

                            @Override
                            public void valueWritten(DHTPluginContact target, DHTPluginValue value) {
                            }

                            @Override
                            public void complete(byte[] key, boolean timeout_occurred) {
                                publishNext();
                            }
                        });
                    } catch (Throwable e) {
                        Debug.printStackTrace(e);
                        publishNext();
                    }
                } else {
                    publishNext();
                }
            } finally {
                checkAlternativePubs(to_info, map_bytes, f_cutoff);
            }
        }
    });
    return (true);
}
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)

Example 9 with DownloadManagerState

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

the class DiskManagerFileInfoImpl method setSkippedInternal.

protected void setSkippedInternal(boolean _skipped) {
    skipped_internal = _skipped;
    DownloadManager dm = getDownloadManager();
    if (dm != null && !dm.isDestroyed()) {
        DownloadManagerState dm_state = diskManager.getDownloadState();
        String dnd_sf = dm_state.getAttribute(DownloadManagerState.AT_DND_SUBFOLDER);
        if (dnd_sf != null) {
            File link = getLink();
            File file = getFile(false);
            if (_skipped) {
                if (link == null || link.equals(file)) {
                    File parent = file.getParentFile();
                    if (parent != null) {
                        File new_parent = new File(parent, dnd_sf);
                        // add prefix if not already present
                        String prefix = dm_state.getAttribute(DownloadManagerState.AT_DND_PREFIX);
                        String file_name = file.getName();
                        if (prefix != null && !file_name.startsWith(prefix)) {
                            file_name = prefix + file_name;
                        }
                        File new_file = new File(new_parent, file_name);
                        if (!new_file.exists()) {
                            if (!new_parent.exists()) {
                                new_parent.mkdirs();
                            }
                            if (new_parent.canWrite()) {
                                boolean ok;
                                try {
                                    dm_state.setFileLink(file_index, file, new_file);
                                    cache_file.moveFile(new_file);
                                    ok = true;
                                } catch (Throwable e) {
                                    ok = false;
                                    Debug.out(e);
                                }
                                if (!ok) {
                                    dm_state.setFileLink(file_index, file, link);
                                }
                            }
                        }
                    }
                }
            } else {
                if (link != null && !file.exists()) {
                    File parent = file.getParentFile();
                    if (parent != null && parent.canWrite()) {
                        File new_parent = parent.getName().equals(dnd_sf) ? parent : new File(parent, dnd_sf);
                        // use link name to handle incomplete file suffix if set
                        File new_file = new File(new_parent, link.getName());
                        if (new_file.equals(link)) {
                            boolean ok;
                            try {
                                String file_name = file.getName();
                                String prefix = dm_state.getAttribute(DownloadManagerState.AT_DND_PREFIX);
                                boolean prefix_removed = false;
                                if (prefix != null && file_name.startsWith(prefix)) {
                                    file_name = file_name.substring(prefix.length());
                                    prefix_removed = true;
                                }
                                String incomp_ext = dm_state.getAttribute(DownloadManagerState.AT_INCOMP_FILE_SUFFIX);
                                if (incomp_ext != null && incomp_ext.length() > 0 && getDownloaded() != getLength()) {
                                    if (prefix == null) {
                                        prefix = "";
                                    }
                                    File new_link = new File(file.getParentFile(), prefix + file_name + incomp_ext);
                                    dm_state.setFileLink(file_index, file, new_link);
                                    cache_file.moveFile(new_link);
                                } else if (prefix_removed) {
                                    File new_link = new File(file.getParentFile(), file_name);
                                    dm_state.setFileLink(file_index, file, new_link);
                                    cache_file.moveFile(new_link);
                                } else {
                                    dm_state.setFileLink(file_index, file, null);
                                    cache_file.moveFile(file);
                                }
                                File[] files = new_parent.listFiles();
                                if (files != null && files.length == 0) {
                                    new_parent.delete();
                                }
                                ok = true;
                            } catch (Throwable e) {
                                ok = false;
                                Debug.out(e);
                            }
                            if (!ok) {
                                dm_state.setFileLink(file_index, file, link);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) CacheFile(com.biglybt.core.diskmanager.cache.CacheFile) File(java.io.File)

Example 10 with DownloadManagerState

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

the class DiskManagerFileInfoSetImpl method setSkipped.

@Override
public void setSkipped(boolean[] toChange, boolean setSkipped) {
    if (toChange.length != files.length)
        throw new IllegalArgumentException("array length mismatches the number of files");
    DownloadManagerState dmState = diskManager.getDownloadState();
    try {
        dmState.suppressStateSave(true);
        if (!setSkipped) {
            String[] types = diskManager.getStorageTypes();
            boolean[] toLinear = new boolean[toChange.length];
            boolean[] toReorder = new boolean[toChange.length];
            int num_linear = 0;
            int num_reorder = 0;
            for (int i = 0; i < toChange.length; i++) {
                if (toChange[i]) {
                    int old_type = DiskManagerUtil.convertDMStorageTypeFromString(types[i]);
                    if (old_type == DiskManagerFileInfo.ST_COMPACT) {
                        toLinear[i] = true;
                        num_linear++;
                    } else if (old_type == DiskManagerFileInfo.ST_REORDER_COMPACT) {
                        toReorder[i] = true;
                        num_reorder++;
                    }
                }
            }
            if (num_linear > 0) {
                if (!Arrays.equals(toLinear, setStorageTypes(toLinear, DiskManagerFileInfo.ST_LINEAR))) {
                    return;
                }
            }
            if (num_reorder > 0) {
                if (!Arrays.equals(toReorder, setStorageTypes(toReorder, DiskManagerFileInfo.ST_REORDER))) {
                    return;
                }
            }
        }
        for (int i = 0; i < files.length; i++) if (toChange[i]) {
            files[i].setSkippedInternal(setSkipped);
            diskManager.skippedFileSetChanged(files[i]);
        }
        if (!setSkipped)
            DiskManagerUtil.doFileExistenceChecks(this, toChange, diskManager.getDownloadState().getDownloadManager(), true);
    } finally {
        dmState.suppressStateSave(false);
    }
}
Also used : DownloadManagerState(com.biglybt.core.download.DownloadManagerState)

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