changeset 152:09ad66f62f4a before_REP_meeting_on_080822

*** empty log message ***
author pin
date Fri, 22 Aug 2008 15:48:52 +0900
parents 1768e68ba98e
children 6326e5ea4595
files bin/remoteeditor/network/REP.class src/pathfinder/mergetest/DocumentSimulator.java src/pathfinder/mergetest/EditorObject.java src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java src/pathfinder/mergetest/UsersSimulator.java src/remoteeditor/editors/RemoteEditor.java src/remoteeditor/network/REP.java src/sample/merge/TranslaterImp1.java
diffstat 9 files changed, 276 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
Binary file bin/remoteeditor/network/REP.class has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/DocumentSimulator.java	Fri Aug 22 15:48:52 2008 +0900
@@ -0,0 +1,5 @@
+package pathfinder.mergetest;
+
+public class DocumentSimulator {
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/EditorObject.java	Fri Aug 22 15:48:52 2008 +0900
@@ -0,0 +1,82 @@
+package pathfinder.mergetest;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import remoteeditor.command.REPCommand;
+import sample.merge.TranslaterImp1;
+
+public class EditorObject<P> {
+	
+	int eid;
+	private ChannelSimulator<P> channel;
+	private TranslaterImp1 translater;
+	private List<REPCommand> sentList;
+
+	public EditorObject(int i, ChannelSimulator<P> cs) {
+		// TODO Auto-generated constructor stub
+		eid = i;
+		channel = cs;
+	}
+
+	public EditorObject(int i, ChannelSimulator<P> cs, TranslaterImp1 imp1) {
+		// TODO Auto-generated constructor stub
+		eid = i;
+		channel = cs;
+		translater = imp1;
+		sentList = new ArrayList<REPCommand>();
+	}
+
+	public ChannelSimulator<P> getChannel() {
+		// TODO Auto-generated method stub
+		return channel;
+	}
+
+	public int getEID() {
+		// TODO Auto-generated method stub
+		return eid;
+	}
+
+	public REPCommand receive(REPCommand command) {
+		// TODO Auto-generated method stub
+		
+		if(command.eid == eid){
+			if(checkReturnCommand(command)){
+				REPCommand[] cmds = translater.catchOwnCommand(command);
+				sendMergedCommand(cmds);
+				return null;
+			}else{
+				sentList.add(command);
+			}	
+		}
+		
+		return command;
+	}
+
+	private boolean checkReturnCommand(REPCommand command) {
+		// TODO Auto-generated method stub
+		
+		if(sentList.size() > 0){
+			if(sentList.get(0).seq == command.seq){
+				return true;
+			}
+		}
+		
+		return false;
+	}
+
+	private void sendMergedCommand(REPCommand[] cmds) {
+		// TODO Auto-generated method stub
+		for(int i = 0; i < cmds.length; i++){
+			channel.write((P) cmds[i]);
+		}
+	}
+
+	public void send(REPCommand command) {
+		// TODO Auto-generated method stub
+		if(command !=null){
+			channel.write((P) command);
+		}
+	}
+
+}
--- a/src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java	Tue Aug 05 15:09:39 2008 +0900
+++ b/src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java	Fri Aug 22 15:48:52 2008 +0900
@@ -7,6 +7,8 @@
 
 public class EditorSimulatorWithoutMerger extends EditorSimulator {
 
+	private boolean lock;
+
 	public EditorSimulatorWithoutMerger(int _eid, NetworkSimulator<REPCommand> _ns, Queue<REPCommand> q, String _name) {
 		super(_eid, _ns, q, _name);
 	}
@@ -25,24 +27,23 @@
 				ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq, 3);
 				//if (eid == 3) ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" :"+cmd, 1);
 
-				//System.out.println(eid + ":" + cmd.eid);
+				//manage(cmd);
 
 				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)
+					if (cmd.cmd==REP.SMCMD_QUIT) {
+						//sendOneCommand(cmd);
+						cs.write(cmd);
 						synchronized(ns){ ns.writeLog("send Quit cmd.", 1); }
+						continue;
+					}
 					
+					if(lock) continue;
 					sendOneCommand(cmd);
 					
 				}else if(cmd.eid == -2){
@@ -68,17 +69,57 @@
 		}
 	}
 	
