Le 23/02/15 09:08, Abeer Al-Anazi a écrit :
> can any one tell me How can I write decode function for this encode
> function?
> public void encode(IoSession session, Object message, ProtocolEncoderOutput
> out) throws Exception {
>
> IoBuffer buffer = IoBuffer.allocate(120, false);
> buffer.setAutoExpand(true);
> if (message instanceof Long )
> {
> long controllerCurrentLoad= (Long) message;
> buffer.putLong(controllerCurrentLoad);
>
> }
>
> else if ( message instanceof Object )
> {
> controllerInfo request = (controllerInfo ) message;
> buffer.putObject(request.getControllerName());
> buffer.putObject(request.getBackupControllers());
> buffer.putLong(request.getControllerCapacity());
>
>
> }
>
>
> buffer.flip();
>
> out.write(buffer);
>
>
>
>
>
> }
>
> My attempt is:
>
> protected boolean doDecode(IoSession session, IoBuffer in,
> ProtocolDecoderOutput out) throws Exception {
> String cName =(String) in.getObject();HashMap <String, Long>
> backupControllers = (HashMap<String, Long>) in.getObject();long
> controllerCapacity = in.getLong();
> controllerInfo request = new controllerInfo (cName, backupControllers,
> controllerCapacity); // this is object data type
> long controllerCurrentLoad = in.getLong();//this is long data type
> request.setControllerLoad(controllerCurrentLoad);
>
> out.write(request);
> return false;
> }
> }
>
> But it does not work :\
>
You are writing the encoder in a way it makes it impossible to detect
which kind of object you'll have on the other side. How possibly can you
know if you are receiving a serialized long or a serialized ControllerInfo ?
You have to add a flag at the beginning of your message to know what
kind of Object you are receiving.
BTW, naming a class 'controlerInfo' instead of 'ControlerInfo' is bad
Java taste. Java classes starts with a upper case.