Mercurial > hg > RemoteEditor > REPSessionManager
view rep/Editor.java @ 295:6deb6de8d0eb
*** empty log message ***
author | pin |
---|---|
date | Mon, 29 Sep 2008 14:13:19 +0900 |
parents | c3969dd625b2 |
children | c35f01b5060f |
line wrap: on
line source
package rep; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import rep.channel.REPSocketChannel; import rep.optimizers.*; import rep.translater.TranslaterImp1; public class Editor { private int eid; // unique id in a session private int sid = -1 ; // globally unique session id private REPSocketChannel<REPCommand> myChannel; private String host; private String file; 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>(); sentMergedList = new LinkedList<REPCommand>(); if (doOptimize) optimizer = new DeleteInsertOptimizer(); //タカノがつくったおぷてぃまいざ else optimizer = new NullOptimizer(); //なにもしないけどOptimizer. } public Editor(int editorNo, REPSocketChannel<REPCommand> channel){ this.eid = editorNo; this.myChannel = channel; translater = new TranslaterImp1(eid); sentList = new LinkedList<REPCommand>(); sentMergedList = new LinkedList<REPCommand>(); setHostAndPort(myChannel); } public Editor(REPSocketChannel<REPCommand> channel) { this.myChannel = channel; setHostAndPort(myChannel); translater = new TranslaterImp1(eid); sentList = new LinkedList<REPCommand>(); } public List<REPCommand> translate(REPCommand command){ List<REPCommand> list = new LinkedList<REPCommand>(); if(command.eid == eid){ if(checkReturnedCommand(command)){ //エディタからのコマンドが元のエディタに戻ってきた //マージして送信 ArrayList<REPCommand> cmds = translater.catchOwnCommand(command); //optimizer //マージ中のエディタからの割り込み検知に使う sentMergedList.addAll(cmds); sendMergedCommand(cmds); return list; }else{ //エディタからの新たな編集コマンド sentList.add(command); translater.transSendCmd(command); list.add(command); } }else if(eid == REP.MERGE_EID.id){ //マージコマンドが返ってきた if(translater.checkMergeConflict(command)){ //マージ中にエディタからの割り込みがあった場合 List<REPCommand> mergeAgainList = translater.getMergeAgain(); mergeAgainList = optimizer.optimize(mergeAgainList); for(REPCommand againCommand: mergeAgainList){ myChannel.write(againCommand); } } }else{ //他のエディタからの編集コマンド REPCommand[] cmds = translater.transReceiveCmd(command); for(REPCommand cmd : cmds){ list.add(cmd); } } return list; } private void sendMergedCommand(ArrayList<REPCommand> cmds) { // 一気に送ると、向こう側(Editor)で、dead lock する可能性がある。 // select loop の中で一つ一つ送るしかない。Editor側から割り込まれる可能性も // ある。その時に複数のコマンドを送っていると、どこに割り込まれたかを判断する // ことが出来ない。そこで、一つ一つReturnを確認する必要がある。つまり、 // select loop で送るしかない。 for(REPCommand mergeCommand : cmds){ mergeCommand.setEID(REP.MERGE_EID.id); myChannel.write(mergeCommand); } } private boolean checkReturnedCommand(REPCommand command) { if(sentList.size() > 0){ if(sentList.get(0).seq == command.seq){ sentList.remove(0); return true; }else{ System.err.println("error:Editor.checkReturnedCommand() : command = " + command); System.out.println("Editor.checkReturndCommand() : sentList = " + sentList); } } return false; } private void setHostAndPort(REPSocketChannel<REPCommand> myChannel2) { //host = myChannel2.socket().getRemoteSocketAddress().toString(); } public REPSocketChannel<REPCommand> getChannel() { return myChannel; } public void setHost(String host){ this.host = host; } public String getHost(){ return host; } public int getEID() { return eid; } public void setEID(int eid) { this.eid = eid; } public String toString(){ return (host + ":" + file); } public String getName() { return file; } public void setName(String string) { file = string; } public void send(REPCommand command) { myChannel.write(command); } public void setChannel(REPSocketChannel<REPCommand> channel) { myChannel = channel; } public boolean isMerging() { return translater.isMerging(); } public boolean hasSession() { return sid != -1; } public void setSID(int sessionID) { sid = sessionID; } }