changeset 328:6ceb222570cb

merge is working now.
author kono
date Sat, 11 Oct 2008 22:23:45 +0900
parents 7b6dede07f4a
children a2624f6f7d0d
files rep/Editor.java rep/Forwarder.java rep/SessionManager.java test/sematest/TestEditor.java
diffstat 4 files changed, 36 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/rep/Editor.java	Sat Oct 11 19:31:13 2008 +0900
+++ b/rep/Editor.java	Sat Oct 11 22:23:45 2008 +0900
@@ -33,15 +33,15 @@
 		this.channel = channel;
 	}
 
-	public void translate(Editor nextEditor, REPCommand command){
-		if(command.eid == nextEditor.getEID()){
-			nextEditor.checkReturnedCommand(command);
+	public void translate(REPCommand command){
+		if(command.eid == next.getEID()){
+			((Editor) next).checkReturnedCommand(command);
 		} else if(command.eid == eid){
 			//エディタからの新たな編集コマンド
 			translator.transSendCmd(command);
 			sentList.add(new REPCommand(command));
 			assert(sentList.size()<limit);
-			nextEditor.send(command);
+			next.send(command);
 			return;
 		}else if(command.eid == REP.MERGE_EID.id){
 			//マージコマンドが返ってきた
@@ -53,10 +53,10 @@
 		}else{
 			//他のエディタからの編集コマンド
 			if(!isMerging()) {
-				translator.transReceiveCmd(nextEditor,command);
+				translator.transReceiveCmd(next,command);
 				return;
 			}
-			manager.addWaitingCommand(new PacketSet(getChannel(), this, command));
+			manager.addWaitingCommand(new PacketSet(getChannel(), this, new REPCommand(command)));
 		}
 		return;
 	}
@@ -120,7 +120,6 @@
 		if (sentList.size()==0&&!isMerging()) {
 			send(quit2);
 			quit2 = null;
-			manager.remove(this);
 			return true;
 		}
 		return false;
@@ -136,7 +135,7 @@
 		case REPCMD_INSERT:
 		case REPCMD_NOP:
 		{
-			translate((Editor)next, receivedCommand);
+			translate(receivedCommand);
 			break;
 		}
 
@@ -157,6 +156,8 @@
 		}
 		case SMCMD_QUIT_2:
 		{
+			// QUIT_2 is returned.
+			//manager.remove(this);
 			Forwarder editor1 = getNextForwarder();
 			// don't send quit2 to the editor until all pending
 			// merge is processed.
--- a/rep/Forwarder.java	Sat Oct 11 19:31:13 2008 +0900
+++ b/rep/Forwarder.java	Sat Oct 11 22:23:45 2008 +0900
@@ -23,7 +23,7 @@
 	public void send(REPCommand command) {
 		assert(command!=null);
 		REPCommand c = new REPCommand(command);
-		manager.addWriteQueue(new PacketSet(channel, null, c));
+		manager.addWriteQueue(new PacketSet(channel,null,  c));
 	}
 	
 	public REPSocketChannel<REPCommand> getChannel() {
--- a/rep/SessionManager.java	Sat Oct 11 19:31:13 2008 +0900
+++ b/rep/SessionManager.java	Sat Oct 11 22:23:45 2008 +0900
@@ -115,9 +115,9 @@
 
 	public void mainLoop() throws IOException {
 		while(true){
+		    checkWaitingCommandInMerge();
 			if (checkInputEvent() ||
-			    checkWaitingWrite() || 
-			    checkWaitingCommandInMerge()) {
+			    checkWaitingWrite()) { 
 				   // try to do fair execution for waiting task
 				   if(selector.selectNow() > 0) select();
 				   continue;
@@ -151,15 +151,22 @@
 	 * @return true if there is a processed waiting command
 	 * @throws IOException
 	 */
-	private boolean checkWaitingCommandInMerge() throws IOException {
-		for(Iterator<PacketSet> it = waitingCommandInMerge.iterator(); it.hasNext();){
-			PacketSet p = it.next();
-			if(p.getEditor().isMerging()) { // still merging do nothing
-				continue;
-			}else{
-				// process one command and return true
-				manage(p.channel, p.command);
-				it.remove();
+	private void checkWaitingCommandInMerge() throws IOException {
+		List<PacketSet> w = waitingCommandInMerge;
+		waitingCommandInMerge = new LinkedList<PacketSet>();
+		for(PacketSet p: w) {
+			Editor e = p.getEditor();
+			if(e!=null &&e.isMerging()) { // still merging do nothing
+				waitingCommandInMerge.add(p);
+			} else {
+				manage(p.channel, p.command);		
+			}
+		}
+	}
+	
+	public boolean hasWaitingCommand(REPSocketChannel<REPCommand>c) {
+		for(PacketSet p:waitingCommandInMerge) {
+			if (p.channel==c) {
 				return true;
 			}
 		}
@@ -205,6 +212,12 @@
 		Session s = getSession(receivedCommand.sid);
 		Editor e = s.getEditor(channel);
 		// if(e==null) throw new IOException();
+		if (receivedCommand.eid!=REP.MERGE_EID.id && receivedCommand.eid!=e.eid ) { 
+			if (hasWaitingCommand(channel)) {
+				addWaitingCommand(new PacketSet(channel, e, receivedCommand));
+				return;
+			}
+		}
 		e.manage(receivedCommand);
 	}
 
--- a/test/sematest/TestEditor.java	Sat Oct 11 19:31:13 2008 +0900
+++ b/test/sematest/TestEditor.java	Sat Oct 11 22:23:45 2008 +0900
@@ -246,11 +246,13 @@
 		 case SMCMD_JOIN_ACK	:
 			 sid = cmd.sid;
 			 eid = cmd.eid;
+			 name += "(eid="+eid+",sid="+sid+")";
 			 inputLock = false;
 			 break;
 		 case SMCMD_PUT_ACK	:
 			 sid = cmd.sid;
 			 eid = cmd.eid;
+			 name += "(eid="+eid+",sid="+sid+")";
 			 inputLock = false;
 			 break;
 		 case SMCMD_QUIT		: