83
|
1 package pathfinder.BlockingQnoSeMa;
|
|
2
|
|
3 import java.util.Queue;
|
|
4
|
|
5 import remoteeditor.command.REPCommand;
|
|
6 import remoteeditor.network.REP;
|
|
7 import sample.merge.Translater;
|
105
|
8 import sample.merge.TranslaterImp1;
|
83
|
9
|
|
10 public class EditorSimulator extends Thread{
|
|
11 private int eid;
|
|
12 private int seq;
|
|
13 private boolean isOwner;
|
|
14 private NetworkSimulator<REPCommand> ns;
|
|
15 private ChannelSimulator<REPCommand> cs;
|
|
16 private Queue<REPCommand> CmdList;
|
105
|
17 private TranslaterImp1 translater;
|
83
|
18 private Text text;
|
|
19 private boolean running=true;
|
|
20
|
|
21 public EditorSimulator(int _eid, NetworkSimulator<REPCommand> _ns, Queue<REPCommand> q, String _name) {
|
|
22 super(_name);
|
|
23 eid = _eid;
|
|
24 ns = _ns;
|
|
25 CmdList = q;
|
105
|
26 translater = new TranslaterImp1(_eid);
|
83
|
27 text = new Text();
|
|
28 cs = ns.connect();
|
|
29 }
|
|
30
|
|
31 public void setOwner(boolean f){
|
|
32 isOwner = f;
|
|
33 }
|
|
34 synchronized public void finish(){
|
|
35 running = false;
|
|
36 }
|
|
37
|
|
38 public void run(){
|
|
39 System.out.println("Editor"+eid+" start.");
|
|
40
|
|
41 // Send All Command that is included CmdList.
|
|
42 sendAllCommand();
|
|
43
|
|
44 // MainLoop,
|
|
45 while(running){
|
|
46 REPCommand cmd = cs.read();
|
105
|
47 REPCommand[] cmds;
|
83
|
48
|
91
|
49 //終了条件
|
83
|
50 if (cmd.eid==eid && cmd.cmd==REP.SMCMD_QUIT){
|
|
51 System.out.println("\tEditor"+eid+" catch QUIT command emited by itself.");
|
|
52 translater.transReceiveCmd(cmd);
|
|
53 running=false; break;
|
|
54 }
|
|
55 System.out.println("\tEditor"+eid+" catch command:>> "+cmd.toString());
|
105
|
56 cmds = translater.transReceiveCmd(cmd);
|
|
57 if (cmds==null) continue;
|
83
|
58
|
105
|
59 for (int i=0; i<cmds.length; i++){
|
|
60 cmd = cmds[i];
|
|
61 if (isOwner) cmd.setThroughMaster(true);
|
|
62 text.edit(cmd);
|
|
63 cs.write(cmd);
|
|
64 }
|
83
|
65 }
|
|
66
|
|
67 System.out.println("Editor"+eid+" finish.");
|
|
68 }
|
|
69
|
|
70 private void sendAllCommand() {
|
|
71 for (REPCommand cmd: CmdList){
|
|
72 cmd.seq = seq;
|
|
73 cmd.eid = eid;
|
|
74 cmd.setString("this is inserted or replaced by Editor"+eid+":"+seq);
|
|
75 cmd = translater.transSendCmd(cmd);
|
|
76 if (isOwner) cmd.setThroughMaster(true);
|
|
77 text.edit(cmd);
|
|
78 cs.write(cmd);
|
|
79 seq++;
|
|
80 }
|
|
81 // Send Quit Command
|
|
82 cs.write( translater.transSendCmd( new REPCommand(REP.SMCMD_QUIT, 0, eid, seq++, 0, 0, "QUIT by Editor"+eid)));
|
|
83 }
|
|
84 /*
|
|
85 private boolean checkQuit(REPCommand cmd) {
|
91
|
86 // 最初に全部のコマンドを送信するから、自分のQUITが来るのは最後
|
83
|
87 return (cmd.eid==eid && cmd.cmd==REP.SMCMD_QUIT);
|
|
88 }
|
|
89 */
|
|
90 public Text getText(){
|
|
91 return text;
|
|
92 }
|
|
93 } |