Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ProtocolParser |
|
| 0.0;0 |
1 | /* | |
2 | * | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
4 | * | |
5 | * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. | |
6 | * | |
7 | * The contents of this file are subject to the terms of either the GNU | |
8 | * General Public License Version 2 only ("GPL") or the Common Development | |
9 | * and Distribution License("CDDL") (collectively, the "License"). You | |
10 | * may not use this file except in compliance with the License. You can obtain | |
11 | * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html | |
12 | * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific | |
13 | * language governing permissions and limitations under the License. | |
14 | * | |
15 | * When distributing the software, include this License Header Notice in each | |
16 | * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. | |
17 | * Sun designates this particular file as subject to the "Classpath" exception | |
18 | * as provided by Sun in the GPL Version 2 section of the License file that | |
19 | * accompanied this code. If applicable, add the following below the License | |
20 | * Header, with the fields enclosed by brackets [] replaced by your own | |
21 | * identifying information: "Portions Copyrighted [year] | |
22 | * [name of copyright owner]" | |
23 | * | |
24 | * Contributor(s): | |
25 | * | |
26 | * If you wish your version of this file to be governed by only the CDDL or | |
27 | * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
28 | * elects to include this software in this distribution under the [CDDL or GPL | |
29 | * Version 2] license." If you don't indicate a single choice of license, a | |
30 | * recipient has the option to distribute your version of this file under | |
31 | * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
32 | * its licensees as provided above. However, if you add GPL Version 2 code | |
33 | * and therefore, elected the GPL Version 2 license, then the option applies | |
34 | * only if the new code is made subject to such option by the copyright | |
35 | * holder. | |
36 | * | |
37 | */ | |
38 | ||
39 | package com.sun.grizzly; | |
40 | ||
41 | import java.nio.ByteBuffer; | |
42 | ||
43 | /** | |
44 | * An interface that knows how to parse bytes into a protocol data unit. | |
45 | * | |
46 | * @author Charlie Hunt | |
47 | */ | |
48 | public interface ProtocolParser<T> { | |
49 | ||
50 | /** | |
51 | * Is this ProtocolParser expecting more data ? | |
52 | * | |
53 | * This method is typically called after a call to <code>parseBytes()</code> | |
54 | * to determine if the {@link ByteBuffer} which has been parsed | |
55 | * contains a partial message | |
56 | * | |
57 | * @return - <tt>true</tt> if more bytes are needed to construct a | |
58 | * message; <tt>false</tt>, if no | |
59 | * additional bytes remain to be parsed into a <code>T</code>. | |
60 | * Note that if no partial message exists, this method should | |
61 | * return false. | |
62 | */ | |
63 | public boolean isExpectingMoreData(); | |
64 | ||
65 | /** | |
66 | * Are there more bytes to be parsed in the {@link ByteBuffer} given | |
67 | * to this ProtocolParser's <code>setBuffer</code> ? | |
68 | * | |
69 | * This method is typically called after a call to <code>parseBytes()</code> | |
70 | * to determine if the {@link ByteBuffer} has more bytes which need to | |
71 | * parsed into a message. | |
72 | * | |
73 | * @return <tt>true</tt> if there are more bytes to be parsed. | |
74 | * Otherwise <tt>false</tt>. | |
75 | */ | |
76 | public boolean hasMoreBytesToParse(); | |
77 | ||
78 | ||
79 | /** | |
80 | * Get the next complete message from the buffer, which can then be | |
81 | * processed by the next filter in the protocol chain. Because not all | |
82 | * filters will understand protocol messages, this method should also | |
83 | * set the position and limit of the buffer at the start and end | |
84 | * boundaries of the message. Filters in the protocol chain can | |
85 | * retrieve this message via context.getAttribute(MESSAGE) | |
86 | * | |
87 | * @return The next message in the buffer. If there isn't such a message, | |
88 | * return <code>null.</code> | |
89 | * | |
90 | */ | |
91 | public T getNextMessage(); | |
92 | ||
93 | /** | |
94 | * Indicates whether the buffer has a complete message that can be | |
95 | * returned from <code>getNextMessage</code>. Smart implementations of | |
96 | * this will set up all the information so that an actual call to | |
97 | * <code>getNextMessage</code> doesn't need to re-parse the data. | |
98 | */ | |
99 | public boolean hasNextMessage(); | |
100 | ||
101 | /** | |
102 | * Set the buffer to be parsed. This method should store the buffer and | |
103 | * its state so that subsequent calls to <code>getNextMessage</code> | |
104 | * will return distinct messages, and the buffer can be restored after | |
105 | * parsing when the <code>releaseBuffer</code> method is called. | |
106 | */ | |
107 | public void startBuffer(ByteBuffer bb); | |
108 | ||
109 | /** | |
110 | * No more parsing will be done on the buffer passed to | |
111 | * <code>startBuffer.</code> | |
112 | * Set up the buffer so that its position is the first byte that was | |
113 | * not part of a full message, and its limit is the original limit of | |
114 | * the buffer. | |
115 | * | |
116 | * @return -- true if the parser has saved some state (e.g. information | |
117 | * data in the buffer that hasn't been returned in a full message); | |
118 | * otherwise false. If this method returns true, the framework will | |
119 | * make sure that the same parser is used to process the buffer after | |
120 | * more data has been read. | |
121 | */ | |
122 | public boolean releaseBuffer(); | |
123 | ||
124 | /** | |
125 | * Used to associate a particular parser with a connection | |
126 | */ | |
127 | public static String PARSER = "ProtocolParser"; | |
128 | ||
129 | /** | |
130 | * Holds the message returned from <code>getNextMessage</code> for | |
131 | * use by downstream filters | |
132 | */ | |
133 | public static String MESSAGE = "ProtocolMessage"; | |
134 | } | |
135 |