📄 peerview.java
字号:
/* * Copyright (c) 2002-2004 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and * "Project JXTA" must not be used to endorse or promote products * derived from this software without prior written permission. * For written permission, please contact Project JXTA at * http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache * Foundation. * * $Id: PeerView.java,v 1.101 2006/07/24 20:53:30 bondolo Exp $ */package net.jxta.impl.rendezvous.rpv;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URI;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.Enumeration;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Random;import java.util.Set;import java.util.SortedSet;import java.util.Timer;import java.util.TimerTask;import java.util.TreeSet;import java.io.IOException;import java.io.File;import java.net.URISyntaxException;import java.util.NoSuchElementException;import org.apache.log4j.Level;import org.apache.log4j.Logger;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.MimeMediaType;import net.jxta.document.XMLDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.endpoint.EndpointAddress;import net.jxta.endpoint.EndpointListener;import net.jxta.endpoint.EndpointService;import net.jxta.endpoint.Message;import net.jxta.endpoint.Messenger;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.MessageTransport;import net.jxta.endpoint.StringMessageElement;import net.jxta.endpoint.TextDocumentMessageElement;import net.jxta.id.ID;import net.jxta.id.IDFactory;import net.jxta.peer.PeerID;import net.jxta.peergroup.PeerGroup;import net.jxta.pipe.InputPipe;import net.jxta.pipe.OutputPipe;import net.jxta.pipe.PipeID;import net.jxta.pipe.PipeMsgEvent;import net.jxta.pipe.PipeMsgListener;import net.jxta.pipe.PipeService;import net.jxta.protocol.AccessPointAdvertisement;import net.jxta.protocol.ConfigParams;import net.jxta.protocol.PeerAdvertisement;import net.jxta.protocol.PipeAdvertisement;import net.jxta.protocol.RdvAdvertisement;import net.jxta.protocol.RouteAdvertisement;import net.jxta.rendezvous.RendezvousEvent;import net.jxta.rendezvous.RendezvousListener;import net.jxta.impl.access.AccessList;import net.jxta.impl.endpoint.relay.RelayClient;import net.jxta.impl.protocol.RdvConfigAdv;import net.jxta.impl.rendezvous.RendezVousServiceImpl;import net.jxta.impl.util.TimeUtils;import net.jxta.impl.util.TimerThreadNamer;/** * This class models a Rendezvous Peer View (RPV): * ordered collection of all other Rendezvous Peers visible to * this Peer. * * <p/>Presently this class implements a random "diffusion" algorithm * where each Peer periodically selects a randomly selected peer * advertisement from its view and sends it over to a randomly * selected peer from its view. Over time, this causes every peer to * learn about every other peer, resulting in a "consistent" peer * view. * * <p/>This diffusion process is bootstrapped by every peer sending their * own peer advertisements to some well-known, stable, "seed" peers on * startup. **/public final class PeerView implements EndpointListener, RendezvousListener { /** * Log4J Logger **/ private static final transient Logger LOG = Logger.getLogger(PeerView.class.getName()); /** * Our service name **/ static final String SERVICE_NAME = "PeerView"; /** * Namespace used for rdv message elements. **/ static final String MESSAGE_NAMESPACE = "jxta"; /** * Element name of outgoing messages. Note that the element contains a * RdvAvertisement and <emphasis>not</emphasis> a Peer Advertisement. **/ static final String MESSAGE_ELEMENT_NAME = "PeerView.PeerAdv"; /** * Element name of responses. Note that the element contains a * RdvAvertisement and <emphasis>not</emphasis> a Peer Advertisement. */ static final String RESPONSE_ELEMENT_NAME = "PeerView.PeerAdv.Response"; /** * Message element name for PeerView "Cached" Message Element */ static final String CACHED_RADV_ELEMENT_NAME = "PeerView.Cached"; /** * Optional message element that specifies by its presence in a peerview * message that the referenced peer is not the provider of the * RdvAdvertisement and the advertisement is a "hint" or referal from the * responding peer. * * In practice, when sending its own RdvAdvertisement, a peer does not * include this element, but when sending another peer's RdvAdvertisement, * this element is included. */ static final MessageElement CACHED_RADV_ELEMENT = new StringMessageElement(CACHED_RADV_ELEMENT_NAME, Boolean.TRUE.toString(), null); /** * Message element name that specifies the route advertisement of the * source of the message. */ static final String SRCROUTEADV_ELEMENT_NAME = "PeerView.SrcRouteAdv"; /** * Message element name for PeerView "Edge" Message Element */ static final String EDGE_ELEMENT_NAME = "PeerView.EdgePeer"; /** * Optional message element that specifies by its presence in a peerview * message that the referenced peer is an edge peer and not a member of the * peerview. */ static final MessageElement EDGE_ELEMENT = new StringMessageElement(EDGE_ELEMENT_NAME, Boolean.TRUE.toString(), null); /** * Message element name for PeerView "Failure" Message Element */ static final String FAILURE_ELEMENT_NAME = "PeerView.Failure"; /** * Optional message element that specifies by its presence in a peerview * message that the referenced peer has either failed or is quitting. If the * "cached" element is also set then the error is being reported by a third * party. */ static final MessageElement FAILURE_ELEMENT = new StringMessageElement(FAILURE_ELEMENT_NAME, Boolean.TRUE.toString(), null); /** * This is the interval between adv exchange in seconds. This is * the main tunable runtime parameter for the diffusion * process. An interval that is too low will improve view * consistency at the expense of gratuitous network traffic. On * the other hand, an interval that is too high will cause the * view to become inconsistent. It is desirable to err on the side * of extra traffic. */ private static final long DEFAULT_SEEDING_PERIOD = 5 * TimeUtils.ASECOND; private static final long WATCHDOG_PERIOD = 30 * TimeUtils.ASECOND; private static final long ACL_REFRESH_PERIOD = 30 * TimeUtils.AMINUTE; private static final long SEEDING_URI_REFRESH_PERIOD = 60 * TimeUtils.AMINUTE; private static final long WATCHDOG_GRACE_DELAY = 5 * TimeUtils.AMINUTE; private static final long DEFAULT_BOOTSTRAP_KICK_INTERVAL = 3 * TimeUtils.ASECOND; private static final int MIN_BOOTLEVEL = 0; private static final int BOOTLEVEL_INCREMENT = 1; private static final int MAX_EDGE_PEER_BOOTLEVEL = 8; private static final int MAX_RDV_PEER_BOOTLEVEL = 6; /** * Default lifetimes for RdvAdvertisement that are published. **/ private static final long DEFAULT_RADV_LIFETIME = 1 * TimeUtils.ADAY; private static final long DEFAULT_RADV_EXPIRATION = 1 * TimeUtils.ANHOUR; /** * DEFAULT_SEEDING_RDVPEERS * * This value is the maximum number of rendezvous peers that will be * send our own advertisement at boot time. **/ private static final int DEFAULT_SEEDING_RDVPEERS = 5; private final PeerGroup group; private final PeerGroup advertisingGroup; private final RendezVousServiceImpl rdvService; private final EndpointService endpoint; /** * The name of this PeerView. * * <p>FIXME 20040623 bondolo This should be a CodatID. **/ private final String name; /** * Delay in relative milliseconds to apply before contacting seeding rdvs. * 0 is supposed to be reserved by RdvConfig to mean "use the default". * However, it is in fact a valid value and also the one we want for the default. * The only problem with that is that it is not possible to configure this value * explicitly, should it one day not be the default. The issue is actually in RdvConfig. **/ private long seedingRdvConnDelay = 0; /** * Whether relays should be probed for rendezvous advertisements. **/ private boolean probeRelays = true; /** * If the peerview is smaller than this we will try harder to find * additional peerview members. **/ private int minHappyPeerView = 4; /** * Whether we are restricted to using seed rdvs only. **/ private boolean useOnlySeeds = false; /** * These are seed peers which were specified as part of the configuration * data or programmatically. These seeds are never deleted. * * <ul> * <li>Values are {@link java.net.URI}.</li> * </ul> */ private final Set permanentSeedHosts = Collections.synchronizedSet( new HashSet() ); /** * The active list of seed peers. Formed by the union of the permanent * seed hosts and the lists of seed peers downloaded from the seeding * URIs. * * <ul> * <li>Values are {@link java.net.URI}.</li> * </ul> */ private final Set activeSeedHosts = Collections.synchronizedSet( new HashSet() ); /** * These URIs specify location of seed peer lists. The URIs will be resolved * via URLConnection and are assumed to refer to plain text lists of URIs. * * <ul> * <li>Values are {@link java.net.URI}.</li> * </ul> */ private final Set seedingURIs = Collections.synchronizedSet( new HashSet() ); private long nextSeedingURIrefreshTime = 0; /** * A single timer is used to periodically kick each PeerView * into activity. For the Random PeerView, this activity consists * of selecting a PeerViewElement at random from its view and * sending it across to a randomly-selected peer from its view. * * <p/>FIXME 20021121 lomax * * <p/>The original idea of using a common timer in order to save threads IS a * very good idea. However, limitations, and actually, bugs, in java.util.Timer * creates the following problems when using a static Timer: * * <ul> * <li>Memory Leak: Cancelling a TimerTask does not remove it from the * execution queue of the Timer until the Timer is cancelled or the * TimerTask is fired. Since most of the TimerTasks are inner classes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -