Search in sources :

Example 1 with DownloadManager

use of com.biglybt.pif.download.DownloadManager in project BiglyBT by BiglySoftware.

the class RelatedContentManager method checkI2PSearcher.

void checkI2PSearcher(boolean force) {
    synchronized (searchers) {
        if (added_i2p_searcher) {
            return;
        }
        if (!force) {
            DownloadManager dm = plugin_interface.getDownloadManager();
            Download[] downloads = dm.getDownloads();
            boolean found = false;
            for (Download download : downloads) {
                String[] nets = PluginCoreUtils.unwrap(download).getDownloadState().getNetworks();
                if (nets.length == 1 && nets[0] == AENetworkClassifier.AT_I2P) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                return;
            }
        }
        List<DistributedDatabase> ddbs = DDBaseImpl.getDDBs(new String[] { AENetworkClassifier.AT_I2P });
        for (DistributedDatabase ddb : ddbs) {
            if (ddb.getNetwork() == AENetworkClassifier.AT_I2P) {
                DHTPluginInterface i2p_dht = ddb.getDHTPlugin();
                RelatedContentSearcher i2p_searcher = new RelatedContentSearcher(RelatedContentManager.this, transfer_type, i2p_dht, false);
                searchers.add(i2p_searcher);
                added_i2p_searcher = true;
            }
        }
    }
}
Also used : DistributedDatabase(com.biglybt.pif.ddb.DistributedDatabase) DownloadManager(com.biglybt.pif.download.DownloadManager) Download(com.biglybt.pif.download.Download)

Example 2 with DownloadManager

use of com.biglybt.pif.download.DownloadManager in project BiglyBT by BiglySoftware.

the class SubscriptionSchedulerImpl method download.

