# HG changeset patch # User kono # Date 1223383711 -32400 # Node ID c83a3faec4875dfad142f961adfca5d6c8b4d2e5 # Parent 77f443f6dc9f2e07e6edd314080028b52eb2aab0 *** empty log message *** diff -r 77f443f6dc9f -r c83a3faec487 rep/Editor.java --- 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 sentList = new LinkedList(); // 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 channel){ - this(false,editorNo); - this.myChannel = channel; + public Editor(SessionManager manager,int editorNo, REPSocketChannel channel){ + this(manager,false,editorNo); + myChannel = channel; setHostAndPort(myChannel); } - - public Editor(REPSocketChannel 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() 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() channel, REPCommand repCmd) { + public int addEditor(SessionManager manager,REPSocketChannel channel, REPCommand repCmd) { numberOfEditor++; - editorList.add(new Editor(numberOfEditor, channel)); + editorList.add(new Editor(manager,numberOfEditor, channel)); return numberOfEditor; } - public void addEditor(REPSocketChannel channel) { - editorList.add(new Editor(0, channel)); + public void addEditor(SessionManager manager,REPSocketChannel channel) { + editorList.add(new Editor(manager,0, channel)); } public void setEID(REPCommand repCmd) { diff -r 77f443f6dc9f -r c83a3faec487 rep/Forwarder.java --- 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 myChannel; // REPCommands we sent to the next editor - LinkedList writeQueue = new LinkedList(); 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() 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 diff -r 77f443f6dc9f -r c83a3faec487 rep/Session.java --- 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 channel) { - editorList.add(new Editor(editorID, channel)); + public void addEditor(SessionManager manager,int editorID, REPSocketChannel channel) { + editorList.add(new Editor(manager,editorID, channel)); } public LinkedList getEditorList() { if(editorList == null) System.out.println("null!"); diff -r 77f443f6dc9f -r c83a3faec487 rep/SessionList.java --- 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 channel, int sid, REPCommand repCmd) { + public void addEditor(SessionManager manager,REPSocketChannel 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 editorChannel, int sid, int eid) { + public void addEditor(SessionManager manager,REPSocketChannel 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) { diff -r 77f443f6dc9f -r c83a3faec487 rep/SessionManager.java --- 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 waitingCommandInMerge; REPHandler normalHandler = new REPEditorHandler(this); - REPHandler handlerInMerge =new REPHandlerEditorInMerge(this); private BlockingQueue waitingEventQueue = new LinkedBlockingQueue();; private String myHost; + private LinkedList writeQueue = new LinkedList(); 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 sList = new LinkedList(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); + } + } diff -r 77f443f6dc9f -r c83a3faec487 rep/handler/REPHandlerEditorInMerge.java --- 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 key) throws IOException { - //マージ中のエディタの前のエディタのコマンドをWaitingListに追加する - REPSocketChannel 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 socketChannel) { - manager.remove(socketChannel); - } -} diff -r 77f443f6dc9f -r c83a3faec487 rep/translater/TranslaterImp1.java --- 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 output = new LinkedList(); LinkedList cmds = new LinkedList(); - 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); diff -r 77f443f6dc9f -r c83a3faec487 rep/xml/SessionXMLDecoder.java --- 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); } } } diff -r 77f443f6dc9f -r c83a3faec487 test/XMLTest.java --- 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");