annotate src/pathfinder/BlockingQnoSeMa/EditorSimulator.java @ 105:6c209de0dd99

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