/*
 * LOGICAL-PARADOX.ORG
 * Copyright (C)2005 satoshi akabane(akabane@logical-paradox.org)
 * $Id: expand.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 java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.logical_paradox.shell.Argument;
import org.logical_paradox.shell.Command;
import org.logical_paradox.shell.Option;
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.Response;
import org.logical_paradox.vsan.dac.ResponseCode;
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>
 * ■expand
 * 
 * 【動作仕様】
 * DACに対して、領域拡張を要求します。
 * 領域拡張は、データファイルの追加によって行われます。
 * このコマンドは、dacコマンドによって設定された'dac'環境変数を参照します。
 * 
 * 【書式】
 * expand --record-begin=<begin record no.> --record-end=<end record no.> [<hostname>[:<port>]]
 * 
 * </pre>
 * 
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.2 $
 */
public class expand implements Command {
	public static final String USAGE = "expand --record-begin=<begin record no.> --record-end=<end record no.> [<hostname>[:<port>]]";

	public String usage() {
		return USAGE;
	}

	public ResultCode execute(OutputStream o, ShellEnv env, Argument a) throws CommandExecutionException, TerminationException {
		// 引数の解析
		if(a == null || a.getOptions().length != 2 || a.getArguments().length > 1) {
			throw new CommandExecutionException();
		}

		Option[] options = a.getOptions();
		long recordBegin = -1;
		long recordEnd = -1;

		for(int i = 0; i < options.length; i++) {
			Option option = options[i];
			if("record-begin".equals(option.getName())) {
				recordBegin = Long.parseLong(option.getValue());
			} else if("record-end".equals(option.getName())) {
				recordEnd = Long.parseLong(option.getValue());
			} else {
				// 想定外の不正なオプションが指定された場合
				throw new CommandExecutionException();
			}
		}

		// レコードの開始/終了番号の大小関係がおかしい場合
		if(recordBegin <= 0 || recordEnd <= 0 || recordBegin >= recordEnd) {
			throw new CommandExecutionException();
		}

		String dac = null;

		if(a == null || a.getArguments().length == 0) {
			dac = env.getProperty(DacConsoleConstant.ENV_DAC);
		} else if(a.getArguments().length > 1) {
			throw new CommandExecutionException();
		} else {
			dac = a.getArguments()[0];
		}

		try {
			// 領域拡張要求
			PrintWriter pw = new PrintWriter(o);
			pw.println("expanding storage..\n");

			ByteBuffer b = ByteBuffer.allocate(1+8+8).order(ByteOrder.BIG_ENDIAN);
			// 引数の設定
			b.put((byte)Protocol.P_EXPAND_DATAF);
			b.putLong(recordBegin);
			b.putLong(recordEnd);

			Response response = SocketWriter.send(dac, b.array(), false);

			if(response.getResponseCode() == ResponseCode.OK) {
				pw.println("done. check status using 'status' command.");
				pw.flush();
				pw.close();

				return new ResultCode(ResultCode.STATUS_OK);
			} else {
				// 実行ステータスコードがOK以外の場合
				pw.println("fatal error is caused in executing EXPAND command: code=[" + response.getResponseCode() + "]\n");
				pw.flush();
				pw.close();

				return new ResultCode(ResultCode.STATUS_ERROR);
			}
		} catch(IOException e) {
			throw new CommandExecutionException(e.getMessage());
		}
	}

}
