Mercurial > hg > RemoteEditor > REPSessionManager
view rep/Editor.java @ 224:6b0dd92b8e45
add optimizer to Editor
author | kent |
---|---|
date | Sun, 31 Aug 2008 13:28:34 +0900 |
parents | 4d9b32666ed2 |
children | 6c2f54265471 |
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; 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>(); 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>(); 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 null; }else{ //エディタからの新たな編集コマンド sentList.add(command); translater.transSendCmd(command); list.add(command); } }else if(eid == REP.MERGE_EID){ //マージコマンドが返ってきた 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) { for(REPCommand mergeCommand : cmds){ mergeCommand.setEID(REP.MERGE_EID); 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.out.println("error:Editor.checkReturnedCommand()"); } } 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(); } }