I'm trying to create a custom ProtocolParser. The problem is when we don't
find 'EOQ' in the buffer, I want to put the buffer back into grizzly and do
a loop again to see if the string will be find the next time.
but it's only loop once.. maybe I started from the wrong sample. I think I
have to move my login into hasMoreBytesToParse .. and use startBuffer to get
the bytebuffer from grizzly and when the OEQ in not found in
hasMoreBytesToParse, I put the buffer back into grizzly by
workerThread.setByteBuffer(savedBuffer); where savedBuffer is the buffer
receive in the startBuffer ?
here a sample of my code that doesn't work.. just want to confirm if my
previous tought were OK.
but it's stop at : hasMoreBytesToParse the second time.
listening for incomming TCP Connections on port : 5000
isExpectingMoreData
hasMoreBytesToParse
startBuffer
hasNextMessage
msg=sa[19~[19~
hasMoreBytesToParse
here what I have
// method after hasMoreBytesToParse if it's true
public void startBuffer(ByteBuffer bb) {
System.out.println("startBuffer");
if (debug) System.out.println("startBuffer / bb = " + bb);
savedBuffer = bb;
savedBuffer.flip();
if (debug) System.out.println("startBuffer / bb = " + savedBuffer);
if (debug) printByteBuffer();
limit = savedBuffer.limit();
//partial = false;
}
public boolean hasNextMessage() {
System.out.println("hasNextMessage");
String query = null;
if (debug) System.out.print("hasNextMessage() before savedBuffer = "
+ savedBuffer);
if (debug) printByteBuffer();
if (savedBuffer == null) return false;
if (savedBuffer.hasRemaining()) {
// decode the buffer
String msg=null;
try {
msg = f_asciiDecoder.decode(savedBuffer).toString();
} catch (CharacterCodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("msg=" + msg);
int index = msg.indexOf("[eoq]");
if(index>-1){
query = msg.substring(0,index);
//System.out.println("Query = " + query);
// We need to kept what is after the EOQ
f_cumulatifBB.clear();
f_cumulatifBB.put(msg.substring(index +
"[eoq]".length()).getBytes());
WorkerThread workerThread = (WorkerThread)
Thread.currentThread();
workerThread.setByteBuffer(f_cumulatifBB);
partial = false;
} else {
//System.out.println("no EOQ in this iteration");
int newCapacity = savedBuffer.capacity() + LIMITBB;
ByteBuffer newBuffer =
ByteBufferFactory.allocateView(newCapacity, savedBuffer.isDirect());
newBuffer.put(savedBuffer);
WorkerThread workerThread = (WorkerThread)
Thread.currentThread();
savedBuffer = newBuffer;
workerThread.setByteBuffer(savedBuffer);
partial = true;
}
}
if (debug) System.out.println("hasNextMessage() result = " +
partial);
return !partial;
}