📄 rendezvousserviceimpl.java
字号:
/* * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved. * * The Sun Project JXTA(TM) Software License * * 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 Sun Microsystems, Inc. for JXTA(TM) technology." * 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. * * JXTA is a registered trademark of Sun Microsystems, Inc. in the United * States and other countries. * * Please see the license information page at : * <http://www.jxta.org/project/www/license.html> for instructions on use of * the license in source files. * * ==================================================================== * * 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. */package net.jxta.impl.rendezvous;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Enumeration;import java.util.HashSet;import java.util.Iterator;import java.util.Random;import java.util.Set;import java.util.Timer;import java.util.TimerTask;import java.util.Vector;import java.util.concurrent.atomic.AtomicBoolean;import java.io.IOException;import java.util.Collection;import java.util.List;import java.util.NoSuchElementException;import java.util.logging.Level;import net.jxta.logging.Logging;import java.util.logging.Logger;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.XMLDocument;import net.jxta.endpoint.EndpointAddress;import net.jxta.endpoint.EndpointListener;import net.jxta.endpoint.EndpointService;import net.jxta.endpoint.Message;import net.jxta.id.ID;import net.jxta.impl.endpoint.EndpointUtils;import net.jxta.meter.MonitorResources;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupID;import net.jxta.platform.Module;import net.jxta.protocol.ConfigParams;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.protocol.PeerAdvertisement;import net.jxta.protocol.RdvAdvertisement;import net.jxta.protocol.RouteAdvertisement;import net.jxta.rendezvous.RendezVousService;import net.jxta.rendezvous.RendezvousEvent;import net.jxta.rendezvous.RendezVousStatus;import net.jxta.rendezvous.RendezvousListener;import net.jxta.service.Service;import net.jxta.impl.id.UUID.UUID;import net.jxta.impl.id.UUID.UUIDFactory;import net.jxta.impl.meter.MonitorManager;import net.jxta.impl.protocol.RdvConfigAdv;import net.jxta.impl.rendezvous.adhoc.AdhocPeerRdvService;import net.jxta.impl.rendezvous.edge.EdgePeerRdvService;import net.jxta.impl.rendezvous.rdv.RdvPeerRdvService;import net.jxta.impl.rendezvous.rendezvousMeter.RendezvousMeterBuildSettings;import net.jxta.impl.rendezvous.rendezvousMeter.RendezvousServiceMonitor;import net.jxta.impl.rendezvous.rpv.PeerView;import net.jxta.impl.rendezvous.rpv.PeerViewElement;import net.jxta.impl.util.TimeUtils;/** * A JXTA {@link net.jxta.rendezvous.RendezVousService} implementation which * implements the standard JXTA Rendezvous Protocol (RVP). * * @see net.jxta.rendezvous.RendezVousService * @see <a href="https://jxta-spec.dev.java.net/nonav/JXTAProtocols.html#proto-rvp" target="_blank">JXTA Protocols Specification : Rendezvous Protocol</a> */public final class RendezVousServiceImpl implements RendezVousService { /** * Logger */ private final static transient Logger LOG = Logger.getLogger(RendezVousServiceImpl.class.getName()); private final static long rdv_watchdog_interval_default = 5 * TimeUtils.AMINUTE; // 5 Minutes private static final double DEMOTION_FACTOR = 0.05; private static final long DEMOTION_MIN_PEERVIEW_COUNT = 5; private static final long DEMOTION_MIN_CLIENT_COUNT = 3; protected static final int MAX_MSGIDS = 1000; private final static Random random = new Random(); private PeerGroup group = null; private ID assignedID = null; private ModuleImplAdvertisement implAdvertisement = null; public EndpointService endpoint = null; private RendezvousServiceMonitor rendezvousServiceMonitor; private Timer timer; private RdvWatchdogTask autoRdvTask = null; private long rdv_watchdog_interval = 5 * TimeUtils.AMINUTE; // 5 Minutes private final Set<RendezvousListener> eventListeners = Collections.synchronizedSet(new HashSet<RendezvousListener>()); /** * The message IDs we have seen. Used for duplicate removal. */ private final List<UUID> msgIds = new ArrayList<UUID>(MAX_MSGIDS); /** * Total number of messages which have been received. */ private int messagesReceived; private RdvConfigAdv.RendezVousConfiguration config = RdvConfigAdv.RendezVousConfiguration.EDGE; private boolean autoRendezvous = false; private String[] savedArgs = null; /** * If {@code true} then a rdv provider change is in progress. */ private AtomicBoolean rdvProviderSwitchStatus = new AtomicBoolean(false); /** * The current provider */ private RendezVousServiceProvider provider = null; /** * Our interface object. We currently always return the same object. */ private final RendezVousServiceInterface rendezvousInterface = new RendezVousServiceInterface(this); /** * Constructor for the RendezVousServiceImpl object */ public RendezVousServiceImpl() { } /** * {@inheritDoc} */ public RendezVousService getInterface() { return rendezvousInterface; } /** * {@inheritDoc} */ public ModuleImplAdvertisement getImplAdvertisement() { return implAdvertisement; } /** * Return the assigned ID for this service. * * @return The assigned ID for this service. */ public ID getAssignedID() { return assignedID; } /** * {@inheritDoc} * <p/> * <p/><b>Note</b>: it is permissible to pass null as the impl parameter * when this instance is not being loaded via the module framework. */ public synchronized void init(PeerGroup g, ID assignedID, Advertisement impl) { this.group = g; this.assignedID = assignedID; this.implAdvertisement = (ModuleImplAdvertisement) impl; RdvConfigAdv rdvConfigAdv = null; ConfigParams confAdv = group.getConfigAdvertisement(); // Get the config. If we do not have a config, we're done; we just keep // the defaults (edge peer/no auto-rdv) if (confAdv != null) { Advertisement adv = null; try { XMLDocument configDoc = (XMLDocument) confAdv.getServiceParam(getAssignedID()); if (null != configDoc) { adv = AdvertisementFactory.newAdvertisement(configDoc); } } catch (NoSuchElementException failed) { //ignored } if (adv instanceof RdvConfigAdv) { rdvConfigAdv = (RdvConfigAdv) adv; } } if (null == rdvConfigAdv) { // Make a new advertisement for defaults. rdvConfigAdv = (RdvConfigAdv) AdvertisementFactory.newAdvertisement(RdvConfigAdv.getAdvertisementType()); } config = rdvConfigAdv.getConfiguration(); autoRendezvous = rdvConfigAdv.getAutoRendezvousCheckInterval() > 0; rdv_watchdog_interval = rdvConfigAdv.getAutoRendezvousCheckInterval(); // force AD-HOC config for World Peer Group. if (PeerGroupID.worldPeerGroupID.equals(group.getPeerGroupID())) { config = RdvConfigAdv.RendezVousConfiguration.AD_HOC; } if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) { StringBuilder configInfo = new StringBuilder("Configuring RendezVous Service : " + assignedID); if (implAdvertisement != null) { configInfo.append("\n\tImplementation :"); configInfo.append("\n\t\tModule Spec ID: ").append(implAdvertisement.getModuleSpecID()); configInfo.append("\n\t\tImpl Description : ").append(implAdvertisement.getDescription()); configInfo.append("\n\t\tImpl URI : ").append(implAdvertisement.getUri()); configInfo.append("\n\t\tImpl Code : ").append(implAdvertisement.getCode()); } configInfo.append("\n\tGroup Params :"); configInfo.append("\n\t\tGroup : ").append(group); configInfo.append("\n\t\tPeer ID : ").append(group.getPeerID()); configInfo.append("\n\tConfiguration :"); configInfo.append("\n\t\tRendezVous : ").append(config); configInfo.append("\n\t\tAuto RendezVous : ").append(autoRendezvous); configInfo.append("\n\t\tAuto-RendezVous Reconfig Interval : ").append(rdv_watchdog_interval); LOG.config(configInfo.toString()); } // "start" a rendezvous provider switch. We will finish in startApp() rdvProviderSwitchStatus.set(true); } /** * {@inheritDoc} */ public int startApp(String[] arg) { endpoint = group.getEndpointService(); if (null == endpoint) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Stalled until there is an endpoint service"); } return START_AGAIN_STALLED; } Service needed = group.getMembershipService(); if (null == needed) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Stalled until there is a membership service"); } return START_AGAIN_STALLED; } // if( !PeerGroupID.worldPeerGroupID.equals(group.getPeerGroupID())) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -