users@grizzly.java.net

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

From: Simon Trudeau <strudeau_at_bluetreewireless.com>
Date: Thu, 13 Mar 2008 09:20:41 -0400

Well, I don't want to retrieve info from a previous transaction, I want to retrieve infos about the data I have just sent.

I did a

connectorHandler.write(outputByteBuffer, false);

operation and I want to make sure my bytes have been sent by intercepting the sending of the bytes at the CallBackhandler.OnWrite() level.

Simon

-----Original Message-----
From: Jeanfrancois.Arcand_at_Sun.COM [mailto:Jeanfrancois.Arcand_at_Sun.COM]
Sent: March-12-08 8:39 PM
To: users_at_grizzly.dev.java.net
Subject: Re: [Q] Getting the content of the byteBuffer from the CallBackHandler?

Salut,

Simon Trudeau wrote:
> Sorry to disapoint but channel.write() doesn't work...
>
> I tried doing:
>
> **********
> public void onWrite(IOEvent<Context> ioEvent) {
> SelectionKey k = ioEvent.attachment().getSelectionKey();
> SocketChannel channel = (SocketChannel)k.channel();
> if(LOG.isTraceEnabled())
> {
> Date currentDate = new Date(System.currentTimeMillis());
> final WorkerThread workerThread = ((WorkerThread)Thread.currentThread());
> ByteBuffer buffer = workerThread.getByteBuffer();
> System.err.println("write bufferPosition: "+buffer.position() + " bufferLimit: "+buffer.limit());
> ByteBuffer duplicate = buffer.duplicate();
> int packetSize = 0;
> try {
>
>
> packetSize = channel.write(duplicate);
>
>
> LOG.trace("Packet ("+packetSize+" bytes) sent at " + timeFormatter.format(currentDate) + " to " + remoteAddress.toString() + ":" + remotePort);
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> ioEvent.attachment().getSelectorHandler().register(k, SelectionKey.OP_READ);
> }
> **********
>
> My buffer size written is 8192! And I am pretty sure that not the full buffer gets written but only the bytes I am sending! :.)
>
> I tried flipping it for fun... now the amount of bytes written to the channel was 0... Not much better off! :.)
>
>
> What do you think?

It seems your buffer is empty (I'm 100% sure). What are you expecting
inside the bb? I think you need to retrieve a bb from a previous
transaction, right?

Thanks

-- jeanfrancois


>
> Thanks,
>
>
>
> Simon
>
> -----Original Message-----
> From: Jeanfrancois.Arcand_at_Sun.COM [mailto:Jeanfrancois.Arcand_at_Sun.COM]
> Sent: March-12-08 4:44 PM
> To: users_at_grizzly.dev.java.net
> Subject: Re: [Q] Getting the content of the byteBuffer from the CallBackHandler?
>
>
>
> Simon Trudeau wrote:
>> Thanks Jean-François, it works for OnRead operation... now, how to make it work for OnWrite operation?
>>
>> I tried reading from the channel but it didn't work, guess bytes were not yet on the channel when I reach the OnWrite()!
>
> The underlying Channel is not ready for reading when onWrite is invoked.
>
>
> Any good ideas where I should be looking for my bytes to Write?
>
> Inside the onWrite, doing channel.write should work. If you want to
> re-enable the channel to get read events, just do:
>
> ioEvent.attachment().getSelectorHandler().register(key);
>
> so the onRead will be invoked as soon as bytes are available.
>
> A+
>
> -- jeanfrancois
>
>
>> Thanks,
>>
>> Simon
>>
>> -----Original Message-----
>> From: Jeanfrancois.Arcand_at_Sun.COM [mailto:Jeanfrancois.Arcand_at_Sun.COM]
>> Sent: March-12-08 4:10 PM
>> To: users_at_grizzly.dev.java.net
>> Subject: Re: [Q] Getting the content of the byteBuffer from the CallBackHandler?
>>
>>
>>
>> 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
>>>
>> ---------------------------------------------------------------------
>> 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
>>
>
> ---------------------------------------------------------------------
> 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
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
For additional commands, e-mail: users-help_at_grizzly.dev.java.net