001package jmri.util.zeroconf;
002
003import java.util.prefs.BackingStoreException;
004import java.util.prefs.Preferences;
005import jmri.beans.PreferencesBean;
006import jmri.profile.Profile;
007import jmri.profile.ProfileUtils;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Preferences manager for ZeroConf networking.
013 * <p>
014 * <strong>NOTE:</strong> preferences are immediately changed and stored when
015 * set, although not all code that reads these preferences responds to changes
016 * in the preferences immediately.
017 * <p>
018 * <strong>NOTE:</strong> these preferences apply to all JMRI applications and
019 * all profiles on the computer on which they are set.
020 *
021 * @author Randall Wood (C) 2018
022 */
023public class ZeroConfPreferences extends PreferencesBean {
024
025    // Setting and default values
026    private boolean useIPv4 = true;
027    private boolean useIPv6 = true;
028    private boolean useLoopback = false;
029    private boolean useLinkLocal = true;
030
031    // API constants
032    /**
033     * Preferences name in profile.properties and property to subscribe to
034     * notification changes for.
035     * <p>
036     * {@value #USE_IP_V4}
037     */
038    public static final String USE_IP_V4 = "useIPv4";
039    /**
040     * Preferences name in profile.properties and property to subscribe to
041     * notification changes for.
042     * <p>
043     * {@value #USE_IP_V6}
044     */
045    public static final String USE_IP_V6 = "useIPv6";
046    /**
047     * Preferences name in profile.properties and property to subscribe to
048     * notification changes for.
049     * <p>
050     * {@value #USE_LOOPBACK}
051     */
052    public static final String USE_LOOPBACK = "useLoopback";
053    /**
054     * Preferences name in profile.properties and property to subscribe to
055     * notification changes for.
056     * <p>
057     * {@value #USE_LINK_LOCAL}
058     */
059    public static final String USE_LINK_LOCAL = "useLinkLocal";
060
061    private final static Logger log = LoggerFactory.getLogger(ZeroConfPreferences.class);
062
063    public ZeroConfPreferences(Profile profile) {
064        super(profile);
065        Preferences localPreferences = ProfileUtils.getPreferences(null, this.getClass(), false);
066
067        // search all-profile local for IPv4 and IPv6 use control
068        this.useIPv4 = localPreferences.getBoolean(USE_IP_V4, this.useIPv4);
069        this.useIPv6 = localPreferences.getBoolean(USE_IP_V6, this.useIPv6);
070
071        this.useLinkLocal = localPreferences.getBoolean(USE_LINK_LOCAL, this.useLinkLocal);
072        this.useLoopback = localPreferences.getBoolean(USE_LOOPBACK, this.useLoopback);
073    }
074
075    public boolean isUseIPv4() {
076        return useIPv4;
077    }
078
079    public void setUseIPv4(boolean useIPv4) {
080        boolean old = this.useIPv4;
081        this.useIPv4 = useIPv4;
082        savePreferences(getProfile());
083        firePropertyChange(USE_IP_V4, old, useIPv4);
084    }
085
086    public boolean isUseIPv6() {
087        return useIPv6;
088    }
089
090    public void setUseIPv6(boolean useIPv6) {
091        boolean old = this.useIPv6;
092        this.useIPv6 = useIPv6;
093        savePreferences(getProfile());
094        firePropertyChange(USE_IP_V6, old, useIPv6);
095    }
096
097    public boolean isUseLoopback() {
098        return useLoopback;
099    }
100
101    public void setUseLoopback(boolean useLoopback) {
102        boolean old = this.useLoopback;
103        this.useLoopback = useLoopback;
104        savePreferences(getProfile());
105        firePropertyChange(USE_LOOPBACK, old, useIPv6);
106    }
107
108    public boolean isUseLinkLocal() {
109        return useLinkLocal;
110    }
111
112    public void setUseLinkLocal(boolean useLinkLocal) {
113        boolean old = this.useLinkLocal;
114        this.useLinkLocal = useLinkLocal;
115        savePreferences(getProfile());
116        firePropertyChange(USE_LINK_LOCAL, old, useLinkLocal);
117    }
118
119    public void savePreferences(Profile profile) {
120        Preferences localPreferences = ProfileUtils.getPreferences(null, this.getClass(), false);
121        localPreferences.putBoolean(USE_IP_V4, useIPv4);
122        localPreferences.putBoolean(USE_IP_V6, useIPv6);
123        localPreferences.putBoolean(USE_LINK_LOCAL, useLinkLocal);
124        localPreferences.putBoolean(USE_LOOPBACK, useLoopback);
125        try {
126            localPreferences.sync();
127        } catch (BackingStoreException ex) {
128            log.error("Unable to save preferences", ex);
129        }
130    }
131
132}