# HG changeset patch # User kent # Date 1220156914 -32400 # Node ID 6b0dd92b8e452f1b1d9df1992181b3544eb793f7 # Parent 3680d83574290fa5d32e7de186648eb89db7d985 add optimizer to Editor diff -r 3680d8357429 -r 6b0dd92b8e45 rep/Editor.java --- 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 sentList; private List sentMergedList; + private REPCommandOptimizer optimizer; public Editor(){ + this(true); + } + public Editor(boolean doOptimize){ setHostAndPort(myChannel); translater = new TranslaterImp1(eid); sentList = new LinkedList(); + + if (doOptimize) optimizer = new DeleteInsertOptimizer(); //タカノがつくったおぷてぃまいざ + else optimizer = new NullOptimizer(); //なにもしないけどOptimizer. + } public Editor(int editorNo, REPSocketChannel channel){ @@ -37,8 +46,8 @@ sentList = new LinkedList(); } - public LinkedList translate(REPCommand command){ - LinkedList list = new LinkedList(); + public List translate(REPCommand command){ + List list = new LinkedList(); if(command.eid == eid){ if(checkReturnedCommand(command)){ //エディタからのコマンドが元のエディタに戻ってきた @@ -59,8 +68,9 @@ //マージコマンドが返ってきた if(translater.checkMergeConflict(command)){ //マージ中にエディタからの割り込みがあった場合 - LinkedList mergeAgainList = translater.getMergeAgain(); - //optimizer + List mergeAgainList = translater.getMergeAgain(); + + mergeAgainList = optimizer.optimize(mergeAgainList); for(REPCommand againCommand: mergeAgainList){ myChannel.write(againCommand); } diff -r 3680d8357429 -r 6b0dd92b8e45 rep/REPCommandPacker.java --- 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 { // 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; } - } diff -r 3680d8357429 -r 6b0dd92b8e45 rep/Session.java --- 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 channel, REPCommand command) { Editor editor = getEditor(channel); - LinkedList commandList = editor.translate(command); + List commandList = editor.translate(command); Editor nextEditor = getNextEditor(editor); for(REPCommand cmd: commandList){ diff -r 3680d8357429 -r 6b0dd92b8e45 rep/SessionManager.java --- 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 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 channel, String maxHost2) { if(maxHost.compareTo(maxHost2) > 0){ return false; }else{