view src/pathfinder/mergetest/EditorSimulator.java @ 149:5942e0e3c632

*** empty log message ***
author pin
date Mon, 04 Aug 2008 20:10:25 +0900
parents bc162b1a7ebf
children 6326e5ea4595
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{
	protected int eid;
	protected int seq;
	//private boolean isOwner;
	protected NetworkSimulator<REPCommand> ns;
	protected ChannelSimulator<REPCommand> cs;
	protected Queue<REPCommand> CmdList;
	private TranslaterImp1 translater;
	protected Text text;
	protected 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 + ":");
			
			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);
	}

	protected 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;
	}
}