Dears,
I've the same problem...
I have a mina peer that communicate with myClass called BasicMessage.
My problem is on decoder filter in fuction doDecode(), I convert the message
into byte and receive the corret byte, but I'm not able to convert it into
BasicMessage.
I try two solution:
<FIRST SOLUTION> My problem is that the program print line ++++++++++++++++
many times, I
belive that host receive the message many times because array() will not
"move"
forward in the IoByteBuffer. Is there a fuction that cancel the IoByteBuffer?
public class MessageDecoder extends CumulativeProtocolDecoder {
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
byte[] byteVal = null;
if (in.hasRemaining()){
byteVal = in.array();
BasicMessage m = createBasicMessage(byteVal);
System.out.println("RICEVUTO DA " + m.getSender() +
"\n");++++++++++++++++
out.write(m);
in.get();
return true;
} else {
return false;
}
}
public static BasicMessage createBasicMessage(byte[] bytes){
Object object = null;
try{
object = new java.io.ObjectInputStream(new java.io.ByteArrayInputStream
(bytes)).readObject();
}catch(java.io.IOException ioe){
java.util.logging.Logger.global.log(java.util.logging.Level.SEVERE, ioe.
getMessage());
}catch(java.lang.ClassNotFoundException cnfe){
java.util.logging.Logger.global.log(java.util.logging.Level.SEVERE, cnfe.
getMessage());
}
return (BasicMessage)object;
}
}
<SECOND SOLUTION> In this case the receiver throws the exception org.apache.
mina.filter.codec.ProtocolDecoderException: java.nio.BufferUnderflowException
(Hexdump: AC ... 72).
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
byte[] byteVal = new byte[in.capacity()];
if (in.hasRemaining()){
in.get(byteVal , 0, in.capacity());
BasicMessage m = createBasicMessage(byteVal);
System.out.println("RECEIVED FROM " + m.getSender() +
"\n");
out.write(m);
return true;
} else {
return false;
}
Thanks
Sia