changeset 149:5942e0e3c632

*** empty log message ***
author pin
date Mon, 04 Aug 2008 20:10:25 +0900
parents bc162b1a7ebf
children e33871f4b39a
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, 241 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
--- a/src/pathfinder/mergetest/EditorSimulator.java	Thu Jul 10 05:19:45 2008 +0900
+++ b/src/pathfinder/mergetest/EditorSimulator.java	Mon Aug 04 20:10:25 2008 +0900
@@ -8,11 +8,11 @@
 
 public class EditorSimulator extends Thread{
 	protected int eid;
-	private int seq;
+	protected int seq;
 	//private boolean isOwner;
 	protected NetworkSimulator<REPCommand> ns;
 	protected ChannelSimulator<REPCommand> cs;
-	private Queue<REPCommand> CmdList;
+	protected Queue<REPCommand> CmdList;
 	private TranslaterImp1 translater;
 	protected Text text;
 	protected boolean running=true;
@@ -87,7 +87,7 @@
 		if (cmd==null) cmd = CmdList.poll();
 		if (cmd==null) return;
 
-		cmd.eid = eid;
+		//cmd.eid = eid;
 		cmds = translater.transSendCmd(cmd);
 		cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq);
 
--- a/src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java	Thu Jul 10 05:19:45 2008 +0900
+++ b/src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java	Mon Aug 04 20:10:25 2008 +0900
@@ -12,44 +12,67 @@
 	}
 	
 	public void run(){
-		ns.writeLog("Editor"+eid+" start.", 1);
-		
+		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;
+
+			// MainLoop
+			while(running){
+
+				REPCommand cmd = cs.read();
+				/* received Command */
+
+				ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq, 3);
+				//if (cmd.eid != -2 && eid == 0) ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" :"+cmd, 1);
+
+				//System.out.println(eid + ":" + cmd.eid);
+
+				if (cmd.eid==eid){
+
+					text.edit(cmd);
+					cs.write(new REPCommand(cmd));
+					
+					/* 終了条件  */
+					if (cmd.cmd==REP.SMCMD_QUIT){
+						ns.writeLog("\tEditor"+eid+" catch QUIT command emited by itself.", 3);
+						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 if(cmd.eid == -2){
+					// Merged Commands.
+					text.edit(cmd);
+				} else {
+
+					ns.writeLog("\t\tEditor"+eid+" edit text and pass Cmd. " + " : " + cmd, 3);
+					text.edit(cmd);
+					cs.write(new REPCommand(cmd));
 				}
-			}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);
+
+			ns.writeLog("Editor"+eid+" finish.", 1);
 		}
 	}
 	
-	protected void sendOneCommand(REPCommand command){
-		REPCommand cmd = new REPCommand(command);
-		cmd .eid = eid;
-		
+	protected void sendOneCommand(REPCommand cmd){
+
+		if (cmd==null) cmd = CmdList.poll();
+		if (cmd==null) return;
+
+		cmd.eid = eid;
+		cmd.seq = seq++;
 		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));
-		//}
+		text.edit(cmd);
+		cs.write(new REPCommand(cmd));
+
 		Thread.yield();
 	}
-
+	
 }
--- a/src/pathfinder/mergetest/SeMaSimulator.java	Thu Jul 10 05:19:45 2008 +0900
+++ b/src/pathfinder/mergetest/SeMaSimulator.java	Mon Aug 04 20:10:25 2008 +0900
@@ -3,8 +3,10 @@
 import java.util.ArrayList;
 import java.util.List;
 
