|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sun.grizzly.filter.CustomProtocolParser
public class CustomProtocolParser
Filter for parsing Messages of the CustomProtocol. The parsing is done in two stages.
First the Protocol Header bytes are read. With this anMessageBase is constructed
Then the payload of the message is read and on completion
a MessageBase is handled to the next Filter in the chain.
CustomProtocolParser tries to avoid byte copying. Therefore when
a new Message has being parsed depending on its size and left space
a ByteBuffer slice is created
or the message takes ownership of the complete ByteBuffer and gives
WorkerThread a completely new ByteBuffer.
| Field Summary | |
|---|---|
protected java.nio.ByteBuffer |
byteBuffer
|
protected BytesTrafficListener |
bytesArrivedListener
|
protected boolean |
debug
|
protected boolean |
expectingMoreData
|
protected boolean |
hasMoreBytesToParse
|
protected MessageBase |
msg
|
protected int |
nextMsgStartPos
|
protected ReplyMessageFactory |
replyMessageFactory
|
protected SSLConfig |
sslConfig
|
| Fields inherited from interface com.sun.grizzly.ProtocolParser |
|---|
MESSAGE, PARSER |
| Constructor Summary | |
|---|---|
protected |
CustomProtocolParser()
|
protected |
CustomProtocolParser(SSLConfig sslConfig)
|
| Method Summary | |
|---|---|
static ParserProtocolFilter |
createParserProtocolFilter(BytesTrafficListener listener,
ReplyMessageFactory replyInputStreamFactory,
SSLConfig sslConfig)
Since CustomProtocolParser is a statefull createParserProtocolFilter this indicates proper initilization. |
MessageBase |
getNextMessage()
When getNextMessage() is called parser first has to check if
anouther incomplete MessageBase is in the current ByteBuffer. |
boolean |
hasMoreBytesToParse()
Are there more bytes to be parsed in the current ByteBuffer |
boolean |
hasNextMessage()
Indicates whether the buffer has a complete message that can be returned from getNextMessage
Basic algorithm :
1. |
boolean |
isExpectingMoreData()
Does message need more bytes to be read in? |
boolean |
releaseBuffer()
if this parser is expecting more bytes to be read then the current bytebuffer will not be released. |
void |
setBytesArrivedListener(BytesTrafficListener bytesArrivedListener)
|
void |
setReplyMessageFactory(ReplyMessageFactory replyMessageFactory)
Clients can be regsitered on a ReplyMessage The Factory holds to these Messages |
void |
startBuffer(java.nio.ByteBuffer byteBuffer)
Gets called when new bytes have been added to the current ByteBuffer. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected int nextMsgStartPos
protected boolean hasMoreBytesToParse
protected boolean expectingMoreData
protected MessageBase msg
protected java.nio.ByteBuffer byteBuffer
protected BytesTrafficListener bytesArrivedListener
protected ReplyMessageFactory replyMessageFactory
protected boolean debug
protected SSLConfig sslConfig
| Constructor Detail |
|---|
protected CustomProtocolParser()
protected CustomProtocolParser(SSLConfig sslConfig)
| Method Detail |
|---|
public static ParserProtocolFilter createParserProtocolFilter(BytesTrafficListener listener,
ReplyMessageFactory replyInputStreamFactory,
SSLConfig sslConfig)
listener - gets notified if a chunk of bytes arrivesreplyInputStreamFactory - need for creating a replymessagesslConfig - configures parser tob used wit SSL
public MessageBase getNextMessage()
getNextMessage() is called parser first has to check if
anouther incomplete MessageBase is in the current ByteBuffer.
If yes hasMoreBytesToParse() returns true and <@link CustomProtocolParser will get
another chance to parse the current ByteBuffer starting at position nextMsgStartPos
The current MessageBase now takes ownership of a slice of the
current ByteBuffer. By slicing the costs of an byte array copy are avoided.
getNextMessage in interface ProtocolParser<MessageBase>public boolean hasMoreBytesToParse()
ByteBuffer
hasMoreBytesToParse in interface ProtocolParser<MessageBase>public boolean hasNextMessage()
getNextMessage
Basic algorithm :
1. When in state no message check if there are enough bytes to parse the header.
1.1 If not check if there is enough space in ByteBuffer to hold the needed bytes.
1.1.1 If not create a new ByteBuffer with current header bytes.
Remark: This should not happen very often so the copy is affordable.
1.1.2 return and isExpectingMoreData() should be true.
2. When no uncomplete message parse header and construct a new MessageBase
2.1 If more bytes are needed and ByteBuffer has enough space return and expect more bytes.
2.1.1 if not enough space hand MessageBase the current ByteBuffer and
give Grizzly a new one and return and expect more bytes.
3. The ByteBuffer contains enough bytes to construct a complete
MessageBase so parser does not expect more
bytes and getNextMessage will be called.
hasNextMessage in interface ProtocolParser<MessageBase>public boolean isExpectingMoreData()
isExpectingMoreData in interface ProtocolParser<MessageBase>T.public boolean releaseBuffer()
WorkerThread. Normally this would not
be necessary but since MessageDispatcher is going to hand this byteBuffer
to another Thread a new byteBuffer is given to WorkerThread
releaseBuffer in interface ProtocolParser<MessageBase>public void startBuffer(java.nio.ByteBuffer byteBuffer)
ByteBuffer.
startBuffer in interface ProtocolParser<MessageBase>byteBuffer - the current ByteBufferpublic void setBytesArrivedListener(BytesTrafficListener bytesArrivedListener)
bytesArrivedListener - a listener getting notified whene byte chunks arrive.public void setReplyMessageFactory(ReplyMessageFactory replyMessageFactory)
replyMessageFactory -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||