/*
 * LOGICAL-PARADOX.ORG
 * Copyright (C)2005 satoshi akabane(akabane@logical-paradox.org)
 * $Id: shutdown.java,v 1.2 2005/09/07 13:38:06 rampil Exp $
 */
package org.logical_paradox.vsan.tools.console.command;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import org.logical_paradox.shell.Argument;
import org.logical_paradox.shell.Command;
import org.logical_paradox.shell.ResultCode;
import org.logical_paradox.shell.ShellEnv;
import org.logical_paradox.shell.exception.CommandExecutionException;
import org.logical_paradox.shell.exception.TerminationException;
import org.logical_paradox.vsan.dac.command.Protocol;
import org.logical_paradox.vsan.tools.console.DacConsoleConstant;
import org.logical_paradox.vsan.tools.console.util.SocketWriter;

/**
 * <pre>
 * ■shutdown
 * 
 * 【動作仕様】
 * DACに対してシャットダウンを要求します。
 * このコマンドは、dacコマンドによって設定された'dac'環境変数を参照します。
 * 
 * 【書式】
 * shutdown [<hostname>[:<port>]]
 * 
 * </pre>
 * 
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.2 $
 */
public class shutdown implements Command {
	public static final String USAGE = "shutdown [<hostname>[:<port>]]";

	public String usage() {
		return USAGE;
	}
	public ResultCode execute(OutputStream o, ShellEnv env, Argument a) throws CommandExecutionException, TerminationException {

		// シャットダウンコマンドを送信する
		try {
			PrintWriter pw = new PrintWriter(o);
			pw.println("sending shutdown command..");

			SocketWriter.send(env.getProperty(DacConsoleConstant.ENV_DAC), new byte[] {(byte)Protocol.P_SHUTDOWN}, true);

			pw.println("done.");
			pw.flush();
			pw.close();
		} catch(IOException e) {
			throw new CommandExecutionException(e.getMessage());
		}
		return new ResultCode(ResultCode.STATUS_OK);
	}

}
