/*
 * DelayedSyncTest
 * 遅延同期のテスト
 * このテストツールを使用するには，下記のサービスを開始しておく必要がある
 *
 * ・経路決定ノード選択子
 * ・経路決定ノードクライアント生成子
 */
package org.logical_paradox.rss.test.dsync;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.Naming;

import org.logical_paradox.rss.dsr.RSSDistributedServiceRegistry;
import org.logical_paradox.rss.dsync.RSSSyncDaemon;
import org.logical_paradox.rss.router.RNodeClientFactory;
import org.logical_paradox.rss.router.RoutingNodeClient;

public class DelayedSyncTest {
	public static final String DELAYED_SYNC_TEST_CONFIG = "conf/test/dsync/dsynctest.conf";

	RSSDistributedServiceRegistry dsr;
	RNodeClientFactory rnode;
	DelayedSyncConfig config;
	RSSSyncDaemon syncd;
	String nodeKey;

	/*
	 * デフォルトコンストラクタ
	 */
	public DelayedSyncTest() throws Exception {
		config = new DelayedSyncConfig( DELAYED_SYNC_TEST_CONFIG );
		dsr = (RSSDistributedServiceRegistry)Naming.lookup(config.getDistServRegURL());
		rnode = (RNodeClientFactory)dsr.getService(RSSDistributedServiceRegistry.RSS_SERVID_ROUTING_NODE_CLIENT_FACTORY);
		RoutingNodeClient client = (RoutingNodeClient)rnode.bindSearchNode();
		nodeKey = client.getNodeId();
		syncd = new RSSSyncDaemon( config, client );
	}

	public void doTest() {
		syncd.start();
	}

	/*
	 * メインメソッド
	 */
	public static void main( String[] args ) throws Exception {
		final DelayedSyncTest tester = new DelayedSyncTest();

		DelayedSyncTestGUI mainWnd = new DelayedSyncTestGUI( "RSS/1b delayed sync mechanism test client", tester.syncd );
		mainWnd.addWindowListener( new WindowAdapter() {
					public void windowClosing( WindowEvent we ) {
						try {
							tester.rnode.unbindSearchNode(tester.nodeKey);
							tester.syncd.shutdown();
						} catch( Exception e ) {
							e.printStackTrace();
						}
						System.exit(0);
					}
			}
		);

		mainWnd.initApplication();
		mainWnd.repaint();

		tester.doTest();
	}
}

// end of DelayedSyncTest.java
