You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Lorenzo Bolzani <l....@gmail.com> on 2008/10/01 14:20:07 UTC
When is safe to write on the session?
Hi,
I have a written a service following this article
http://mina.apache.org/tutorial-on-protocolcodecfilter-for-mina-2x.html
and it works perfectly.
Now, on this same channel, I need to send some messages that are not
replies to client requests.
The system receive an event from a GUI and I need to send a message to
the client.
Where should I place my session.write() ?
Is it safe to use it outside the callback methods and call it at any
time? Should I synchronize on something? There is something about this
I've missed completely?
Thanks in advance
Bye
Lorenzo
Re: When is safe to write on the session?
Posted by Lorenzo Bolzani <l....@gmail.com>.
2008/10/2 Maarten Bosteels <mb...@gmail.com>:
>
> Can't you use the order() method ?
> http://mina.apache.org/report/trunk/apidocs/org/apache/mina/core/buffer/IoBuffer.html#order(java.nio.ByteOrder)
> http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html?is-external=true#order(java.nio.ByteOrder)
>
Can I use it inside the decode method after the buffer has already been filled?
I do not know why but I just assumed that the buffer has to be created
with the right order before the use.
Now I see no reason for this: I suppose the buffer contains what has
actually been read from the wire in that specific order and endianess
define only how the "get methods" interpret those bytes.
I'll try tomorrow.
BTW, I have another question, but it's better in another thread.
Thanks, again.
Lorenzo
Re: When is safe to write on the session?
Posted by Maarten Bosteels <mb...@gmail.com>.
On Thu, Oct 2, 2008 at 7:02 PM, Lorenzo Bolzani <l....@gmail.com> wrote:
> 2008/10/2 Maarten Bosteels <mb...@gmail.com>:
>>
>> Good to hear that you like the tutorial :-)
>
> Yes, It is clear and complete.
> The only problems I found were reading LittleEndian encoded numbers
> (because I did not found a BufferAllocator that creates this kind of
> Buffers and subclassing existing ones to configure this setting is not
> easy, at this time I'm reading byte blocks and switching order "by
> hand".
Can't you use the order() method ?
http://mina.apache.org/report/trunk/apidocs/org/apache/mina/core/buffer/IoBuffer.html#order(java.nio.ByteOrder)
http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html?is-external=true#order(java.nio.ByteOrder)
Maarten
No problem for writing) and the doubt about async writes. The
> javadoc clearly states that IOSession is thread-safe but I found no
> example (chat, netcat, tennis, etc.) working in this way. Maybe a
> "keep-alive" feature could be added to the chat example to show the
> right way to do it. I'll try to find some time to provide a little
> patch...
>
>> You don't have to synchronize anything, unless your threads are
>> sharing non-MINA data structures of course.
>> MINA will add the WriteRequest to the queue and the IoProcessor
>> thread(s) will put the message on the wire.
>
> Ok, I'll try. Thanks for your reply to this simple question.
>
> Bye
>
>
> Lorenzo
>
Re: When is safe to write on the session?
Posted by Lorenzo Bolzani <l....@gmail.com>.
2008/10/2 Maarten Bosteels <mb...@gmail.com>:
>
> Good to hear that you like the tutorial :-)
Yes, It is clear and complete.
The only problems I found were reading LittleEndian encoded numbers
(because I did not found a BufferAllocator that creates this kind of
Buffers and subclassing existing ones to configure this setting is not
easy, at this time I'm reading byte blocks and switching order "by
hand". No problem for writing) and the doubt about async writes. The
javadoc clearly states that IOSession is thread-safe but I found no
example (chat, netcat, tennis, etc.) working in this way. Maybe a
"keep-alive" feature could be added to the chat example to show the
right way to do it. I'll try to find some time to provide a little
patch...
> You don't have to synchronize anything, unless your threads are
> sharing non-MINA data structures of course.
> MINA will add the WriteRequest to the queue and the IoProcessor
> thread(s) will put the message on the wire.
Ok, I'll try. Thanks for your reply to this simple question.
Bye
Lorenzo
Re: When is safe to write on the session?
Posted by Maarten Bosteels <mb...@gmail.com>.
Hello,
On Wed, Oct 1, 2008 at 2:20 PM, Lorenzo Bolzani <l....@gmail.com> wrote:
> Hi,
> I have a written a service following this article
>
> http://mina.apache.org/tutorial-on-protocolcodecfilter-for-mina-2x.html
>
> and it works perfectly.
Good to hear that you like the tutorial :-)
> Now, on this same channel, I need to send some messages that are not
> replies to client requests.
>
> The system receive an event from a GUI and I need to send a message to
> the client.
>
> Where should I place my session.write() ?
>
> Is it safe to use it outside the callback methods and call it at any
> time?
yes, as long as the session is valid.
> Should I synchronize on something? There is something about this
> I've missed completely?
You don't have to synchronize anything, unless your threads are
sharing non-MINA data structures of course.
MINA will add the WriteRequest to the queue and the IoProcessor
thread(s) will put the message on the wire.
regards,
Maarten
>
> Thanks in advance
>
>
> Bye
>
> Lorenzo
>