annotate src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java @ 152:09ad66f62f4a before_REP_meeting_on_080822

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