changeset 148:bc162b1a7ebf

*** empty log message ***
author pin
date Thu, 10 Jul 2008 05:19:45 +0900
parents a9fdbcbe351f
children 5942e0e3c632
files src/pathfinder/mergetest/EditorSimulator.java src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java src/pathfinder/mergetest/SeMaSimulator.java src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java src/pathfinder/mergetest/TestMerger.java src/pathfinder/mergetest/TestMerger2.java
diffstat 6 files changed, 304 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/pathfinder/mergetest/EditorSimulator.java	Tue May 27 18:32:03 2008 +0900
+++ b/src/pathfinder/mergetest/EditorSimulator.java	Thu Jul 10 05:19:45 2008 +0900
@@ -7,15 +7,15 @@
 import sample.merge.TranslaterImp1;
 
 public class EditorSimulator extends Thread{
-	private int eid;
+	protected int eid;
 	private int seq;
 	//private boolean isOwner;
-	private NetworkSimulator<REPCommand> ns;
-	private ChannelSimulator<REPCommand> cs;
+	protected NetworkSimulator<REPCommand> ns;
+	protected ChannelSimulator<REPCommand> cs;
 	private Queue<REPCommand> CmdList;
 	private TranslaterImp1 translater;
-	private Text text;
-	private boolean running=true;
+	protected Text text;
+	protected boolean running=true;
 
 	public EditorSimulator(int _eid, NetworkSimulator<REPCommand> _ns, Queue<REPCommand> q, String _name) {
 		super(_name);
@@ -44,7 +44,7 @@
 		while(running){
 			REPCommand cmd = cs.read();
 			/* received Command */
-			/*if(eid == 0)*/System.out.println("editor" + eid + ":" + cmd.string + ":");
+			//*if(eid == 0)*/System.out.println("editor" + eid + ":" + cmd.string + ":");
 			
 			REPCommand[] cmds;
 
@@ -82,7 +82,7 @@
 		ns.writeLog("Editor"+eid+" finish.", 1);
 	}
 
-	private void sendOneCommand(REPCommand cmd) {
+	protected void sendOneCommand(REPCommand cmd) {
 		REPCommand[] cmds;
 		if (cmd==null) cmd = CmdList.poll();
 		if (cmd==null) return;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java	Thu Jul 10 05:19:45 2008 +0900
@@ -0,0 +1,55 @@
+package pathfinder.mergetest;
+
+import java.util.Queue;
+
+import remoteeditor.command.REPCommand;
+import remoteeditor.network.REP;
+
+public class EditorSimulatorWithoutMerger extends EditorSimulator {
+
+	public EditorSimulatorWithoutMerger(int _eid, NetworkSimulator<REPCommand> _ns, Queue<REPCommand> q, String _name) {
+		super(_eid, _ns, q, _name);
+	}
+	
+	public void run(){
+		ns.writeLog("Editor"+eid+" start.", 1);
+		
+		while(running){
+			//ns.writeLog("EditorWithoutMerger:"+ eid + ":" +running, 1);
+			REPCommand command = cs.read();
+			//ns.writeLog("EditorWithoutMerger:"+ eid + ":" +running, 1);
+			if(command.eid==eid){
+				/* 終了条件  */
+				if (command.cmd==REP.SMCMD_QUIT){
+					ns.writeLog("\tEditor"+eid+" catch QUIT command emited by itself.", 2);
+					running=false; break;
+				}
+			}else if(command.eid == -1){
+				/* 制御プロセスからの指令  */
+				ns.writeLog("\tEditor"+eid+" send command.", 2);
+				if (command.cmd==REP.SMCMD_QUIT)
+					synchronized(ns){ ns.writeLog("send Quit cmd.", 1); }
+				sendOneCommand(command);
+			}else{
+				text.edit(command);
+				cs.write(command);
+			}
+			//ns.writeLog("EditorWithoutMerger:"+ eid + ":" +running, 1);
+		}
+	}
+	
+	protected void sendOneCommand(REPCommand command){
+		REPCommand cmd = new REPCommand(command);
+		cmd .eid = eid;
+		
+		cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq);
+
+		//if (isOwner) cmd.setThroughMaster(true);
+		//for (int i=0; i<cmds.length; i++){
+			text.edit(cmd);
+			cs.write(new REPCommand(cmd));
+		//}
+		Thread.yield();
+	}
+
+}
--- a/src/pathfinder/mergetest/SeMaSimulator.java	Tue May 27 18:32:03 2008 +0900
+++ b/src/pathfinder/mergetest/SeMaSimulator.java	Thu Jul 10 05:19:45 2008 +0900
@@ -5,10 +5,10 @@
 
 public class SeMaSimulator<P> extends Thread {
 	private int MAX_PACKET;
-	private int MAX_CLIENT;
-	private boolean running=true;
-	private NetworkSimulator<P> ns;
-	private List<ChannelSimulator<P>> csList;
+	protected int MAX_CLIENT;
+	protected boolean running=true;
+	protected NetworkSimulator<P> ns;
+	protected List<ChannelSimulator<P>> csList;
 
 	public SeMaSimulator(NetworkSimulator<P> _ns, int max_client, int max_packet){
 		ns = _ns;
@@ -33,13 +33,14 @@
 	/**
 	 * Check whether the NetworkSimulator hold waiting connections.
 	 */
-	private void checkAccept(){
+	protected void checkAccept(){
 		ChannelSimulator<P> cs;
 		while((cs=ns.accept())!=null){
 			csList.add(cs);
 		}
 	}
 	public void init(){
+		ns.writeLog("SessionManager.init", 1);
 		while(csList.size()<MAX_CLIENT){ checkAccept(); Thread.yield(); }
 	}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java	Thu Jul 10 05:19:45 2008 +0900
@@ -0,0 +1,99 @@
+package pathfinder.mergetest;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import remoteeditor.command.REPCommand;
+import sample.merge.TranslaterImp1;
+
+public class SessionManagerSimulatorWithMerger<P> extends SeMaSimulator<P> {
+
+	//private TranslaterImp1 translater;
+	private List<TranslaterImp1> editorList;
+	
+	public SessionManagerSimulatorWithMerger(NetworkSimulator<P> _ns, int max_client, int max_packet) {
+		super(_ns, max_client, max_packet);
+		editorList = new ArrayList<TranslaterImp1>();
+	}
+
+	public SessionManagerSimulatorWithMerger(NetworkSimulator<P> ns, int ne) {
+		super(ns, ne);
+	}
+
+
+	public void run(){
+		int i = 0;
+		P packet;
+		//REPCommand command;
+		ns.writeLog("SessionManager start.", 1);
+		
+		ChannelSimulator<P> cs = csList.get(i);
+		
+		while(running){
+			synchronized(ns){
+				while((packet=cs.read())==null && running){
+					
+					
+					i = (i+1)%csList.size();   // i++
+					cs = csList.get(i);        // 次のChennelをゲット
+				}
+			}
+		}
+	}
+	
+	protected void checkAccept(){
+		ChannelSimulator<P> cs;
+		while((cs=ns.accept())!=null){
+			//ns.writeLog("SessionManager.accept", 1);
+			csList.add(cs);
+			editorList.add(new TranslaterImp1(editorList.size()));
+		}
+	}
+	
+	private void merge(int channelno, P packet){
+		REPCommand cmd;
+		int eid = channelno;
+		TranslaterImp1 translater = editorList.get(eid);
+		
+		//if(packet instanceof REPCommand){
+			cmd = (REPCommand) packet;
+		//}else {		}
+		
+			REPCommand[] cmds;
+
+			ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq, 3);
+
+			if (cmd.eid==eid){
+				cmds = translater.catchOwnCommand(cmd);
+				for (int i=0; i<cmds.length; i++){
+					REPCommand tmp = cmds[i];
+					ns.writeLog("\t\tEditor"+eid+" edit text. ", 4);
+					//text.edit(tmp);
+					((ChannelSimulator<P>) csList.get(eid)).write((P) tmp);
+				}
+				/* 終了条件  */
+				//if (cmd.cmd==REP.SMCMD_QUIT){
+				//	ns.writeLog("\tEditor"+eid+" catch QUIT command emited by itself.", 2);
+					//running=false; break;
+				//}
+			} else if (cmd.eid==-1){
+				/* 制御プロセスからの指令  */
+				//ns.writeLog("\tEditor"+eid+" send command.", 2);
+				//if (cmd.cmd==REP.SMCMD_QUIT)
+				//	synchronized(ns){ ns.writeLog("send Quit cmd.", 1); }
+				//sendOneCommand(cmd);
+			} else {
+				cmds = translater.transReceiveCmd(cmd);
+				for (int i=0; i<cmds.length; i++){
+					cmd = cmds[i];
+					ns.writeLog("\t\tEditor"+eid+" edit text and pass Cmd. ", 4);
+					//text.edit(cmd);
+					csList.get(eid).write((P) cmd);
+					//cs.write(new REPCommand(cmd));
+					
+				}
+			}
+		
+	}
+
+}
--- a/src/pathfinder/mergetest/TestMerger.java	Tue May 27 18:32:03 2008 +0900
+++ b/src/pathfinder/mergetest/TestMerger.java	Thu Jul 10 05:19:45 2008 +0900
@@ -6,10 +6,10 @@
 
 public class TestMerger {
 	static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE };
-	private NetworkSimulator<REPCommand> ns=null;
-	private LinkedList<EditorSimulator> editors;
-	private SeMaSimulator<REPCommand> sema;
-	private UsersSimulator users;
+	protected NetworkSimulator<REPCommand> ns=null;
+	protected LinkedList<EditorSimulator> editors;
+	protected SeMaSimulator<REPCommand> sema;
+	protected UsersSimulator users;
 
 	public TestMerger(){
 		editors = new LinkedList<EditorSimulator>();
@@ -26,7 +26,8 @@
 
 		/* create, initialize and start test.  */
 		tm = new TestMerger();
-		tm.init(false, i, j, 1);  // init( boolean SM?, int max_client, int max_packet, int loglevel);
+		//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();
@@ -37,7 +38,7 @@
 		assert tm.checkEquality();
 	}
 
-	private void startTest() {
+	protected void startTest() {
 		/* start all Editors.  */
 		for (EditorSimulator ee: editors){
 			ee.start();
@@ -60,7 +61,7 @@
 		if (sema!=null) sema.finish();
 	}
 
-	private void init(boolean sm, int ne, int np, int ll){
+	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>();
@@ -98,7 +99,7 @@
 		}
 	}
 
-	private void printAllTexts(){
+	protected void printAllTexts(){
 		for(EditorSimulator ee: editors){
 			System.out.println("--"+ee.getName()+"------------------------");
 			ee.getText().printAllText();
@@ -109,7 +110,7 @@
 		return ns.checkAllCS();
 	}
 */
-	private boolean checkEquality(){
+	protected boolean checkEquality(){
 		/*
 		Text ee0 = editors.remove().getText();
 		return editors.remove().getText().equals(ee0);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/TestMerger2.java	Thu Jul 10 05:19:45 2008 +0900
@@ -0,0 +1,127 @@
+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 UsersSimulator users;
+
+	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]) : 2;
+		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();
+		users.init();
+		users.start();
+
+		/* wait Editors finish.  */
+		for (EditorSimulatorWithoutMerger ee: editors){
+			try {
+				ee.join();
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			}
+		}
+		/* inform SessionManager to finish.  */
+		if (sema!=null) sema.finish();
+	}
+
+	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.  */
+		users = new UsersSimulator(ns, ne, np*ne);
+
+		/* 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, cmds, "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;
+	}
+}