Search in sources :

Example 1 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class PlatformManagerUpdateChecker method checkForUpdate.

@Override
public void checkForUpdate(final UpdateChecker checker) {
    try {
        SFPluginDetails sf_details = SFPluginDetailsLoaderFactory.getSingleton().getPluginDetails(plugin_interface.getPluginID());
        String current_version = plugin_interface.getPluginVersion();
        if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "PlatformManager:OSX update check starts: current = " + current_version));
        boolean current_az_is_cvs = Constants.isCVSVersion();
        String sf_plugin_version = sf_details.getVersion();
        String sf_comp_version = sf_plugin_version;
        if (current_az_is_cvs) {
            String sf_cvs_version = sf_details.getCVSVersion();
            if (sf_cvs_version.length() > 0) {
                // sf cvs version ALWAYS entry in _CVS
                sf_plugin_version = sf_cvs_version;
                sf_comp_version = sf_plugin_version.substring(0, sf_plugin_version.length() - 4);
            }
        }
        String target_version = null;
        if (sf_comp_version.length() == 0 || !Character.isDigit(sf_comp_version.charAt(0))) {
            if (Logger.isEnabled())
                Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "PlatformManager:OSX no valid version to check against (" + sf_comp_version + ")"));
        } else if (Constants.compareVersions(current_version, sf_comp_version) < 0) {
            target_version = sf_comp_version;
        }
        checker.reportProgress("OSX: current = " + current_version + ", latest = " + sf_comp_version);
        if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "PlatformManager:OSX update required = " + (target_version != null)));
        if (target_version != null) {
            String target_download = sf_details.getDownloadURL();
            if (current_az_is_cvs) {
                String sf_cvs_version = sf_details.getCVSVersion();
                if (sf_cvs_version.length() > 0) {
                    target_download = sf_details.getCVSDownloadURL();
                }
            }
            ResourceDownloaderFactory rdf = ResourceDownloaderFactoryImpl.getSingleton();
            ResourceDownloader direct_rdl = rdf.create(new URL(target_download));
            String torrent_download = Constants.URL_PLUGINS_TORRENT_BASE;
            int slash_pos = target_download.lastIndexOf("/");
            if (slash_pos == -1) {
                torrent_download += target_download;
            } else {
                torrent_download += target_download.substring(slash_pos + 1);
            }
            torrent_download += ".torrent";
            if (I2PHelpers.isI2PInstalled()) {
                torrent_download += "?i2p=1";
            }
            ResourceDownloader torrent_rdl = rdf.create(new URL(torrent_download));
            torrent_rdl = rdf.getSuffixBasedDownloader(torrent_rdl);
            // create an alternate downloader with torrent attempt first
            ResourceDownloader alternate_rdl = rdf.getAlternateDownloader(new ResourceDownloader[] { torrent_rdl, direct_rdl });
            // get size here so it is cached
            rdf.getTimeoutDownloader(rdf.getRetryDownloader(alternate_rdl, RD_SIZE_RETRIES), RD_SIZE_TIMEOUT).getSize();
            List update_desc = new ArrayList();
            List desc_lines = HTMLUtils.convertHTMLToText("", sf_details.getDescription());
            update_desc.addAll(desc_lines);
            List comment_lines = HTMLUtils.convertHTMLToText("    ", sf_details.getComment());
            update_desc.addAll(comment_lines);
            String[] update_d = new String[update_desc.size()];
            update_desc.toArray(update_d);
            final Update update = checker.addUpdate(UPDATE_NAME, update_d, current_version, target_version, alternate_rdl, Update.RESTART_REQUIRED_YES);
            update.setDescriptionURL(sf_details.getInfoURL());
            alternate_rdl.addListener(new ResourceDownloaderAdapter() {

                @Override
                public boolean completed(final ResourceDownloader downloader, InputStream data) {
                    installUpdate(checker, update, downloader, data);
                    return (true);
                }

                @Override
                public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
                    Debug.out(downloader.getName() + " failed", e);
                    update.complete(false);
                }
            });
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        checker.reportProgress("Failed to load plugin details for the platform manager: " + Debug.getNestedExceptionMessage(e));
        checker.failed();
    } finally {
        checker.completed();
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ArrayList(java.util.ArrayList) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) Update(com.biglybt.pif.update.Update) URL(java.net.URL) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) ArrayList(java.util.ArrayList) List(java.util.List) SFPluginDetails(com.biglybt.pifimpl.update.sf.SFPluginDetails)

Example 2 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class IpFilterAutoLoaderImpl method downloadFiltersAsync.

/**
 * @param url
 *
 * @since 3.0.1.5
 */
