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

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

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

CigarMan

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

 

      final WorkerThread workerThread = ((WorkerThread)Thread.currentThread());

      ByteBuffer buffer = workerThread.getByteBuffer();

 

Doesn’t work as I would have expected.

 

 

Any ideas?

 

 

Simon

 

Reply | Threaded
Open this post in threaded view
|

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

Harsha Godugu
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
Reply | Threaded
Open this post in threaded view
|

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

CigarMan

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…

 

What do you think?

 

 

Simon


From: [hidden email] [mailto:[hidden email]]
Sent: March-12-08 4:29 PM
To: [hidden email]
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

Reply | Threaded
Open this post in threaded view
|

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

Jeanfrancois Arcand-2


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:* [hidden email] [mailto:[hidden email]]
> *Sent:* March-12-08 4:29 PM
> *To:* [hidden email]
> *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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

CigarMan
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()! Any good ideas where I should be looking for my bytes to Write?

Thanks,

Simon

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: March-12-08 4:10 PM
To: [hidden email]
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:* [hidden email] [mailto:[hidden email]]
> *Sent:* March-12-08 4:29 PM
> *To:* [hidden email]
> *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: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

Jeanfrancois Arcand-2


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: [hidden email] [mailto:[hidden email]]
> Sent: March-12-08 4:10 PM
> To: [hidden email]
> 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:* [hidden email] [mailto:[hidden email]]
>> *Sent:* March-12-08 4:29 PM
>> *To:* [hidden email]
>> *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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

CigarMan
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?

Thanks,



Simon
 
-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
Sent: March-12-08 4:44 PM
To: [hidden email]
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: [hidden email] [mailto:[hidden email]]
> Sent: March-12-08 4:10 PM
> To: [hidden email]
> 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:* [hidden email] [mailto:[hidden email]]
>> *Sent:* March-12-08 4:29 PM
>> *To:* [hidden email]
>> *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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

Jeanfrancois Arcand-2
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: [hidden email] [mailto:[hidden email]]
> Sent: March-12-08 4:44 PM
> To: [hidden email]
> 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: [hidden email] [mailto:[hidden email]]
>> Sent: March-12-08 4:10 PM
>> To: [hidden email]
>> 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:* [hidden email] [mailto:[hidden email]]
>>> *Sent:* March-12-08 4:29 PM
>>> *To:* [hidden email]
>>> *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: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

CigarMan
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: [hidden email] [mailto:[hidden email]]
Sent: March-12-08 8:39 PM
To: [hidden email]
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: [hidden email] [mailto:[hidden email]]
> Sent: March-12-08 4:44 PM
> To: [hidden email]
> 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: [hidden email] [mailto:[hidden email]]
>> Sent: March-12-08 4:10 PM
>> To: [hidden email]
>> 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:* [hidden email] [mailto:[hidden email]]
>>> *Sent:* March-12-08 4:29 PM
>>> *To:* [hidden email]
>>> *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: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

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

Jeanfrancois Arcand-2


Simon Trudeau wrote:
> 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.

The it should have worked because internally we do:


>         if (blocking){
>             return OutputWriter.flushChannel(socketChannel,byteBuffer);
>         } else {
>            
>             if (callbackHandler == null){
>                 throw new IllegalStateException
>                         ("Non blocking write needs a CallbackHandler");
>             }
>            
>             SelectionKey key = socketChannel.keyFor(selectorHandler.getSelector());
>             int nWrite = 1;
>             int totalWriteBytes = 0;
>             while (nWrite > 0 && byteBuffer.hasRemaining()){
>                 nWrite = socketChannel.write(byteBuffer);
>                 totalWriteBytes += nWrite;
>             }
>            
>             if (totalWriteBytes == 0 && byteBuffer.hasRemaining()){
>                 selectorHandler.register(key,SelectionKey.OP_WRITE);
>             }
>             return totalWriteBytes;
>         }

What is the return value of connector.write(..)

A+

-- Jeanfrancois

>
> Simon
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> Sent: March-12-08 8:39 PM
> To: [hidden email]
> 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: [hidden email] [mailto:[hidden email]]
>> Sent: March-12-08 4:44 PM
>> To: [hidden email]
>> 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: [hidden email] [mailto:[hidden email]]
>>> Sent: March-12-08 4:10 PM
>>> To: [hidden email]
>>> 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:* [hidden email] [mailto:[hidden email]]
>>>> *Sent:* March-12-08 4:29 PM
>>>> *To:* [hidden email]
>>>> *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: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]