basicnetworkstatus.java
来自「world wind java sdk 源码」· Java 代码 · 共 423 行 · 第 1/2 页
JAVA
423 行
* unavailability count is effectively set to 0. * * @param url a url containing the host to mark as available. */ public synchronized void logAvailableHost(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) { this.hostMap.remove(hostName); // host is available again firePropertyChange(NetworkStatus.HOST_AVAILABLE, null, url); } this.lastAvailableLogTime.set(System.currentTimeMillis()); } /** * Indicates whether the host has been marked as unavailable. To be marked unavailable a host's attempt count must * exceed the specified attempt limit. * * @param url a url containing the host to check for availability. * @return true if the host is marked as unavailable, otherwise false. */ public synchronized boolean isHostUnavailable(URL url) { if (this.offlineMode) return true; 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) return false; if (hi.isTimeToTryAgain()) { hi.logCount.set(0); // info removed from table in logAvailableHost return false; } return hi.isUnavailable(); } /** * Indicates whether a public network can be reached or has been reached in the previous five seconds. * * @return false if the network can be reached or has been reached in the previous five seconds, otherwise true. */ public boolean isNetworkUnavailable() { return this.offlineMode || this.isNetworkUnavailable(5000L); } /** * Indicates whether a public network can be reached or has been reached in a specified previous amount of time. * * @param checkInterval the number of milliseconds in the past used to determine whether the server was avaialble * recently. * @return false if the network can be reached or has been reached in a specified time, otherwise true. */ public synchronized boolean isNetworkUnavailable(long checkInterval) { if (this.offlineMode) return true; // If there's been success since failure, network assumed to be reachable. if (this.lastAvailableLogTime.get() > this.lastUnavailableLogTime.get()) { this.lastNetworkUnavailableResult.set(false); return this.lastNetworkUnavailableResult.get(); } long now = System.currentTimeMillis(); // If there's been success recently, network assumed to be reachable. if (!this.lastNetworkUnavailableResult.get() && now - this.lastAvailableLogTime.get() < checkInterval) { return this.lastNetworkUnavailableResult.get(); } // If query comes too soon after an earlier one that addressed the network, return the earlier result. if (now - this.lastNetworkCheckTime.get() < checkInterval) { return this.lastNetworkUnavailableResult.get(); } this.lastNetworkCheckTime.set(now); if (!this.isWorlWindServerUnavailable()) { this.lastNetworkUnavailableResult.set(false); // network not unreachable return this.lastNetworkUnavailableResult.get(); } for (String testHost : networkTestSites) { if (isHostReachable(testHost)) { { this.lastNetworkUnavailableResult.set(false); // network not unreachable return this.lastNetworkUnavailableResult.get(); } } } if (now - this.lastNetworkStatusReportTime.get() > NETWORK_STATUS_REPORT_INTERVAL) { this.lastNetworkStatusReportTime.set(now); String message = Logging.getMessage("NetworkStatus.NetworkUnreachable"); Logging.logger().info(message); } this.lastNetworkUnavailableResult.set(true); // if no successful contact then network is unreachable return this.lastNetworkUnavailableResult.get(); } /** * Indicates whether the NASA World Wind servers can be reached. * * @return false if the servers can be reached, otherwise true. */ public boolean isWorlWindServerUnavailable() { return this.offlineMode || !isHostReachable("worldwind.arc.nasa.gov"); } private static boolean isHostReachable(String hostName) { try { // Assume host is unreachable if we can't get its dns entry without getting an exception //noinspection ResultOfMethodCallIgnored InetAddress.getByName(hostName); } catch (UnknownHostException e) { String message = Logging.getMessage("NetworkStatus.UnreachableTestHost", hostName); Logging.logger().fine(message); return false; } catch (Exception e) { String message = Logging.getMessage("NetworkStatus.ExceptionTestingHost", hostName); Logging.logger().info(message); return false; } // Was able to get internet address, but host still might not be reachable because the address might have been // cached earlier when it was available. So need to try something else. URLConnection connection = null; try { URL url = new URL("http://" + hostName); Proxy proxy = WWIO.configureProxy(); if (proxy != null) connection = url.openConnection(proxy); else connection = url.openConnection(); connection.setConnectTimeout(2000); String ct = connection.getContentType(); if (ct != null) return true; } catch (IOException e) { String message = Logging.getMessage("NetworkStatus.ExceptionTestingHost", hostName); Logging.logger().info(message); } finally { if (connection != null && connection instanceof HttpURLConnection) ((HttpURLConnection) connection).disconnect(); } return false; }//// public static void main(String[] args)// {// try// {// NetworkStatus ns = new BasicNetworkStatus();// boolean tf = ns.isWorlWindServerUnavailable();// tf = ns.isNetworkUnavailable();// }// catch (Exception e)// {// e.printStackTrace();// }// }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?