📄 wirepipe.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.pipe;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.XMLDocument;import net.jxta.endpoint.*;import net.jxta.id.ID;import net.jxta.impl.cm.SrdiIndex;import net.jxta.impl.id.UUID.UUID;import net.jxta.impl.id.UUID.UUIDFactory;import net.jxta.logging.Logging;import net.jxta.peer.PeerID;import net.jxta.peergroup.PeerGroup;import net.jxta.pipe.InputPipe;import net.jxta.pipe.PipeService;import net.jxta.protocol.PipeAdvertisement;import net.jxta.rendezvous.RendezVousService;import java.io.IOException;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;/** * WirePipe (aka Propagated pipe) is very similar to IGMP, where a creation of an * input pipe results in a propagated pipe membership registration with the peer's * rendezvous peer, and a closure results in a propagated pipe group resignation, * these group registration/resignation are simply PipeService SRDI messages. */public class WirePipe implements EndpointListener, InputPipe, PipeRegistrar { /** * Logger */ private final static transient Logger LOG = Logger.getLogger(WirePipe.class.getName()); /** * The number of message ID we track to eliminate duplicate messages. */ private static final int MAX_RECORDED_MSGIDS = 250; private volatile boolean closed = false; private final PeerGroup peerGroup; private final PipeResolver pipeResolver; private final WirePipeImpl wireService; private final PipeAdvertisement pipeAdv; private final RendezVousService rendezvous; private final PeerID localPeerId; private NonBlockingWireOutputPipe repropagater; int messagesReceived = 0; /** * Table of local input pipes listening on this pipe. Weak map (used as a * Set) so that we don't keep pipes unnaturally alive and consuming * resources. */ private final Map<InputPipe, Object> wireinputpipes = new WeakHashMap<InputPipe, Object>(); /** * The list of message ids we have already seen. The most recently seen * messages are at the end of the list. * <p/> * XXX 20031102 bondolo@jxta.org: We might want to consider three * performance enhancements: * <p/> * <ul> * <li>Reverse the order of elements in the list. This would result * in faster searching since the default strategy for ArrayList to * search in index order. We are most likely to see duplicate messages * amongst the messages we have seen recently. This would make * additions more costly.</li> * <p/> * <li>When we do find a duplicate in the list, exchange it's location * with the newest message in the list. This will keep annoying dupes * close to the start of the list.</li> * <p/> * <li>When the array reaches MaxNbOfStoredIds treat it as a ring.</li> * </ul> */ private final List<UUID> msgIds = new ArrayList<UUID>(MAX_RECORDED_MSGIDS); /** * Constructor * * @param group The Group associated with this service * @param pipeResolver the associated pipe resolver * @param wireService The pipe service associated with this pipe * @param adv pipe advertisement */ public WirePipe(PeerGroup group, PipeResolver pipeResolver, WirePipeImpl wireService, PipeAdvertisement adv) { this.peerGroup = group; this.pipeResolver = pipeResolver; this.wireService = wireService; this.pipeAdv = adv; localPeerId = peerGroup.getPeerID(); rendezvous = group.getRendezVousService(); } /** * {@inheritDoc} * <p/> * Closes the pipe. */ @Override protected synchronized void finalize() throws Throwable { if (!closed) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Pipe is being finalized without being previously closed. This is likely a bug."); } } close(); super.finalize(); } /** * {@inheritDoc} */ public synchronized boolean register(InputPipe wireinputpipe) { if (closed) { return false; } if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Registering input pipe for " + pipeAdv.getPipeID()); } wireinputpipes.put(wireinputpipe, null); boolean registered; if (1 == wireinputpipes.size()) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Registering " + pipeAdv.getPipeID() + " with pipe resolver."); } registered = pipeResolver.register(this); } else { registered = true; } return registered; } /** * {@inheritDoc} */ public synchronized boolean forget(InputPipe wireinputpipe) { boolean removed = null != wireinputpipes.remove(wireinputpipe); if (wireinputpipes.isEmpty()) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Deregistering wire pipe with pipe resolver"); } pipeResolver.forget(this); } if (removed && Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Removed input pipe for " + pipeAdv.getPipeID()); } return removed; } // This is the InputPipe API implementation. // This is needed only to be able to register an InputPipe to the PipeResolver. // Not everything has to be implemented. /** * {@inheritDoc} */ public Message waitForMessage() throws InterruptedException { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("This method is not really supported."); } return null; } /** * {@inheritDoc} */ public Message poll(int timeout) throws InterruptedException { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("This method is not really supported."); } return null; } /** * {@inheritDoc} */ public synchronized void close() { if (closed) { return; } closed = true; if (null != repropagater) { repropagater.close(); repropagater = null; } List<InputPipe> inputPipes = new ArrayList<InputPipe>(wireinputpipes.keySet()); for (InputPipe anInputPipe : inputPipes) { anInputPipe.close(); } wireinputpipes.clear(); msgIds.clear(); wireService.forgetWirePipe(pipeAdv.getPipeID()); } /** * {@inheritDoc} */ public String getType() { return pipeAdv.getType(); } /** * {@inheritDoc} */ public ID getPipeID() { return pipeAdv.getPipeID(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -