113
|
1 package pathfinder.mergetest;
|
|
2
|
|
3 import java.util.Queue;
|
|
4
|
|
5 import remoteeditor.command.REPCommand;
|
|
6 import remoteeditor.network.REP;
|
|
7 import sample.merge.TranslaterImp1;
|
|
8
|
|
9 public class EditorSimulator extends Thread{
|
|
10 private int eid;
|
|
11 private int seq;
|
125
|
12 //private boolean isOwner;
|
113
|
13 private NetworkSimulator<REPCommand> ns;
|
|
14 private ChannelSimulator<REPCommand> cs;
|
|
15 private Queue<REPCommand> CmdList;
|
|
16 private TranslaterImp1 translater;
|
|
17 private Text text;
|
|
18 private boolean running=true;
|
|
19
|
|
20 public EditorSimulator(int _eid, NetworkSimulator<REPCommand> _ns, Queue<REPCommand> q, String _name) {
|
|
21 super(_name);
|
|
22 eid = _eid;
|
|
23 ns = _ns;
|
|
24 CmdList = q;
|
|
25 translater = new TranslaterImp1(_eid);
|
|
26 text = new Text();
|
|
27 cs = ns.connect();
|
|
28 }
|
|
29
|
|
30 public void setOwner(boolean f){
|
125
|
31 //isOwner = f;
|
113
|
32 }
|
|
33 synchronized public void finish(){
|
|
34 running = false;
|
|
35 }
|
|
36
|
|
37 public void run(){
|
|
38 System.out.println("Editor"+eid+" start.");
|
|
39
|
|
40 // Send All Command that is included CmdList.
|
125
|
41 //sendAllCommand();
|
113
|
42
|
|
43 // MainLoop,
|
|
44 while(running){
|
|
45 REPCommand cmd = cs.read();
|
|
46 REPCommand[] cmds;
|
|
47
|
125
|
48 //System.out.println("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq);
|
124
|
49
|
113
|
50 if (cmd.eid==eid){
|
|
51 cmds = translater.catchOwnCommand(cmd);
|
|
52 for (int i=0; i<cmds.length; i++){
|
124
|
53 REPCommand tmp = cmds[i];
|
125
|
54 //System.out.println("\t\tEditor"+eid+" edit text. ");
|
124
|
55 text.edit(tmp);
|
|
56 }
|
|
57 /* 終了条件 */
|
|
58 if (cmd.cmd==REP.SMCMD_QUIT){
|
125
|
59 //System.out.println("\tEditor"+eid+" catch QUIT command emited by itself.");
|
124
|
60 running=false; break;
|
113
|
61 }
|
125
|
62 } else if (cmd.eid==-1){
|
|
63 /* 制御プロセスからの指令 */
|
|
64 System.out.println("\tEditor"+eid+" send command.");
|
|
65 sendOneCommand(cmd);
|
113
|
66 } else {
|
|
67 cmds = translater.transReceiveCmd(cmd);
|
|
68 for (int i=0; i<cmds.length; i++){
|
|
69 cmd = cmds[i];
|
125
|
70 //System.out.println("\t\tEditor"+eid+" edit text and pass Cmd. ");
|
113
|
71 text.edit(cmd);
|
|
72 cs.write(new REPCommand(cmd));
|
|
73 }
|
|
74 }
|
|
75 }
|
|
76
|
|
77 System.out.println("Editor"+eid+" finish.");
|
|
78 }
|
|
79
|
125
|
80 private void sendOneCommand(REPCommand cmd) {
|
113
|
81 REPCommand[] cmds;
|
125
|
82 if (cmd==null) cmd = CmdList.poll();
|
|
83 if (cmd==null) return;
|
|
84
|
113
|
85 cmd.eid = eid;
|
|
86 cmds = translater.transSendCmd(cmd);
|
125
|
87 cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq);
|
113
|
88
|
125
|
89 //if (isOwner) cmd.setThroughMaster(true);
|
113
|
90 for (int i=0; i<cmds.length; i++){
|
|
91 text.edit(cmds[i]);
|
|
92 cs.write(new REPCommand(cmds[i]));
|
|
93 }
|
125
|
94 Thread.yield();
|
113
|
95 }
|
|
96 private void sendAllCommand() {
|
|
97 REPCommand[] cmds;
|
|
98 for (REPCommand cmd: CmdList){
|
|
99 cmd.seq = seq;
|
|
100 cmd.eid = eid;
|
|
101 cmds = translater.transSendCmd(cmd);
|
|
102 cmd.setString("this is inserted or replaced by Editor"+cmd.eid+":"+cmd.seq);
|
|
103 //if (isOwner) cmd.setThroughMaster(true);
|
|
104 for (int i=0; i<cmds.length; i++){
|
|
105 text.edit(cmds[i]);
|
|
106 cs.write(new REPCommand(cmds[i]));
|
|
107 }
|
|
108 }
|
|
109
|
|
110 // Send Quit Command
|
|
111 cmds = translater.transSendCmd( new REPCommand(REP.SMCMD_QUIT, 0, eid, seq++, 0, 0, "QUIT by Editor"+eid));
|
|
112 for (int i=0; i<cmds.length; i++){
|
|
113 text.edit(cmds[i]);
|
|
114 cs.write(new REPCommand(cmds[i]));
|
|
115 }
|
|
116
|
|
117 }
|
|
118 /*
|
|
119 private boolean checkQuit(REPCommand cmd) {
|
|
120 // 最初に全部のコマンドを送信するから、自分のQUITが来るのは最後
|
|
121 return (cmd.eid==eid && cmd.cmd==REP.SMCMD_QUIT);
|
|
122 }
|
|
123 */
|
|
124 public Text getText(){
|
|
125 return text;
|
|
126 }
|
|
127 } |