/* * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the license at * https://glassfish.dev.java.net/public/CDDLv1.0.html or * glassfish/bootstrap/legal/CDDLv1.0.txt. * See the License for the specific language governing * permissions and limitations under the License. * * When distributing Covered Code, include this CDDL * Header Notice in each file and include the License file * at glassfish/bootstrap/legal/CDDLv1.0.txt. * If applicable, add the following below the CDDL Header, * with the fields enclosed by brackets [] replaced by * you own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * Copyright 2007 Sun Microsystems, Inc. All rights reserved. */ package com.sun.grizzly; import java.nio.ByteBuffer; /** * An interface that knows how to parse bytes into a protocol data unit. * * @author Charlie Hunt */ public interface ProtocolParser { /** * Is this ProtocolParser expecting more data ? * * This method is typically called after a call to parseBytes() * to determine if the ByteBuffer which has been parsed * contains a partial message * * @return - true if more bytes are needed to construct a * message; false, if no * additional bytes remain to be parsed into a T. * Note that if no partial message exists, this method should * return false. */ public boolean isExpectingMoreData(); /** * Are there more bytes to be parsed in the ByteBuffer given * to this ProtocolParser's setBuffer ? * * This method is typically called after a call to parseBytes() * to determine if the ByteBuffer has more bytes which need to * parsed into a message. * * @return true if there are more bytes to be parsed. * Otherwise false. */ public boolean hasMoreBytesToParse(); /** * Get the next complete message from the buffer, which can then be * processed by the next filter in the protocol chain. Because not all * filters will understand protocol messages, this method should also * set the position and limit of the buffer at the start and end * boundaries of the message. Filters in the protocol chain can * retrieve this message via context.getAttribute(MESSAGE) * * @return The next message in the buffer. If there isn't such a message, * return null. * */ public T getNextMessage(); /** * Indicates whether the buffer has a complete message that can be * returned from getNextMessage. Smart implementations of * this will set up all the information so that an actual call to * getNextMessage doesn't need to re-parse the data. */ public boolean hasNextMessage(); /** * Set the buffer to be parsed. This method should store the buffer and * its state so that subsequent calls to getNextMessage * will return distinct messages, and the buffer can be restored after * parsing when the releaseBuffer method is called. */ public void startBuffer(ByteBuffer bb); /** * No more parsing will be done on the buffer passed to * startBuffer. * Set up the buffer so that its position is the first byte that was * not part of a full message, and its limit is the original limit of * the buffer. * * @return -- true if the parser has saved some state (e.g. information * data in the buffer that hasn't been returned in a full message); * otherwise false. If this method returns true, the framework will * make sure that the same parser is used to process the buffer after * more data has been read. */ public boolean releaseBuffer(); /** * Used to associate a particular parser with a connection */ public static String PARSER = "ProtocolParser"; /** * Holds the message returned from getNextMessage for * use by downstream filters */ public static String MESSAGE = "ProtocolMessage"; }