did you mean like this:
encode function:
    public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
    byte b;
        IoBuffer buffer = IoBuffer.allocate(1200, false);
       buffer.setAutoExpand(true);
        if (message instanceof Long )
        {    b = '@';

             buffer.put (b);
             long controllerCurrentLoad= (Long) message;
             byte[] bytes3 = getBytes(controllerCurrentLoad);
             buffer.putInt(bytes3.length);
             buffer.put(bytes3);

        }
        else if ( message instanceof HashMap )
        {    b = '#';
             HashMap <SocketAddress, Long> loadTable = new
HashMap<SocketAddress, Long> ();
              loadTable = (HashMap<SocketAddress, Long>) message;
               byte[] bytes1 = getBytes( loadTable);
                buffer.put(b);
                buffer.putInt(bytes1.length);
                buffer.put(bytes1);

        }
        else if ( message instanceof Object )
        {
            b = '&';
             buffer.put (b);
        controllerInfo  request = (controllerInfo ) message;
        byte[] bytes2 = getBytes(request);
        buffer.putInt(bytes2.length);
        buffer.put(bytes2);
        }


           buffer.flip();
           out.write(buffer);
    }
*********************************
public static byte[] getBytes(Object obj) throws IOException {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(b);
            o.writeObject(obj);
            return b.toByteArray();
*****************************************************************
decode function:
rotected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
    byte b = 0 ;
    long    controllerCurrentLoad = 0;
    controllerInfo r = new controllerInfo();
    while (in.hasRemaining())
    {
        b = in.get();

      if (b == '&')
      {

          if(in.prefixedDataAvailable(4))
          {
             r = (controllerInfo) deserialize(in);

          }
    }
     if (b == '@')
       {
            if(in.prefixedDataAvailable(4))
          {
          controllerCurrentLoad = (Long) deserialize(in);

          }

       }
     if (b == '#')
     {
        if (in.prefixedDataAvailable(4))
        {

        HashMap<SocketAddress, Long> l = (HashMap<SocketAddress, Long>)
in.getObject();
        l = (HashMap<SocketAddress, Long>) deserialize(in);
         loadTable = l;
        }

     }
    }
    controllerInfo request =r;
    request.setControllerLoad(controllerCurrentLoad);
    request.setSwitchesLoad(loadTable);
    out.write(request);
        return false;
    }

*********************************************
public static Object deserialize(IoBuffer in) throws IOException,
ClassNotFoundException {
        int length = in.getInt();
        byte[] bytes = new byte[length];
        in.get(bytes);
        ByteArrayInputStream b = new ByteArrayInputStream(bytes);
        ObjectInputStream o = new ObjectInputStream(b);
        return o.readObject();
**********************************************************
I tested it but it did not work, and the decoder did not read any things.
If I discard using getByte and deserialize function, the decoder read the
object only and when I print the renaming in buffer -after read object- the
result is zero. I mean the decoder read only one of the message cases
(object-long-hashmap) and discard others.
I think the problem is in buffer size, because it cant read more than one
(if cases).
I need your help please, I spent a lot of my time to try solving it :(

Reply via email to