users@grizzly.java.net

Re: Grizzly problem when end of query not found in ProtocolParser

From: Survivant 00 <survivant00_at_gmail.com>
Date: Wed, 9 Jul 2008 11:29:17 -0400

it's only a demo, so I could put a max length.. and choose a fixed header
too, but I want to keep it more dynamic. There will be a MAX lenght.. we
don't want to parse to the infiny.. like a message over 5000 bytes and
didn't find a valid query in buffer.. that mean that the client send crap..
so we must close is connection.

I post the first implementation in grizzly of my demo.

I think that I just didn't start with the right example.


2008/7/9 Oleksiy Stashok <Oleksiy.Stashok_at_sun.com>:

> Hi,
>
> do you have any limit on incoming message size? It could make code easier
> and more optimal, because you'll avoid ByteBuffer reallocation.
> otherwise the code part you posted here looks good, can you pls. attach
> whole parser implementation, may be it's something wrong in other parts?
>
> Thanks.
>
> WBR,
> Alexey.
>
>
> On Jul 6, 2008, at 12:27 , Survivant 00 wrote:
>
> 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;
>> }
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>
>