-public class SeMaSimulator<P> extends Thread {
-	private int MAX_PACKET;
+import remoteeditor.command.REPCommand;
+
+public class SeMaSimulator<P extends REPCommand> extends Thread {
+	protected int MAX_PACKET;
 	protected int MAX_CLIENT;
 	protected boolean running=true;
 	protected NetworkSimulator<P> ns;
--- a/src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java	Thu Jul 10 05:19:45 2008 +0900
+++ b/src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java	Mon Aug 04 20:10:25 2008 +0900
@@ -1,99 +1,215 @@
 package pathfinder.mergetest;
 
 import java.util.ArrayList;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Stack;
+import java.util.concurrent.BlockingQueue;
 
 import remoteeditor.command.REPCommand;
+import remoteeditor.network.REP;
 import sample.merge.TranslaterImp1;
 
-public class SessionManagerSimulatorWithMerger<P> extends SeMaSimulator<P> {
+public class SessionManagerSimulatorWithMerger<P extends REPCommand> 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>();
+	private List<ArrayList<REPCommand>> mergedList;
+	//private List<LinkedList<REPCommand>> editorList2;
+
+	public SessionManagerSimulatorWithMerger(NetworkSimulator<P> _ns) {
+		super(_ns);
 	}
 
 	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をゲット
-				}
-			}
-		}
+	
+	public SessionManagerSimulatorWithMerger(NetworkSimulator<P> _ns, int max_client, int max_packet) {
+		super(_ns, max_client, max_packet);
+		editorList = new ArrayList<TranslaterImp1>();
+		mergedList = new ArrayList<ArrayList<REPCommand>>();
+		//editorList2 = new ArrayList<LinkedList<REPCommand>>();
 	}
 	
 	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()));
+			mergedList.add(new ArrayList<REPCommand>());
+			//editorList2.add(new LinkedList<REPCommand>());
+			//System.out.println(csList.size() + " : " + editorList.size() + " : " + mergedList.size());
 		}
 	}
 	
-	private void merge(int channelno, P packet){
-		REPCommand cmd;
-		int eid = channelno;
-		TranslaterImp1 translater = editorList.get(eid);
+	public void run(){
+
+		//int i=0;
+		int count=0;
+		P packet = null;
+		//REPCommand[] cmds;
+
+		ns.writeLog("SessionManager start.", 1);
+
+		/* Main Loop */
+		ChannelSimulator<P> cs = null; // = csList.get(i);
 		
-		//if(packet instanceof REPCommand){
-			cmd = (REPCommand) packet;
-		//}else {		}
+		try {
+			Thread.sleep(4000);
+		} catch (InterruptedException e1) {
+			e1.printStackTrace();
+		}
 		
-			REPCommand[] cmds;
+		while(running && (MAX_PACKET==0 || count<MAX_PACKET)){
 
-			ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq, 3);
+			int eid = 0;
+			// should be move to NetworkSimulator.  ns.select(csList); 
+			boolean result = true;
+			synchronized(ns){
 
-			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);
+				for(int i = 0; i < csList.size(); i++){
+					cs = csList.get(i);
+					if((packet=cs.poll())!=null) {
+						ns.writeLog("SeMa pass packet to "+i+":>> "+packet.toString(), 3);
+						eid = i;
+						break;
+					}
+				}
+				
+				if(cs == null) {
+					try { ns.wait(); } catch (InterruptedException e) { e.printStackTrace(); }
+					//continue;
+					result = false;
 				}
-				/* 終了条件  */
-				//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));
+				if(packet == null) result = false;
+			}
+			if(!result) continue;
+			if(!running) break;
+			
+			int neid = (eid+1)%csList.size();
+
+			REPCommand[] cmds3 = receivePacket(eid, packet);
+			if(cmds3 != null) sendPacket(neid, cmds3);
+
+			
+/*			// Merge
+			REPCommand command = unpack(packet);
+			REPCommand[] cmds;
+			
+			//int neid = (eid+1)%csList.size();
+			cs = csList.get(neid);
+			
+			if(neid == command.eid){
+				
+				// コマンドが一周して戻ってきた場合
+				cmds = editorList.get(neid).catchOwnCommand(command);
+				for (int j=0; j<cmds.length; j++){
+					P tmp = (P)cmds[j];
+					P tmp2 = (P) new REPCommand(tmp);
+					tmp2.eid = -2;
 					
+					if ( !cs.write(tmp2) ){
+						ns.writeLog("Session Manager failed to write.", 0);
+					}
 				}
-			}
+				if (command.cmd==REP.SMCMD_QUIT){
+					if ( !cs.write(packet) ){
+						ns.writeLog("Session Manager failed to write.", 0);
+					}
+				}
+			}else if(eid == command.eid){
+				//editorからの最初のコマンド
+				cmds = editorList.get(eid).transSendCmd(command);
+
+				REPCommand[] cmds2 = editorList.get(neid).transReceiveCmd(command);
+				for (int j=0; j<cmds2.length; j++){
+					cs.write((P) new REPCommand(cmds2[j]));
+				}
+
+			
+			}else{
+				//それ以外の場合は隣のエディタにコマンドを転送
+
+				cmds = editorList.get(neid).transReceiveCmd(command);
+				for (int j=0; j<cmds.length; j++){
+					REPCommand cmd = cmds[j];
+					if ( !cs.write((P) new REPCommand(cmd)) ){
+						ns.writeLog("Session Manager failed to write.", 0);
+					}
+				}
+
+			}*/
+			count++;
+		}
+		ns.writeLog("SessionManager finish.", 1);
+	}
+
+	private void sendPacket(int neid, REPCommand[] cmds) {
+		ChannelSimulator<P> cs = csList.get(neid);
+		REPCommand[] cmds2;
+		
+		//QUITコマンドの場合はマージせずにそのまま送信
+		if(cmds[0].eid == neid && cmds[0].cmd == REP.SMCMD_QUIT){
+			cs.write((P) new REPCommand(cmds[0]));
+			return;
+		}
+		
+		//コマンドを次のエディタに送信
+		for(REPCommand command : cmds){
+			cs.write((P) new REPCommand(command));
+		}
 		
 	}
 
