changeset 224:6b0dd92b8e45

add optimizer to Editor
author kent
date Sun, 31 Aug 2008 13:28:34 +0900
parents 3680d8357429
children e173411a2499
files rep/Editor.java rep/REPCommandPacker.java rep/Session.java rep/SessionManager.java
diffstat 4 files changed, 51 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/rep/Editor.java	Sun Aug 31 13:06:36 2008 +0900
+++ b/rep/Editor.java	Sun Aug 31 13:28:34 2008 +0900
@@ -5,6 +5,7 @@
 import java.util.List;
 
 import rep.channel.REPSocketChannel;
+import rep.optimizers.*;
 import rep.translater.TranslaterImp1;
 
 public class Editor {
@@ -15,11 +16,19 @@
 	private TranslaterImp1 translater;
 	private List<REPCommand> sentList;
 	private List<REPCommand> sentMergedList;
+	private REPCommandOptimizer optimizer;
 	
 	public Editor(){
+		this(true);
+	}
+	public Editor(boolean doOptimize){
 		setHostAndPort(myChannel);
 		translater = new TranslaterImp1(eid);
 		sentList = new LinkedList<REPCommand>();
+
+		if (doOptimize) optimizer = new DeleteInsertOptimizer(); //タカノがつくったおぷてぃまいざ
+		else            optimizer = new NullOptimizer();         //なにもしないけどOptimizer.
+		
 	}
 
 	public Editor(int editorNo, REPSocketChannel<REPCommand> channel){
@@ -37,8 +46,8 @@
 		sentList = new LinkedList<REPCommand>();
 	}
 	
-	public LinkedList<REPCommand> translate(REPCommand command){
-		LinkedList<REPCommand> list = new LinkedList<REPCommand>();
+	public List<REPCommand> translate(REPCommand command){
+		List<REPCommand> list = new LinkedList<REPCommand>();
 		if(command.eid == eid){
 			if(checkReturnedCommand(command)){
 				//エディタからのコマンドが元のエディタに戻ってきた
@@ -59,8 +68,9 @@
 			//マージコマンドが返ってきた
 			if(translater.checkMergeConflict(command)){
 				//マージ中にエディタからの割り込みがあった場合
-				LinkedList<REPCommand> mergeAgainList = translater.getMergeAgain();
-				//optimizer
+				List<REPCommand> mergeAgainList = translater.getMergeAgain();
+
+				mergeAgainList = optimizer.optimize(mergeAgainList);
 				for(REPCommand againCommand: mergeAgainList){
 					myChannel.write(againCommand);
 				}
--- a/rep/REPCommandPacker.java	Sun Aug 31 13:06:36 2008 +0900
+++ b/rep/REPCommandPacker.java	Sun Aug 31 13:28:34 2008 +0900
@@ -11,6 +11,21 @@
 
 import rep.channel.REPPack;
 
+/*
+//+-------+--------+--------+-------+--------+---------+------+
+//| cmd   | session| editor | seqid | lineno | textsiz | text |
+//|       | id     | id     |       |        |         |      |
+//+-------+--------+--------+-------+--------+---------+------+
+//o-------header section (network order)-------------o
+int cmd;	// command
+int sid;	// session ID : uniqu to editing file
+int eid;	// editor ID : owner editor ID = 1。Session に対して unique
+int seqno;	// Sequence number : sequence number はエディタごとに管理
+int lineno;	// line number
+int textsize;   // textsize : bytesize
+byte[] text;
+*/
+
 
 public class REPCommandPacker implements REPPack<REPCommand> {
 	// JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
@@ -28,11 +43,14 @@
     	}
     	ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHAR_ORDER));
     	buffer.clear();  // position = 0 
-    	buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
-    	buffer.putInt(command.seq); buffer.putInt(command.lineno);     	
+    	buffer.putInt(command.cmd);
+    	buffer.putInt(command.sid);
+    	buffer.putInt(command.eid);
+    	buffer.putInt(command.seq);
+    	buffer.putInt(command.lineno);     	
     	
     	int pos = buffer.position();
-    	buffer.putInt(0);     	
+    	buffer.putInt(0);
     	
     	//Encode to UTF8
     	CharBuffer cb = CharBuffer.wrap(command.string);
@@ -63,13 +81,18 @@
 		ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
 		long len = 0;
 		header.clear();
+		/* 
 		len = sc.read(header);
 		if(len <= 0){
 			return null;
 		}
 		if (len !=HEADER_SIZE) {
 			throw new IOException();
-		}
+		}  下のwhileループで OK ?  */
+		while(header.remaining()>0){
+			sc.read(header);
+		} // 壊れたパケットに対してはブロックする可能性あり まぁTCPだしいいか?
+		
 		header.rewind();  // position = 0
 
 		int cmd = header.getInt();
@@ -80,13 +103,17 @@
 		int textsiz = header.getInt();
 		
 		ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
-		
+
+		/*
 		len = sc.read(textBuffer);
 		if(len <= 0){
 			return null;
 		}
 		if (len != textsiz) {
 			throw new IOException();
+		}*/
+		while(textBuffer.remaining()>0){
+			sc.read(textBuffer);
 		}
 		textBuffer.rewind();
 
@@ -110,5 +137,4 @@
 		return repcommand;		
 	}
 
-
 }
--- a/rep/Session.java	Sun Aug 31 13:06:36 2008 +0900
+++ b/rep/Session.java	Sun Aug 31 13:28:34 2008 +0900
@@ -1,6 +1,8 @@
 package rep;
 
 import java.util.LinkedList;
+import java.util.List;
+
 import rep.channel.REPSocketChannel;
 
 public class Session {
@@ -76,7 +78,7 @@
 	}
 	public void translate(REPSocketChannel<REPCommand> channel, REPCommand command) {
 		Editor editor = getEditor(channel);
-		LinkedList<REPCommand> commandList = editor.translate(command);
+		List<REPCommand> commandList = editor.translate(command);
 		Editor nextEditor = getNextEditor(editor);
 		
 		for(REPCommand cmd: commandList){
--- a/rep/SessionManager.java	Sun Aug 31 13:06:36 2008 +0900
+++ b/rep/SessionManager.java	Sun Aug 31 13:28:34 2008 +0900
@@ -409,7 +409,7 @@
 		return null;
 	}
 	
-	public Editor getEditor(REPSocketChannel channel){
+	public Editor getEditor(REPSocketChannel<REPCommand> channel){
 		for(Editor editor : editorList){
 			if(editor.getChannel() == channel){
 				return editor;
@@ -425,7 +425,7 @@
 		return null;
 	}
 
-	private boolean setMaxHost(REPSocketChannel channel, String maxHost2) {
+	private boolean setMaxHost(REPSocketChannel<REPCommand> channel, String maxHost2) {
 		if(maxHost.compareTo(maxHost2) > 0){
 			return false;
 		}else{