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

Reply via email to