You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Luciano Greiner <lu...@gmail.com> on 2011/06/15 17:14:13 UTC
IoBuffer Infinite Read Loop
Hello.
I wrote a telnet server application using Apache Mina. The clients are
barcode collectors, and the inputs are either a command keys (Ex.: TAB
or F4) or a single char like a number or a letter.
I found a circunstance in which the user sends a fast sequence of
bytes, and it causes my processing code to never ends. If, for
example, the last key is ' K ', it seems like Apache mina never stops
sending it to decode until i have to kill the process because of a
high CPU usage. I could reproduce the issue by typing alternate
keyboard keys for a few seconds.
I bet it might be some bad threatment in my code, so any thoughts is
really welcome:
Decoder:
decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws Exception {
if(in.hasRemaining()) {
Screen screen = getScreen(session);
screen.input(in);
session.write(screen);
}
}
Screen: (I cleaned up the logging code to simplify)
public void input(ByteBuffer buffer) {
log.debug("Processing input for {} bytes", buffer.remaining());
while (buffer.hasRemaining()) {
log.debug("Bytes remaining to proccess: {}", buffer.remaining());
Command command = commandParser.parse(buffer);
if(command != null) {
commandEntered(command);
break;
}
byte data = buffer.get();
if(log.isTraceEnabled())
log.trace("Char: {}", Character.valueOf((char) data));
keyPressed((char) data);
}
log.debug("Finished input processing");
}
On client side, after typing 'admin' real fast, the char 'd'
processing gets stuck:
Log:
DEBUG Qua 15/6/2011 11:29:18:268: Processing input for 1 bytes
DEBUG Qua 15/6/2011 11:29:18:268: Bytes remaining to proccess: 1
DEBUG Qua 15/6/2011 11:29:18:268: Char: d
DEBUG Qua 15/6/2011 11:29:18:268: Finished input processing
DEBUG Qua 15/6/2011 11:29:18:331: Processing input for 1 bytes
DEBUG Qua 15/6/2011 11:29:18:331: Bytes remaining to proccess: 1
DEBUG Qua 15/6/2011 11:29:18:268: Char: d
DEBUG Qua 15/6/2011 11:29:18:331: Finished input processing
DEBUG Qua 15/6/2011 11:29:19:816: Processing input for 1 bytes
DEBUG Qua 15/6/2011 11:29:19:816: Bytes remaining to proccess: 1
DEBUG Qua 15/6/2011 11:29:18:268: Char: d
DEBUG Qua 15/6/2011 11:29:19:816: Finished input processing
DEBUG Qua 15/6/2011 11:29:19:863: Processing input for 1 bytes
DEBUG Qua 15/6/2011 11:29:19:863: Bytes remaining to proccess: 1
DEBUG Qua 15/6/2011 11:29:18:268: Char: d
DEBUG Qua 15/6/2011 11:29:19:863: Finished input processing
...
...
It just happens if the the bytes come almost without a relevant interval.
I am not sure if the IoBuffer instance sent to decode method is thread
safe, so i thought it might be better to read the bytes before sending
to screen processing:
public void decode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
int remaining = in.remaining();
if(remaining > 0) {
byte[] bytes = new byte[remaining];
in.get(bytes);
Screen screen = getScreen(session);
screen.input(IoBuffer.wrap(bytes));
session.write(screen);
}
}
The problem persists. The decode method is being called from Mina core
indefinitely.
Any thoughts?
Thank you very much
Luciano Greiner
(51) 81780200
Re: IoBuffer Infinite Read Loop
Posted by Luciano Greiner <lu...@gmail.com>.
The problem was fixed.
I was wrongly calling the "rewind" method on CommandParser when the
byte read couldn't be parsed as a command.
The right behaviour is to use mark / reset methods.
Thanks anyway
Luciano Greiner
(51) 81780200
On Wed, Jun 15, 2011 at 1:03 PM, Luciano Greiner
<lu...@gmail.com> wrote:
> Hi Emmanuel.
>
> When i receive a command, the it just discard the rest of the buffer
> moving it to the end. For simplification i just avoided this code:
>
> if(command != null) {
> commandEntered(command);
> if(buffer.hasRemaining()) {
> buffer.position(buffer.position() + buffer.remaining());
> }
> break;
> }
>
> Anyway, the problem doesn't get there, because the command is null for
> this situation. It's always a Key, not a command.
>
> Thank you
>
> Luciano Greiner
> (51) 81780200
>
>
>
>
> On Wed, Jun 15, 2011 at 12:22 PM, Emmanuel Lecharny <el...@gmail.com> wrote:
>> On 6/15/11 5:14 PM, Luciano Greiner wrote:
>>>
>>> Hello.
>>>
>>> I wrote a telnet server application using Apache Mina. The clients are
>>> barcode collectors, and the inputs are either a command keys (Ex.: TAB
>>> or F4) or a single char like a number or a letter.$
>>
>> if you don't do a buffer.get(), the buffer will always contain something,
>> and will then be read again and again and again.
>>
>> This part of the code is dubious :
>>
>> while (buffer.hasRemaining()) {
>> log.debug("Bytes remaining to proccess: {}", buffer.remaining());
>> Command command = commandParser.parse(buffer);
>> if(command != null) {
>> commandEntered(command);
>> break;
>> }
>> byte data = buffer.get();
>> ...
>>
>>
>> the break; will exit the while loop whitout consuming a byte in the bufffer,
>> IMO.
>>
>> Just check around.
>>
>> --
>> Regards,
>> Cordialement,
>> Emmanuel Lécharny
>> www.iktek.com
>>
>>
>
Re: IoBuffer Infinite Read Loop
Posted by Luciano Greiner <lu...@gmail.com>.
Hi Emmanuel.
When i receive a command, the it just discard the rest of the buffer
moving it to the end. For simplification i just avoided this code:
if(command != null) {
commandEntered(command);
if(buffer.hasRemaining()) {
buffer.position(buffer.position() + buffer.remaining());
}
break;
}
Anyway, the problem doesn't get there, because the command is null for
this situation. It's always a Key, not a command.
Thank you
Luciano Greiner
(51) 81780200
On Wed, Jun 15, 2011 at 12:22 PM, Emmanuel Lecharny <el...@gmail.com> wrote:
> On 6/15/11 5:14 PM, Luciano Greiner wrote:
>>
>> Hello.
>>
>> I wrote a telnet server application using Apache Mina. The clients are
>> barcode collectors, and the inputs are either a command keys (Ex.: TAB
>> or F4) or a single char like a number or a letter.$
>
> if you don't do a buffer.get(), the buffer will always contain something,
> and will then be read again and again and again.
>
> This part of the code is dubious :
>
> while (buffer.hasRemaining()) {
> log.debug("Bytes remaining to proccess: {}", buffer.remaining());
> Command command = commandParser.parse(buffer);
> if(command != null) {
> commandEntered(command);
> break;
> }
> byte data = buffer.get();
> ...
>
>
> the break; will exit the while loop whitout consuming a byte in the bufffer,
> IMO.
>
> Just check around.
>
> --
> Regards,
> Cordialement,
> Emmanuel Lécharny
> www.iktek.com
>
>
Re: IoBuffer Infinite Read Loop
Posted by Emmanuel Lecharny <el...@gmail.com>.
On 6/15/11 5:14 PM, Luciano Greiner wrote:
> Hello.
>
> I wrote a telnet server application using Apache Mina. The clients are
> barcode collectors, and the inputs are either a command keys (Ex.: TAB
> or F4) or a single char like a number or a letter.$
if you don't do a buffer.get(), the buffer will always contain
something, and will then be read again and again and again.
This part of the code is dubious :
while (buffer.hasRemaining()) {
log.debug("Bytes remaining to proccess: {}", buffer.remaining());
Command command = commandParser.parse(buffer);
if(command != null) {
commandEntered(command);
break;
}
byte data = buffer.get();
...
the break; will exit the while loop whitout consuming a byte in the
bufffer, IMO.
Just check around.
--
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com