Mercurial > hg > RemoteEditor > Eclipse
view src/pathfinder/mergetest/EditorSimulator.java @ 146:0d49e1116336
*** empty log message ***
author | pin |
---|---|
date | Tue, 13 May 2008 18:26:59 +0900 (2008-05-13) |
parents | b56b5950cb18 |
children | a9fdbcbe351f |
line wrap: on
line source
package pathfinder.mergetest; import java.util.Queue; import remoteeditor.command.REPCommand; import remoteeditor.network.REP; import sample.merge.TranslaterImp1; public class EditorSimulator extends Thread{ private int eid; private int seq; //private boolean isOwner; private NetworkSimulator<REPCommand> ns; private ChannelSimulator<REPCommand> cs; private Queue<REPCommand> CmdList; private TranslaterImp1 translater; private Text text; private boolean running=true; public EditorSimulator(int _eid, NetworkSimulator<REPCommand> _ns, Queue<REPCommand> q, String _name) { super(_name); eid = _eid; ns = _ns; CmdList = q; translater = new TranslaterImp1(_eid); text = new Text(); cs = ns.connect(); } public void setOwner(boolean f){ //isOwner = f; } synchronized public void finish(){ running = false; } public void run(){ ns.writeLog("Editor"+eid+" start.", 1); // Send All Command that is included CmdList. //sendAllCommand(); // MainLoop, while(running){ REPCommand cmd = cs.read(); /* received Command */ if(eid == 0)System.out.println("editor" + eid + ":" + cmd.string + ":" + cmd.eid); REPCommand[] cmds; ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq, 3); if (cmd.eid==eid){ cmds = translater.catchOwnCommand(cmd); for (int i=0; i<cmds.length; i++){ REPCommand tmp = cmds[i]; ns.writeLog("\t\tEditor"+eid+" edit text. ", 4); text.edit(tmp); } /* 終了条件 */ if (cmd.cmd==REP.SMCMD_QUIT){ ns.writeLog("\tEditor"+eid+" catch QUIT command emited by itself.", 2); running=false; break; } } else if (cmd.eid==-1){ /* 制御プロセスからの指令 */ ns.writeLog("\tEditor"+eid+" send command.", 2); if (cmd.cmd==REP.SMCMD_QUIT) synchronized(ns){ ns.writeLog("send Quit cmd.", 1); } sendOneCommand(cmd); } else { cmds = translater.transReceiveCmd(cmd); for (int i=0; i<cmds.length; i++){ cmd = cmds[i]; ns.writeLog("\t\tEditor"+eid+" edit text and pass Cmd. ", 4); text.edit(cmd); cs.write(new REPCommand(cmd)); } } } ns.writeLog("Editor"+eid+" finish.", 1); } private void sendOneCommand(REPCommand cmd) { REPCommand[] cmds; if (cmd==null) cmd = CmdList.poll(); if (cmd==null) return; cmd.eid = eid; cmds = translater.transSendCmd(cmd); cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq); //if (isOwner) cmd.setThroughMaster(true); for (int i=0; i<cmds.length; i++){ text.edit(cmds[i]); cs.write(new REPCommand(cmds[i])); } Thread.yield(); } private void sendAllCommand() { REPCommand[] cmds; for (REPCommand cmd: CmdList){ cmd.seq = seq; cmd.eid = eid; cmds = translater.transSendCmd(cmd); cmd.setString("this is inserted or replaced by Editor"+cmd.eid+":"+cmd.seq); //if (isOwner) cmd.setThroughMaster(true); for (int i=0; i<cmds.length; i++){ text.edit(cmds[i]); cs.write(new REPCommand(cmds[i])); } } // Send Quit Command cmds = translater.transSendCmd( new REPCommand(REP.SMCMD_QUIT, 0, eid, seq++, 0, 0, "QUIT by Editor"+eid)); for (int i=0; i<cmds.length; i++){ text.edit(cmds[i]); cs.write(new REPCommand(cmds[i])); } } /* private boolean checkQuit(REPCommand cmd) { // 最初に全部のコマンドを送信するから、自分のQUITが来るのは最後 return (cmd.eid==eid && cmd.cmd==REP.SMCMD_QUIT); } */ public Text getText(){ return text; } }