+	private REPCommand[] receivePacket(int eid, P packet) {
+		REPCommand command;
+		command = unpack(packet);
+		REPCommand[] cmds;
+		
+		
+		ArrayList<REPCommand> list = mergedList.get(eid);
+		if(list.size() > 0){
+			REPCommand command2 = list.get(0);
+			if(command2.eid == command.eid && command2.seq == command.seq){
+				//マージしてエディタへ送信したコマンドが戻ってきた場合
+				list.remove(0);
+				if(eid == 0) System.out.println(list.size());
+				return null;
+			}
+		}
+		
+		
+		//QUITコマンドの場合はマージャに追加せずそのまま次のエディタへ送信
+		if(command.cmd == REP.SMCMD_QUIT){
+			return new REPCommand[]{command};
+		}
+		
+		if(command.eid == eid) {
+			LinkedList<REPCommand> sentCmds = editorList.get(eid).getSentCmds();
+			for(REPCommand sentCommand : sentCmds){
+				if(sentCommand.seq == command.seq){
+					return editorList.get(eid).catchOwnCommand(command);
+				}
+			}
+			//ユーザからの新しい編集コマンド
+			//エディタからの編集コマンドをマージのリストに追加
+			cmds = editorList.get(eid).transSendCmd(command);
+		}else{
+			//他のエディタから転送されてきた編集コマンド
+			cmds = editorList.get(eid).transReceiveCmd(command);
+		}
+		return cmds;
+	}
+
+	private REPCommand unpack(P packet) {
+		REPCommand cmd = null;
+		if(packet instanceof REPCommand){
+			cmd = (REPCommand) packet;
+			//ns.writeLog("Error!! :Packet type is NOT REPCommand!", 1);
+		}else{
+			ns.writeLog("Error!! :Packet type is NOT REPCommand!", 1);
+		}
+		return new REPCommand(cmd);
+	}
+	
 }
--- a/src/pathfinder/mergetest/TestMerger.java	Thu Jul 10 05:19:45 2008 +0900
+++ b/src/pathfinder/mergetest/TestMerger.java	Mon Aug 04 20:10:25 2008 +0900
@@ -18,7 +18,7 @@
 	public static void main(String[] args){
 		TestMerger tm;
 		/* get the number of Editors.  */
-		int i = (args.length>0) ? Integer.parseInt(args[0]) : 2;
+		int i = (args.length>0) ? Integer.parseInt(args[0]) : 3;
 		System.out.println("number of Editor = "+i);
 		/* get the number of Command.  */
 		int j = (args.length>1) ? Integer.parseInt(args[1]) : 3;
--- a/src/pathfinder/mergetest/TestMerger2.java	Thu Jul 10 05:19:45 2008 +0900
+++ b/src/pathfinder/mergetest/TestMerger2.java	Mon Aug 04 20:10:25 2008 +0900
@@ -18,7 +18,7 @@
 	public static void main(String[] args){
 		TestMerger2 tm;
 		/* get the number of Editors.  */
-		int i = (args.length>0) ? Integer.parseInt(args[0]) : 2;
+		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;