void downloadFiltersAsync(URL url) {
    ResourceDownloader rd = ResourceDownloaderFactoryImpl.getSingleton().create(url);
    // old dl exists, load old one while new one downloads async
    rd.addListener(new ResourceDownloaderAdapter() {

        // @see com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter#reportPercentComplete(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader, int)
        @Override
        public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
        }

        @Override
        public boolean completed(ResourceDownloader downloader, InputStream data) {
            try {
                setNextAutoDownload(true);
                Logger.log(new LogEvent(LOGID, "downloaded..waiting"));
                // since this is a different thread, we can use class_mon as
                // a cheap semaphore to wait until previous load completes
                class_mon.enter();
                Logger.log(new LogEvent(LOGID, "downloaded.. copying"));
                try {
                    FileUtil.copyFile(data, FileUtil.getUserFile("ipfilter.dl"));
                    AEThread thread = new AEThread("reload ipfilters", true) {

                        @Override
                        public void runSupport() {
                            try {
                                UIFunctions uif = UIFunctionsManager.getUIFunctions();
                                if (uif != null) {
                                    uif.setStatusText("reloading.filters");
                                }
                                ipFilter.reload(false);
                                if (uif != null) {
                                    uif.setStatusText(null);
                                }
                            } catch (Exception e) {
                                Debug.out(e);
                            }
                        }
                    };
                    thread.setPriority(Thread.NORM_PRIORITY - 1);
                    thread.start();
                } catch (Exception e) {
                    Debug.out(e);
                }
            } finally {
                class_mon.exit();
            }
            return true;
        }
    });
    rd.asyncDownload();
}
Also used : ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) LogEvent(com.biglybt.core.logging.LogEvent) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) UIFunctions(com.biglybt.ui.UIFunctions) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) MalformedURLException(java.net.MalformedURLException) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException)

Example 3 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class PlayerInstaller method install.

public boolean install() {
    try {
        installer = CoreFactory.getSingleton().getPluginManager().getPluginInstaller();
        StandardPlugin sp = installer.getStandardPlugin("azemp");
        Map<Integer, Object> properties = new HashMap<>();
        properties.put(UpdateCheckInstance.PT_UI_STYLE, UpdateCheckInstance.PT_UI_STYLE_NONE);
        properties.put(UpdateCheckInstance.PT_UI_DISABLE_ON_SUCCESS_SLIDEY, true);
        final AESemaphore sem = new AESemaphore("emp install");
        final boolean[] result = new boolean[1];
        instance = installer.install(new InstallablePlugin[] { sp }, false, properties, new PluginInstallationListener() {

            @Override
            public void completed() {
                result[0] = true;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }

            @Override
            public void cancelled() {
                result[0] = false;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }

            @Override
            public void failed(PluginException e) {
                result[0] = false;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }
        });
        boolean kill_it;
        synchronized (this) {
            kill_it = cancelled;
        }
        if (kill_it) {
            instance.cancel();
            return (false);
        }
        instance.addListener(new UpdateCheckInstanceListener() {

            @Override
            public void cancelled(UpdateCheckInstance instance) {
            }

            @Override
            public void complete(UpdateCheckInstance instance) {
                Update[] updates = instance.getUpdates();
                for (final Update update : updates) {
                    ResourceDownloader[] rds = update.getDownloaders();
                    for (ResourceDownloader rd : rds) {
                        rd.addListener(new ResourceDownloaderAdapter() {

                            @Override
                            public void reportActivity(ResourceDownloader downloader, String activity) {
                            }

                            @Override
                            public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
                                if (listener != null) {
                                    listener.progress(percentage);
                                }
                            }
                        });
                    }
                }
            }
        });
        sem.reserve();
        return result[0];
    } catch (Throwable e) {
    }
    return false;
}
Also used : UpdateCheckInstance(com.biglybt.pif.update.UpdateCheckInstance) UpdateCheckInstanceListener(com.biglybt.pif.update.UpdateCheckInstanceListener) HashMap(java.util.HashMap) PluginException(com.biglybt.pif.PluginException) StandardPlugin(com.biglybt.pif.installer.StandardPlugin) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) AESemaphore(com.biglybt.core.util.AESemaphore) Update(com.biglybt.pif.update.Update) InstallablePlugin(com.biglybt.pif.installer.InstallablePlugin) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) PluginInstallationListener(com.biglybt.pif.installer.PluginInstallationListener)

Example 4 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class ImageBytesDownloader method loadImage.

