package mailinglist; import java.nio.ByteBuffer; import com.sun.grizzly.ProtocolParser; import com.sun.grizzly.util.WorkerThread; /** * @author kloh * */ public class MyProtocolParser implements ProtocolParser{ private ByteBuffer mSavedBuffer; private int mSavedPosition; private boolean mWantsMoreData; private boolean mHasUnparsedData; private String mMessage; private static int sDEFAULT_BYTEBUFFER_SIZE = 8192; /* (non-Javadoc) * @see com.sun.grizzly.ProtocolParser#getNextMessage() */ public String getNextMessage() { if (this.mMessage == null) this.mMessage = extractMessage(); final String lTmp = new String(this.mMessage); this.mMessage = null; return lTmp; } /* (non-Javadoc) * @see com.sun.grizzly.ProtocolParser#hasMoreBytesToParse() */ public boolean hasMoreBytesToParse() { if (this.mHasUnparsedData) System.out.println("I have more bytes to parse!"); return this.mHasUnparsedData; } /* (non-Javadoc) * @see com.sun.grizzly.ProtocolParser#hasNextMessage() */ public boolean hasNextMessage() { if (this.mMessage == null) this.mMessage = extractMessage(); return this.mMessage != null; } /* (non-Javadoc) * @see com.sun.grizzly.ProtocolParser#isExpectingMoreData() */ public boolean isExpectingMoreData() { if (this.mWantsMoreData) System.out.println("I want more data to come!"); return this.mWantsMoreData; } /* (non-Javadoc) * @see com.sun.grizzly.ProtocolParser#releaseBuffer() */ public boolean releaseBuffer() { if (this.mWantsMoreData) { reallocateBuffer(); } else { this.mSavedBuffer = ByteBuffer.allocate(sDEFAULT_BYTEBUFFER_SIZE); } return this.mWantsMoreData; } /* (non-Javadoc) * @see com.sun.grizzly.ProtocolParser#startBuffer(java.nio.ByteBuffer) */ public void startBuffer(final ByteBuffer pByteBuffer) { this.mSavedBuffer = pByteBuffer; this.mSavedBuffer.flip(); } private String extractMessage() { if (!this.mSavedBuffer.hasRemaining()) { this.mWantsMoreData = false; this.mHasUnparsedData = false; this.mSavedBuffer.clear(); return null; } if (this.mSavedBuffer.remaining() < 36) { this.mWantsMoreData = true; this.mHasUnparsedData = false; System.out.println("Received incomplete receive file message"); return null; } this.mSavedPosition = this.mSavedBuffer.position(); final byte[] lUUIDAsBytes = new byte[36]; this.mSavedBuffer.get(lUUIDAsBytes); final String lUUID = new String(lUUIDAsBytes); System.out.println("Received message for id " + lUUID); if (this.mSavedBuffer.remaining() < 36 + 36) { this.mWantsMoreData = true; this.mHasUnparsedData = false; System.out.println("Received incomplete receive file message"); return null; } final byte[] lANOTHER_ID_AS_BYTES = new byte[36]; this.mSavedBuffer.get(lANOTHER_ID_AS_BYTES); final String lANOTHER_ID = new String(lANOTHER_ID_AS_BYTES); final byte[] lTHIRD_ID_AS_BYTES = new byte[36]; this.mSavedBuffer.get(lTHIRD_ID_AS_BYTES); final String lTHIRD_ID = new String(lTHIRD_ID_AS_BYTES); final String lMessage = new String(lUUID + lANOTHER_ID + lTHIRD_ID); System.out.println("Received receive message: " + lMessage); if (this.mSavedBuffer.remaining() == 0) { this.mHasUnparsedData = false; this.mWantsMoreData = false; } else { this.mHasUnparsedData = true; this.mWantsMoreData = false; this.mSavedPosition = this.mSavedBuffer.position(); } return lMessage; } private void reallocateBuffer() { if (this.mSavedBuffer.position() != this.mSavedPosition) this.mSavedBuffer.position(this.mSavedPosition); int lNewBufferSize = this.mSavedBuffer.remaining() == 0 ? sDEFAULT_BYTEBUFFER_SIZE * 2 : sDEFAULT_BYTEBUFFER_SIZE; while (lNewBufferSize <= this.mSavedBuffer.remaining()) lNewBufferSize += sDEFAULT_BYTEBUFFER_SIZE; System.out.println("Reallocate WorkerThreads ByteBuffer to " + lNewBufferSize + " b."); final ByteBuffer lTmpBuffer = ByteBuffer.allocate(lNewBufferSize); lTmpBuffer.put(this.mSavedBuffer); this.mSavedBuffer = lTmpBuffer; ((WorkerThread) Thread.currentThread()).setByteBuffer(this.mSavedBuffer); } }