package net.northking.cctp.upperComputer.driver.agent.command;


import net.northking.cctp.upperComputer.driver.agent.command.protocol.AbstractProtocolCommand;
import net.northking.cctp.upperComputer.driver.agent.command.protocol.ProtocolStreamUtil;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class EchoCommand extends AbstractProtocolCommand<String> {

    private static EchoCommand instance = null;

    public static EchoCommand getInstance() {
        if (instance == null) {
            instance = new EchoCommand();
        }
        return instance;
    }

    private String text = "";

    private EchoCommand() {
    }

    public EchoCommand(String text) {
        if (text == null) throw new IllegalArgumentException("文本不能为null");
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        if (text == null) throw new IllegalArgumentException("文本不能为null");
        this.text = text;
    }

    @Override
    public int getCommandId() {
        return 2;
    }

    @Override
    public void doSend(DataOutput output) throws IOException {
        ProtocolStreamUtil.writeText(output, text);
    }

    @Override
    public String buildReceive(DataInput input) throws IOException {
        return ProtocolStreamUtil.readText(input);
    }
}