public static void loadImage(final String url, final ImageDownloaderListener l) {
    // System.out.println("download " + url);
    mon_map.enter();
    try {
        List<ImageDownloaderListener> list = map.get(url);
        if (list != null) {
            list.add(l);
            return;
        }
        list = new ArrayList<>(1);
        list.add(l);
        map.put(url, list);
    } finally {
        mon_map.exit();
    }
    try {
        URL u = new URL(url);
        ResourceDownloader rd;
        if (AENetworkClassifier.categoriseAddress(u.getHost()) == AENetworkClassifier.AT_PUBLIC) {
            rd = ResourceDownloaderFactoryImpl.getSingleton().create(u);
        } else {
            rd = ResourceDownloaderFactoryImpl.getSingleton().createWithAutoPluginProxy(u);
        }
        rd.addListener(new ResourceDownloaderAdapter() {

            @Override
            public boolean completed(ResourceDownloader downloader, InputStream is) {
                mon_map.enter();
                try {
                    List<ImageDownloaderListener> list = map.get(url);
                    if (list != null) {
                        try {
                            if (is != null && is.available() > 0) {
                                byte[] newImageBytes = new byte[is.available()];
                                is.read(newImageBytes);
                                for (ImageDownloaderListener l : list) {
                                    try {
                                        l.imageDownloaded(newImageBytes);
                                    } catch (Exception e) {
                                        Debug.out(e);
                                    }
                                }
                            }
                        } catch (Exception e) {
                            Debug.out(e);
                        }
                    }
                    map.remove(url);
                } finally {
                    mon_map.exit();
                }
                return false;
            }
        });
        rd.asyncDownload();
    } catch (Exception e) {
        Debug.out(url, e);
    }
}
Also used : ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) InputStream(java.io.InputStream) List(java.util.List) ArrayList(java.util.ArrayList) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) URL(java.net.URL)

Example 5 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class MagnetPlugin method doSecondaryLookup.

protected void doSecondaryLookup(final MagnetPluginProgressListener listener, final Object[] result, byte[] hash, Set<String> networks_enabled, String args) {
    if (listener != null) {
        listener.reportActivity(getMessageText("report.secondarylookup", null));
    }
    PluginProxy plugin_proxy = null;
    try {
        URL original_sl_url = new URL(SECONDARY_LOOKUP + "magnetLookup?hash=" + Base32.encode(hash) + (args.length() == 0 ? "" : ("&args=" + UrlUtils.encode(args))));
        URL sl_url = original_sl_url;
        Proxy proxy = null;
        if (!networks_enabled.contains(AENetworkClassifier.AT_PUBLIC)) {
            plugin_proxy = AEProxyFactory.getPluginProxy("secondary magnet lookup", sl_url);
            if (plugin_proxy == null) {
                throw (new NoRouteToHostException("plugin proxy unavailable"));
            } else {
                proxy = plugin_proxy.getProxy();
                sl_url = plugin_proxy.getURL();
            }
        }
        ResourceDownloaderFactory rdf = plugin_interface.getUtilities().getResourceDownloaderFactory();
        ResourceDownloader rd;
        if (proxy == null) {
            rd = rdf.create(sl_url);
        } else {
            rd = rdf.create(sl_url, proxy);
            rd.setProperty("URL_HOST", original_sl_url.getHost());
        }
        final PluginProxy f_pp = plugin_proxy;
        rd.addListener(new ResourceDownloaderAdapter() {

            @Override
            public boolean completed(ResourceDownloader downloader, InputStream data) {
                try {
                    if (listener != null) {
                        listener.reportActivity(getMessageText("report.secondarylookup.ok", null));
                    }
                    synchronized (result) {
                        result[0] = data;
                    }
                    return (true);
                } finally {
                    complete();
                }
            }

            @Override
            public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
                try {
                    synchronized (result) {
                        result[0] = e;
                    }
                    if (listener != null) {
                        listener.reportActivity(getMessageText("report.secondarylookup.fail"));
                    }
                } finally {
                    complete();
                }
            }

            private void complete() {
                if (f_pp != null) {
                    // outcome doesn't really indicate whether the result was wholesome
                    f_pp.setOK(true);
                }
            }
        });
        rd.asyncDownload();
    } catch (Throwable e) {
        if (plugin_proxy != null) {
            // tidy up, no indication of proxy badness here so say its ok
            plugin_proxy.setOK(true);
        }
        if (listener != null) {
            listener.reportActivity(getMessageText("report.secondarylookup.fail", Debug.getNestedExceptionMessage(e)));
        }
    }
}
Also used : Proxy(java.net.Proxy) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) InputStream(java.io.InputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) NoRouteToHostException(java.net.NoRouteToHostException) URL(java.net.URL)

Aggregations

ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)12 ResourceDownloaderAdapter (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter)12 Update (com.biglybt.pif.update.Update)7 ResourceDownloaderException (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException)7 InputStream (java.io.InputStream)6 URL (java.net.URL)5 ZipInputStream (java.util.zip.ZipInputStream)5 ResourceDownloaderFactory (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory)4 ArrayList (java.util.ArrayList)4 LogEvent (com.biglybt.core.logging.LogEvent)3 AESemaphore (com.biglybt.core.util.AESemaphore)3 PluginException (com.biglybt.pif.PluginException)3 InstallablePlugin (com.biglybt.pif.installer.InstallablePlugin)3 PluginInstallationListener (com.biglybt.pif.installer.PluginInstallationListener)3 StandardPlugin (com.biglybt.pif.installer.StandardPlugin)3 UpdateCheckInstance (com.biglybt.pif.update.UpdateCheckInstance)3 UpdateCheckInstanceListener (com.biglybt.pif.update.UpdateCheckInstanceListener)3 SFPluginDetails (com.biglybt.pifimpl.update.sf.SFPluginDetails)3 File (java.io.File)3 HashMap (java.util.HashMap)3