@Override
public void download(final Subscription subs, final SubscriptionResult original_result) {
    String download_link = original_result.getDownloadLink();
    if (download_link == null) {
        log(subs.getName() + ": can't download " + original_result.getID() + " as no direct download link available");
        return;
    }
    final String key = subs.getID() + ":" + original_result.getID();
    final String dl = download_link;
    synchronized (active_result_downloaders) {
        if (active_result_downloaders.contains(key)) {
            return;
        }
        log(subs.getName() + ": queued result for download - " + original_result.getID() + "/" + download_link);
        active_result_downloaders.add(key);
        result_downloader.run(new AERunnable() {

            @Override
            public void runSupport() {
                // need to fix up to the latest history due to the lazy nature of things :(
                SubscriptionResult result = subs.getHistory().getResult(original_result.getID());
                boolean success = false;
                try {
                    if (result == null) {
                        log(subs.getName() + ": result has been deleted - " + original_result.getID());
                        success = true;
                    } else if (result.getRead()) {
                        log(subs.getName() + ": result already marked as read, skipping - " + result.getID());
                        success = true;
                    } else {
                        boolean retry = true;
                        boolean use_ref = subs.getHistory().getDownloadWithReferer();
                        boolean tried_ref_switch = false;
                        while (retry) {
                            retry = false;
                            try {
                                TorrentUtils.setTLSDescription("Subscription: " + subs.getName());
                                URL original_url = new URL(dl);
                                PluginProxy plugin_proxy = null;
                                if (dl.startsWith("tor:")) {
                                    String target_resource = dl.substring(4);
                                    original_url = new URL(target_resource);
                                    Map<String, Object> options = new HashMap<>();
                                    options.put(AEProxyFactory.PO_PEER_NETWORKS, new String[] { AENetworkClassifier.AT_TOR });
                                    plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download of '" + target_resource + "'", original_url, options, true);
                                    if (plugin_proxy == null) {
                                        throw (new Exception("No Tor plugin proxy available for '" + dl + "'"));
                                    }
                                }
                                URL current_url = plugin_proxy == null ? original_url : plugin_proxy.getURL();
                                Torrent torrent = null;
                                try {
                                    while (true) {
                                        try {
                                            ResourceDownloaderFactory rdf = StaticUtilities.getResourceDownloaderFactory();
                                            ResourceDownloader url_rd = rdf.create(current_url, plugin_proxy == null ? null : plugin_proxy.getProxy());
                                            if (plugin_proxy != null) {
                                                url_rd.setProperty("URL_HOST", plugin_proxy.getURLHostRewrite() + (current_url.getPort() == -1 ? "" : (":" + current_url.getPort())));
                                            }
                                            String referer = use_ref ? subs.getReferer() : null;
                                            UrlUtils.setBrowserHeaders(url_rd, referer);
                                            Engine engine = subs.getEngine();
                                            if (engine instanceof WebEngine) {
                                                WebEngine we = (WebEngine) engine;
                                                if (we.isNeedsAuth()) {
                                                    String cookies = we.getCookies();
                                                    if (cookies != null && cookies.length() > 0) {
                                                        url_rd.setProperty("URL_Cookie", cookies);
                                                    }
                                                }
                                            }
                                            ResourceDownloader mr_rd = rdf.getMetaRefreshDownloader(url_rd);
                                            InputStream is = mr_rd.download();
                                            torrent = new TorrentImpl(TOTorrentFactory.deserialiseFromBEncodedInputStream(is));
                                            break;
                                        } catch (Throwable e) {
                                            if (plugin_proxy == null) {
                                                plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download", original_url);
                                                if (plugin_proxy != null) {
                                                    current_url = plugin_proxy.getURL();
                                                    continue;
                                                }
                                            }
                                            throw (e);
                                        }
                                    }
                                } finally {
                                    if (plugin_proxy != null) {
                                        plugin_proxy.setOK(torrent != null);
                                    }
                                }
                                byte[] hash = torrent.getHash();
                                // PlatformTorrentUtils.setContentTitle(torrent, torr );
                                DownloadManager dm = PluginInitializer.getDefaultInterface().getDownloadManager();
                                Download download;
                                // if we're assigning a tag/networks then we need to add it stopped in case the tag has any pre-start actions (e.g. set initial save location)
                                // this is because the assignments are done in SubscriptionManagerImpl on the download(willbe)added event
                                boolean stop_override = subs.getTagID() >= 0 || subs.getHistory().getDownloadNetworks() != null;
                                boolean auto_start = manager.shouldAutoStart(torrent);
                                manager.addPrepareTrigger(hash, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                try {
                                    if (auto_start && !stop_override) {
                                        download = dm.addDownload(torrent);
                                    } else {
                                        download = dm.addDownloadStopped(torrent, null, null);
                                    }
                                } finally {
                                    manager.removePrepareTrigger(hash);
                                }
                                log(subs.getName() + ": added download " + download.getName() + ": auto-start=" + auto_start);
                                // maybe remove this as should be actioned in the trigger?
                                manager.prepareDownload(download, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                subs.addAssociation(hash);
                                if (auto_start && stop_override) {
                                    download.restart();
                                }
                                result.setRead(true);
                                success = true;
                                if (tried_ref_switch) {
                                    subs.getHistory().setDownloadWithReferer(use_ref);
                                }
                            } catch (Throwable e) {
                                log(subs.getName() + ": Failed to download result " + dl, e);
                                if (e instanceof TOTorrentException && !tried_ref_switch) {
                                    use_ref = !use_ref;
                                    tried_ref_switch = true;
                                    retry = true;
                                    log(subs.getName() + ": Retrying " + (use_ref ? "with referer" : "without referer"));
                                }
                            } finally {
                                TorrentUtils.setTLSDescription(null);
                            }
                        }
                    }
                } finally {
                    try {
                        if (!success) {
                            if (dl.startsWith("azplug:") || dl.startsWith("chat:")) {
                                // whatever the outcome these have been handled async
                                result.setRead(true);
                            } else {
                                int rad = manager.getAutoDownloadMarkReadAfterDays();
                                if (rad > 0) {
                                    long rad_millis = rad * 24 * 60 * 60 * 1000L;
                                    long time_found = result.getTimeFound();
                                    if (time_found > 0 && time_found + rad_millis < SystemTime.getCurrentTime()) {
                                        log(subs.getName() + ": result expired, marking as read - " + result.getID());
                                        result.setRead(true);
                                    }
                                }
                            }
                        }
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                    synchronized (active_result_downloaders) {
                        active_result_downloaders.remove(key);
                    }
                    calculateSchedule();
                }
            }
        });
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TorrentImpl(com.biglybt.pifimpl.local.torrent.TorrentImpl) InputStream(java.io.InputStream) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) DownloadManager(com.biglybt.pif.download.DownloadManager) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) Download(com.biglybt.pif.download.Download) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) Engine(com.biglybt.core.metasearch.Engine)

Example 3 with DownloadManager

use of com.biglybt.pif.download.DownloadManager in project BiglyBT by BiglySoftware.

the class InitialisationFunctions method hookDownloadAddition.

protected static void hookDownloadAddition() {
    PluginInterface pi = PluginInitializer.getDefaultInterface();
    DownloadManager dm = pi.getDownloadManager();
    // need to get in early to ensure property present on initial announce
    dm.addDownloadWillBeAddedListener(new DownloadWillBeAddedListener() {

        @Override
        public void initialised(Download download) {
            // unfortunately the has-been-opened state is updated by the client when a user opens content
            // but is also preserved across torrent export/import (e.g. when downloaded via magnet
            // URL. So reset it here if it is found to be set
            com.biglybt.core.download.DownloadManager dm = PluginCoreUtils.unwrap(download);
            if (PlatformTorrentUtils.getHasBeenOpened(dm)) {
                PlatformTorrentUtils.setHasBeenOpened(dm, false);
            }
        }
    });
}
Also used : DownloadWillBeAddedListener(com.biglybt.pif.download.DownloadWillBeAddedListener) PluginInterface(com.biglybt.pif.PluginInterface) DownloadManager(com.biglybt.pif.download.DownloadManager) Download(com.biglybt.pif.download.Download)

Example 4 with DownloadManager

use of com.biglybt.pif.download.DownloadManager in project BiglyBT by BiglySoftware.

the class MagnetPluginMDDownloader method startSupport.

private void startSupport(final DownloadListener listener) {
    String hash_str = ByteFormatter.encodeString(hash);
    File tmp_dir = null;
    File data_file = null;
    File torrent_file = null;
    DownloadManager download_manager = plugin_interface.getDownloadManager();
    Download download = null;
    final Throwable[] error = { null };
    final boolean[] manually_removed = { false };
    final ByteArrayOutputStream result = new ByteArrayOutputStream(32 * 1024);
    TOTorrentAnnounceURLSet[] url_sets = null;
    try {
        synchronized (active_set) {
            if (active_set.contains(hash_str)) {
                throw (new Exception("Download already active for hash " + hash_str));
            }
            active_set.add(hash_str);
        }
        Download existing_download = download_manager.getDownload(hash);
        if (existing_download != null) {
            throw (new Exception("download already exists"));
        }
        tmp_dir = AETemporaryFileHandler.createTempDir();
        int rand = RandomUtils.generateRandomIntUpto(10000);
        data_file = new File(tmp_dir, hash_str + "_" + rand + ".torrent");
        torrent_file = new File(tmp_dir, hash_str + "_" + rand + ".metatorrent");
        RandomAccessFile raf = new RandomAccessFile(data_file, "rw");
        try {
            byte[] buffer = new byte[512 * 1024];
            Arrays.fill(buffer, (byte) 0xff);
            for (long i = 0; i < 64 * 1024 * 1024; i += buffer.length) {
                raf.write(buffer);
            }
        } finally {
            raf.close();
        }
        URL announce_url = TorrentUtils.getDecentralisedURL(hash);
        TOTorrentCreator creator = TOTorrentFactory.createFromFileOrDirWithFixedPieceLength(data_file, announce_url, 16 * 1024);
        TOTorrent meta_torrent = creator.create();
        String[] bits = args.split("&");
        List<String> trackers = new ArrayList<>();
        String name = "magnet:" + Base32.encode(hash);
        Map<String, String> magnet_args = new HashMap<>();
        for (String bit : bits) {
            String[] x = bit.split("=");
            if (x.length == 2) {
                String lhs = x[0].toLowerCase();
                String rhs = UrlUtils.decode(x[1]);
                magnet_args.put(lhs, rhs);
                if (lhs.equals("tr")) {
                    String tracker = rhs;
                    trackers.add(tracker);
                } else if (lhs.equals("dn")) {
                    name = rhs;
                }
            }
        }
        if (trackers.size() > 0) {
            // stick the decentralised one we created above in position 0 - this will be
            // removed later if the torrent is downloaded
            trackers.add(0, announce_url.toExternalForm());
            TOTorrentAnnounceURLGroup ag = meta_torrent.getAnnounceURLGroup();
            List<TOTorrentAnnounceURLSet> sets = new ArrayList<>();
            for (String tracker : trackers) {
                try {
                    URL tracker_url = new URL(tracker);
                    sets.add(ag.createAnnounceURLSet(new URL[] { tracker_url }));
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
            if (sets.size() > 0) {
                url_sets = sets.toArray(new TOTorrentAnnounceURLSet[sets.size()]);
                ag.setAnnounceURLSets(url_sets);
            }
        }
        if (!data_file.delete()) {
            throw (new Exception("Failed to delete " + data_file));
        }
        meta_torrent.setHashOverride(hash);
        TorrentUtils.setFlag(meta_torrent, TorrentUtils.TORRENT_FLAG_METADATA_TORRENT, true);
        TorrentUtils.setFlag(meta_torrent, TorrentUtils.TORRENT_FLAG_LOW_NOISE, true);
        meta_torrent.serialiseToBEncodedFile(torrent_file);
        download_manager.clearNonPersistentDownloadState(hash);
        download = download_manager.addNonPersistentDownloadStopped(PluginCoreUtils.wrap(meta_torrent), torrent_file, data_file);
        String display_name = MessageText.getString("MagnetPlugin.use.md.download.name", new String[] { name });
        DownloadManagerState state = PluginCoreUtils.unwrap(download).getDownloadState();
        state.setDisplayName(display_name + ".torrent");
        if (networks.size() == 0 || (networks.size() == 1 && networks.contains(AENetworkClassifier.AT_PUBLIC))) {
            for (String network : AENetworkClassifier.AT_NETWORKS) {
                state.setNetworkEnabled(network, true);
            }
        } else {
            for (String network : networks) {
                state.setNetworkEnabled(network, true);
            }
            if (!networks.contains(AENetworkClassifier.AT_PUBLIC)) {
                state.setNetworkEnabled(AENetworkClassifier.AT_PUBLIC, false);
            }
        }
        if (!plugin.isNetworkEnabled(AENetworkClassifier.AT_PUBLIC)) {
            state.setNetworkEnabled(AENetworkClassifier.AT_PUBLIC, false);
        }
        final List<InetSocketAddress> peers_to_inject = new ArrayList<>();
        if (addresses != null && addresses.length > 0) {
            String[] enabled_nets = state.getNetworks();
            for (InetSocketAddress address : addresses) {
                String host = AddressUtils.getHostAddress(address);
                String net = AENetworkClassifier.categoriseAddress(host);
                for (String n : enabled_nets) {
                    if (n == net) {
                        peers_to_inject.add(address);
                        break;
                    }
                }
            }
        }
        final Set<String> peer_networks = new HashSet<>();
        final List<Map<String, Object>> peers_for_cache = new ArrayList<>();
        download.addPeerListener(new DownloadPeerListener() {

            @Override
            public void peerManagerAdded(final Download download, final PeerManager peer_manager) {
                if (cancelled || completed) {
                    download.removePeerListener(this);
                    return;
                }
                final PEPeerManager pm = PluginCoreUtils.unwrap(peer_manager);
                peer_manager.addListener(new PeerManagerListener2() {

                    private PeerManagerListener2 pm_listener = this;

                    private int md_size;

                    @Override
                    public void eventOccurred(PeerManagerEvent event) {
                        if (cancelled || completed) {
                            peer_manager.removeListener(this);
                            return;
                        }
                        if (event.getType() != PeerManagerEvent.ET_PEER_ADDED) {
                            return;
                        }
                        final Peer peer = event.getPeer();
                        try {
                            String peer_ip = peer.getIp();
                            String network = AENetworkClassifier.categoriseAddress(peer_ip);
                            synchronized (peer_networks) {
                                peer_networks.add(network);
                                Map<String, Object> map = new HashMap<>();
                                peers_for_cache.add(map);
                                map.put("ip", peer_ip.getBytes("UTF-8"));
                                map.put("port", new Long(peer.getPort()));
                            }
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                        peer.addListener(new PeerListener2() {

                            @Override
                            public void eventOccurred(PeerEvent event) {
                                if (cancelled || completed || md_size > 0) {
                                    peer.removeListener(this);
                                    return;
                                }
                                if (event.getType() != PeerEvent.ET_STATE_CHANGED) {
                                    return;
                                }
                                if ((Integer) event.getData() != Peer.TRANSFERING) {
                                    return;
                                }
                                synchronized (pm_listener) {
                                    if (md_size > 0) {
                                        return;
                                    }
                                    md_size = pm.getTorrentInfoDictSize();
                                    if (md_size > 0) {
                                        peer_manager.removeListener(pm_listener);
                                    } else {
                                        return;
                                    }
                                }
                                listener.reportProgress(0, md_size);
                                new AEThread2("") {

                                    @Override
                                    public void run() {
                                        DiskManagerChannel channel = null;
                                        try {
                                            channel = download.getDiskManagerFileInfo()[0].createChannel();
                                            final DiskManagerRequest request = channel.createRequest();
                                            request.setType(DiskManagerRequest.REQUEST_READ);
                                            request.setOffset(0);
                                            request.setLength(md_size);
                                            request.setMaximumReadChunkSize(16 * 1024);
                                            request.addListener(new DiskManagerListener() {

                                                @Override
                                                public void eventOccurred(DiskManagerEvent event) {
                                                    int type = event.getType();
                                                    if (type == DiskManagerEvent.EVENT_TYPE_FAILED) {
                                                        error[0] = event.getFailure();
                                                        running_sem.releaseForever();
                                                    } else if (type == DiskManagerEvent.EVENT_TYPE_SUCCESS) {
                                                        PooledByteBuffer buffer = null;
                                                        try {
                                                            buffer = event.getBuffer();
                                                            byte[] bytes = buffer.toByteArray();
                                                            int dl_size;
                                                            synchronized (MagnetPluginMDDownloader.this) {
                                                                result.write(bytes);
                                                                dl_size = result.size();
                                                                if (dl_size == md_size) {
                                                                    completed = true;
                                                                    listener.reportProgress(md_size, md_size);
                                                                    running_sem.releaseForever();
                                                                }
                                                            }
                                                            if (!completed) {
                                                                listener.reportProgress(dl_size, md_size);
                                                            }
                                                        } catch (Throwable e) {
                                                            error[0] = e;
                                                            request.cancel();
                                                            running_sem.releaseForever();
                                                        } finally {
                                                            if (buffer != null) {
                                                                buffer.returnToPool();
                                                            }
                                                        }
                                                    } else if (type == DiskManagerEvent.EVENT_TYPE_BLOCKED) {
                                                    // System.out.println( "Waiting..." );
                                                    }
                                                }
                                            });
                                            synchronized (MagnetPluginMDDownloader.this) {
                                                if (cancelled) {
                                                    return;
                                                }
                                                requests.add(request);
                                            }
                                            request.run();
                                            synchronized (MagnetPluginMDDownloader.this) {
                                                requests.remove(request);
                                            }
                                        } catch (Throwable e) {
                                            error[0] = e;
                                            running_sem.releaseForever();
                                        } finally {
                                            if (channel != null) {
                                                channel.destroy();
                                            }
                                        }
                                    }
                                }.start();
                            }
                        });
                    }
                });
            }

            @Override
            public void peerManagerRemoved(Download download, PeerManager peer_manager) {
            }
        });
        final Download f_download = download;
        DownloadManagerListener dl_listener = new DownloadManagerListener() {

            private Object lock = this;

            private TimerEventPeriodic timer_event;

            private boolean removed;

            @Override
            public void downloadAdded(final Download download) {
                if (download == f_download) {
                    synchronized (lock) {
                        if (!removed) {
                            if (timer_event == null) {
                                timer_event = SimpleTimer.addPeriodicEvent("announcer", 30 * 1000, new TimerEventPerformer() {

                                    @Override
                                    public void perform(TimerEvent event) {
                                        synchronized (lock) {
                                            if (removed) {
                                                return;
                                            }
                                            if (running_sem.isReleasedForever()) {
                                                if (timer_event != null) {
                                                    timer_event.cancel();
                                                    timer_event = null;
                                                }
                                                return;
                                            }
                                        }
                                        download.requestTrackerAnnounce(true);
                                        injectPeers(download);
                                    }
                                });
                            }
                            if (peers_to_inject.size() > 0) {
                                SimpleTimer.addEvent("injecter", SystemTime.getOffsetTime(5 * 1000), new TimerEventPerformer() {

                                    @Override
                                    public void perform(TimerEvent event) {
                                        injectPeers(download);
                                    }
                                });
                            }
                        }
                    }
                }
            }

            private void injectPeers(Download download) {
                PeerManager pm = download.getPeerManager();
                if (pm != null) {
                    for (InetSocketAddress address : peers_to_inject) {
                        pm.addPeer(AddressUtils.getHostAddress(address), address.getPort());
                    }
                }
            }

            @Override
            public void downloadRemoved(Download dl) {
                if (dl == f_download) {
                    synchronized (lock) {
                        removed = true;
                        if (timer_event != null) {
                            timer_event.cancel();
                            timer_event = null;
                        }
                    }
                    if (!(cancelled || completed)) {
                        error[0] = new Exception("Download manually removed");
                        manually_removed[0] = true;
                        running_sem.releaseForever();
                    }
                }
            }
        };
        download_manager.addListener(dl_listener, true);
        try {
            download.moveTo(1);
            download.setForceStart(true);
            download.setFlag(Download.FLAG_DISABLE_AUTO_FILE_MOVE, true);
            running_sem.reserve();
        } finally {
            download_manager.removeListener(dl_listener);
        }
        if (completed) {
            byte[] bytes = result.toByteArray();
            Map info = BDecoder.decode(bytes);
            Map map = new HashMap();
            map.put("info", info);
            TOTorrent torrent = TOTorrentFactory.deserialiseFromMap(map);
            byte[] final_hash = torrent.getHash();
            if (!Arrays.equals(hash, final_hash)) {
                throw (new Exception("Metadata torrent hash mismatch: expected=" + ByteFormatter.encodeString(hash) + ", actual=" + ByteFormatter.encodeString(final_hash)));
            }
            if (url_sets != null) {
                // first entry should be the decentralised one that we want to remove now
                List<TOTorrentAnnounceURLSet> updated = new ArrayList<>();
                for (TOTorrentAnnounceURLSet set : url_sets) {
                    if (!TorrentUtils.isDecentralised(set.getAnnounceURLs()[0])) {
                        updated.add(set);
                    }
                }
                if (updated.size() == 0) {
                    url_sets = null;
                } else {
                    url_sets = updated.toArray(new TOTorrentAnnounceURLSet[updated.size()]);
                }
            }
            if (url_sets != null) {
                torrent.setAnnounceURL(url_sets[0].getAnnounceURLs()[0]);
                torrent.getAnnounceURLGroup().setAnnounceURLSets(url_sets);
            } else {
                torrent.setAnnounceURL(TorrentUtils.getDecentralisedURL(hash));
            }
            if (peers_for_cache.size() > 0) {
                Map<String, List<Map<String, Object>>> peer_cache = new HashMap<>();
                peer_cache.put("tracker_peers", peers_for_cache);
                TorrentUtils.setPeerCache(torrent, peer_cache);
            }
            try {
                String dn = magnet_args.get("dn");
                if (dn != null) {
                    PlatformTorrentUtils.setContentTitle(torrent, dn);
                }
                String pfi_str = magnet_args.get("pfi");
                if (pfi_str != null) {
                    PlatformTorrentUtils.setContentPrimaryFileIndex(torrent, Integer.parseInt(pfi_str));
                }
            } catch (Throwable e) {
            }
            listener.complete(torrent, peer_networks);
        } else {
            if (cancelled) {
                throw (new Exception("Download cancelled"));
            } else {
                cancelSupport(true);
                try {
                    if (error[0] != null) {
                        throw (error[0]);
                    } else {
                        throw (new Exception("Download terminated prematurely"));
                    }
                } catch (Throwable e) {
                    listener.failed(manually_removed[0], e);
                    Debug.out(e);
                    throw (e);
                }
            }
        }
    } catch (Throwable e) {
        boolean was_cancelled = cancelled;
        cancelSupport(true);
        if (!was_cancelled) {
            listener.failed(manually_removed[0], e);
            Debug.out(e);
        }
    } finally {
        try {
            if (download != null) {
                try {
                    download.stop();
                } catch (Throwable e) {
                }
                try {
                    download.remove();
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
            List<DiskManagerRequest> to_cancel;
            synchronized (this) {
                to_cancel = new ArrayList<>(requests);
                requests.clear();
            }
            for (DiskManagerRequest request : to_cancel) {
                request.cancel();
            }
            if (torrent_file != null) {
                torrent_file.delete();
            }
            if (data_file != null) {
                data_file.delete();
            }
            if (tmp_dir != null) {
                tmp_dir.delete();
            }
        } catch (Throwable e) {
            Debug.out(e);
        } finally {
            synchronized (active_set) {
                active_set.remove(hash_str);
            }
            complete_sem.releaseForever();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DownloadManager(com.biglybt.pif.download.DownloadManager) DiskManagerListener(com.biglybt.pif.disk.DiskManagerListener) DiskManagerChannel(com.biglybt.pif.disk.DiskManagerChannel) DownloadPeerListener(com.biglybt.pif.download.DownloadPeerListener) RandomAccessFile(java.io.RandomAccessFile) PEPeerManager(com.biglybt.core.peer.PEPeerManager) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) DownloadManagerListener(com.biglybt.pif.download.DownloadManagerListener) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) URL(java.net.URL) PooledByteBuffer(com.biglybt.pif.utils.PooledByteBuffer) Download(com.biglybt.pif.download.Download) DiskManagerRequest(com.biglybt.pif.disk.DiskManagerRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PEPeerManager(com.biglybt.core.peer.PEPeerManager) DiskManagerEvent(com.biglybt.pif.disk.DiskManagerEvent)

Aggregations

Download (com.biglybt.pif.download.Download)4 DownloadManager (com.biglybt.pif.download.DownloadManager)4 URL (java.net.URL)2 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)1 Engine (com.biglybt.core.metasearch.Engine)1 WebEngine (com.biglybt.core.metasearch.impl.web.WebEngine)1 PEPeerManager (com.biglybt.core.peer.PEPeerManager)1 PluginProxy (com.biglybt.core.proxy.AEProxyFactory.PluginProxy)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 PluginInterface (com.biglybt.pif.PluginInterface)1 DistributedDatabase (com.biglybt.pif.ddb.DistributedDatabase)1 DiskManagerChannel (com.biglybt.pif.disk.DiskManagerChannel)1 DiskManagerEvent (com.biglybt.pif.disk.DiskManagerEvent)1 DiskManagerListener (com.biglybt.pif.disk.DiskManagerListener)1 DiskManagerRequest (com.biglybt.pif.disk.DiskManagerRequest)1 DownloadManagerListener (com.biglybt.pif.download.DownloadManagerListener)1 DownloadPeerListener (com.biglybt.pif.download.DownloadPeerListener)1 DownloadWillBeAddedListener (com.biglybt.pif.download.DownloadWillBeAddedListener)1 Torrent (com.biglybt.pif.torrent.Torrent)1 PooledByteBuffer (com.biglybt.pif.utils.PooledByteBuffer)1