You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by "silviasignorini@libero.it" <si...@libero.it> on 2010/02/05 12:13:00 UTC
why host received many message?
Dears...
I have always problem with my first mina Decoder that work on a P2P system.
When I send a message cast it into a byte[]; I control that I receive and send
the same byte[].
The cast from byte[] to my Object (BasicMessage) is correct!
My problem is that the program print line ++++++++++++++++ many times, I
belive that host receive the message many times...
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;
}
}
public class MessageEncoder implements ProtocolEncoder {
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
BasicMessage m = (BasicMessage) message;
IoBuffer buffer = IoBuffer.allocate(12);
buffer.setAutoExpand(true);
byte[] byteVal = createArrayByte(m);
buffer.put(byteVal);
buffer.flip();
out.write(buffer);
}
public byte[] createArrayByte (BasicMessage obj) throws IOException{
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ObjectOutputStream oStream = new ObjectOutputStream( bStream );
oStream.writeObject ( obj );
oStream.flush();
oStream.close();
bStream.close();
byte[] byteVal = bStream. toByteArray();
return byteVal;
}
}
Thanks
Sia
Re: why host received many message?
Posted by siasia <si...@libero.it>.
Thanks Norman,
I try to use your example and I checked that when the host send a message
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;
}
If I read the buffer with the fuction array() the insertion into byteVal
works but the buffer is not consumed and the receiver throws the ecception
java.lang.IllegalStateException: doDecode() can't return true when buffer is
not consumed. (Hexdump: AC ... 63). There is the function that consumes the
buffer?
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
byte[] byteVal = new byte[in.capacity()];
if (in.hasRemaining()){
byteVal = in.array();
BasicMessage m = createBasicMessage(byteVal);
System.out.println("RECEIVED FROM " + m.getSender() + "\n");
out.write(m);
return true;
} else {
return false;
}
Thanks
Sia
Norman Maurer-4 wrote:
>
> You should not use IoByteBuffer.array(), array() will not "move"
> forward in the IoByteBuffer. Use something like:
>
> byte[] byteVal = new byte[in.capacity()];
> in.get(byteVal , 0, byteVal.length);
> BasicMessage m = createBasicMessage(byteVal);
>
>
> Bye,
> Norman
>
> 2010/2/5 silviasignorini@libero.it <si...@libero.it>:
>> Dears...
>> I have always problem with my first mina Decoder that work on a P2P
>> system.
>> When I send a message cast it into a byte[]; I control that I receive and
>> send
>> the same byte[].
>> The cast from byte[] to my Object (BasicMessage) is correct!
>>
>> My problem is that the program print line ++++++++++++++++ many times, I
>> belive that host receive the message many times...
>>
>>
>>
>>
>> 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;
>> }
>> }
>>
>>
>>
>>
>> public class MessageEncoder implements ProtocolEncoder {
>>
>> public void encode(IoSession session, Object message,
>> ProtocolEncoderOutput out) throws Exception {
>> BasicMessage m = (BasicMessage) message;
>> IoBuffer buffer = IoBuffer.allocate(12);
>> buffer.setAutoExpand(true);
>> byte[] byteVal = createArrayByte(m);
>> buffer.put(byteVal);
>> buffer.flip();
>> out.write(buffer);
>> }
>>
>> public byte[] createArrayByte (BasicMessage obj) throws IOException{
>> ByteArrayOutputStream bStream = new ByteArrayOutputStream();
>> ObjectOutputStream oStream = new ObjectOutputStream( bStream );
>> oStream.writeObject ( obj );
>> oStream.flush();
>> oStream.close();
>> bStream.close();
>> byte[] byteVal = bStream. toByteArray();
>> return byteVal;
>> }
>>
>> }
>>
>>
>> Thanks
>> Sia
>>
>
>
--
View this message in context: http://old.nabble.com/why-host-received-many-message--tp27467512p27477642.html
Sent from the Apache MINA User Forum mailing list archive at Nabble.com.
Re: why host received many message?
Posted by Norman Maurer <no...@googlemail.com>.
You should not use IoByteBuffer.array(), array() will not "move"
forward in the IoByteBuffer. Use something like:
byte[] byteVal = new byte[in.capacity()];
in.get(byteVal , 0, byteVal.length);
BasicMessage m = createBasicMessage(byteVal);
Bye,
Norman
2010/2/5 silviasignorini@libero.it <si...@libero.it>:
> Dears...
> I have always problem with my first mina Decoder that work on a P2P system.
> When I send a message cast it into a byte[]; I control that I receive and send
> the same byte[].
> The cast from byte[] to my Object (BasicMessage) is correct!
>
> My problem is that the program print line ++++++++++++++++ many times, I
> belive that host receive the message many times...
>
>
>
>
> 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;
> }
> }
>
>
>
>
> public class MessageEncoder implements ProtocolEncoder {
>
> public void encode(IoSession session, Object message,
> ProtocolEncoderOutput out) throws Exception {
> BasicMessage m = (BasicMessage) message;
> IoBuffer buffer = IoBuffer.allocate(12);
> buffer.setAutoExpand(true);
> byte[] byteVal = createArrayByte(m);
> buffer.put(byteVal);
> buffer.flip();
> out.write(buffer);
> }
>
> public byte[] createArrayByte (BasicMessage obj) throws IOException{
> ByteArrayOutputStream bStream = new ByteArrayOutputStream();
> ObjectOutputStream oStream = new ObjectOutputStream( bStream );
> oStream.writeObject ( obj );
> oStream.flush();
> oStream.close();
> bStream.close();
> byte[] byteVal = bStream. toByteArray();
> return byteVal;
> }
>
> }
>
>
> Thanks
> Sia
>