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

*** empty log message ***
author pin
date Mon, 04 Aug 2008 20:10:25 +0900
parents bc162b1a7ebf
children 1768e68ba98e
line wrap: on
line source

package pathfinder.mergetest;

import java.util.Queue;

import remoteeditor.command.REPCommand;
import remoteeditor.network.REP;

public class EditorSimulatorWithoutMerger extends EditorSimulator {

	public EditorSimulatorWithoutMerger(int _eid, NetworkSimulator<REPCommand> _ns, Queue<REPCommand> q, String _name) {
		super(_eid, _ns, q, _name);
	}
	
	public void run(){
		ns.writeLog("Editor" + eid + " start.", 1);

		while(running){

			// MainLoop
			while(running){

				REPCommand cmd = cs.read();
				/* received Command */

				ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq, 3);
				//if (cmd.eid != -2 && eid == 0) ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" :"+cmd, 1);

				//System.out.println(eid + ":" + cmd.eid);

				if (cmd.eid==eid){

					text.edit(cmd);
					cs.write(new REPCommand(cmd));
					
					/* 終了条件  */
					if (cmd.cmd==REP.SMCMD_QUIT){
						ns.writeLog("\tEditor"+eid+" catch QUIT command emited by itself.", 3);
						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 if(cmd.eid == -2){
					// Merged Commands.
					text.edit(cmd);
				} else {

					ns.writeLog("\t\tEditor"+eid+" edit text and pass Cmd. " + " : " + cmd, 3);
					text.edit(cmd);
					cs.write(new REPCommand(cmd));
				}
			}

			ns.writeLog("Editor"+eid+" finish.", 1);
		}
	}
	
	protected void sendOneCommand(REPCommand cmd){

		if (cmd==null) cmd = CmdList.poll();
		if (cmd==null) return;

		cmd.eid = eid;
		cmd.seq = seq++;
		cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq);

		text.edit(cmd);
		cs.write(new REPCommand(cmd));

		Thread.yield();
	}
	
}