+	private void manage(REPCommand cmd) {
+		// TODO Auto-generated method stub
+		switch(cmd.cmd){
+		case REP.SMCMD_START_MERGE:
+			lockEdit(true);
+			break;
+		case REP.SMCMD_END_MERGE:
+			lockEdit(false);
+			break;
+		default:
+			break;
+		}
+	}
+
+	private void lockEdit(boolean b) {
+		// TODO Auto-generated method stub
+		lock = b;
+	}
+
 	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(cmd.cmd == REP.REPCMD_INSERT){
+			cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq);
+			cs.write(cmd);
+			
+		}else if(cmd.cmd == REP.REPCMD_DELETE){
+			String line = text.get(cmd.lineno);
+			cmd.setString(line);
+			cs.write(cmd);
+			
+		}else if(cmd.cmd ==  REP.REPCMD_REPLACE){
+
+			REPCommand undoCmd = new REPCommand(REP.SMCMD_UNDO_REPLACE, 0, eid, seq-1, cmd.lineno, 0, "");
+			undoCmd.setString(text.get(cmd.lineno));
+			cs.write(undoCmd);
+			
+			cmd.setString("replaced by Editor"+cmd.eid+":"+cmd.seq);
+			cs.write(cmd);
+			
+		}else if(cmd.cmd == REP.SMCMD_QUIT){
+			cmd.setString("sent by Editor"+cmd.eid+":"+cmd.seq);
+			cs.write(cmd);
+		}
 
 		text.edit(cmd);
-		cs.write(new REPCommand(cmd));
 
 		Thread.yield();
 	}
--- a/src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java	Tue Aug 05 15:09:39 2008 +0900
+++ b/src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java	Fri Aug 22 15:48:52 2008 +0900
@@ -9,11 +9,10 @@
 public class SessionManagerSimulatorWithMerger<P extends REPCommand> extends SeMaSimulator<P> {
 
 	private List<TranslaterImp1> editorList;
-	private List<ArrayList<REPCommand>> mergedList;
-	//private List<LinkedList<REPCommand>> editorList2;
 	private SelectorSimulator<P> selector;
 	private List<ArrayList<REPCommand>> sentList;
-	private REPCommand quitCommand;
+	private List<EditorObject<P>> editorList2;
+	private int countQuit;
 
 	public SessionManagerSimulatorWithMerger(NetworkSimulator<P> _ns) {
 		super(_ns);
@@ -26,10 +25,9 @@
 	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>>();
 		sentList = new ArrayList<ArrayList<REPCommand>>();
 		selector = new SelectorSimulator<P>(_ns);
-		//quitCommand = new REPCommand();
+		editorList2 = new ArrayList<EditorObject<P>>();
 	}
 	
 	protected void checkAccept(){
@@ -37,8 +35,8 @@
 		while((cs=ns.accept())!=null){
 			csList.add(cs);
 			editorList.add(new TranslaterImp1(editorList.size()));
-			mergedList.add(new ArrayList<REPCommand>());
 			sentList.add(new ArrayList<REPCommand>());
+			editorList2.add(new EditorObject<P>(editorList2.size(), cs, new TranslaterImp1(editorList2.size())));
 			registerChannel (selector, cs, SelectionKeySimulator.OP_READ);
 		}
 	}
@@ -80,19 +78,62 @@
 
 	private void manage(ChannelSimulator<P> channel, REPCommand command) {
 		// コマンドの処理
-		int eid = csList.indexOf(channel);
-		int neid = (eid+1)%csList.size();
-		ChannelSimulator<P> nextChannel = csList.get(neid);
-		REPCommand[] cmds;
+
+		int eid = getEID(channel);
+		int neid = (eid+1)%editorList2.size();
+		
+		EditorObject editor = getEditor(eid);
+		EditorObject nextEditor = getEditor(neid);
+		//editor.receive(command);
+		
+		switch(command.cmd){
+		
+		case REP.SMCMD_QUIT:
+			quit(nextEditor, command);
+			break;
 		
-		translate(eid, neid, command);
+		case REP.SMCMD_UNDO_REPLACE:
+			for(TranslaterImp1 trans : editorList){
+				trans.setUndoCommand(command);
+			}
+			break;
+			
+		default:
+			translate(eid, neid, command);
+			//REPCommand sendCommand = editor.receive(command);
+			//nextEditor.send(command);
+		}
+		
+	}
+
 
+	private void quit(EditorObject nextEditor, REPCommand command) {
+		if(countQuit > editorList2.size()*2){
+			REPCommand quitCommand = new REPCommand(command);
+			quitCommand.eid = -2;
+			for(EditorObject editor : editorList2){
+				editor.send(quitCommand);
+			}
+		}else{
+			nextEditor.send(command);
+		}
+		countQuit++;
+	}
+
+	private void checkQuit(ChannelSimulator<P> channel, REPCommand command) {
+		
 	}
 
 	private void translate(int eid, int neid, REPCommand command) {
 		// TODO Auto-generated method stub
-		ChannelSimulator<P> channel = csList.get(eid);
-		ChannelSimulator<P> nextChannel = csList.get(neid);
+//		ChannelSimulator<P> channel = csList.get(eid);
+//		ChannelSimulator<P> nextChannel = csList.get(neid);
+		
+		ChannelSimulator<P> channel = getChannel(eid);
+		ChannelSimulator<P> nextChannel = getChannel(neid);
+		
+		EditorObject editor = getEditor(eid);
+		
 		if(command.eid == eid){
 			if(checkOwnCommand(command)){
 				//エディタからの編集コマンドが他の全てのエディタを通って戻ってきた場合
@@ -100,15 +141,14 @@
 					//QUITコマンドの場合
 					REPCommand tmp = new REPCommand(command);
 					tmp.eid = -2;
-					quitCommand = tmp;
-					//channel.write(pack(tmp));
+					//quitCommand = tmp;
+					channel.write(pack(tmp));
 				}else{
 					//エディタからの編集コマンドが戻ってきた場合、マージしてエディタへ反映
 					REPCommand[] cmds = editorList.get(eid).catchOwnCommand(command);
 					for(REPCommand cmd : cmds){
 						REPCommand tmp2 = new REPCommand(cmd);
 						tmp2.eid = -2;
-						mergedList.get(eid).add(cmd);		//マージして送信したコマンドを覚えておくためのリスト
 						channel.write(pack(tmp2));
 					}
 				}
@@ -120,20 +160,15 @@
 					nextChannel.write(pack(command));
 				}else{
 					//エディタからの新たな編集コマンド
-					if(mergedList.get(eid).size() > 0){
-						//マージの際にユーザから割り込みがあった場合
-					}
 					sentList.get(eid).add(command);
 					editorList.get(eid).transSendCmd(command);
 					nextChannel.write(pack(command));
 				}
 			}
 		}else if(command.eid == -2){
-			//マージを行っている間にエディタからの割り込みがなかったか確認
-			mergedList.get(eid).remove(0);
-			if(mergedList.get(eid).size() == 0){
-				channel.write(pack(quitCommand));
-			}
+//			if(mergedList.get(eid).size() == 0){
+//				if(quitCommand != null) channel.write(pack(quitCommand));
+//			}
 		}else{
 			if(command.cmd == REP.SMCMD_QUIT){
 				//QUITコマンドはマージャへ追加せずそのまま次のエディタへ送信する
@@ -148,6 +183,43 @@
 		}
 	}
 
