/*
 * $Id: topology.java,v 1.6 2005/07/16 05:10:15 rampil Exp $
 * LOGICAL-PARADOX.ORG
 * Copyright (C)2005 satoshi akabane(akabane@logical-paradox.org)
 *
 */
package org.logical_paradox.gdrn.shell.command;

import java.io.OutputStream;

import org.logical_paradox.common.swing.Window;
import org.logical_paradox.gdrn.shell.command.topo.AppContext;
import org.logical_paradox.gdrn.shell.command.topo.DrawableNode;
import org.logical_paradox.gdrn.shell.command.topo.Topology;
import org.logical_paradox.gdrn.shell.command.topo.TopologyViewPanel;
import org.logical_paradox.rss.dsr.RSSDistributedServiceRegistry;
import org.logical_paradox.rss.dsr.routing.Node;
import org.logical_paradox.rss.lookup.Lookup;
import org.logical_paradox.rss.lookup.LookupFactory;
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;

/**
 * <pre>
 * ■topology
 * 
 * 【動作仕様】
 * ネットワーク構造を表示する
 * 
 * 【書式】
 * topology [--depth=<trace-depth>] [dsruri]
 * </pre>
 * 
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.6 $
 */
public class topology implements Command {
	public static final String USAGE = "topology [--depth=<trace-depth>] [dsruri]";

	public String usage() {
		return USAGE;
	}

	public ResultCode execute(OutputStream o, ShellEnv env, Argument a) throws CommandExecutionException, TerminationException {
		String[] arguments = a != null ? a.getArguments() : new String[0];
		String dsruri = env.getProperty("DEFAULT_DSR_URI");
		Option[] options = a == null || a.getOptions() == null ? new Option[0] : a.getOptions();
		int depth = 0;

		// 引数の解析
		if(arguments.length > 1) {
			throw new CommandExecutionException("Too many arguments");
		} else if(arguments.length == 1) {
			dsruri = arguments[0];
		}
		// オプションの解析
		for(int i = 0; i < options.length; i++) {
			if("depth".equals(options[i].getName()) == true) {
				depth = Integer.parseInt(options[i].getValue());
			} else {
				new CommandExecutionException("Unknown option '--" + options[i].getName() + "' is specified.");
			}
		}
		// 起点となるDSRを取得する
		try {
			Lookup lookup = LookupFactory.getLookup(dsruri);
			// ウィンドウオープン
			AppContext ctx = new AppContext();
			Topology topo = new Topology();

			// 起点となるDSRのノードをトポロジに追加しておく
			RSSDistributedServiceRegistry dsr = (RSSDistributedServiceRegistry)lookup.lookup(dsruri);
			topo.deployNewNode(new DrawableNode(new Node(dsr, dsr.getName())));
			// トポロジの構築
			topo.configureAll();

			ctx.setTopology(topo);

			// 別ウィンドウでネットワークブラウザを開く
			Window window = new Window("network browser", new TopologyViewPanel(ctx), 640, 480);
			window.setVisible(true);
		} catch (Exception e) {
			throw new CommandExecutionException("Fatal error caused in looking up current dsr.");
		}

		return new ResultCode(ResultCode.STATUS_OK);
	}

}
