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