+	private EditorObject getEditor(int eid) {
+		// TODO Auto-generated method stub
+		for(EditorObject editor : editorList2){
+			if(editor.getEID() == eid) {
+				return editor;
+			}
+		}
+		return null;
+	}
+	
+	private EditorObject getEditor(ChannelSimulator<P> channel){
+		int eid = getEID(channel);
+		EditorObject editor = getEditor(eid);
+		return editor;
+	}
+
+	private int getEID(ChannelSimulator<P> channel) {
+		int eid = 0;
+		for(EditorObject editor : editorList2){
+			if(editor.getChannel() == channel){
+				eid = editor.getEID();
+			}
+		}
+		return eid;
+	}
+
+	private ChannelSimulator<P> getChannel(int eid) {
+		// TODO Auto-generated method stub
+		ChannelSimulator<P> channel = null;
+		for(EditorObject editor : editorList2){
+			if(editor.getEID() == eid){
+				channel = editor.getChannel();
+			}
+		}
+		return channel;
+	}
+
 	private boolean checkOwnCommand(REPCommand command) {
 		// TODO Auto-generated method stub
 		boolean ownCommand = false;
--- a/src/pathfinder/mergetest/UsersSimulator.java	Tue Aug 05 15:09:39 2008 +0900
+++ b/src/pathfinder/mergetest/UsersSimulator.java	Fri Aug 22 15:48:52 2008 +0900
@@ -31,13 +31,19 @@
 			nextChannelIndex++;
 			if (nextChannelIndex>=channelList.size()) nextChannelIndex=0;
 		}
+		
+		// Quitコマンドを送るエディタはeid=0のエディタのみ
+//		ChannelSimulator<REPCommand> channel = channelList.get(0);
+//		REPCommand qCommand = createQuitCmd();
+//		channel.write(qCommand);
 
 		/* send quitPacket to all editors.  */
 		synchronized (ns){
-		for ( ChannelSimulator<REPCommand> cs : channelList){
+		//	for ( ChannelSimulator<REPCommand> cs : channelList){
+			ChannelSimulator<REPCommand> cs = channelList.get(0);
 			REPCommand cmd0 = createQuitCmd();
 			cs.write(cmd0);
-		}
+		//	}
 		}
 
 		ns.writeLog("UsersSimulator finish.", 1);
@@ -45,7 +51,8 @@
 
 	private REPCommand createCmd(){
 		String str = new String("this text was created by Control thread.");
-		REPCommand cmd = new REPCommand(REP.REPCMD_INSERT, 0, -1, 0, 10, str.length(), str);
+		//REPCommand cmd = new REPCommand(REP.REPCMD_INSERT, 0, -1, 0, 10, str.length(), str);
+		REPCommand cmd = new REPCommand(REP.REPCMD_REPLACE, 0, -1, 0, 10, str.length(), str);
 		return cmd;
 	}
 	private REPCommand createQuitCmd(){
--- a/src/remoteeditor/editors/RemoteEditor.java	Tue Aug 05 15:09:39 2008 +0900
+++ b/src/remoteeditor/editors/RemoteEditor.java	Fri Aug 22 15:48:52 2008 +0900
@@ -289,7 +289,7 @@
 		final int offset = document.getLineOffset(lineNo-1);
 		final String changedText = text;
 
-			final int replaceLength = document.getLineLength(lineNo-1);
+		final int replaceLength = document.getLineLength(lineNo-1);
 		
 		viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
 
--- a/src/remoteeditor/network/REP.java	Tue Aug 05 15:09:39 2008 +0900
+++ b/src/remoteeditor/network/REP.java	Fri Aug 22 15:48:52 2008 +0900
@@ -37,11 +37,12 @@
 	public static final int SMCMD_GET_UNDO = 71;
 	public static final int SMCMD_GET_UNDO_ACK = 72;
 	
-	public static final int SMCMD_START_MERGE = 73;
-	public static final int SMCMD_START_MERGE_ACK = 74;
+	public static final int SMCMD_UNDO_REPLACE = 73;
+	public static final int SMCMD_START_MERGE = 75;
+	public static final int SMCMD_START_MERGE_ACK = 76;
+	public static final int SMCMD_END_MERGE = 77;
 	
-//    public static final int REPCMD_INSERT_UNDO	= 106;
-//    public static final int REPCMD_DELETE_UNDO	= 109;
-//    public static final int REPCMD_REPLACE_UNDO	= 113;
+	//public static final int SMCMD_START_MERGE = 73;
+	//public static final int SMCMD_START_MERGE_ACK = 74;
 
 }
--- a/src/sample/merge/TranslaterImp1.java	Tue Aug 05 15:09:39 2008 +0900
+++ b/src/sample/merge/TranslaterImp1.java	Fri Aug 22 15:48:52 2008 +0900
@@ -15,11 +15,13 @@
 	private Stack<REPCommand> unMergedCmds;
 	public int eid;
 	private int seq;
+	private LinkedList<REPCommand> undoReplaceList;
 
 	public TranslaterImp1(int _eid){
 		eid = _eid;
 		sentCmds = new LinkedList<REPCommand>();
 		unMergedCmds = new Stack<REPCommand>();
+		undoReplaceList = new LinkedList<REPCommand>();
 	}
 
 	/**
@@ -77,9 +79,24 @@
 
 		if (cmd.cmd==REP.REPCMD_INSERT) retCmd.cmd=REP.REPCMD_DELETE;
 		else if (cmd.cmd==REP.REPCMD_DELETE) retCmd.cmd=REP.REPCMD_INSERT;
+		else if (cmd.cmd == REP.REPCMD_REPLACE) retCmd = createUndoReplace(retCmd);
 		return retCmd;
 	}
 
+	private REPCommand createUndoReplace(REPCommand cmd) {
+		for(REPCommand command : undoReplaceList){
+			if(command.eid == cmd.eid && command.seq == cmd.seq){
+				REPCommand tmp = new REPCommand(command);
+				tmp.setCMD(REP.REPCMD_REPLACE);
+				return tmp;
+			}
+		}
+		System.out.println(undoReplaceList);
+		System.out.println(cmd);
+		System.out.println();
+		return null;	
+	}
+
 	private ArrayList<REPCommand> sortCmds(ArrayList<REPCommand> cmds) {
 		ArrayList<REPCommand> sortedCmds = new ArrayList<REPCommand>();
 		int top;
@@ -191,4 +208,9 @@
 		// TODO Auto-generated method stub
 		return sentCmds;
 	}
+
+	public void setUndoCommand(REPCommand command) {
+		// TODO Auto-generated method stub
+		undoReplaceList.add(command);
+	}
 }