users@grizzly.java.net

Re: [Q] Getting the content of the byteBuffer from the CallBackHandler?

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Wed, 12 Mar 2008 16:10:03 -0400

Simon Trudeau wrote:
> Sorry but I disagree with your interpretation:
>
>
>
> *public* *void* onRead(IOEvent<Context> ioEvent) {
>
> *if*(/LOG/.isTraceEnabled())
>
> {
>
> Date currentDate = *new* Date(System./currentTimeMillis/());
>
> *final* WorkerThread workerThread =
> ((WorkerThread)Thread./currentThread/());
>
> ByteBuffer buffer = workerThread.getByteBuffer();
>
> System./err/.println("bufferPosition: "+buffer.position() +
> " bufferLimit: "+buffer.limit());
>
> ByteBuffer duplicate = buffer.asReadOnlyBuffer();
>
> duplicate.flip();
>
> *int* packetSize = duplicate.remaining();
>
> /LOG/.trace("Packet ("+packetSize+" bytes) received at " +
> timeFormatter.format(currentDate) + " from " + remoteAddress.toString()
> + ":" + remotePort);
>
> }
>
> *try*
>
> {
>
>
> ioEvent.attachment().getProtocolChain().execute(ioEvent.attachment());
>
> } *catch* (Exception e) {
>
> /LOG/.error("ProtocolChain error while handling received
> packet from " + remoteAddress.toString() + ":" + remotePort, e);
>
> }
>
> }
>
>
>
> Printing the buffer position I always almost always get 0 bytes (on rare
> occasion I see something…) but checking my ReadFilter, I always have
> something into my buffer. When is the CallBackHandler invoked? Before or
> after the selectorHandler’s ProtocolChain? If invoked after, that would
> explain why the position is always 0…

Right. If you want to do some read, you need to do something similar to
what ReadFilter is doing:

channel.read(bb);

Thanks

-- Jeanfrancois


>
>
>
> What do you think?
>
>
>
>
>
> Simon
>
> ------------------------------------------------------------------------
>
> *From:* Harsha.Godugu_at_Sun.COM [mailto:Harsha.Godugu_at_Sun.COM]
> *Sent:* March-12-08 4:29 PM
> *To:* users_at_grizzly.dev.java.net
> *Subject:* Re: [Q] Getting the content of the byteBuffer from the
> CallBackHandler?
>
>
>
> Simon Trudeau wrote:
>
> How do I get access to the content of the byteBuffer from the the
> CallBackHandler?
>
>
>
> I tried putting:
>
>
>
> *public* *void* onRead(IOEvent<Context> ioEvent) {
>
> *if*(/LOG/.isTraceEnabled())
>
> {
>
> Date currentDate = *new* Date(System./currentTimeMillis/());
>
> *final* WorkerThread workerThread =
> ((WorkerThread)Thread./currentThread/());
>
> ByteBuffer buffer = workerThread.getByteBuffer();
>
> ByteBuffer duplicate = buffer.asReadOnlyBuffer();
>
> duplicate.flip();
>
> *int* packetSize = duplicate.remaining();
>
> /LOG/.trace("Packet ("+packetSize+" bytes) received at " +
> timeFormatter.format(currentDate) + " from " + remoteAddress.toString()
> + ":" + remotePort);
>
> }
>
> *try*
>
> {
>
>
> ioEvent.attachment().getProtocolChain().execute(ioEvent.attachment());
>
> } *catch* (Exception e) {
>
> /LOG/.error("ProtocolChain error while handling received packet
> from " + remoteAddress.toString() + ":" + remotePort, e);
>
> }
>
> }
>
>
>
> But I get the following log:
>
>
>
> 14:57:19,673 TRACE BtNIOClient:253 - Packet (0 bytes) received at
> 14:57:19.673 EDT from strudeau/192.168.222.107:5070
>
> 14:57:19,670 TRACE BtNIOClient:253 - Packet (0 bytes) received at
> 14:57:19.670 EDT from strudeau/192.168.222.107:5070
>
> 14:57:19,669 TRACE BtNIOClient:253 - Packet (0 bytes) received at
> 14:57:19.669 EDT from strudeau/192.168.222.107:5070
>
> 14:57:19,675 TRACE BtNIOClient:253 - Packet (0 bytes) received at
> 14:57:19.675 EDT from strudeau/192.168.222.107:5070
>
> 14:57:19,676 TRACE BtNIOClient:253 - Packet (0 bytes) received at
> 14:57:19.676 EDT from strudeau/192.168.222.107:5070
>
>
>
> It looks like either my CallBackHandler gets invoked by 0 bytes, which
> looks very suspicious or
>
> This is also working as expected. Check the bytebuffer pos. and limit
> before reading. It might have NO room to read and hence, may not read
> any more data.
> That's ONE possibility. The other possibility is, nio does not guarantee
> reading at least non-zero bytes in the first attempt of trying to read.
> It could be that.
>
>
>
> *final* WorkerThread workerThread =
> ((WorkerThread)Thread./currentThread/());
>
> ByteBuffer buffer = workerThread.getByteBuffer();
>
>
>
> Doesn’t work as I would have expected.
>
> This is working as expected.
>
> -hg
>