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