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