📄 wirepipe.java
字号:
/* * * $Id: WirePipe.java,v 1.40 2006/08/10 19:45:07 bondolo 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.pipe;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.WeakHashMap;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.XMLDocument;import net.jxta.endpoint.EndpointAddress;import net.jxta.endpoint.EndpointListener;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.TextDocumentMessageElement;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.peergroup.PeerGroup;import net.jxta.pipe.InputPipe;import net.jxta.pipe.PipeService;import net.jxta.protocol.PipeAdvertisement;import net.jxta.rendezvous.RendezVousService;import org.apache.log4j.Level;import org.apache.log4j.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 { /** * Log4J Logger */ private final static transient Logger LOG = Logger.getLogger(WirePipe.class.getName()); private static final int MAX_RECORDED_MSGIDS = 250; private volatile boolean closed = false; private PeerGroup myGroup = null; private PipeResolver pipeResolver = null; private WirePipeImpl pipeService = null; private PipeAdvertisement pipeAdv = null; private RendezVousService rendezvous = null; private final String localPeerId; private NonBlockingWireOutputPipe repropagater = null; /** * Table of local input pipes listening on this pipe. Weak map so that * we don't keep pipes unnaturally alive and consuming resources. * * <p/><ul> * <li>Values are {@link net.jxta.pipe.InputPipe}.</li> * </ul> */ private Map wireinputpipes = new WeakHashMap(); /** * Count of the number of local input pipes. Though it's mostly the same we * can't use <code>wireinputpipes.size()</code> because it's too twitchy. * We can guarantee that this field's value will change in predictable ways. */ private int nbInputPipes = 0; /** * 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: * * <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> * * <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> * * <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 g The Group associated with this service * @param pipeResolver the associated pipe resolver * @param pipeService The pipe service associated with this pipe * @param adv pipe advertisement */ public WirePipe(PeerGroup g, PipeResolver pipeResolver, WirePipeImpl pipeService, PipeAdvertisement adv) { this.myGroup = g; this.pipeResolver = pipeResolver; this.pipeService = pipeService; this.pipeAdv = adv; localPeerId = myGroup.getPeerID().toString(); rendezvous = g.getRendezVousService(); pipeResolver.register(this); repropagater = pipeService.createOutputPipe(adv, Collections.EMPTY_SET); } /** * {@inheritDoc} * * <p/>Closes the pipe. */ protected synchronized void finalize() throws Throwable { if(!closed) { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("Pipe is being finalized without being previously closed. This is likely a bug."); } } close(); super.finalize(); } /** * {@inheritDoc} */ public synchronized boolean register(InputPipe wireinputpipe) { wireinputpipes.put(wireinputpipe, null); nbInputPipes++; if(1 == nbInputPipes) { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("Registering wire pipe with SRDI"); } boolean registered = myGroup.getEndpointService().addIncomingMessageListener((EndpointListener) wireinputpipe, "PipeService", wireinputpipe.getPipeID().toString()); if(!registered) { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("Existing Registered Endpoint Listener for " + wireinputpipe.getPipeID()); } } pipeResolver.pushSrdi(this, true); } return true; } /** * {@inheritDoc} */ public synchronized boolean forget(InputPipe wireinputpipe) { wireinputpipes.remove(wireinputpipe); nbInputPipes--; if(0 == nbInputPipes) { if (LOG.isEnabledFor(Level.INFO)) { LOG.info("Deregistering wire pipe with SRDI"); } pipeResolver.pushSrdi(this, false); EndpointListener removed = myGroup.getEndpointService().removeIncomingMessageListener("PipeService", wireinputpipe.getPipeID().toString()); if((null == removed) || (this != removed)) { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("removeIncomingMessageListener() did not remove correct pipe!"); } } } if(nbInputPipes < 0) { // we reset this to zero so that re-registration works. nbInputPipes = 0; if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("Number of pipe listeners was < 0"); } } return true; } // 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 (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("This method is not really supported."); } return null; } /** * {@inheritDoc} */ public Message poll(int timeout) throws InterruptedException { if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("This method is not really supported."); } return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -