basicnetworkstatus.java
来自「world wind java sdk 源码」· Java 代码 · 共 423 行 · 第 1/2 页
JAVA
423 行
/*Copyright (C) 2001, 2006 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.util;import gov.nasa.worldwind.*;import gov.nasa.worldwind.avlist.*;import java.io.*;import java.net.*;import java.util.concurrent.*;import java.util.concurrent.atomic.*;/** * Provides tracking of per-host network availability. Host computers that fail network requests can be logged to this * class' tracking list. When a host has been logged a specified number of times, it is marked as unreachable. Users can * query instances of this class to determine whether a host has been marked as unreachable. * <p/> * Users are expected to invoke this class' {@link #logUnavailableHost(java.net.URL)} method when an attempt to contact * a host fails. Each invocation increments the failure count by one. When the count exceeds the attempt limit, the host * is marked as unreachable. When attempts to contact the host <em>are</em> successful, users should invoke this class' * {@link #logAvailableHost(java.net.URL)} method to clear its status. * <p/> * A host may become reachable at a time subsequent to its being logged. To detect this, this class will mark a host as * not unreachable after a specifiable interval of time. If the host is once more logged as unavailable, its entry will * return to the unavailable state. This cycle continues indefinitely. * <p/> * Methods are also provided to determine whether the public network can be reached and whether the NASA World Wind * servers cab be reached. * * @author tag * @version $Id: BasicNetworkStatus.java 9224 2009-03-06 03:15:10Z tgaskins $ */public class BasicNetworkStatus extends AVListImpl implements NetworkStatus{ private static final long DEFAULT_TRY_AGAIN_INTERVAL = (long) 60e3; // seconds private static final int DEFAULT_ATTEMPT_LIMIT = 10; // number of unavailable events to declare host unavailable private static final String[] networkTestSites = new String[] {"www.nasa.gov", "worldwind.arc.nasa.gov", "google.com", "microsoft.com", "yahoo.com"}; private static final long NETWORK_STATUS_REPORT_INTERVAL = (long) 60e3; private static class HostInfo { private final long tryAgainInterval; private final int attemptLimit; private AtomicInteger logCount = new AtomicInteger(); private AtomicLong lastLogTime = new AtomicLong(); private HostInfo(int attemptLimit, long tryAgainInterval) { this.lastLogTime.set(System.currentTimeMillis()); this.logCount.set(1); this.tryAgainInterval = tryAgainInterval; this.attemptLimit = attemptLimit; } private boolean isUnavailable() { return this.logCount.get() >= this.attemptLimit; } private boolean isTimeToTryAgain() { return System.currentTimeMillis() - this.lastLogTime.get() >= this.tryAgainInterval; } } private ConcurrentHashMap<String, HostInfo> hostMap = new ConcurrentHashMap<String, HostInfo>(); private AtomicLong tryAgainInterval = new AtomicLong(DEFAULT_TRY_AGAIN_INTERVAL); private AtomicInteger attemptLimit = new AtomicInteger(DEFAULT_ATTEMPT_LIMIT); // Fields for determining overall network status. private boolean offlineMode; private AtomicLong lastUnavailableLogTime = new AtomicLong(System.currentTimeMillis()); private AtomicLong lastAvailableLogTime = new AtomicLong(System.currentTimeMillis() + 1); private AtomicLong lastNetworkCheckTime = new AtomicLong(System.currentTimeMillis()); private AtomicLong lastNetworkStatusReportTime = new AtomicLong(0); private AtomicBoolean lastNetworkUnavailableResult = new AtomicBoolean(false); public BasicNetworkStatus() { String oms = Configuration.getStringValue(AVKey.OFFLINE_MODE, "false"); this.offlineMode = oms.startsWith("t") || oms.startsWith("T"); } /** * Indicates whether World Wind will attempt to connect to the network to retrieve data or for other reasons. * * @return <code>true</code> if World Wind is in off-line mode, <code>false</code> if not. */ public boolean isOfflineMode() { return offlineMode; } /** * Indicate whether World Wind should attempt to connect to the network to retrieve data or for other reasons. * The default value for this attribute is <code>false</code>, indicating that the network should be used. * * @param offlineMode <code>true</code> if World Wind should use the network, <code>false</code> otherwise */ public void setOfflineMode(boolean offlineMode) { this.offlineMode = offlineMode; } /** * Set the number of times a host must be logged as unavailable before it is marked unavailable in this class. * * @param limit the number of log-unavailability invocations necessary to consider the host unreachable. * @throws IllegalArgumentException if the limit is less than 1. */ public void setAttemptLimit(int limit) { if (limit < 1) { String message = Logging.getMessage("NetworkStatus.InvalidAttemptLimit"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } this.attemptLimit.set(limit); } /** * Set the length of time to wait until a host is marked as not unreachable subsequent to its being marked * unreachable. * * @param interval The length of time, in milliseconds, to wait to unmark a host as unreachable. * @throws IllegalArgumentException if the interval is less than 0. */ public void setTryAgainInterval(long interval) { if (interval < 0) { String message = Logging.getMessage("NetworkStatus.InvalidTryAgainInterval"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } this.tryAgainInterval.set(interval); } /** * Returns the number of times a host must be logged as unavailable before it is marked unavailable in this class. * * @return the limit. */ public int getAttemptLimit() { return this.attemptLimit.get(); } /** * Returns the length of time to wait until a host is marked as not unreachable subsequent to its being marked * unreachable. * * @return the interval, in milliseconds. */ public long getTryAgainInterval() { return this.tryAgainInterval.get(); } /** * Log a host as unavailable. Each invocation increments the host's attempt count. When the count equals or exceeds * the attempt limit, the host is marked as unavailable. * * @param url a url containing the host to mark as unavailable. */ public synchronized void logUnavailableHost(URL url) { if (this.offlineMode) return; if (url == null) { String message = Logging.getMessage("nullValue.URLIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } String hostName = url.getHost(); HostInfo hi = this.hostMap.get(hostName); if (hi != null) { if (!hi.isUnavailable()) { hi.logCount.incrementAndGet(); if (hi.isUnavailable()) // host just became unavailable this.firePropertyChange(NetworkStatus.HOST_UNAVAILABLE, null, url); } hi.lastLogTime.set(System.currentTimeMillis()); } else { hi = new HostInfo(this.attemptLimit.get(), this.tryAgainInterval.get()); hi.logCount.set(1); if (hi.isUnavailable()) // the attempt limit may be as low as 1, so handle that case here this.firePropertyChange(NetworkStatus.HOST_UNAVAILABLE, null, url); this.hostMap.put(hostName, hi); } this.lastUnavailableLogTime.set(System.currentTimeMillis()); } /** * Log a host as available. Each invocation causes the host to no longer be marked as unavailable. Its
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?