view src/pathfinder/mergetest/TestMerger2.java @ 153:6326e5ea4595

*** empty log message ***
author pin
date Sat, 23 Aug 2008 11:28:16 +0900 (2008-08-23)
parents 5942e0e3c632
children 6a3c982bd72a
line wrap: on
line source
package pathfinder.mergetest;

import java.util.LinkedList;
import remoteeditor.command.REPCommand;
import remoteeditor.network.REP;

public class TestMerger2 {
	static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE };
	protected NetworkSimulator<REPCommand> ns=null;
	protected LinkedList<EditorSimulatorWithoutMerger> editors;
	protected SessionManagerSimulatorWithMerger<REPCommand> sema;
	protected LinkedList<UsersSimulator> users = new LinkedList<UsersSimulator>();
	
	static private String[] text0 = {
		"aaa", "bbb", "ccc", "ddd", "eee",
		"fff", "ggg", "hhh", "iii", "jjj",
		"kkk", "lll", "mmm", "nnn", "ooo",
		"ppp", "qqq", "rrr", "sss", "ttt",
		"uuu", "vvv", "www", "xxx", "yyy", "zzz"
	};

	public TestMerger2(){
		editors = new LinkedList<EditorSimulatorWithoutMerger>();
	}

	public static void main(String[] args){
		TestMerger2 tm;
		/* get the number of Editors.  */
		int i = (args.length>0) ? Integer.parseInt(args[0]) : 4;
		System.out.println("number of Editor = "+i);
		/* get the number of Command.  */
		int j = (args.length>1) ? Integer.parseInt(args[1]) : 3;
		System.out.println("number of Packet = "+j);

		/* create, initialize and start test.  */
		tm = new TestMerger2();
		//tm.init(false, i, j, 1);  // init( boolean SM?, int max_client, int max_packet, int loglevel);
		tm.init(true, i, j, 1);
		tm.startTest();

		tm.printAllTexts();
		//if (!tm.checkCS())
		//	System.out.println("Error!! :some ChannelSimulator still have packet!");
		if (!tm.checkEquality())
			System.out.println("Error!! :all Editor's text is NOT mutch!");
		assert tm.checkEquality();
	}

	protected void startTest() {
		/* start all Editors.  */
		for (EditorSimulatorWithoutMerger ee: editors){
			ee.start();
		}
		/* start SessionManager if it exist.  */
		if (sema!=null) sema.init();
		if (sema!=null) sema.start();
		
		for(UsersSimulator u: users){
			u.start();
		}
		for(UsersSimulator u: users){
			try {
				u.join();
			} catch (InterruptedException e) { }
		}


		/* wait Editors finish.  */
		for (EditorSimulatorWithoutMerger ee: editors){
			try {
				ee.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		/* inform SessionManager to finish.  */
		if (sema!=null) {
			sema.finish();
			try {
				sema.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	 LinkedList<REPCommand> userCommand(int eid){
		LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
		cmds .add(new REPCommand(REP.REPCMD_REPLACE, 0, eid, 1, 0, 0, "replaced by editor:" + eid + ":1"));
		cmds .add(new REPCommand(REP.REPCMD_REPLACE, 0, eid, 2, 0, 0, "replaced by editor:" + eid + ":2"));
		cmds .add(new REPCommand(REP.REPCMD_REPLACE, 0, eid, 3, 0, 0, "replaced by editor:" + eid + ":3"));
		if(eid == 0) cmds .add(new REPCommand(REP.SMCMD_QUIT, 0, eid, 4, 0, 0, "replaced by editor:" + eid + ":4"));
		return cmds;
	}
	
	protected void init(boolean sm, int ne, int np, int ll){
		/* create NetworkSimulator, and SessionManager if it's required.   */
		if (sm){
			ns = new NetworkSimulatorwithSeMa<REPCommand>();
			sema = new SessionManagerSimulatorWithMerger<REPCommand>(ns, ne, 0);
		} else {
			ns = new NetworkSimulatorwithoutSeMa<REPCommand>();
			sema = null;
		}
		ns.setLogLevel(ll);

		/* create UsersSimulator.  */
		for(int i = 0; i < ne; i++){
			ChannelSimulator<REPCommand> channel = ns.getAcceptedSession(i);
			users.add(new UsersSimulator(channel, userCommand(i)));	
		}

		/* create ne Editors and np commands.  */
		for (int i=0; i<ne; i++){
			//LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
			// 各エディタが送信するコマンド列を生成

			/* create command list.  */
			/*
			for (int j=0; j<np; j++){
				String str = "created by Editor"+i+":"+j;
				REPCommand cmd = new REPCommand(REP.REPCMD_INSERT,
					0, i, j,
					10, //Verify.random(text.size()-1), //size-1?
					str.length(), str);
				cmds.add( cmd);
			}
			*/

			/* create a Editor, and pass command list to it.  */
			EditorSimulatorWithoutMerger ee = new EditorSimulatorWithoutMerger(i, ns, i==0?text0:null, "Editor"+i);
			if(i==0) ee.setOwner(true);
			editors.add(ee);
		}
	}

	protected void printAllTexts(){
		for(EditorSimulatorWithoutMerger ee: editors){
			System.out.println("--"+ee.getName()+"------------------------");
			ee.getText().printAllText();
		}
	}
/*
	private boolean checkCS(){
		return ns.checkAllCS();
	}
*/
	protected boolean checkEquality(){
		/*
		Text ee0 = editors.remove().getText();
		return editors.remove().getText().equals(ee0);
		*/
		Text text0 = editors.element().getText();
		//System.out.println("------------------- 結果 --------------------");
		for(EditorSimulatorWithoutMerger ee: editors){
			//System.out.println(" ");
			//ee.getText().printAllText();
			if (!text0.equals(ee.getText())) return false;
		}
		return true;
	}
}