⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jxtaserverpipe.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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.util;import net.jxta.document.AdvertisementFactory;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.XMLDocument;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.Messenger;import net.jxta.endpoint.StringMessageElement;import net.jxta.endpoint.TextDocumentMessageElement;import net.jxta.id.IDFactory;import net.jxta.impl.endpoint.tcp.TcpMessenger;import net.jxta.logging.Logging;import net.jxta.peergroup.PeerGroup;import net.jxta.pipe.InputPipe;import net.jxta.pipe.PipeMsgEvent;import net.jxta.pipe.PipeMsgListener;import net.jxta.pipe.PipeService;import net.jxta.protocol.PeerAdvertisement;import net.jxta.protocol.PipeAdvertisement;import java.io.IOException;import java.net.SocketException;import java.net.SocketTimeoutException;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.logging.Level;import java.util.logging.Logger;/** * The server side of a JxtaBiDiPipe. The intent of this object is accept connection requests. * JxtaServerPipe follows the same pattern as java.net.ServerSocket, without it no connection can be * established. * */public class JxtaServerPipe implements PipeMsgListener {    private static final Logger LOG = Logger.getLogger(JxtaServerPipe.class.getName());    protected static final String nameSpace = "JXTABIP";    protected static final String credTag = "Cred";    protected static final String reqPipeTag = "reqPipe";    protected static final String remPeerTag = "remPeer";    protected static final String remPipeTag = "remPipe";    protected static final String closeTag = "close";    protected static final String reliableTag = "reliable";    protected static final String directSupportedTag = "direct";    private PeerGroup group;    private InputPipe serverPipe;    private PipeAdvertisement pipeadv;    private int backlog = 50;    private long timeout = 30 * 1000L;    private final Object closeLock = new Object();    protected BlockingQueue<JxtaBiDiPipe> connectionQueue = null;    private boolean bound = false;    private boolean closed = false;    protected StructuredDocument myCredentialDoc = null;    /**     * The exceutor service.     */    private final ExecutorService executor;    /**     * Default constructor for the JxtaServerPipe     * <p/>     * backlog default of 50     * <p> call to accept() for this ServerPipe will     * block for only this amount of time. If the timeout expires,     * a java.net.SocketTimeoutException is raised, though the ServerPipe is still valid.     * <p/>     *     * @param group   JXTA PeerGroup     * @param pipeadv PipeAdvertisement on which pipe requests are accepted     * @throws IOException if an I/O error occurs     */    public JxtaServerPipe(PeerGroup group, PipeAdvertisement pipeadv) throws IOException {        this(group, pipeadv, 50);    }    /**     * Constructor for the JxtaServerPipe     *     * @param group   JXTA PeerGroup     * @param pipeadv PipeAdvertisement on which pipe requests are accepted     * @param backlog the maximum length of the queue.     * @param timeout call to accept() for this ServerPipe will     *                block for only this amount of time. If the timeout expires,     *                a java.net.SocketTimeoutException is raised, though the ServerPipe is still valid.     * @throws IOException if an I/O error occurs     */    public JxtaServerPipe(PeerGroup group, PipeAdvertisement pipeadv, int backlog, int timeout) throws IOException {        this(group, pipeadv, backlog);        this.timeout = timeout;    }    /**     * Constructor for the JxtaServerPipe object     *     * @param group   JXTA PeerGroup     * @param pipeadv PipeAdvertisement on which pipe requests are accepted     * @param backlog the maximum length of the queue.     *                * @exception  IOException  if an I/O error occurs     * @throws IOException if an I/O error occurs     */    public JxtaServerPipe(PeerGroup group, PipeAdvertisement pipeadv, int backlog) throws IOException {        this.group = group;        this.executor = Executors.newFixedThreadPool(3);        this.pipeadv = pipeadv;        this.backlog = backlog;        connectionQueue = new ArrayBlockingQueue<JxtaBiDiPipe>(backlog);        PipeService pipeSvc = group.getPipeService();        serverPipe = pipeSvc.createInputPipe(pipeadv, this);        setBound();    }    /**     * Binds the <code>JxtaServerPipe</code> to a specific pipe advertisement     *     * @param group   JXTA PeerGroup     * @param pipeadv PipeAdvertisement on which pipe requests are accepted     * @throws IOException if an I/O error occurs     */    public void bind(PeerGroup group, PipeAdvertisement pipeadv) throws IOException {        bind(group, pipeadv, backlog);    }    /**     * Binds the <code>JxtaServerPipe</code> to a specific pipe advertisement     *     * @param group   JXTA PeerGroup     * @param pipeadv PipeAdvertisement on which pipe requests are accepted     * @param backlog the maximum length of the queue.     * @throws IOException if an I/O error occurs     */    public void bind(PeerGroup group, PipeAdvertisement pipeadv, int backlog) throws IOException {        this.backlog = backlog;        connectionQueue = new ArrayBlockingQueue<JxtaBiDiPipe>(backlog);        this.group = group;        this.pipeadv = pipeadv;        PipeService pipeSvc = group.getPipeService();        serverPipe = pipeSvc.createInputPipe(pipeadv, this);        setBound();    }    /**     * Listens for a connection to be made to this socket and accepts     * it. The method blocks until a connection is made.     *     * @return the connection accepted, null otherwise     * @throws IOException if an I/O error occurs     */    public JxtaBiDiPipe accept() throws IOException {        if (isClosed()) {            throw new SocketException("JxtaServerPipe is closed");        }        if (!isBound()) {            throw new SocketException("JxtaServerPipe is not bound yet");        }        try {            JxtaBiDiPipe bidi = connectionQueue.poll(timeout, TimeUnit.MILLISECONDS);            if (bidi == null) {                throw new SocketTimeoutException("Timeout reached");            }            return bidi;        } catch (InterruptedException ie) {            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.log(Level.FINE, "Interrupted", ie);            }            throw new SocketException("interrupted");        }    }    /**     * Gets the group associated with this JxtaServerPipe     *     * @return The group value     */    public PeerGroup getGroup() {        return group;    }    /**     * Gets the PipeAdvertisement associated with this JxtaServerPipe     *     * @return The pipeAdv value     */    public PipeAdvertisement getPipeAdv() {        return pipeadv;    }    /**     * Closes this JxtaServerPipe (closes the underlying input pipe).     *     * @throws IOException if an I/O error occurs     */    public void close() throws IOException {        synchronized (closeLock) {            if (isClosed()) {                return;            }            if (bound) {                // close all the pipe                serverPipe.close();                connectionQueue.clear();                executor.shutdownNow();                bound = false;            }            closed = true;        }    }    /**     * Sets the bound attribute of the JxtaServerPipe     */    void setBound() {        bound = true;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -