Search in sources :

Example 51 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class ConfigurationChecker method loadProperties.

protected static void loadProperties(String dir) {
    try {
        File prop_file = new File(dir, "azureus.properties");
        if (prop_file.exists()) {
            Logger.log(new LogEvent(LOGID, "Loading properties file from " + prop_file.getAbsolutePath()));
            Properties props = new Properties();
            InputStream is = new FileInputStream(prop_file);
            try {
                props.load(is);
                Iterator it = props.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    String key = (String) entry.getKey();
                    String value = (String) entry.getValue();
                    Logger.log(new LogEvent(LOGID, "    " + key + "=" + value));
                    System.setProperty(key, value);
                }
            } finally {
                is.close();
            }
        }
    } catch (Throwable e) {
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Iterator(java.util.Iterator) Properties(java.util.Properties) File(java.io.File) Map(java.util.Map) FileInputStream(java.io.FileInputStream)

Example 52 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class ConfigurationChecker method setSystemProperties.

protected static void setSystemProperties() {
    try {
        class_mon.enter();
        if (system_properties_set) {
            return;
        }
        COConfigurationManager.preInitialise();
        // 
        String app_path = SystemProperties.getApplicationPath();
        String user_path = SystemProperties.getUserPath();
        loadProperties(app_path);
        if (!app_path.equals(user_path)) {
            loadProperties(user_path);
        }
        // kinda hard to do this system property setting early enough as we musn't load the
        // config until after checking the "pass to existing process" code and this loads the
        // class InetAddress that caches the current system prop
        COConfigurationManager.addAndFireParameterListener("IPV6 Prefer Addresses", new ParameterListener() {

            private boolean done_something = false;

            @Override
            public void parameterChanged(String name) {
                boolean prefer_ipv6 = COConfigurationManager.getBooleanParameter(name);
                boolean existing = !System.getProperty("java.net.preferIPv6Addresses", "false").equalsIgnoreCase("false");
                if (existing && !done_something) {
                    return;
                }
                if (existing != prefer_ipv6) {
                    done_something = true;
                    System.setProperty("java.net.preferIPv6Addresses", prefer_ipv6 ? "true" : "false");
                    try {
                        Field field = InetAddress.class.getDeclaredField("preferIPv6Address");
                        field.setAccessible(true);
                        field.setBoolean(null, prefer_ipv6);
                    } catch (Throwable e) {
                        Debug.out("Failed to update 'preferIPv6Address'", e);
                    }
                }
            }
        });
        if (Constants.isWindowsVistaOrHigher && Constants.isJava7OrHigher) {
            COConfigurationManager.addAndFireParameterListener("IPV4 Prefer Stack", new ParameterListener() {

                private boolean done_something = false;

                @Override
                public void parameterChanged(String name) {
                    boolean prefer_ipv4 = COConfigurationManager.getBooleanParameter(name);
                    boolean existing = !System.getProperty("java.net.preferIPv4Stack", "false").equalsIgnoreCase("false");
                    if (existing && !done_something) {
                        return;
                    }
                    if (existing != prefer_ipv4) {
                        done_something = true;
                        System.setProperty("java.net.preferIPv4Stack", prefer_ipv4 ? "true" : "false");
                        try {
                            Class<?> plainSocketImpl = getClass().forName("java.net.PlainSocketImpl");
                            Field pref_field = plainSocketImpl.getDeclaredField("preferIPv4Stack");
                            pref_field.setAccessible(true);
                            pref_field.setBoolean(null, prefer_ipv4);
                            Field dual_field = plainSocketImpl.getDeclaredField("useDualStackImpl");
                            dual_field.setAccessible(true);
                            dual_field.setBoolean(null, !prefer_ipv4);
                        } catch (Throwable e) {
                            Debug.out("Failed to update 'preferIPv4Stack'", e);
                        }
                    }
                }
            });
        }
        // socket connect/read timeouts
        int connect_timeout = COConfigurationManager.getIntParameter("Tracker Client Connect Timeout");
        int read_timeout = COConfigurationManager.getIntParameter("Tracker Client Read Timeout");
        if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "TrackerClient: connect timeout = " + connect_timeout + ", read timeout = " + read_timeout));
        System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(connect_timeout * 1000));
        System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(read_timeout * 1000));
        if (COConfigurationManager.getBooleanParameter("Enable.Proxy")) {
            String host = COConfigurationManager.getStringParameter("Proxy.Host");
            String port = COConfigurationManager.getStringParameter("Proxy.Port");
            String user = COConfigurationManager.getStringParameter("Proxy.Username");
            String pass = COConfigurationManager.getStringParameter("Proxy.Password");
            if (user.trim().equalsIgnoreCase("<none>")) {
                user = "";
            }
            if (COConfigurationManager.getBooleanParameter("Enable.SOCKS")) {
                System.setProperty("socksProxyHost", host);
                System.setProperty("socksProxyPort", port);
                if (user.length() > 0) {
                    System.setProperty("java.net.socks.username", user);
                    System.setProperty("java.net.socks.password", pass);
                }
            } else {
                System.setProperty("http.proxyHost", host);
                System.setProperty("http.proxyPort", port);
                System.setProperty("https.proxyHost", host);
                System.setProperty("https.proxyPort", port);
                if (user.length() > 0) {
                    System.setProperty("http.proxyUser", user);
                    System.setProperty("http.proxyPassword", pass);
                    System.setProperty("https.proxyUser", user);
                    System.setProperty("https.proxyPassword", pass);
                }
            }
        }
        SESecurityManager.initialise();
    } finally {
        class_mon.exit();
    }
}
Also used : Field(java.lang.reflect.Field) LogEvent(com.biglybt.core.logging.LogEvent) ParameterListener(com.biglybt.core.config.ParameterListener) InetAddress(java.net.InetAddress)

Example 53 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class ConfigurationChecker method checkConfiguration.

public static void checkConfiguration() {
    try {
        class_mon.enter();
        if (checked)
            return;
        checked = true;
        boolean changed = CustomizationManagerFactory.getSingleton().preInitialize();
        String last_version = COConfigurationManager.getStringParameter("azureus.version", "");
        String this_version = Constants.AZUREUS_VERSION;
        if (!last_version.equals(this_version)) {
            if (!Constants.getBaseVersion(last_version).equals(Constants.getBaseVersion())) {
                COConfigurationManager.setParameter("Last Version", last_version);
                new_version = true;
            }
            if (!COConfigurationManager.hasParameter("First Recorded Version", true)) {
                COConfigurationManager.setParameter("First Recorded Version", last_version.length() == 0 ? this_version : last_version);
            } else {
                String sFirstVersion = COConfigurationManager.getStringParameter("First Recorded Version");
                String sMinVersion = Constants.compareVersions(sFirstVersion, this_version) > 0 ? this_version : sFirstVersion;
                if (last_version.length() > 0) {
                    sMinVersion = Constants.compareVersions(sMinVersion, last_version) > 0 ? last_version : sMinVersion;
                }
                COConfigurationManager.setParameter("First Recorded Version", sMinVersion);
            }
            COConfigurationManager.setParameter("azureus.version", this_version);
            changed = true;
        }
        if (last_version.length() == 0) {
            // this is a virgin installation, i.e. first time running, called only once ever
            new_install = true;
            if (COConfigurationManager.doesParameterNonDefaultExist("diagnostics.tidy_close")) {
                if (!COConfigurationManager.doesParameterNonDefaultExist("Tracker Port Enable")) {
                    COConfigurationManager.setParameter("Tracker Port Enable", true);
                    changed = true;
                }
            }
            // enable Beginner user mode for first time
            if (!COConfigurationManager.doesParameterNonDefaultExist("User Mode")) {
                COConfigurationManager.setParameter("User Mode", 0);
                changed = true;
            }
            // make sure we set and save a random listen port
            if (!COConfigurationManager.doesParameterNonDefaultExist("TCP.Listen.Port")) {
                int rand_port = RandomUtils.generateRandomNetworkListenPort();
                COConfigurationManager.setParameter("TCP.Listen.Port", rand_port);
                COConfigurationManager.setParameter("UDP.Listen.Port", rand_port);
                COConfigurationManager.setParameter("UDP.NonData.Listen.Port", rand_port);
                changed = true;
            }
        } else {
            if (// disable safe selector mode enabled at some point in the past if we're on java 6 or higher and/or not on windows
            COConfigurationManager.getBooleanParameter("network.tcp.enable_safe_selector_mode") && !(Constants.isWindows && (Constants.JAVA_VERSION.startsWith("1.4") || Constants.JAVA_VERSION.startsWith("1.5")))) {
                COConfigurationManager.removeParameter("network.tcp.enable_safe_selector_mode");
                changed = true;
            }
            // transition from tracker-only port override to global port override
            if (COConfigurationManager.doesParameterNonDefaultExist("TCP.Announce.Port")) {
                COConfigurationManager.setParameter("TCP.Listen.Port.Override", COConfigurationManager.getStringParameter("TCP.Announce.Port", ""));
                COConfigurationManager.removeParameter("TCP.Announce.Port");
                changed = true;
            }
            // enable Advanced user mode for existing users by default, to ease 2304-->2306 migrations
            if (!COConfigurationManager.doesParameterNonDefaultExist("User Mode")) {
                COConfigurationManager.setParameter("User Mode", 2);
                changed = true;
            }
        }
        if (!COConfigurationManager.doesParameterNonDefaultExist("UDP.Listen.Port")) {
            COConfigurationManager.setParameter("UDP.Listen.Port", COConfigurationManager.getIntParameter("TCP.Listen.Port"));
            changed = true;
        }
        if (!COConfigurationManager.getBooleanParameter("Plugin.DHT.dht.portdefault", true)) {
            COConfigurationManager.removeParameter("Plugin.DHT.dht.portdefault");
            int tcp_port = COConfigurationManager.getIntParameter("TCP.Listen.Port");
            int udp_port = COConfigurationManager.getIntParameter("UDP.Listen.Port");
            int dht_port = COConfigurationManager.getIntParameter("Plugin.DHT.dht.port", udp_port);
            if (dht_port != udp_port) {
                if (tcp_port == udp_port) {
                    COConfigurationManager.setParameter("UDP.Listen.Port", dht_port);
                }
            }
            changed = true;
        }
        if (!COConfigurationManager.doesParameterNonDefaultExist("UDP.NonData.Listen.Port")) {
            COConfigurationManager.setParameter("UDP.NonData.Listen.Port", COConfigurationManager.getIntParameter("UDP.Listen.Port"));
            changed = true;
        }
        if (!COConfigurationManager.ENABLE_MULTIPLE_UDP_PORTS) {
            int udp1 = COConfigurationManager.getIntParameter("UDP.Listen.Port");
            int udp2 = COConfigurationManager.getIntParameter("UDP.NonData.Listen.Port");
            if (udp1 != udp2) {
                COConfigurationManager.setParameter("UDP.NonData.Listen.Port", udp1);
                changed = true;
            }
        }
        boolean randomize_ports = COConfigurationManager.getBooleanParameter("Listen.Port.Randomize.Enable");
        if (randomize_ports) {
            String random_range = COConfigurationManager.getStringParameter("Listen.Port.Randomize.Range");
            if (random_range == null || random_range.trim().length() == 0) {
                random_range = RandomUtils.LISTEN_PORT_MIN + "-" + RandomUtils.LISTEN_PORT_MAX;
            } else {
                random_range = random_range.trim();
            }
            int min_port = RandomUtils.LISTEN_PORT_MIN;
            int max_port = RandomUtils.LISTEN_PORT_MAX;
            String[] bits = random_range.split("-");
            boolean valid = bits.length == 2;
            if (!valid) {
                // try alternative split based on any non-numeric char (there are lots of unicode chars that
                // look like a - sign...)
                char[] chars = random_range.toCharArray();
                for (int i = 0; i < chars.length - 1; i++) {
                    if (!Character.isDigit(chars[i])) {
                        bits = new String[] { random_range.substring(0, i), random_range.substring(i + 1) };
                        valid = true;
                        break;
                    }
                }
            }
            if (valid) {
                String lhs = bits[0].trim();
                if (lhs.length() > 0) {
                    try {
                        min_port = Integer.parseInt(lhs);
                        valid = min_port > 0 && min_port < 65536;
                    } catch (Throwable e) {
                        valid = false;
                    }
                }
                String rhs = bits[1].trim();
                if (rhs.length() > 0) {
                    try {
                        max_port = Integer.parseInt(rhs);
                        valid = max_port > 0 && max_port < 65536;
                    } catch (Throwable e) {
                        valid = false;
                    }
                }
            }
            if (valid) {
                boolean randomize_together = COConfigurationManager.getBooleanParameter("Listen.Port.Randomize.Together");
                if (randomize_together) {
                    int port = RandomUtils.generateRandomNetworkListenPort(min_port, max_port);
                    COConfigurationManager.setParameter("TCP.Listen.Port", port);
                    COConfigurationManager.setParameter("UDP.Listen.Port", port);
                    COConfigurationManager.setParameter("UDP.NonData.Listen.Port", port);
                } else {
                    int old_udp1 = COConfigurationManager.getIntParameter("UDP.Listen.Port");
                    int old_udp2 = COConfigurationManager.getIntParameter("UDP.NonData.Listen.Port");
                    int port1 = RandomUtils.generateRandomNetworkListenPort(min_port, max_port);
                    COConfigurationManager.setParameter("TCP.Listen.Port", port1);
                    int port2 = RandomUtils.generateRandomNetworkListenPort(min_port, max_port);
                    COConfigurationManager.setParameter("UDP.Listen.Port", port2);
                    if (old_udp1 == old_udp2) {
                        COConfigurationManager.setParameter("UDP.NonData.Listen.Port", port2);
                    } else {
                        int port3 = RandomUtils.generateRandomNetworkListenPort(min_port, max_port);
                        COConfigurationManager.setParameter("UDP.NonData.Listen.Port", port3);
                    }
                }
            }
        }
        int tcp_port = COConfigurationManager.getIntParameter("TCP.Listen.Port");
        if (tcp_port == Constants.INSTANCE_PORT || (tcp_port >= 45100 && tcp_port <= 45103)) {
            int new_tcp_port = RandomUtils.generateRandomNetworkListenPort();
            COConfigurationManager.setParameter("TCP.Listen.Port", new_tcp_port);
            if (COConfigurationManager.getIntParameter("UDP.Listen.Port") == tcp_port) {
                COConfigurationManager.setParameter("UDP.Listen.Port", new_tcp_port);
            }
            if (COConfigurationManager.getIntParameter("UDP.NonData.Listen.Port") == tcp_port) {
                COConfigurationManager.setParameter("UDP.NonData.Listen.Port", new_tcp_port);
            }
            changed = true;
        }
        if (!COConfigurationManager.doesParameterDefaultExist("Tracker Key Enable Client")) {
            boolean old_value = COConfigurationManager.getBooleanParameter("Tracker Key Enable");
            COConfigurationManager.setParameter("Tracker Key Enable Client", old_value);
            COConfigurationManager.setParameter("Tracker Key Enable Server", old_value);
            changed = true;
        }
        int maxUpSpeed = COConfigurationManager.getIntParameter("Max Upload Speed KBs", 0);
        int maxDownSpeed = COConfigurationManager.getIntParameter("Max Download Speed KBs", 0);
        if (maxUpSpeed > 0 && maxUpSpeed < COConfigurationManager.CONFIG_DEFAULT_MIN_MAX_UPLOAD_SPEED && (maxDownSpeed == 0 || maxDownSpeed > (2 * maxUpSpeed))) {
            changed = true;
            COConfigurationManager.setParameter("Max Upload Speed KBs", COConfigurationManager.CONFIG_DEFAULT_MIN_MAX_UPLOAD_SPEED);
        }
        int peersRatio = COConfigurationManager.getIntParameter("Stop Peers Ratio", 0);
        if (peersRatio > 14) {
            COConfigurationManager.setParameter("Stop Peers Ratio", 14);
            changed = true;
        }
        int minQueueingShareRatio = COConfigurationManager.getIntParameter("StartStopManager_iFirstPriority_ShareRatio");
        if (minQueueingShareRatio < 500) {
            COConfigurationManager.setParameter("StartStopManager_iFirstPriority_ShareRatio", 500);
            changed = true;
        }
        int iSeedingMin = COConfigurationManager.getIntParameter("StartStopManager_iFirstPriority_SeedingMinutes");
        if (iSeedingMin < 90 && iSeedingMin != 0) {
            COConfigurationManager.setParameter("StartStopManager_iFirstPriority_SeedingMinutes", 90);
            changed = true;
        }
        int iDLMin = COConfigurationManager.getIntParameter("StartStopManager_iFirstPriority_DLMinutes");
        if (iDLMin < 60 * 3 && iDLMin != 0) {
            COConfigurationManager.setParameter("StartStopManager_iFirstPriority_DLMinutes", 60 * 3);
            changed = true;
        }
        int iIgnoreSPRatio = COConfigurationManager.getIntParameter("StartStopManager_iFirstPriority_ignoreSPRatio");
        if (iIgnoreSPRatio < 10 && iIgnoreSPRatio != 0) {
            COConfigurationManager.setParameter("StartStopManager_iFirstPriority_ignoreSPRatio", 10);
            changed = true;
        }
        String uniqueId = COConfigurationManager.getStringParameter("ID", null);
        if (uniqueId == null || uniqueId.length() != 20) {
            uniqueId = RandomUtils.generateRandomAlphanumerics(20);
            COConfigurationManager.setParameter("ID", uniqueId);
            changed = true;
        }
        int cache_max = COConfigurationManager.getIntParameter("diskmanager.perf.cache.size");
        if (cache_max > COConfigurationManager.CONFIG_CACHE_SIZE_MAX_MB) {
            COConfigurationManager.setParameter("diskmanager.perf.cache.size", COConfigurationManager.CONFIG_CACHE_SIZE_MAX_MB);
            changed = true;
        }
        if (cache_max < 1) {
            // oops
            COConfigurationManager.setParameter("diskmanager.perf.cache.size", 4);
            changed = true;
        }
        /**
         * Special Patch for OSX users
         */
        if (Constants.isOSX) {
            boolean sound = COConfigurationManager.getBooleanParameter("Play Download Finished", true);
            // Command + Q destroys the window, then notifies SWT, making it
            // hard to do a confirmation exit.
            boolean confirmExit = COConfigurationManager.getBooleanParameter("confirmationOnExit");
            if (sound || confirmExit) {
                COConfigurationManager.setParameter("Play Download Finished", false);
                COConfigurationManager.setParameter("confirmationOnExit", false);
                changed = true;
            }
        }
        if (Constants.isOSX) {
            if (COConfigurationManager.getBooleanParameter("enable_small_osx_fonts")) {
                System.setProperty("org.eclipse.swt.internal.carbon.smallFonts", "1");
            } else {
                System.getProperties().remove("org.eclipse.swt.internal.carbon.smallFonts");
            }
            System.setProperty("org.eclipse.swt.internal.carbon.noFocusRing", "1");
        }
        // remove a trailing slash, due to user manually entering the path in config
        String[] path_params = { "Default save path", "General_sDefaultTorrent_Directory", "Watch Torrent Folder Path", "Completed Files Directory" };
        for (int i = 0; i < path_params.length; i++) {
            if (path_params[i].endsWith(SystemProperties.SEP)) {
                String new_path = path_params[i].substring(0, path_params[i].length() - 1);
                COConfigurationManager.setParameter(path_params[i], new_path);
                changed = true;
            }
        }
        // cause display text problems, so let's delete them.
        if (ConfigurationManager.getInstance().doesParameterNonDefaultExist("General_bEnableLanguageUpdate")) {
            File user_dir = new File(SystemProperties.getUserPath());
            File[] files = user_dir.listFiles(new FilenameFilter() {

                @Override
                public boolean accept(File dir, String name) {
                    if (name.startsWith("MessagesBundle") && name.endsWith(".properties")) {
                        return true;
                    }
                    return false;
                }
            });
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    File file = files[i];
                    if (file.exists()) {
                        if (Logger.isEnabled())
                            Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "ConfigurationChecker:: removing old language file: " + file.getAbsolutePath()));
                        file.renameTo(new File(file.getParentFile(), "delme" + file.getName()));
                    }
                }
            }
            ConfigurationManager.getInstance().removeParameter("General_bEnableLanguageUpdate");
            changed = true;
        }
        // 4511: confirm.delete.content (boolean) changed to tb..confirm.delete.content (long)
        final String CFG_CONFIRM_DELETE_CONTENT = "confirm.delete.content";
        if (ConfigurationManager.getInstance().doesParameterNonDefaultExist(CFG_CONFIRM_DELETE_CONTENT)) {
            boolean confirm = COConfigurationManager.getBooleanParameter(CFG_CONFIRM_DELETE_CONTENT);
            if (!confirm && !ConfigurationManager.getInstance().doesParameterNonDefaultExist("tb.confirm.delete.content")) {
                COConfigurationManager.setParameter("tb.confirm.delete.content", 1);
            }
            COConfigurationManager.removeParameter(CFG_CONFIRM_DELETE_CONTENT);
            changed = true;
        }
        // 5601 propagate swt crash settings
        {
            if (System.getProperty(SystemProperties.SYSPROP_INTERNAL_BROWSER_DISABLE, "0").equals("1")) {
                if (!COConfigurationManager.getBooleanParameter("browser.internal.disable", false)) {
                    COConfigurationManager.setParameter("browser.internal.disable", true);
                    changed = true;
                }
            }
            if (COConfigurationManager.getBooleanParameter("azpromo.dump.disable.plugin", false)) {
                if (!COConfigurationManager.doesParameterNonDefaultExist("browser.internal.disable")) {
                    COConfigurationManager.setParameter("browser.internal.disable", true);
                    changed = true;
                }
            }
            COConfigurationManager.addAndFireParameterListener("browser.internal.disable", new ParameterListener() {

                @Override
                public void parameterChanged(String parameterName) {
                    if (COConfigurationManager.getBooleanParameter("browser.internal.disable", false)) {
                        COConfigurationManager.setParameter("azpromo.dump.disable.plugin", true);
                        COConfigurationManager.setDirty();
                    }
                }
            });
        }
        if (FeatureAvailability.isAutoSpeedDefaultClassic()) {
            // 1 == classic, 2 == beta
            ConfigurationDefaults.getInstance().addParameter(SpeedManagerImpl.CONFIG_VERSION, 1);
        }
        int check_level = COConfigurationManager.getIntParameter("config.checker.level", 0);
        if (check_level < 1) {
            COConfigurationManager.setParameter("config.checker.level", 1);
            changed = true;
            // initial setting of auto-config for upload slots etc
            String[] params = { "Max Uploads", "enable.seedingonly.maxuploads", "Max Uploads Seeding", "Max.Peer.Connections.Per.Torrent", "Max.Peer.Connections.Per.Torrent.When.Seeding.Enable", "Max.Peer.Connections.Per.Torrent.When.Seeding", "Max.Peer.Connections.Total", "Max Seeds Per Torrent" };
            boolean has_been_set = false;
            for (String param : params) {
                if (COConfigurationManager.doesParameterNonDefaultExist(param)) {
                    has_been_set = true;
                    break;
                }
            }
            if (has_been_set) {
                COConfigurationManager.setParameter("Auto Adjust Transfer Defaults", false);
            }
        }
        if (Constants.isOSX && check_level < 2) {
            // turn on piece-reorder mode for osx
            COConfigurationManager.setParameter("config.checker.level", 2);
            changed = true;
            if (!COConfigurationManager.getBooleanParameter("Zero New")) {
                COConfigurationManager.setParameter("Enable reorder storage mode", true);
            }
        }
        if (COConfigurationManager.doesParameterNonDefaultExist("Watch Torrent Folder Interval")) {
            long mins = COConfigurationManager.getIntParameter("Watch Torrent Folder Interval");
            COConfigurationManager.removeParameter("Watch Torrent Folder Interval");
            COConfigurationManager.setParameter("Watch Torrent Folder Interval Secs", 60 * mins);
            changed = true;
        }
        if (changed) {
            COConfigurationManager.save();
        }
        setupVerifier();
    } finally {
        class_mon.exit();
    }
    ConfigurationDefaults.getInstance().runVerifiers();
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) FilenameFilter(java.io.FilenameFilter) ParameterListener(com.biglybt.core.config.ParameterListener) File(java.io.File)

Example 54 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class RDResumeHandler method checkAllPieces.

public void checkAllPieces(boolean newfiles) {
    // long	start = System.currentTimeMillis();
    DiskManagerRecheckInstance recheck_inst = disk_manager.getRecheckScheduler().register(disk_manager, false);
    int overall_piece_size = disk_manager.getPieceLength();
    final AESemaphore run_sem = new AESemaphore("RDResumeHandler::checkAllPieces:runsem", overall_piece_size > 32 * 1024 * 1024 ? 1 : 2);
    final List<DiskManagerCheckRequest> failed_pieces = new ArrayList<>();
    try {
        boolean resume_data_complete = false;
        try {
            check_in_progress = true;
            boolean resumeEnabled = use_fast_resume;
            if (newfiles) {
                resumeEnabled = false;
            }
            final AESemaphore pending_checks_sem = new AESemaphore("RD:PendingChecks");
            int pending_check_num = 0;
            DiskManagerPiece[] pieces = disk_manager.getPieces();
            // calculate the current file sizes up front for performance reasons
            DiskManagerFileInfo[] files = disk_manager.getFiles();
            Map file_sizes = new HashMap();
            for (int i = 0; i < files.length; i++) {
                try {
                    Long len = new Long(((DiskManagerFileInfoImpl) files[i]).getCacheFile().getLength());
                    file_sizes.put(files[i], len);
                } catch (CacheFileManagerException e) {
                    Debug.printStackTrace(e);
                }
            }
            if (resumeEnabled) {
                boolean resumeValid = false;
                byte[] resume_pieces = null;
                Map partialPieces = null;
                Map resume_data = getResumeData();
                if (resume_data != null) {
                    try {
                        resume_pieces = (byte[]) resume_data.get("resume data");
                        if (resume_pieces != null) {
                            if (resume_pieces.length != pieces.length) {
                                Debug.out("Resume data array length mismatch: " + resume_pieces.length + "/" + pieces.length);
                                resume_pieces = null;
                            }
                        }
                        partialPieces = (Map) resume_data.get("blocks");
                        resumeValid = ((Long) resume_data.get("valid")).intValue() == 1;
                        if (isTorrentResumeDataComplete(disk_manager.getDownloadManager().getDownloadState(), resume_data)) {
                            resume_data_complete = true;
                        } else {
                            // set it so that if we crash the NOT_DONE pieces will be
                            // rechecked
                            resume_data.put("valid", new Long(0));
                            saveResumeData(resume_data);
                        }
                    } catch (Exception ignore) {
                    // ignore.printStackTrace();
                    }
                }
                if (resume_pieces == null) {
                    check_is_full_check = true;
                    resumeValid = false;
                    resume_pieces = new byte[pieces.length];
                    Arrays.fill(resume_pieces, PIECE_RECHECK_REQUIRED);
                }
                check_resume_was_valid = resumeValid;
                boolean recheck_all = use_fast_resume_recheck_all;
                if (!recheck_all) {
                    // override if not much left undone
                    long total_not_done = 0;
                    int piece_size = disk_manager.getPieceLength();
                    for (int i = 0; i < pieces.length; i++) {
                        if (resume_pieces[i] != PIECE_DONE) {
                            total_not_done += piece_size;
                        }
                    }
                    if (total_not_done < 64 * 1024 * 1024) {
                        recheck_all = true;
                    }
                }
                if (Logger.isEnabled()) {
                    int total_not_done = 0;
                    int total_done = 0;
                    int total_started = 0;
                    int total_recheck = 0;
                    for (int i = 0; i < pieces.length; i++) {
                        byte piece_state = resume_pieces[i];
                        if (piece_state == PIECE_NOT_DONE) {
                            total_not_done++;
                        } else if (piece_state == PIECE_DONE) {
                            total_done++;
                        } else if (piece_state == PIECE_STARTED) {
                            total_started++;
                        } else {
                            total_recheck++;
                        }
                    }
                    String str = "valid=" + resumeValid + ",not done=" + total_not_done + ",done=" + total_done + ",started=" + total_started + ",recheck=" + total_recheck + ",rc all=" + recheck_all + ",full=" + check_is_full_check;
                    Logger.log(new LogEvent(disk_manager, LOGID, str));
                }
                for (int i = 0; i < pieces.length; i++) {
                    check_position = i;
                    DiskManagerPiece dm_piece = pieces[i];
                    disk_manager.setPercentDone(((i + 1) * 1000) / disk_manager.getNbPieces());
                    boolean pieceCannotExist = false;
                    byte piece_state = resume_pieces[i];
                    if (piece_state == PIECE_DONE || !resumeValid || recheck_all) {
                        // at least check that file sizes are OK for this piece to be valid
                        DMPieceList list = disk_manager.getPieceList(i);
                        for (int j = 0; j < list.size(); j++) {
                            DMPieceMapEntry entry = list.get(j);
                            Long file_size = (Long) file_sizes.get(entry.getFile());
                            if (file_size == null) {
                                piece_state = PIECE_NOT_DONE;
                                pieceCannotExist = true;
                                if (Logger.isEnabled())
                                    Logger.log(new LogEvent(disk_manager, LOGID, LogEvent.LT_WARNING, "Piece #" + i + ": file is missing, " + "fails re-check."));
                                break;
                            }
                            long expected_size = entry.getOffset() + entry.getLength();
                            if (file_size.longValue() < expected_size) {
                                piece_state = PIECE_NOT_DONE;
                                pieceCannotExist = true;
                                if (Logger.isEnabled())
                                    Logger.log(new LogEvent(disk_manager, LOGID, LogEvent.LT_WARNING, "Piece #" + i + ": file is too small, fails re-check. File size = " + file_size + ", piece needs " + expected_size));
                                break;
                            }
                        }
                    }
                    if (piece_state == PIECE_DONE) {
                        dm_piece.setDone(true);
                    } else if (piece_state == PIECE_NOT_DONE && !recheck_all) {
                    // if the piece isn't done and we haven't been asked to recheck all pieces
                    // on restart (only started pieces) then just set as not done
                    } else {
                        // if the resume data is invalid or explicit recheck needed
                        if (pieceCannotExist) {
                            dm_piece.setDone(false);
                        } else if (piece_state == PIECE_RECHECK_REQUIRED || !resumeValid) {
                            run_sem.reserve();
                            while (!stopped) {
                                if (recheck_inst.getPermission()) {
                                    break;
                                }
                            }
                            if (stopped) {
                                break;
                            } else {
                                try {
                                    DiskManagerCheckRequest request = disk_manager.createCheckRequest(i, null);
                                    request.setLowPriority(true);
                                    checker.enqueueCheckRequest(request, new DiskManagerCheckRequestListener() {

                                        @Override
                                        public void checkCompleted(DiskManagerCheckRequest request, boolean passed) {
                                            if (TEST_RECHECK_FAILURE_HANDLING && (int) (Math.random() * 10) == 0) {
                                                disk_manager.getPiece(request.getPieceNumber()).setDone(false);
                                                passed = false;
                                            }
                                            if (!passed) {
                                                synchronized (failed_pieces) {
                                                    failed_pieces.add(request);
                                                }
                                            }
                                            complete();
                                        }

                                        @Override
                                        public void checkCancelled(DiskManagerCheckRequest request) {
                                            complete();
                                        }

                                        @Override
                                        public void checkFailed(DiskManagerCheckRequest request, Throwable cause) {
                                            complete();
                                        }

                                        protected void complete() {
                                            run_sem.release();
                                            pending_checks_sem.release();
                                        }
                                    });
                                    pending_check_num++;
                                } catch (Throwable e) {
                                    Debug.printStackTrace(e);
                                }
                            }
                        }
                    }
                }
                while (pending_check_num > 0) {
                    pending_checks_sem.reserve();
                    pending_check_num--;
                }
                if (partialPieces != null) {
                    Iterator iter = partialPieces.entrySet().iterator();
                    while (iter.hasNext()) {
                        Map.Entry key = (Map.Entry) iter.next();
                        int pieceNumber = Integer.parseInt((String) key.getKey());
                        DiskManagerPiece dm_piece = pieces[pieceNumber];
                        if (!dm_piece.isDone()) {
                            List blocks = (List) partialPieces.get(key.getKey());
                            Iterator iterBlock = blocks.iterator();
                            while (iterBlock.hasNext()) {
                                dm_piece.setWritten(((Long) iterBlock.next()).intValue());
                            }
                        }
                    }
                }
            } else {
                for (int i = 0; i < pieces.length; i++) {
                    check_position = i;
                    disk_manager.setPercentDone(((i + 1) * 1000) / disk_manager.getNbPieces());
                    boolean pieceCannotExist = false;
                    // check if there is an underlying file for this piece, if not set it to not done
                    DMPieceList list = disk_manager.getPieceList(i);
                    for (int j = 0; j < list.size(); j++) {
                        DMPieceMapEntry entry = list.get(j);
                        Long file_size = (Long) file_sizes.get(entry.getFile());
                        if (file_size == null) {
                            pieceCannotExist = true;
                            break;
                        }
                        long expected_size = entry.getOffset() + entry.getLength();
                        if (file_size.longValue() < expected_size) {
                            pieceCannotExist = true;
                            break;
                        }
                    }
                    if (pieceCannotExist) {
                        disk_manager.getPiece(i).setDone(false);
                        continue;
                    }
                    run_sem.reserve();
                    while (!stopped) {
                        if (recheck_inst.getPermission()) {
                            break;
                        }
                    }
                    if (stopped) {
                        break;
                    }
                    try {
                        DiskManagerCheckRequest request = disk_manager.createCheckRequest(i, null);
                        request.setLowPriority(true);
                        checker.enqueueCheckRequest(request, new DiskManagerCheckRequestListener() {

                            @Override
                            public void checkCompleted(DiskManagerCheckRequest request, boolean passed) {
                                if (TEST_RECHECK_FAILURE_HANDLING && (int) (Math.random() * 10) == 0) {
                                    disk_manager.getPiece(request.getPieceNumber()).setDone(false);
                                    passed = false;
                                }
                                if (!passed) {
                                    synchronized (failed_pieces) {
                                        failed_pieces.add(request);
                                    }
                                }
                                complete();
                            }

                            @Override
                            public void checkCancelled(DiskManagerCheckRequest request) {
                                complete();
                            }

                            @Override
                            public void checkFailed(DiskManagerCheckRequest request, Throwable cause) {
                                complete();
                            }

                            protected void complete() {
                                run_sem.release();
                                pending_checks_sem.release();
                            }
                        });
                        pending_check_num++;
                    } catch (Throwable e) {
                        Debug.printStackTrace(e);
                    }
                }
                while (pending_check_num > 0) {
                    pending_checks_sem.reserve();
                    pending_check_num--;
                }
            }
            if (failed_pieces.size() > 0 && !TEST_RECHECK_FAILURE_HANDLING) {
                byte[][] piece_hashes = disk_manager.getTorrent().getPieces();
                ByteArrayHashMap<Integer> hash_map = new ByteArrayHashMap<>();
                for (int i = 0; i < piece_hashes.length; i++) {
                    hash_map.put(piece_hashes[i], i);
                }
                for (DiskManagerCheckRequest request : failed_pieces) {
                    while (!stopped) {
                        if (recheck_inst.getPermission()) {
                            break;
                        }
                    }
                    if (stopped) {
                        break;
                    }
                    byte[] hash = request.getHash();
                    if (hash != null) {
                        final Integer target_index = hash_map.get(hash);
                        int current_index = request.getPieceNumber();
                        int piece_size = disk_manager.getPieceLength(current_index);
                        if (target_index != null && target_index != current_index && disk_manager.getPieceLength(target_index) == piece_size && !disk_manager.isDone(target_index)) {
                            final AESemaphore sem = new AESemaphore("PieceReorder");
                            disk_manager.enqueueReadRequest(disk_manager.createReadRequest(current_index, 0, piece_size), new DiskManagerReadRequestListener() {

                                @Override
                                public void readCompleted(DiskManagerReadRequest request, DirectByteBuffer data) {
                                    try {
                                        disk_manager.enqueueWriteRequest(disk_manager.createWriteRequest(target_index, 0, data, null), new DiskManagerWriteRequestListener() {

                                            @Override
                                            public void writeCompleted(DiskManagerWriteRequest request) {
                                                try {
                                                    DiskManagerCheckRequest check_request = disk_manager.createCheckRequest(target_index, null);
                                                    check_request.setLowPriority(true);
                                                    checker.enqueueCheckRequest(check_request, new DiskManagerCheckRequestListener() {

                                                        @Override
                                                        public void checkCompleted(DiskManagerCheckRequest request, boolean passed) {
                                                            sem.release();
                                                        }

                                                        @Override
                                                        public void checkCancelled(DiskManagerCheckRequest request) {
                                                            sem.release();
                                                        }

                                                        @Override
                                                        public void checkFailed(DiskManagerCheckRequest request, Throwable cause) {
                                                            sem.release();
                                                        }
                                                    });
                                                } catch (Throwable e) {
                                                    sem.release();
                                                }
                                            }

                                            @Override
                                            public void writeFailed(DiskManagerWriteRequest request, Throwable cause) {
                                                sem.release();
                                            }
                                        });
                                    } catch (Throwable e) {
                                        sem.release();
                                    }
                                }

                                @Override
                                public void readFailed(DiskManagerReadRequest request, Throwable cause) {
                                    sem.release();
                                }

                                @Override
                                public int getPriority() {
                                    return (-1);
                                }

                                @Override
                                public void requestExecuted(long bytes) {
                                }
                            });
                            sem.reserve();
                        }
                    }
                }
            }
        } finally {
            check_in_progress = false;
        }
        if (!(stopped || resume_data_complete)) {
            try {
                saveResumeData(true);
            } catch (Exception e) {
                Debug.out("Failed to dump initial resume data to disk");
                Debug.printStackTrace(e);
            }
        }
    } catch (Throwable e) {
        // if something went wrong then log and continue.
        Debug.printStackTrace(e);
    } finally {
        recheck_inst.unregister();
    // System.out.println( "Check of '" + disk_manager.getDownloadManager().getDisplayName() + "' completed in " + (System.currentTimeMillis() - start));
    }
}
Also used : ByteArrayHashMap(com.biglybt.core.util.ByteArrayHashMap) AESemaphore(com.biglybt.core.util.AESemaphore) DiskManagerFileInfoImpl(com.biglybt.core.disk.impl.DiskManagerFileInfoImpl) ByteArrayHashMap(com.biglybt.core.util.ByteArrayHashMap) DMPieceMapEntry(com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry) DMPieceList(com.biglybt.core.disk.impl.piecemapper.DMPieceList) LogEvent(com.biglybt.core.logging.LogEvent) CacheFileManagerException(com.biglybt.core.diskmanager.cache.CacheFileManagerException) DiskManagerRecheckInstance(com.biglybt.core.disk.impl.DiskManagerRecheckInstance) CacheFileManagerException(com.biglybt.core.diskmanager.cache.CacheFileManagerException) DMPieceList(com.biglybt.core.disk.impl.piecemapper.DMPieceList) DMPieceMapEntry(com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry) ByteArrayHashMap(com.biglybt.core.util.ByteArrayHashMap) DirectByteBuffer(com.biglybt.core.util.DirectByteBuffer)

Example 55 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class CacheFileWithCache method writeCache.

protected void writeCache(DirectByteBuffer file_buffer, long file_position, boolean buffer_handed_over) throws CacheFileManagerException {
    checkPendingException();
    boolean buffer_cached = false;
    boolean failed = false;
    try {
        int file_buffer_position = file_buffer.position(SS_CACHE);
        int file_buffer_limit = file_buffer.limit(SS_CACHE);
        int write_length = file_buffer_limit - file_buffer_position;
        if (write_length == 0) {
            // nothing to do
            return;
        }
        if (AEDiagnostics.CHECK_DUMMY_FILE_DATA) {
            long temp_position = file_position + file_offset_in_torrent;
            while (file_buffer.hasRemaining(SS_CACHE)) {
                byte v = file_buffer.get(SS_CACHE);
                if ((byte) temp_position != v) {
                    System.out.println("writeCache: write is bad at " + temp_position + ": expected = " + (byte) temp_position + ", actual = " + v);
                    break;
                }
                temp_position++;
            }
            file_buffer.position(SS_CACHE, file_buffer_position);
        }
        if (manager.isWriteCacheEnabled()) {
            if (TRACE)
                Logger.log(new LogEvent(torrent, LOGID, "writeCache: " + getName() + ", " + file_position + " - " + (file_position + write_length - 1) + ":" + file_buffer_position + "/" + file_buffer_limit));
            if ((!buffer_handed_over) && write_length < piece_size) {
                if (TRACE)
                    Logger.log(new LogEvent(torrent, LOGID, "    making copy of non-handedover buffer"));
                DirectByteBuffer cache_buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_CACHE_WRITE, write_length);
                cache_buffer.put(SS_CACHE, file_buffer);
                cache_buffer.position(SS_CACHE, 0);
                // make it look like this buffer has been handed over
                file_buffer = cache_buffer;
                file_buffer_position = 0;
                file_buffer_limit = write_length;
                buffer_handed_over = true;
            }
            if (buffer_handed_over) {
                // cache this write, allocate outside sync block (see manager for details)
                CacheEntry entry = manager.allocateCacheSpace(CacheEntry.CT_DATA_WRITE, this, file_buffer, file_position, write_length);
                try {
                    this_mon.enter();
                    if (access_mode != CF_WRITE) {
                        throw (new CacheFileManagerException(this, "Write failed - cache file is read only"));
                    }
                    // if we are overwriting stuff already in the cache then force-write overlapped
                    // data (easiest solution as this should only occur on hash-fails)
                    // do the flush and add sychronized to avoid possibility of another
                    // thread getting in-between and adding same block thus causing mutiple entries
                    // for same space
                    flushCache(file_position, write_length, true, -1, 0, -1);
                    cache.add(entry);
                    manager.addCacheSpace(entry);
                } finally {
                    this_mon.exit();
                }
                manager.cacheBytesWritten(write_length);
                bytes_written += write_length;
                buffer_cached = true;
            } else {
                try {
                    this_mon.enter();
                    flushCache(file_position, write_length, true, -1, 0, -1);
                    getFMFile().write(file_buffer, file_position);
                } finally {
                    this_mon.exit();
                }
                manager.fileBytesWritten(write_length);
                bytes_written += write_length;
            }
        } else {
            getFMFile().write(file_buffer, file_position);
            manager.fileBytesWritten(write_length);
            bytes_written += write_length;
        }
    } catch (CacheFileManagerException e) {
        failed = true;
        throw (e);
    } catch (FMFileManagerException e) {
        failed = true;
        manager.rethrow(this, e);
    } finally {
        if (buffer_handed_over) {
            if (!(failed || buffer_cached)) {
                file_buffer.returnToPool();
            }
        }
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) LogEvent(com.biglybt.core.logging.LogEvent) CacheFileManagerException(com.biglybt.core.diskmanager.cache.CacheFileManagerException)

Aggregations

LogEvent (com.biglybt.core.logging.LogEvent)172 LogAlert (com.biglybt.core.logging.LogAlert)20 IOException (java.io.IOException)14 File (java.io.File)11 URL (java.net.URL)11 ArrayList (java.util.ArrayList)9 InetSocketAddress (java.net.InetSocketAddress)8 InputStream (java.io.InputStream)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 ZipInputStream (java.util.zip.ZipInputStream)7 CacheFileManagerException (com.biglybt.core.diskmanager.cache.CacheFileManagerException)6 TOTorrent (com.biglybt.core.torrent.TOTorrent)6 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)6 ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)6 UIFunctions (com.biglybt.ui.UIFunctions)6 SocketChannel (java.nio.channels.SocketChannel)6 Iterator (java.util.Iterator)6 ConnectionEndpoint (com.biglybt.core.networkmanager.ConnectionEndpoint)5 ClientIDException (com.biglybt.pif.clientid.ClientIDException)5 ParameterListener (com.biglybt.core.config.ParameterListener)4