148
|
1 package pathfinder.mergetest;
|
|
2
|
|
3 import remoteeditor.command.REPCommand;
|
|
4 import remoteeditor.network.REP;
|
|
5
|
|
6 public class EditorSimulatorWithoutMerger extends EditorSimulator {
|
|
7
|
153
|
8 public EditorSimulatorWithoutMerger(int _eid, NetworkSimulator<REPCommand> _ns, String[] strings, String _name) {
|
|
9 super(_eid, _ns, strings, _name);
|
148
|
10 }
|
|
11
|
|
12 public void run(){
|
149
|
13 ns.writeLog("Editor" + eid + " start.", 1);
|
|
14
|
148
|
15 while(running){
|
149
|
16
|
|
17 // MainLoop
|
|
18 while(running){
|
|
19
|
|
20 REPCommand cmd = cs.read();
|
|
21 /* received Command */
|
|
22
|
|
23 ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq, 3);
|
154
|
24 //if (eid == 0) ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" :"+cmd, 1);
|
149
|
25
|
152
|
26 //manage(cmd);
|
154
|
27
|
|
28 if(cmd.cmd == REP.SMCMD_QUIT_2){
|
|
29 cs.write(new REPCommand(cmd));
|
|
30 running = false;
|
|
31 break;
|
|
32 }
|
149
|
33
|
|
34 if (cmd.eid==eid){
|
152
|
35 //発行したコマンドが戻ってきた場合
|
149
|
36 cs.write(new REPCommand(cmd));
|
152
|
37
|
149
|
38 } else if (cmd.eid==-1){
|
|
39 /* 制御プロセスからの指令 */
|
|
40 ns.writeLog("\tEditor"+eid+" send command.", 2);
|
152
|
41 if (cmd.cmd==REP.SMCMD_QUIT) {
|
|
42 //sendOneCommand(cmd);
|
|
43 cs.write(cmd);
|
149
|
44 synchronized(ns){ ns.writeLog("send Quit cmd.", 1); }
|
152
|
45 continue;
|
|
46 }
|
149
|
47
|
153
|
48 //if(lock) continue;
|
|
49 String replacedText = text.edit(cmd);
|
|
50 sendOneCommand(cmd, replacedText);
|
149
|
51
|
|
52 }else if(cmd.eid == -2){
|
151
|
53
|
|
54 /* 終了条件 */
|
|
55 if (cmd.cmd==REP.SMCMD_QUIT){
|
|
56 ns.writeLog("\tEditor"+eid+" catch QUIT command emited by itself.", 3);
|
|
57 running=false; break;
|
|
58 }else{
|
155
|
59 // Merged Commands.
|
|
60 String replacedText = text.edit(cmd);
|
|
61 returnMergedCommand(cmd, replacedText);
|
151
|
62 }
|
149
|
63 } else {
|
|
64
|
|
65 ns.writeLog("\t\tEditor"+eid+" edit text and pass Cmd. " + " : " + cmd, 3);
|
156
|
66 //if(eid == 2)ns.writeLog("\t\tEditor"+eid+" edit text and pass Cmd. " + " : " + cmd, 1);
|
149
|
67 text.edit(cmd);
|
|
68 cs.write(new REPCommand(cmd));
|
148
|
69 }
|
|
70 }
|
149
|
71
|
|
72 ns.writeLog("Editor"+eid+" finish.", 1);
|
148
|
73 }
|
|
74 }
|
|
75
|
155
|
76 private void returnMergedCommand(REPCommand cmd, String replacedText) {
|
|
77 REPCommand command = new REPCommand(cmd);
|
|
78 if(command.cmd == REP.REPCMD_DELETE)command.setString(replacedText);
|
|
79 cs.write(command);
|
|
80 }
|
|
81
|
152
|
82 private void manage(REPCommand cmd) {
|
|
83 // TODO Auto-generated method stub
|
|
84 switch(cmd.cmd){
|
|
85 case REP.SMCMD_START_MERGE:
|
|
86 lockEdit(true);
|
|
87 break;
|
|
88 case REP.SMCMD_END_MERGE:
|
|
89 lockEdit(false);
|
|
90 break;
|
|
91 default:
|
|
92 break;
|
|
93 }
|
|
94 }
|
|
95
|
|
96 private void lockEdit(boolean b) {
|
|
97 }
|
|
98
|
153
|
99 protected void sendOneCommand(REPCommand cmd, String replacedText){
|
|
100
|
149
|
101 if (cmd==null) return;
|
152
|
102
|
149
|
103 cmd.eid = eid;
|
|
104 cmd.seq = seq++;
|
152
|
105
|
|
106 if(cmd.cmd == REP.REPCMD_INSERT){
|
153
|
107 //cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq);
|
152
|
108 cs.write(cmd);
|
|
109
|
|
110 }else if(cmd.cmd == REP.REPCMD_DELETE){
|
153
|
111 ///String line = text.get(cmd.lineno);
|
|
112 //cmd.setString(line);
|
|
113 cmd.setString(replacedText);
|
152
|
114 cs.write(cmd);
|
|
115
|
|
116 }else if(cmd.cmd == REP.REPCMD_REPLACE){
|
|
117
|
153
|
118 REPCommand deleteCmd = new REPCommand(REP.REPCMD_DELETE, 0, eid, seq-1, cmd.lineno, 0, "");
|
|
119 //undoCmd.setString(text.get(cmd.lineno));
|
|
120 deleteCmd.setString(replacedText);
|
|
121 cs.write(deleteCmd);
|
152
|
122
|
153
|
123 //cmd.setString("replaced by Editor"+cmd.eid+":"+cmd.seq);
|
|
124 cmd.setCMD(REP.REPCMD_INSERT);
|
152
|
125 cs.write(cmd);
|
|
126
|
|
127 }else if(cmd.cmd == REP.SMCMD_QUIT){
|
153
|
128 //cmd.setString("sent by Editor"+cmd.eid+":"+cmd.seq);
|
152
|
129 cs.write(cmd);
|
|
130 }
|
148
|
131
|
153
|
132 //text.edit(cmd);
|
149
|
133
|
153
|
134 //Thread.yield();
|
148
|
135 }
|
149
|
136
|
148
|
137 }
|