users@grizzly.java.net

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

From: JianXing Yi <jianxing.yi_at_gmail.com>
Date: Mon, 16 Jun 2008 22:19:42 +0800

Hi Alexey & John,

Many thanks for your quick reply!

It still does not work correctly :-(.
Consider the newly implemented HttpCallback, the onRead() method will not
call the HttpResponseFilter as expected. In testing, it is never be called
(it seems that registering the OP_READ intrest in
HttpRequestFilter.execute() after writing is useless, neither in
HttpRequestFilter.execute() nor in HttpRequestFilter.postExecute() :-( ).
Also, maybe it conflicts with the HttpResponseFilter being invoked after its
previous filter HttpRequestFilter.

I'll have more tries. Anyway, thank you! :-)

Regards,
-Yi Bing

2008/6/16 Oleksiy Stashok <Oleksiy.Stashok_at_sun.com>:

> Hi again
>
> John is right with following comment:
>
>
>> In HttpResponseParserFilter you have a
>> public boolean releaseBuffer() {
>> savedBuffer = null;
>> return true;
>> }
>>
>> This leads to
>> com.sun.grizzly.filter.ParserProtocolFilter.saveParser(SelectionKey key,
>> ProtocolParser parser)
>> which overwrites anything in the SelectionKey which is not under the
>> control of WorkerThread.
>>
>> So in your Code you need something like:owever,
>> CallbackHandler.onConnect() is called directly from main selector thread,
>> which means we can not cast it to WorkerThread.
>>
>
> So I would suggest to not put much logic to the onConnect method, also
> there is no much sense to call ProtocolChain from onConnect, because,
> possibly at that point your channel is not ready neither for reading nor
> writing. I would suggest to change your CallbackHandler impl. to something
> like that:
>
> package grizzly.http.client;
>
> import java.util.HashMap;
> import java.util.Map;
>
> import com.sun.grizzly.CallbackHandler;
> import com.sun.grizzly.Context;
> import com.sun.grizzly.IOEvent;
> import com.sun.grizzly.TCPConnectorHandler;
> import com.sun.grizzly.Context.AttributeScope;
> import com.sun.grizzly.util.AttributeHolder;
>
> public class HttpCallback implements CallbackHandler<Context> {
> public static final String ATTIB_CONNECTION = "connection";
> private TCPConnectorHandler connectorHandler;
>
> public HttpCallback(TCPConnectorHandler ch) {
> connectorHandler = ch;
> }
>
> public void onConnect(IOEvent<Context> ioEvent) {
> Context ctx = ioEvent.attachment();
> connectorHandler.finishConnect(ctx.getSelectionKey());
> ctx.getSelectorHandler().register(key, SelectionKey.OP_READ
> | SelectionKey.OP_WRITE); // Depends on your requirements you can
> choose just one interest
> }
>
> public void onRead(IOEvent<Context> ioEvent) {
> callFilterChain(ioEvent.attachment());
> }
>
> public void onWrite(IOEvent<Context> ioEvent) {
> callFilterChain(ioEvent.attachment());
> }
>
> private void callFilterChain(Context ctx) {
> AttributeHolder ah =
> ctx.getAttributeHolderByScope(AttributeScope.CONNECTION);
> if (ah == null) {
> ah=((WorkerThread)
> Thread.currentThread()).getAttachment();
> ctx.getSelectionKey().attach(ah);
> }
> ctx.getProtocolChain().execute(ctx);
> }
>
> }
>
>
> Let me know if it works for you :))
>
> Thanks.
>
> WBR,
> Alexey.
>
>
>
>>
>> Anyway with your code in a real situation you may also run into other
>> problems,
>> because your write("Get.Http..") will be called on any
>> Selection.Key.OP_READ event
>> Probably something you do not want...
>>
>> Many Greetings
>> John
>>
>> --
>> 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
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>
>