📄 reliableinputstream.java
字号:
/* * Copyright (c) 2003-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.util.pipe.reliable;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InterruptedIOException;import java.net.SocketTimeoutException;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;import net.jxta.endpoint.ByteArrayMessageElement;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.WireFormatMessageFactory;import net.jxta.impl.util.TimeUtils;import java.util.logging.Level;import net.jxta.logging.Logging;import java.util.logging.Logger;/** * Acts as a reliable input stream. Accepts data which * arrives in messages and orders it. */public class ReliableInputStream extends InputStream implements Incoming { /** * Logger */ private static final Logger LOG = Logger.getLogger(ReliableInputStream.class.getName()); /** * Connection we are working for. */ private Outgoing outgoing; private volatile boolean closed = false; private boolean closing = false; private MsgListener listener = null; /** * The amount of time that read() operation will block. > 0 */ private long timeout; /** * The current sequence number we are reading bytes from. */ private volatile int sequenceNumber = 0; /** * Queue of incoming messages. */ private final List<IQElt> inputQueue = new ArrayList<IQElt>(); /** * The I/O record for the message we are currently using for stream data. */ private final Record record; /** * Input record Object */ private static class Record { public InputStream inputStream; // next inbuff byte public long nextByte; // size of Record public long size; public Record() { inputStream = null; // allocated by caller nextByte = 0; // We read here (set by caller) size = 0; // Record size(set by caller) } /** reset the record element * */ public void resetRecord() { if (null != inputStream) { try { inputStream.close(); } catch (IOException ignored) {} } inputStream = null; size = nextByte = 0; } } /** * An input queue element which breaks out a received message in * enqueueMessage(). */ private static class IQElt implements Comparable { final int seqnum; final MessageElement elt; boolean ackd = false; IQElt(int sequence, MessageElement element) { seqnum = sequence; elt = element; } /** * {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof IQElt) { IQElt targ = (IQElt) obj; return (this.seqnum == targ.seqnum); } return false; } public int compareTo(IQElt el) { return this.seqnum < el.seqnum ? -1 : this.seqnum == el.seqnum ? 0 : 1; } /** * {@inheritDoc} */ public int compareTo(Object o) { return compareTo((IQElt) o); } } public ReliableInputStream(Outgoing outgoing, int timeout) { this(outgoing, timeout, null); } public ReliableInputStream(Outgoing outgoing, int timeout, MsgListener listener) { this.outgoing = outgoing; setTimeout(timeout); record = new Record(); this.listener = listener; // 1 <= seq# <= maxint, monotonically increasing // Incremented before compare. sequenceNumber = 0; if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { if (listener != null) { LOG.info("Listener based ReliableInputStream created"); } } } /** * {@inheritDoc} * * <p/>This is an explicit close operation. All subsequent {@code read()} * operations will fail. */ @Override public void close() throws IOException { super.close(); synchronized (inputQueue) { closed = true; inputQueue.clear(); inputQueue.notifyAll(); } } /** * Returns true if closed * * @return true if closed */ public boolean isInputShutdown() { return closed; } /** * Prepare this input stream to being closed. It will still deliver the * packets that have been received, but nothing more. This is meant to be * called in response to the other side having initiated closure. We assume * that when the other side does it it means that it is satisfied with what * we have acknowledged so far. */ public void softClose() { synchronized (inputQueue) { closing = true; inputQueue.notifyAll(); } } /** * Sets the Timeout attribute. A timeout of 0 blocks forever * * @param timeout The new soTimeout value */ public void setTimeout(int timeout) { if (timeout < 0) { throw new IllegalArgumentException("Timeout must be >=0"); } this.timeout = (0 == timeout) ? Long.MAX_VALUE : timeout; } /** * {@inheritDoc} */ @Override public int read() throws IOException { if (closed) { return -1; } byte[] a = new byte[1]; while (true) { int len = local_read(a, 0, 1); if (len < 0) { break; } if (len > 0) { if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) { LOG.finer("Read() : " + (a[0] & 255)); } return a[0] & 0xFF; // The byte } } // If we've reached EOF, there's nothing to do but close(). close(); return -1; } /** * {@inheritDoc} */ @Override public int read(byte[] a, int offset, int length) throws IOException { if (closed) { return -1; } if (0 == length) { return 0; } int i = local_read(a, offset, length); if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) { LOG.finer("Read(byte[], int, " + length + "), bytes read = " + i); } // If we've reached EOF; there's nothing to do but close(). if (i == -1) { close(); } return i; } /** * Send a sequential ACK and selective ACKs for all of * the queued messages. * * @param seqnAck the sequence number being sequential ACKed */ private void sendACK(int seqnAck) { // No need to sync on inputQueue, acking as many as we can is want we want List<Integer> selectedAckList = new ArrayList<Integer>(); List<IQElt> queue; synchronized (inputQueue) { queue = new ArrayList<IQElt>(inputQueue); } Iterator<IQElt> eachInQueue = queue.iterator(); while (eachInQueue.hasNext() && (selectedAckList.size() < Defs.MAXQUEUESIZE)) { IQElt anIQElt = eachInQueue.next(); if (anIQElt.seqnum > seqnAck) { if (!anIQElt.ackd) { selectedAckList.add(anIQElt.seqnum); anIQElt.ackd = true; } } } // PERMIT DUPLICATE ACKS. Just a list and one small message. sendACK(seqnAck, selectedAckList); } /** * Build an ACK message. The message provides a sequential ACK count and * an optional list of selective ACKs. * * @param seqnAck the sequence number being sequential ACKed * @param sackList a list of selective ACKs. Must be sorted in increasing * order. */ private void sendACK(int seqnAck, List<Integer> sackList) { ByteArrayOutputStream bos = new ByteArrayOutputStream((1 + sackList.size()) * 4); DataOutputStream dos = new DataOutputStream(bos); try { dos.writeInt(seqnAck); for (Integer aSackList : sackList) { dos.writeInt(aSackList); } dos.close(); bos.close(); Message ACKMsg = new Message(); MessageElement elt = new ByteArrayMessageElement(Defs.ACK_ELEMENT_NAME, Defs.MIME_TYPE_ACK, bos.toByteArray(), null); ACKMsg.addMessageElement(Defs.NAMESPACE, elt); outgoing.send(ACKMsg); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("SENT ACK, seqn#" + seqnAck + " and " + sackList.size() + " SACKs "); } } catch (IOException e) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -