users@grizzly.java.net

Re: How to implement the client's message flow based on async read/write?

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Fri, 13 Jun 2008 10:27:09 +0200

Hello Yi,

> Great thanks for your help! European soccer champinship also attract
> lot of us :-) Enjoy! It's really a good news for your samples, the
> sooner the better :-)
> In my case, I invoke the ProtocolFilter in
> CallbackHandler.onConnect() to send the request to the server and it
> works, or in another word, the CallbackHandler.onConnect() kicks off
> the protocol interaction. Because I really want the ProtocolFilter
> to manage its own state/operation itselft without interaction with
> the CallbackHandler.
> But there are still problems, those are about
> 1. Context operation type switching or SelectionKey interests
> switching
> Could you pls. give a brief explanation of these switching, maybe
> something about Context.setCurrentOpType(), KeyRegistrationState, or
> anything else?
context.getSelectorHandler().register(key, SelectionKey.OP_XXXX);


> 2. Asynchronously reading/writing in ProtocolFilters
> In my case, I use an asynchronous reading operaiton to receive the
> server's simple response. Unfortunately, this leads to about 50% cpu
> polling :-(.
If you can provide your sources - I can check why it is happening.


> 3. State management in ProtocolFilter
> In my case, I try to test a stateful protocol interaction by
> extending the interaction process to this: the request-response
> interaction will be repeated 5 times and then the client initiates
> to close the connection. Could you pls. give a brief explanation
> about how to implement state management in ProtocolFilter?
Probably you need ThreadAttachment, which you can detach from current
WorkerThread and attach it (SelectionKey.attach(threadAttachment)) to
the SelectionKey. This way you will be able to save state in
ThreadAttachment, associated with the specific connection.

Please take a look at the com.sun.grizzly.filter.ParserProtocolFilter
it saves Parser state in ThreadAttachment.


///////////// Example how to save the
state ////////////////////////////////////////////
     private void saveByteBuffer(SelectionKey key) {
         WorkerThread workerThread = (WorkerThread)
Thread.currentThread();
         // Detach the current Thread data.
         ThreadAttachment threadAttachment =
                 workerThread.updateAttachment(Mode.BYTE_BUFFER);

         // Attach it to the SelectionKey so the it can be resumed
latter.
         key.attach(threadAttachment);
     }

     private void saveParser(SelectionKey key, ProtocolParser parser) {
         WorkerThread workerThread = (WorkerThread)
Thread.currentThread();
         // Detach the current Thread data.
         ThreadAttachment threadAttachment =
workerThread.getAttachment();
         threadAttachment.setAttribute(ProtocolParser.PARSER, parser);
         // Attach it to the SelectionKey so the it can be resumed
latter.
         key.attach(threadAttachment);
     }


///////////// Example how to restore the
state ////////////////////////////////////////////

         AttributeHolder connectionAttrs =
                  
ctx.getAttributeHolderByScope(AttributeScope.CONNECTION);
         if (connectionAttrs != null) {
             parser = (ProtocolParser)
connectionAttrs.getAttribute(ProtocolParser.PARSER);
         }


Hope this will help.

WBR,
Alexey.

>
>
> 2008/6/12 John ROM <snake-john_at_gmx.de>:
> ProtocolParser,Filter are mainly for reading.
> Of course you can always call your TCPConnectorHandler from a filter.
>
> you could look at
>
> "Obtaining the selectorHandler for a given ConnectorHandler"
>
> https://grizzly.dev.java.net/servlets/ReadMsg?listName=users&msgNo=678
>
> for advice on setting up a chain in a client for reading.
>
> many Greetings.
> p.s:
> I am working on a complete example for client/server communication
> with a client protocolParser
> and will try to publish it under the samples directory.
> I should have it done at the end of the month after the european
> soccer championship...
>
>
>
> --
> Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
> Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>
>