changeset 317:c83a3faec487

*** empty log message ***
author kono
date Tue, 07 Oct 2008 21:48:31 +0900
parents 77f443f6dc9f
children dc57e24ea3df
files rep/Editor.java rep/EditorList.java rep/Forwarder.java rep/Session.java rep/SessionList.java rep/SessionManager.java rep/handler/REPHandlerEditorInMerge.java rep/translater/TranslaterImp1.java rep/xml/SessionXMLDecoder.java test/XMLTest.java
diffstat 10 files changed, 79 insertions(+), 143 deletions(-) [+]
line wrap: on
line diff
--- a/rep/Editor.java	Tue Oct 07 18:15:33 2008 +0900
+++ b/rep/Editor.java	Tue Oct 07 21:48:31 2008 +0900
@@ -4,6 +4,7 @@
 import java.util.List;
 
 import rep.channel.REPSocketChannel;
+import rep.handler.PacketSet;
 import rep.optimizers.*;
 import rep.translater.TranslaterImp1;
 
@@ -18,16 +19,12 @@
 	private List<REPCommand> sentList = new LinkedList<REPCommand>();
 	// REPCommands we are going to send to the next editor
 	private REPCommandOptimizer optimizer;
-	private REPCommand quit2 = null;
-
+	//private REPCommand quit2 = null;
+	private SessionManager manager;
 
