users@grizzly.java.net

Re: ProtocolParser implementation II

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Fri, 09 May 2008 10:54:19 -0700

Hi Ken,

we are currently setting up our JavaOne session (start @ 13h30 PST)...we
will try to help you after (as soon as possible).

Thanks

-- Jeanfrancois

Ken--_at_newsgroupstats.hk wrote:
> I cannot compile Scott's code. It just a some outline.
>
> Now, I got some problem in savedBuffer.clear of releaseBuffer(). Please
> help. My protocol is simple: <STX>data</STX>
>
>
> /*
> * MyProtocolParser.java
> *
> * Created on 2008年05月08日 星期四, 下午9:06
> *
> * To change this template, choose Tools | Template Manager
> * and open the template in the editor.
> */
>
> package grizzlyserver;
>
> import com.sun.grizzly.ProtocolParser;
> import java.nio.BufferUnderflowException;
> import java.nio.ByteBuffer;
>
> /**
> *
> * @author kentsang
> */
> public class MyProtocolParser implements ProtocolParser<ByteBuffer> {
>
> private final static int BUFFER_SIZE = 16000;
>
> public static final byte STX = 0x02;
>
> public static final byte ETX = 0x03;
>
> private ByteBuffer packetBuffer = null;
>
> private ByteBuffer savedBuffer = null;
>
> private boolean partial = true;
>
> private byte[] data = new byte[BUFFER_SIZE];
>
> private int start = 0;
>
> private int pos = 0;
>
> private int oriLimit = 0;
>
> private int limit = 0;
>
> private int counter = 0;
>
> private boolean printArray = true;
> /**
> * Creates a new instance of MyProtocolParser
> */
> public MyProtocolParser() {
> }
>
> public void startBuffer(ByteBuffer bb) {
> System.out.println("startBuffer / bb = " + bb);
> packetBuffer = null;
> savedBuffer = bb;
> savedBuffer.flip();
> System.out.println("startBuffer (after flip()) bb.cap = " +
> savedBuffer);
> partial = false;
> oriLimit = savedBuffer.limit();
> }
>
> public boolean hasMoreBytesToParse() {
> System.out.println("hasMoreBytesToParse()");
> if (savedBuffer == null) {
> System.out.println("hasMoreBytesToParse() = false (as
> savedBuffer = null) ");
> return false;
> }
> boolean result = pos < savedBuffer.limit();
> System.out.println("hasMoreBytesToParse() pos / bb / return = " +
> pos + " / " + savedBuffer + " / " + result);
> return result;
> }
>
> public boolean isExpectingMoreData() {
> System.out.println("isExpectingMoreData() = " + partial);
> return partial;
> }
>
> public ByteBuffer getNextMessage() {
> System.out.println("\r\n\r\ngetMessage() = " + packetBuffer +
> "\r\n");
> return packetBuffer;
> }
>
> public boolean hasNextMessage() {
> System.out.println("hasNextMessage() pos / savedBuffer = " + pos + "
> / "+ savedBuffer);
> while (savedBuffer.hasRemaining()) {
> boolean flag = false;
> byte b = savedBuffer.get();
> data[pos] = b;
>
> System.out.println("counter / b = " + counter + " / " +
> Integer.toHexString((int)b));
> if (b == STX) {
> System.out.println("STX !!!! pos = " + pos);
> start = pos;
> }
> if (start != -1 && b == ETX) {
> System.out.println("ETX !!!! pos = " + pos);
> try {
> System.out.println("len / remainning() = " + pos + " / "
> + savedBuffer.remaining());
> int length = pos - start + 1;
> byte[] newBuffer = new byte[length];
> printByteBuffer();
> System.arraycopy(data, start, newBuffer, 0, length);
> for (int i = 0 ; i < newBuffer.length ; i++) {
>
> System.out.print(Integer.toHexString((int)newBuffer[i]) + " ");
> }
> packetBuffer = ByteBuffer.wrap(newBuffer);
> partial = false;
> start = -1;
> flag = true;
> } catch (Exception e) {
> e.printStackTrace();
> }
> } else {
> partial = true;
> }
> pos++;
> counter++;
> if (flag){
> break;
> }
> }
>
> System.out.println("hasNextMessage() = " + !partial);
> return !partial;
> }
>
> public boolean releaseBuffer() {
> System.out.println("releaseBuffer() pos / savedBuffer = " + pos + "
> / " + savedBuffer);
> if (!hasMoreBytesToParse()) {
> System.out.println("buffer.clear()!");
> savedBuffer.clear();
> } else {
> //savedBuffer.position(pos);
> System.out.println("releaseBuffer position(pos) = " + pos);
> //savedBuffer.limit(oriLimit);
> }
> System.out.println("releaseBuffer() (before return) pos /
> savedBuffer / result = " + pos + " / " + savedBuffer + " / " + partial);
> return partial;
> }
>
> public void printByteArray(){
> if (!printArray) return;
> System.out.print("PrintByteArray :");
> for (int i = 0 ; i < data.length ; i++) {
> System.out.print(Integer.toHexString((int)data[i]) + " ");
> }
> }
> public void printByteBuffer(){
> if (!printArray) return;
> System.out.print("PrintByteBuffer (savedBuffer = ) = " +
> savedBuffer);
> for (int i = 0 ; i < savedBuffer.limit() ; i++) {
> System.out.print(Integer.toHexString((int)savedBuffer.get(i)) +
> " ");
> }
> }
> }
>