view src/main/java/christie/datagear/command/RemoteTakeCommand.java @ 210:9a1d9c99e708

fix Command run to execute
author akahori
date Mon, 11 Mar 2019 16:45:37 +0900
parents 2ecb3a93b8ae
children 473f10f24b13
line wrap: on
line source

package christie.datagear.command;

import christie.daemon.Connection;
import christie.datagear.RemoteMessage;
import christie.datagear.command.Command;
import christie.datagear.command.CommandType;
import christie.datagear.dg.DataGear;
import christie.datagear.dg.MessagePackDataGear;
import org.msgpack.MessagePack;

import java.io.IOException;
import java.nio.ByteBuffer;

public class RemoteTakeCommand extends Command {

    public RemoteTakeCommand(RemoteMessage msg, Connection cn) throws ClassNotFoundException {
        this(msg.fromDgmName, msg.key, cn, Class.forName(msg.clazz));
    }

    public RemoteTakeCommand(String fromDgmName, Command cm, Connection cn) {
        this(fromDgmName, cm.key, cn, cm.clazz);
    }

    private RemoteTakeCommand(String fromDgmName, String key, Connection cn, Class clazz){
        this.type = CommandType.REMOTETAKE;
        this.fromDgmName = fromDgmName;
        this.key = key;
        this.clazz = clazz;
        this.dg = new MessagePackDataGear(this.clazz);
        this.connection = cn;

    }

    @Override
    public void execute() {
        new ReplyCommand(this).run();
    }

    @Override
    public ByteBuffer convert() {
        ByteBuffer buf = null;
        MessagePack packer = new MessagePack();

        try {
            byte[] command = packer.write(createRemoteMessage());
            buf = ByteBuffer.allocate(command.length);
            buf.put(command);

            buf.flip();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return buf;
    }
}