📄 rendezvousserviceimpl.java
字号:
/* * $Id: RendezVousServiceImpl.java,v 1.116 2006/05/30 21:01:50 hamada Exp $ * * Copyright (c) 2001 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. */package net.jxta.impl.rendezvous;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Enumeration;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Random;import java.util.Set;import java.util.Timer;import java.util.TimerTask;import java.util.Vector;import java.io.IOException;import java.util.NoSuchElementException;import org.apache.log4j.Level;import org.apache.log4j.Logger;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.XMLDocument;import net.jxta.document.XMLElement;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.meter.MonitorResources;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupID;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;import net.jxta.impl.util.TimerThreadNamer;/** * A JXTA {@link net.jxta.rendezvous.RendezvousService} implementation which * implements the standard JXTA Rendezvous Protocol (RVP). * * @see net.jxta.rendezvous.RendezvousService * @see <a href="http://spec.jxta.org/nonav/v1.0/docbook/JXTAProtocols.html#proto-rvp" target="_blank">JXTA Protocols Specification : Rendezvous Protocol</a> */public final class RendezVousServiceImpl implements RendezVousService { /** * Log4J 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; private PeerGroup advGroup = null; public EndpointService endpoint = null; private RendezvousServiceMonitor rendezvousServiceMonitor; private final Timer timer = new Timer(true); private RdvWatchdogTask autoRdvTask = null; private long rdv_watchdog_interval = 5 * TimeUtils.AMINUTE; // 5 Minutes private final Set eventListeners = Collections.synchronizedSet(new HashSet()); private final Map propListeners = new HashMap(); /** * The peer view for this peer group. */ public volatile PeerView rpv = null; private final List msgIds = new ArrayList(MAX_MSGIDS); private int messagesReceived; private RdvConfigAdv.RendezVousConfiguration config = RdvConfigAdv.RendezVousConfiguration.EDGE; private boolean autoRendezvous = false; private String[] savedArgs = null; /** * Object to lock on while changing rdv states. */ private final Object rdvProviderSwitchLock = new String("Provider switch lock"); /** * If <code>true</code> then a rdv provider change is in progress. */ private boolean rdvProviderSwitchStatus = false; private RendezVousServiceProvider provider = null; /** * Constructor for the RendezVousServiceImpl object */ public RendezVousServiceImpl() {} public final static RouteAdvertisement extractRouteAdv(PeerAdvertisement adv) { try { // Get its EndpointService advertisement XMLElement endpParam = (XMLElement) adv.getServiceParam(PeerGroup.endpointClassID); if (endpParam == null) { if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("No Endpoint Params"); } return null; } // get the Route Advertisement element Enumeration paramChilds = endpParam.getChildren(RouteAdvertisement.getAdvertisementType()); XMLElement param; if (paramChilds.hasMoreElements()) { param = (XMLElement) paramChilds.nextElement(); } else { if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("No Route Adv in Peer Adv"); } return null; } // build the new route RouteAdvertisement route = (RouteAdvertisement) AdvertisementFactory.newAdvertisement((XMLElement) param); route.setDestPeerID(adv.getPeerID()); return route; } catch (Exception e) { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("failed to extract radv", e); } } return null; } /** * {@inheritDoc} */ protected void finalize() throws Throwable { stopApp(); super.finalize(); } /** * {@inheritDoc} */ public Service getInterface() { return new RendezVousServiceInterface(this); } /** * {@inheritDoc} */ public Advertisement getImplAdvertisement() { return implAdvertisement; } public ID getAssignedID() { return assignedID; } /** * {@inheritDoc} * * <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; timer.schedule(new TimerThreadNamer("RendezVousServiceImpl Timer for " + group.getPeerGroupID()), 0); advGroup = g.getParentGroup(); if ((null == advGroup) || PeerGroupID.worldPeerGroupID.equals(advGroup.getPeerGroupID())) { // For historical reasons, we publish in our own group rather than // the parent if our parent is the world group. advGroup = group; } ConfigParams confAdv = (ConfigParams) g.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) { // XXX 20041027 backwards compatibility configDoc.addAttribute( "type", RdvConfigAdv.getAdvertisementType() ); adv = AdvertisementFactory.newAdvertisement(configDoc); } } catch (NoSuchElementException failed) { ; } if (adv instanceof RdvConfigAdv) { RdvConfigAdv rdvConfigAdv = (RdvConfigAdv) adv; config = rdvConfigAdv.getConfiguration(); autoRendezvous = rdvConfigAdv.getAutoRendezvousCheckInterval() > 0; rdv_watchdog_interval = rdvConfigAdv.getAutoRendezvousCheckInterval(); } } if (PeerGroupID.worldPeerGroupID.equals(group.getPeerGroupID())) { config = RdvConfigAdv.RendezVousConfiguration.AD_HOC; } if (LOG.isEnabledFor(Level.INFO)) { StringBuffer configInfo = new StringBuffer("Configuring RendezVous Service : " + assignedID); if (implAdvertisement != null) { configInfo.append("\n\tImplementation :"); configInfo.append("\n\t\tModule Spec ID: " + implAdvertisement.getModuleSpecID()); configInfo.append("\n\t\tImpl Description : " + implAdvertisement.getDescription()); configInfo.append("\n\t\tImpl URI : " + implAdvertisement.getUri()); configInfo.append("\n\t\tImpl Code : " + implAdvertisement.getCode()); } configInfo.append("\n\tGroup Params :"); configInfo.append("\n\t\tGroup : " + group.getPeerGroupName()); configInfo.append("\n\t\tGroup ID : " + group.getPeerGroupID()); configInfo.append("\n\t\tPeer ID : " + group.getPeerID()); configInfo.append("\n\tConfiguration :"); if (null != advGroup) { configInfo.append("\n\t\tAdvertising group : " + advGroup.getPeerGroupName() + " [" + advGroup.getPeerGroupID() + "]"); } else { configInfo.append("\n\t\tAdvertising group : (none)"); } configInfo.append("\n\t\tRendezVous : " + config ); configInfo.append("\n\t\tAuto RendezVous : " + autoRendezvous); configInfo.append("\n\t\tAuto-RendezVous Reconfig Interval : " + rdv_watchdog_interval); LOG.info(configInfo); } synchronized (rdvProviderSwitchLock) { rdvProviderSwitchStatus = true; } } /** * {@inheritDoc} */ public int startApp(String[] arg) { endpoint = group.getEndpointService(); if (null == endpoint) { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("Stalled until there is an endpoint service"); } return START_AGAIN_STALLED; } Service needed = group.getMembershipService(); if (null == needed) { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("Stalled until there is a membership service"); } return START_AGAIN_STALLED; } // Create the PeerView instance if (RdvConfigAdv.RendezVousConfiguration.AD_HOC != config) { rpv = new PeerView(group, advGroup, this, getAssignedID().toString() + group.getPeerGroupID().getUniqueValue().toString()); rpv.start(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -