Hi Ken,
my fault, example was not done right. I've fixed it.
Here is how the parser filter should look like [1].
Thank you.
WBR,
Alexey.
[1]
public class TestMessageParserFilter extends FilterAdapter {
private static final Attribute<Integer> stateAttr =
Grizzly.DEFAULT_ATTRIBUTE_BUILDER.createAttribute("TestParseState");
private static final Attribute<TestMessage> preparsedMessageAttr =
Grizzly
.DEFAULT_ATTRIBUTE_BUILDER.createAttribute("PreparsedTestMessage");
private static final int BUFFER_SIZE = 8192;
private byte[] buffer = new byte[BUFFER_SIZE];
private int index = 0;
@Override
public NextAction handleRead(FilterChainContext ctx,
NextAction nextAction) throws IOException {
Connection connection = ctx.getConnection();
TestMessage message = preparsedMessageAttr.get(connection);
Integer parseState = stateAttr.get(connection);
if (message == null) {
message = new TestMessage();
parseState = new Integer(0);
}
StreamReader reader = ctx.getStreamReader();
boolean isParsing = true;
while (isParsing) {
switch (parseState) {
case 0: // STX
if (reader.availableDataSize() >= 1) {
if (reader.readByte() == TestMessage.STX) {
index = 0;
parseState++;
} else {
// drop the byte
}
} else {
isParsing = false;
}
break;
case 1: // ETX
if (reader.availableDataSize() >= 1) {
byte b = reader.readByte();
if (b == TestMessage.ETX) {
if (index > 0) {
byte[] body = new byte[index];
System.arraycopy(buffer, 0, body, 0,
index);
message.setBody(body);
parseState++;
isParsing = false;
} else {
parseState = new Integer(0);
}
} else {
buffer[index] = b;
index++;
}
} else {
isParsing = false;
}
break;
default:
break;
}
}
if (parseState < 2) { // Not enough data to parse whole
message
// Save the parsing state
preparsedMessageAttr.set(connection, message);
stateAttr.set(connection, parseState);
// Stop the filterchain execution until more data available
return ctx.getStopAction();
} else {
// Remove intermediate parsing state
preparsedMessageAttr.remove(connection);
stateAttr.remove(connection);
// Set the parsed message on context
ctx.setMessage(message);
System.out.println("Message = " + new
String(message.getBody()));
return nextAction;
}
}
/**
* Post read is called to let FilterChain cleanup resources.
*
* @param ctx {_at_link FilterChainContext}
* @param nextAction default {_at_link NextAction} next step
instruction for the {_at_link FilterChain}
* @return {_at_link NextAction} next step instruction for the
{_at_link FilterChain}
* @throws IOException
*/
@Override
public NextAction postRead(FilterChainContext ctx, NextAction
nextAction)
throws IOException {
final StreamReader reader = ctx.getStreamReader();
// Check, if there is some data remaining in the input stream
if (reader.availableDataSize() > 0) {
// if yes - rerun the parser filter to parse next message
return ctx.getRerunChainAction();
}
return nextAction;
}
}
On May 26, 2009, at 20:26 , Ken--_at_newsgroupstats.hk wrote:
>
> Hi All,
>
> I got a strange problem with MessageParserFilter with Grizzly 2.0.
>
> My procotol is:
>
> <STX><BODY><ETX>
>
> I follow the sample source code under
> org\glassfish\grizzly\samples\filterchain. The test NIO server just
> stop
> reading / parsing after first (or sometime second) completed message.
> However, if I sleep 10 millis at client side between sending
> message. It
> works.
>
> Please help.
>
>
> writer = connection.getStreamWriter();
>
> // Enable standalone mode for this connection
> // (don't use Filter chains or other I/O event processors)
> connection.setProcessorSelector(new StandaloneProcessorSelector());
>
> for (int i = 0 ; i < 50 ; i++) {
> TestMessage sentMessage = new TestMessage();
> byte body[] = new String("Test Message Number : " + i).getBytes();
> sentMessage.setBody(body);
> writeTestMessage(writer, sentMessage);
>
> // need to sleep a while, otherwise server will stop reading
> after first
> completed message??
> Thread.sleep(2L);
> }
>
>
> http://www.nabble.com/file/p23728698/src.zip src.zip
>
>
>
> --
> View this message in context: http://www.nabble.com/Grizzly-2.0-MessageParserFilter-Problem-tp23728698p23728698.html
> Sent from the Grizzly - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>