view src/pathfinder/mergetest/EditorSimulator.java @ 186:8910ffda5273

*** empty log message ***
author kono
date Mon, 29 Sep 2008 05:18:05 +0900
parents e9047957acc2
children
line wrap: on
line source

package pathfinder.mergetest;



import pathfinder.mergetest.channels.ChannelSimulator;
import pathfinder.mergetest.channels.NetworkSimulator;

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;
	protected int semaIP;

	public EditorSimulator(int _eid, NetworkSimulator<REPCommand> _ns, String[] strings, String _name) {
		super(_name);
		eid = _eid;
		ns = _ns;
		
		translater = new TranslaterImp1(_eid);
		if(strings != null) text = new Text(strings);
		cs = new ChannelSimulator<REPCommand>(ns);
		cs.connect(semaIP);
	}

	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]));
//		}
		text.edit(cmds);
		cs.write(cmds);
		Thread.yield();
	}
//	private void sendAllCommand() {
//		REPCommand[] cmds;
//
//		// 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;
	}

	public ChannelSimulator<REPCommand> getChannelforUser(){
		return cs.createConjugatedChannel();
	}
	
	
}