com.sun.grizzly.filter
Class CustomProtocolParser

java.lang.Object
  extended by com.sun.grizzly.filter.CustomProtocolParser
All Implemented Interfaces:
ProtocolParser<MessageBase>

public class CustomProtocolParser
extends Object
implements ProtocolParser<MessageBase>

Filter for parsing Messages of the CustomProtocol. The parsing is done in two stages.

First the Protocol Header bytes are read. With this an MessageBase 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.

Version:
1.0
Author:
John Vieten 21.06.2008

Field Summary
protected  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(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

nextMsgStartPos

protected int nextMsgStartPos

hasMoreBytesToParse

protected boolean hasMoreBytesToParse

expectingMoreData

protected boolean expectingMoreData

msg

protected MessageBase msg

byteBuffer

protected ByteBuffer byteBuffer

bytesArrivedListener

protected BytesTrafficListener bytesArrivedListener

replyMessageFactory

protected ReplyMessageFactory replyMessageFactory

debug

protected boolean debug

sslConfig

protected SSLConfig sslConfig
Constructor Detail

CustomProtocolParser

protected CustomProtocolParser()

CustomProtocolParser

protected CustomProtocolParser(SSLConfig sslConfig)
Method Detail

createParserProtocolFilter

public static ParserProtocolFilter createParserProtocolFilter(BytesTrafficListener listener,
                                                              ReplyMessageFactory replyInputStreamFactory,
                                                              SSLConfig sslConfig)
Since CustomProtocolParser is a statefull createParserProtocolFilter this indicates proper initilization.

Parameters:
listener - gets notified if a chunk of bytes arrives
replyInputStreamFactory - need for creating a replymessage
sslConfig - configures parser tob used wit SSL
Returns:
ParserProtocolFilter for creating CustomProtocolParser

getNextMessage

public MessageBase getNextMessage()
When 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.

Specified by:
getNextMessage in interface ProtocolParser<MessageBase>
Returns:
MessageBase the parsed Message

hasMoreBytesToParse

public boolean hasMoreBytesToParse()
Are there more bytes to be parsed in the current ByteBuffer

Specified by:
hasMoreBytesToParse in interface ProtocolParser<MessageBase>
Returns:
true if there are more bytes to be parsed. Otherwise false.

hasNextMessage

public boolean hasNextMessage()
Indicates whether the buffer has a complete message that can be returned from 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.

Specified by:
hasNextMessage in interface ProtocolParser<MessageBase>

isExpectingMoreData

public boolean isExpectingMoreData()
Does message need more bytes to be read in?

Specified by:
isExpectingMoreData in interface ProtocolParser<MessageBase>
Returns:
- true if more bytes are needed to construct a message; false, if no additional bytes remain to be parsed into a T.

releaseBuffer

public boolean releaseBuffer()
if this parser is expecting more bytes to be read then the current bytebuffer will not be released. On the other hand if parser is done with bytebuffer then a new empty bytebuffer is handed to 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

Specified by:
releaseBuffer in interface ProtocolParser<MessageBase>
Returns:
-- 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.

startBuffer

public void startBuffer(ByteBuffer byteBuffer)
Gets called when new bytes have been added to the current ByteBuffer.

Specified by:
startBuffer in interface ProtocolParser<MessageBase>
Parameters:
byteBuffer - the current ByteBuffer

setBytesArrivedListener

public void setBytesArrivedListener(BytesTrafficListener bytesArrivedListener)
Parameters:
bytesArrivedListener - a listener getting notified whene byte chunks arrive.

setReplyMessageFactory

public void setReplyMessageFactory(ReplyMessageFactory replyMessageFactory)
Clients can be regsitered on a ReplyMessage The Factory holds to these Messages

Parameters:
replyMessageFactory -


Copyright © 2013 Oracle Corporation. All Rights Reserved.