mina-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Abeer Al-Anazi <abeer....@gmail.com>
Subject Re: Decode buffer with two different data type
Date Tue, 24 Feb 2015 07:39:54 GMT
Did you mean like this:
public class requestEncoder implements ProtocolEncoder {
    @Override
    public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
    byte b;
        IoBuffer buffer = IoBuffer.allocate(220, false);
        buffer.setAutoExpand(true);
        if (message instanceof Long )
        {    b = '@';
             long controllerCurrentLoad= (Long) message;
             buffer.put (b);
             buffer.putLong(controllerCurrentLoad);

        }

       if ( message instanceof Object )
        {
            b = '&';
        controllerInfo  request = (controllerInfo ) message;
         buffer.put (b);
        buffer.putObject(request.getControllerName());
        buffer.putObject(request.getBackupControllers());
        buffer.putLong(request.getControllerCapacity());


        }


           buffer.flip();
           out.write(buffer);

    }
the decoder will be:
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
    byte b = 0 ;
    String cName = null;
    HashMap <String, Long> backupControllers = new HashMap <String,
Long>()  ;
    long controllerCapacity=0;
    long    controllerCurrentLoad = 0;

    while (in.hasRemaining())
    {
        b = in.get();

    if (b == '&')

    {
         cName =(String) in.getObject();
        @SuppressWarnings("unchecked")
        HashMap<String, Long> bc= (HashMap<String, Long>) in.getObject();
        backupControllers = bc;
        controllerCapacity = in.getLong();

    }

         else if (b == '@')
    {

        controllerCurrentLoad = in.getLong();

    }

    }

    controllerInfo request = new controllerInfo (cName, backupControllers,
controllerCapacity);
    request.setControllerLoad(controllerCurrentLoad);
    out.write(request);

        return false;
    }
I tested it and it read the object, and it did not read the long.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message