390
|
1 package rep.handler;
|
|
2
|
|
3 import java.util.Collection;
|
|
4 import java.util.Comparator;
|
|
5 import java.util.LinkedList;
|
|
6 import java.util.List;
|
|
7 import java.util.TreeSet;
|
|
8
|
|
9 import rep.REP;
|
|
10 import rep.REPCommand;
|
|
11 import rep.optimizers.REPCommandOptimizer;
|
|
12
|
|
13 public class Translator {
|
|
14 public int eid;
|
|
15
|
|
16 public REPCommandOptimizer optimizer;
|
|
17 private LinkedList<REPCommand> unMergedCmds;
|
|
18 public LinkedList<REPCommand> sentMergedList;
|
|
19 private LinkedList<REPCommand> mergeAgainList;
|
|
20 boolean merge_mode = false;
|
|
21
|
|
22 public Translator(int _eid,REPCommandOptimizer opt){
|
|
23 eid = _eid;
|
|
24 optimizer = opt;
|
|
25 unMergedCmds = new LinkedList<REPCommand>();
|
|
26 mergeAgainList = new LinkedList<REPCommand>();
|
|
27 sentMergedList = new LinkedList<REPCommand>();
|
|
28 }
|
|
29
|
|
30 /**
|
|
31 * New command from an editor
|
|
32 * The command is sent to the next editor
|
|
33 * @param cmd
|
|
34 * @return translated command.
|
|
35 */
|
|
36 public REPCommand transSendCmd(REPCommand cmd){
|
|
37 assert(cmd.eid==eid);
|
|
38 unMergedCmds.add(cmd);
|
|
39
|
|
40 //マージ中にユーザから割り込みがあった場合
|
|
41 if(isMerging()){
|
|
42 mergeAgainList.add(cmd);
|
|
43 }
|
|
44
|
|
45 return cmd;
|
|
46 }
|
|
47 /**
|
|
48 * My command is returned from the session ring, and START_MERGE_ACK
|
|
49 * is returned. At this
|
|
50 * stage my writeQueue is empty, our editor is waiting for me.
|
|
51 * Start merge process.
|
|
52 * @param cmd
|
|
53 */
|
|
54 public boolean catchOwnCommand(REPNode editor){
|
|
55 LinkedList<REPCommand> output = new LinkedList<REPCommand>();
|
|
56 LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
|
|
57 //スタック上にあるコマンドを全部undoコマンドにする
|
|
58 while ( !unMergedCmds.isEmpty() ){
|
|
59 REPCommand cmd0 = unMergedCmds.removeLast();
|
|
60 output.add( createUndo(cmd0) );
|
|
61 cmds.add(cmd0);
|
|
62 }
|
|
63
|
|
64 /* 必要な分だけソートして返却用のリストに追加 */
|
|
65 output.addAll( sortCmds(cmds) );
|
|
66
|
|
67 /* 残ったコマンドも再び実行させるが、まだマージされてないのでunMergedにも入れる */
|
|
68 output.addAll(cmds);
|
391
|
69 unMergedCmds.addAll(cmds);
|
390
|
70 return optimizedSend(editor,output);
|
|
71 }
|
|
72
|
|
73 /**
|
|
74 * Sent optimized merged command list
|
|
75 * @param editor
|
|
76 * @param output
|
|
77 * @return if any sent commands output
|
|
78 */
|
|
79 public boolean optimizedSend(REPNode editor, LinkedList<REPCommand> output) {
|
|
80 List<REPCommand> output1 = optimizer.optimize(output);
|
|
81 if (output1.size()==0) {
|
|
82 merge_mode = false;
|
|
83 return false;
|
|
84 }
|
|
85 for(REPCommand c:output1) {
|
|
86 REPCommand m = new REPCommand(c);
|
|
87 m.setEID(REP.MERGE_EID.id);
|
|
88 m.setSEQID(editor.seq());
|
|
89 sentMergedList.add(m);
|
|
90 editor.send(m);
|
|
91 }
|
|
92 merge_mode = true;
|
|
93 return true;
|
|
94 }
|
|
95
|
|
96 private REPCommand createUndo(REPCommand cmd){
|
|
97 REPCommand retCmd = new REPCommand(cmd);
|
|
98 if (cmd.cmd==REP.REPCMD_INSERT) retCmd.cmd=REP.REPCMD_DELETE;
|
|
99 else if (cmd.cmd==REP.REPCMD_DELETE) retCmd.cmd=REP.REPCMD_INSERT;
|
|
100 return retCmd;
|
|
101 }
|
|
102
|
|
103 class REPCommandComparator implements Comparator<REPCommand>{
|
|
104
|
|
105 public int compare(REPCommand o1, REPCommand o2) {
|
|
106
|
|
107 if ( o2.lineno > o1.lineno ) return 1;
|
|
108 else if ( o2.lineno < o1.lineno
|
|
109 || o2.eid > o1.eid )
|
|
110 return -1;
|
|
111
|
|
112 return 1;
|
|
113 }
|
|
114
|
|
115 }
|
|
116
|
|
117 private Collection<REPCommand> sortCmds(LinkedList<REPCommand> cmds) {
|
|
118 TreeSet<REPCommand> sortedCmds1 = new TreeSet<REPCommand>(new REPCommandComparator());
|
|
119 int top;
|
|
120 int prevEid=-1;
|
|
121 while ( -1 != (top=getPrecedence(cmds, prevEid+1)) ){
|
|
122 REPCommand tmp = cmds.remove(top);
|
|
123 sortedCmds1.add(tmp);
|
|
124 prevEid = tmp.eid;
|
|
125 }
|
|
126
|
|
127 return sortedCmds1;
|
|
128 }
|
|
129
|
|
130 /* search cmd. ordering by min EID that is lower lowEid and min SEQ. */
|
|
131 private int getPrecedence(LinkedList<REPCommand> cmds, int lowEid) {
|
|
132 int cEid, cSeq;
|
|
133 cEid=cSeq=Integer.MAX_VALUE;
|
|
134 int ret=-1;
|
|
135 for (int i=0; i<cmds.size(); i++){
|
|
136 REPCommand c = cmds.get(i);
|
|
137 if ( c.eid<lowEid ) continue;
|
|
138 else if ( c.eid>cEid ) continue;
|
|
139 else if ( c.eid==cEid ) {
|
|
140 if ( c.seq>cSeq ) continue;
|
|
141 cSeq=c.seq;
|
|
142 ret = i;
|
|
143 } else { /* tmp.eid<cEid */
|
|
144 cEid = c.eid;
|
|
145 cSeq = c.seq;
|
|
146 ret = i;
|
|
147 }
|
|
148 }
|
|
149 return ret;
|
|
150 }
|
|
151
|
|
152 /**
|
|
153 * Translate Command cmd that was received from SeMa.
|
|
154 * @param cmd the command to be translated.
|
|
155 * @return translated commannd.
|
|
156 */
|
|
157 public void transReceiveCmd(REPNode nextEditor,REPCommand cmd){
|
|
158 assert (cmd.eid != eid);
|
|
159 unMergedCmds.add(cmd);
|
|
160 }
|
|
161
|
|
162 public void setEid(int _eid){
|
|
163 eid = _eid;
|
|
164 }
|
|
165
|
|
166 public boolean checkMergeConflict(REPCommand command) {
|
|
167 REPCommand prev = sentMergedList.remove();
|
|
168 assert (prev.seq==command.seq);
|
|
169
|
|
170 if(mergeAgainList.size() > 0){
|
|
171 mergeAgainList.add(command);
|
|
172 return true;
|
|
173 }
|
|
174 if(sentMergedList.size()==0) {
|
|
175 merge_mode=false;
|
|
176 }
|
|
177 return false;
|
|
178 }
|
|
179
|
|
180 public void getMergeAgain(REPNode editor) {
|
|
181 LinkedList<REPCommand> returnCommand = new LinkedList<REPCommand>();
|
|
182 for(int i = 0; i < mergeAgainList.size(); i++){
|
|
183 //eid = REP.MEGE_EID
|
|
184 returnCommand.add(createUndo(mergeAgainList.get(mergeAgainList.size() - i -1)));
|
|
185 }
|
|
186 for(REPCommand command : mergeAgainList){
|
|
187 if(command.eid == REP.MERGE_EID.id){
|
|
188 returnCommand.add(command);
|
|
189 }
|
|
190 }
|
|
191 for(REPCommand command : mergeAgainList){
|
|
192 if(command.eid == eid){
|
|
193 command.eid = REP.MERGE_EID.id;
|
|
194 returnCommand.add(command);
|
|
195 }
|
|
196 }
|
|
197 mergeAgainList.clear();
|
|
198 optimizedSend(editor, returnCommand);
|
|
199 }
|
|
200
|
|
201 public boolean isFinished() {
|
|
202 if(unMergedCmds.size() > 0) return false;
|
|
203 if(sentMergedList.size() > 0) return false;
|
|
204 return true;
|
|
205 }
|
|
206
|
|
207 public boolean isMerging() {
|
|
208 return merge_mode;
|
|
209 }
|
|
210
|
|
211 public void startMerge(REPCommand cmd) {
|
|
212 merge_mode = true;
|
|
213 }
|
|
214
|
|
215 public void mergeAck() {
|
|
216 }
|
|
217
|
|
218
|
|
219
|
|
220 }
|