-	
-	public Editor(){
-		this(true,-1);
-	}
-	
-	public Editor(boolean doOptimize,int editorNo){
-		setHostAndPort(myChannel);
+	public Editor(SessionManager manager,boolean doOptimize,int editorNo){
+		super(manager);
+		this.manager = manager;
 		eid = editorNo;
 		translater = new TranslaterImp1(eid);
 		if (doOptimize) optimizer = new DeleteInsertOptimizer(); //タカノがつくったおぷてぃまいざ
@@ -35,23 +32,13 @@
 		
 	}
 
-	public Editor(int editorNo, REPSocketChannel<REPCommand> channel){
-		this(false,editorNo);
-		this.myChannel = channel;
+	public Editor(SessionManager manager,int editorNo, REPSocketChannel<REPCommand> channel){
+		this(manager,false,editorNo);
+		myChannel = channel;
 		setHostAndPort(myChannel);
 	}
-	
-	public Editor(REPSocketChannel<REPCommand> channel) {
-		this(false,-1);
-		this.myChannel = channel;
-		setHostAndPort(myChannel);
-	}
-	
-	enum TranslatorResult {
-		START_MERGE, NEW_COMMAND, MERGE_RETURN, MERGE_AGAIN, INCOMMING_COMMAND, MERGE_END 
-	}
-	
-	public TranslatorResult translate(Forwarder nextEditor, REPCommand command){
+
+	public void translate(Forwarder nextEditor, REPCommand command){
 		if(command.eid == nextEditor.getEID()){
 			if(checkReturnedCommand(command)){
 				// エディタからのコマンドが元のエディタに戻ってきた
@@ -61,7 +48,7 @@
 				// Session Manager 側で、このeditorへの他のeditorからの
 				// 入力を止めて、merge にそなえる。merge は、eidtor 側から
 				// ACKが来てから始まる。
-				return TranslatorResult.START_MERGE;
+				return;
 			} else assert(false);
 		} else if(command.eid == eid){
 				//エディタからの新たな編集コマンド
@@ -69,28 +56,32 @@
 				assert(sentList.size()<limit);
 				translater.transSendCmd(command);
 				nextEditor.send(command);
-				return TranslatorResult.NEW_COMMAND;
+				return;
 		}else if(command.eid == REP.MERGE_EID.id){
 			//マージコマンドが返ってきた
 			if(translater.checkMergeConflict(command)){
 				//マージ中にエディタからの割り込みがあった場合
 				if (optimizedSend(translater.getMergeAgain())) {
-					return TranslatorResult.MERGE_AGAIN;
+					return;
 				}
 			}
-			return isMerging()?TranslatorResult.MERGE_RETURN:
-				TranslatorResult.MERGE_END;
+			if(!isMerging()) {
+				endMerge();
+			}
 		}else{
 			//他のエディタからの編集コマンド
-			translater.transReceiveCmd(nextEditor,command);
+			if(!isMerging()) {
+				translater.transReceiveCmd(nextEditor,command);
+				return;
+			}
+			manager.addWaitingCommand(new PacketSet(getChannel(), this, command));
 		}
-		return TranslatorResult.INCOMMING_COMMAND;
+		return;
 	}
 	
 	boolean merge(Editor editor,REPCommand command) {
 		REPCommand prev = translater.prev();
 		if(prev==null) return false;
-		assert(writeQueue.size()==0);
 		assert(prev.eid==command.eid);
 		//マージして送信
 		return translater.catchOwnCommand(editor);
@@ -113,7 +104,7 @@
 
 	
 	public void setQuit2(REPCommand cmd) {
-		quit2 = cmd;
+		//quit2 = cmd;
 	}
 	
 	private void setHostAndPort(REPSocketChannel<REPCommand> myChannel2) {
@@ -163,17 +154,6 @@
 	public int seq() {
 		return seq++;
 	}
-
-	@Override
-	public boolean doWaitingWrite() {
-		if(super.doWaitingWrite()) return true;
-		if (quit2!=null && sentList.size()==0) {
-			myChannel.write(quit2);
-			quit2 = null;
-			return true;
-		}
-		return false;
-	}
 	
 	/**
 	 * Sent optimized merged command list
@@ -187,10 +167,14 @@
 			REPCommand m = new REPCommand(c);
 			m.setEID(REP.MERGE_EID.id);
 			m.setSEQID(seq());
-			writeQueue.add(m);
+			send(m);
 		}				
-		assert(writeQueue.size()<limit);
 		return true;
 	}
 
+	void endMerge() {
+		REPCommand mergeEnd = new REPCommand(REP.SMCMD_END_MERGE,eid,sid,seq(),0,"");
+		send(mergeEnd);
+	}
+
 }
--- a/rep/EditorList.java	Tue Oct 07 18:15:33 2008 +0900
+++ b/rep/EditorList.java	Tue Oct 07 21:48:31 2008 +0900
@@ -30,14 +30,14 @@
 		channel.write(command);
 	}
 
-	public int addEditor(REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
+	public int addEditor(SessionManager manager,REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
 		numberOfEditor++;
-		editorList.add(new Editor(numberOfEditor, channel));
+		editorList.add(new Editor(manager,numberOfEditor, channel));
 		return numberOfEditor;
 	}
 
-	public void addEditor(REPSocketChannel<REPCommand> channel) {
-		editorList.add(new Editor(0, channel));
+	public void addEditor(SessionManager manager,REPSocketChannel<REPCommand> channel) {
+		editorList.add(new Editor(manager,0, channel));
 	}
 
 	public void setEID(REPCommand repCmd) {
--- a/rep/Forwarder.java	Tue Oct 07 18:15:33 2008 +0900
+++ b/rep/Forwarder.java	Tue Oct 07 21:48:31 2008 +0900
@@ -1,18 +1,21 @@
 package rep;
 
-import java.util.LinkedList;
-
 import rep.channel.REPLogger;
 import rep.channel.REPSocketChannel;
+import rep.handler.PacketSet;
 
 public class Forwarder {
 	int eid;            // unique id in a session
 	REPSocketChannel<REPCommand> myChannel;
 	// REPCommands we sent to the next editor
-	LinkedList<REPCommand> writeQueue = new LinkedList<REPCommand>();
 	final int limit=100;
 	REPLogger ns = REPLogger.singleton();
+	SessionManager manager;
 	
+	public Forwarder(SessionManager manager) {
+		this.manager = manager;
+	}
+
 	public int getEID() {
 		return eid;
 	}
@@ -22,8 +25,7 @@
 	}
 	
 	public void send(REPCommand command) {
-		writeQueue.add(command);
-		assert(writeQueue.size()<limit);
+		manager.addWriteQueue(new PacketSet(myChannel, null, command));
 	}
 	
 	public REPSocketChannel<REPCommand> getChannel() {
@@ -34,19 +36,4 @@
 		myChannel = channel;
 	}
 
-	public boolean doWaitingWrite() {
-		//  一気に送ると、向こう側(Editor)で、dead lock する可能性がある。
-		//  select loop の中で一つ一つ送るしかない。Editor側から割り込まれる可能性も
-		//  ある。その時に複数のコマンドを送っていると、どこに割り込まれたかを判断する
-		// ことが出来ない。そこで、一つ一つReturnを確認する必要がある。つまり、
-		// select loop で送るしかない。
-		REPCommand cmd;
-		if (writeQueue.size()>0) {
-			cmd = new REPCommand(writeQueue.remove(0));
-			ns.writeLog("SessionManager write to "+myChannel+" cmd="+cmd);
-			myChannel.write(cmd);
-			return true;
-		} 
-		return false;
-	}
 }
\ No newline at end of file
--- a/rep/Session.java	Tue Oct 07 18:15:33 2008 +0900
+++ b/rep/Session.java	Tue Oct 07 21:48:31 2008 +0900
@@ -27,8 +27,8 @@
 		masterEditor = editor;
 		editorList.add(editor);
 	}
-	public void addEditor(int editorID, REPSocketChannel<REPCommand> channel) {
-		editorList.add(new Editor(editorID, channel));
+	public void addEditor(SessionManager manager,int editorID, REPSocketChannel<REPCommand> channel) {
+		editorList.add(new Editor(manager,editorID, channel));
 	}
 	public LinkedList<Editor> getEditorList() {
 		if(editorList == null) System.out.println("null!");  
--- a/rep/SessionList.java	Tue Oct 07 18:15:33 2008 +0900
+++ b/rep/SessionList.java	Tue Oct 07 21:48:31 2008 +0900
@@ -48,11 +48,11 @@
 		
 	}
 
-	public void addEditor(REPSocketChannel<REPCommand> channel, int sid, REPCommand repCmd) {
+	public void addEditor(SessionManager manager,REPSocketChannel<REPCommand> channel, int sid, REPCommand repCmd) {
 		int editorID = repCmd.eid;
 //		if(session3.get(sid) == null) System.out.println("ぬるぽ!");
 //		session3.get(sid).addEditor(editorID, channel);
-		sessionLinkedList.get(sid-1).addEditor(editorID, channel);		//本当はforループで検索しないといけないよ。
+		sessionLinkedList.get(sid-1).addEditor(manager,editorID, channel);		//本当はforループで検索しないといけないよ。
 	}
 
 //	public int getSessionID(SocketChannel channel) {
@@ -113,9 +113,9 @@
 	}
 
 
-	public void addEditor(REPSocketChannel<REPCommand> editorChannel, int sid, int eid) {
+	public void addEditor(SessionManager manager,REPSocketChannel<REPCommand> editorChannel, int sid, int eid) {
 //		session3.get(sid).addEditor(eid, editorChannel);
-		sessionLinkedList.get(sid-1).addEditor(eid, editorChannel);
+		sessionLinkedList.get(sid-1).addEditor(manager,eid, editorChannel);
 	}
 
 	public void sendSelect(int sid) {
--- a/rep/SessionManager.java	Tue Oct 07 18:15:33 2008 +0900
+++ b/rep/SessionManager.java	Tue Oct 07 21:48:31 2008 +0900
@@ -18,7 +18,6 @@
 import rep.handler.PacketSet;
 import rep.handler.REPHandler;
 import rep.handler.REPEditorHandler;
-import rep.handler.REPHandlerEditorInMerge;
 import rep.handler.REPSessionManagerHandler;
 import rep.channel.REPSelector;
 import rep.xml.SessionXMLDecoder;
@@ -54,9 +53,9 @@
 	private String maxHost;
 	private List<PacketSet> waitingCommandInMerge;
 	REPHandler normalHandler = new REPEditorHandler(this);
-	REPHandler handlerInMerge =new REPHandlerEditorInMerge(this); 
 	private BlockingQueue<SessionManagerEvent> waitingEventQueue = new LinkedBlockingQueue<SessionManagerEvent>();;
 	private String myHost;
+	private LinkedList<PacketSet> writeQueue = new LinkedList<PacketSet>();
 	private static int receive_port;
 	private static int parent_port;
 	static final int DEFAULT_PORT = 8766;
@@ -138,9 +137,11 @@
 	}
 
 	private boolean checkWaitingWrite() throws IOException {
-		for(Session s:sessionList) {
-			for(Forwarder editor: s.getEditorList()) 
-				if (editor.doWaitingWrite()) return true;
+		PacketSet p = writeQueue.poll();
+		if (p!=null) {
+			REPCommand cmd = new REPCommand(p.command);
+			p.channel.write(cmd);
+			return true;
 		}
 		return false;
 	}
@@ -206,6 +207,8 @@
 
 		switch(receivedCommand.cmd){
 
+		// Editor Command
+		
 		case REPCMD_DELETE:
 		case REPCMD_INSERT:
 		case REPCMD_NOP:
@@ -215,19 +218,17 @@
 			if (session==null) throw new IOException();
 			// 次のエディタへコマンドを送信する処理
 			Editor editor = session.getEditor(channel);
-			if (editor.isMerging()) {
-				addWaitingCommand(new PacketSet(channel, editor, receivedCommand));
-				break;
-			}
 			editor.translate(session.getNextEditor(editor), receivedCommand);
 			break;
 		}
+		
 		case SMCMD_JOIN:
 		{
 			//どのSessionにも属さないエディタをリストに追加
-			//エディタとchannelは1対1
+			//エディタとchannelは1対1 (ではない)
 			//エディタが新しくputする場合は新しくソケットを作る
-			Editor editor = new Editor(editorList.size(), channel);
+			// ここのeditorList はsessionのとは別物
+			Editor editor = new Editor(this,editorList.size(),channel);
 			editor.setHost(myHost);
 			editorList.add(editor);
 
@@ -244,12 +245,12 @@
 		case SMCMD_PUT:
 		{
 			//エディタのリストに追加
-			Editor editor = new Editor(editorList.size(), channel);
+			Editor editor = new Editor(this,editorList.size(), channel);
 			//editorList.add(editor);
 
 			//Sessionを生成
 			int sid = sessionList.size();
-			editor = new Editor(0, channel);
+			editor = new Editor(this,0, channel);
 			editor.setHost(myHost);
 			Session session = new Session(sid, receivedCommand.string, editor);
 			session.hasOwner(true);
@@ -277,10 +278,12 @@
 
 		break;
 
+		// SELECT is no longer used in a editor. Select
+		// operation is handled in Session Manager Only
 		case SMCMD_SELECT:
 		{
 			//他のSessionManagerをエディタとしてSessionに追加
-			Editor editor = new Editor(channel);
+			Editor editor = new Editor(this,0,channel);
 			Session session = getSession(receivedCommand.sid);
 			session.addEditor(editor);
 
@@ -320,6 +323,8 @@
 
 		break;
 
+		// Session Manager Command
+		
 		case SMCMD_SM_JOIN:
 
 		{
@@ -407,7 +412,7 @@
 		case SMCMD_UPDATE_ACK:
 		{
 			if(receivedCommand.sid > sessionList.size()){
-				Editor editor = new Editor(channel);
+				Editor editor = new Editor(this,0,channel);
 				editor.setName(receivedCommand.string);
 
 				Session session = new Session(editor);
@@ -447,7 +452,7 @@
 			Editor editor = session.getEditor(channel);
 			if (!editor.merge(editor,receivedCommand)) {
 				// nothing to do, send END_MERGE
-				endMerge(receivedCommand, session, editor);
+				editor.endMerge();
 			}
 			break;
 		}
@@ -473,12 +478,6 @@
 		}
 	}
 
-	private void endMerge(REPCommand receivedCommand, Session session,
-			Editor editor) {
-		REPCommand mergeEnd = new REPCommand(REP.SMCMD_END_MERGE,receivedCommand.sid,editor.getEID(),editor.seq(),0,"");
-		editor.send(mergeEnd);
-	}
-
 	private void updateGUI() {
 		//リストのコピーをGUIに渡す
 		LinkedList<Session> sList = new LinkedList<Session>(sessionList);
@@ -598,7 +597,7 @@
 			channel.write(sendCommand);
 		}else {
 			sid = event.getSID();
-			editor = new Editor(channel);
+			editor = new Editor(this,0,channel);
 			editor.setHost(myHost);
 			session = getSession(sid);
 			session.addEditor(editor);
@@ -648,4 +647,9 @@
 		// can be other session manager? what should I do?
 	}
 
+
+	public void addWriteQueue(PacketSet packetSet) {
+		writeQueue.add(packetSet);
+	}
+
 }
--- a/rep/handler/REPHandlerEditorInMerge.java	Tue Oct 07 18:15:33 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,36 +0,0 @@
-package rep.handler;
-
-import java.io.IOException;
-import rep.Editor;
-import rep.REPCommand;
-import rep.Session;
-import rep.SessionManager;
-import rep.channel.REPSelectionKey;
-import rep.channel.REPSocketChannel;
-
-public class REPHandlerEditorInMerge implements REPHandler {
-
-	private SessionManager manager;
-
-	public REPHandlerEditorInMerge(SessionManager manager) {
-		this.manager = manager;
-	}
-
-
-	public void handle(REPSelectionKey<REPCommand> key) throws IOException {
-		//マージ中のエディタの前のエディタのコマンドをWaitingListに追加する
-		REPSocketChannel<REPCommand> channel = key.channel1();
-		REPCommand command = channel.read();
-		System.out.println("REPHandlerImpl.handle() : command = " + command);
-		// if (manager.isMerging(command.sid()))...
-		//    同じchannelで、merge中のsessionは一つは限らない。
-		//    なので、sid をinstanceで持つのではだめ。
-		Session s = manager.getSession(command.sid);
-		Editor editor = s.getEditor(channel);
-		manager.addWaitingCommand(new PacketSet(channel, editor, command));
-	}
-
-	public void cancel(REPSocketChannel<REPCommand> socketChannel) {
-		manager.remove(socketChannel);
-	}
-}
--- a/rep/translater/TranslaterImp1.java	Tue Oct 07 18:15:33 2008 +0900
+++ b/rep/translater/TranslaterImp1.java	Tue Oct 07 21:48:31 2008 +0900
@@ -57,7 +57,6 @@
 	public boolean catchOwnCommand(Editor editor){
 		LinkedList<REPCommand> output = new LinkedList<REPCommand>();
 		LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
-		prev();
 				//スタック上にあるコマンドを全部undoコマンドにする
 		while ( !unMergedCmds.isEmpty() ){
 			REPCommand cmd0 = unMergedCmds.pop();
@@ -169,7 +168,7 @@
 	}
 
 	public boolean checkMergeConflict(REPCommand command) {
-		sentMergedList.remove();
+		// sentMergedList.remove();
 		
 		if(mergeAgainList.size() > 0){
 			mergeAgainList.add(command);
--- a/rep/xml/SessionXMLDecoder.java	Tue Oct 07 18:15:33 2008 +0900
+++ b/rep/xml/SessionXMLDecoder.java	Tue Oct 07 21:48:31 2008 +0900
@@ -7,7 +7,6 @@
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.InputSource;
 
@@ -78,18 +77,17 @@
 					Element elementFile = (Element) nodelistEditorFile.item(0); 
 					String file = elementFile.getFirstChild().getNodeValue();
 					
-					Editor editor = new Editor();
+					Editor editor = new Editor(null, false, 0);
 					editor.setHost(host);/* editor.setPort(port)*/; editor.setName(file); editor.setEID(Integer.parseInt(eid)); 
 					session = new Session(editor);
 					session.addEditor(editor);
 					sessionlist.addSession(session);
 					
 				}else {
-					Editor editor = new Editor();
+					Editor editor = new Editor(null, false, 0);
 					editor.setHost(host);/* editor.setPort(port)*/; editor.setName(null); editor.setEID(Integer.parseInt(eid));
 					if(session != null){
 						session.addEditor(editor);
-						//sessionlist.addSession(session);
 					}
 				}
 			}
--- a/test/XMLTest.java	Tue Oct 07 18:15:33 2008 +0900
+++ b/test/XMLTest.java	Tue Oct 07 21:48:31 2008 +0900
@@ -10,13 +10,13 @@
 	
 	public static void main(String[] args){
 		
-		Editor editor = new Editor(0, null);
+		Editor editor = new Editor(null,0, null);
 		editor.setEID(1);
 		editor.setHost("firefly.cr.ie.u-ryukyu.ac.jp");
 		//editor.setPort("56789");
 		editor.setName("Test.java");
 		
-		Editor editor2 = new Editor(1, null);
+		Editor editor2 = new Editor(null,1, null);
 		editor2.setEID(2);
 		editor2.setHost("teto.cr.ie.u-ryukyu.ac.jp");
 		//editor2.setPort("45678");