view src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java @ 178:bf0ca6c43834

*** empty log message ***
author pin
date Sat, 30 Aug 2008 19:15:40 +0900
parents e9047957acc2
children
line wrap: on
line source

package pathfinder.mergetest;

import pathfinder.mergetest.channels.NetworkSimulator;
import remoteeditor.command.REPCommand;
import remoteeditor.network.REP;

public class EditorSimulatorWithoutMerger extends EditorSimulator {

	public EditorSimulatorWithoutMerger(int _eid, NetworkSimulator<REPCommand> _ns, String[] strings, String _name) {
		super(_eid, _ns, strings, _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 (eid == 0) ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" :"+cmd, 1);
				
				if(cmd.cmd == REP.SMCMD_QUIT_2){
					cs.write(new REPCommand(cmd));
					running = false;
					break;
				}

				if (cmd.eid==eid){
					//発行したコマンドが戻ってきた場合
					cs.write(new REPCommand(cmd));

				} else if (cmd.eid==-1){
					/* 制御プロセスからの指令  */
					ns.writeLog("\tEditor"+eid+" send command.", 2);
					if (cmd.cmd==REP.SMCMD_QUIT) {
						//sendOneCommand(cmd);
						cs.write(cmd);
						synchronized(ns){ ns.writeLog("send Quit cmd.", 1); }
						continue;
					}
					
					//if(lock) continue;
					String replacedText = text.edit(cmd);
					sendOneCommand(cmd, replacedText);
					
				}else if(cmd.eid == -2){
					
					/* 終了条件  */
					if (cmd.cmd==REP.SMCMD_QUIT){
						ns.writeLog("\tEditor"+eid+" catch QUIT command emited by itself.", 3);
						running=false; break;
					}else{
						// Merged Commands.
						String replacedText = text.edit(cmd);
						returnMergedCommand(cmd, replacedText);
					}
				} else {

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

			ns.writeLog("Editor"+eid+" finish.", 1);
		}
	}
	
	private void returnMergedCommand(REPCommand cmd, String replacedText) {
		REPCommand command = new REPCommand(cmd);
		if(command.cmd == REP.REPCMD_DELETE)command.setString(replacedText);
		cs.write(command);
	}

	private void manage(REPCommand cmd) {
		// TODO Auto-generated method stub
		switch(cmd.cmd){
		case REP.SMCMD_START_MERGE:
			lockEdit(true);
			break;
		case REP.SMCMD_END_MERGE:
			lockEdit(false);
			break;
		default:
			break;
		}
	}

	private void lockEdit(boolean b) {
	}

	protected void sendOneCommand(REPCommand cmd, String replacedText){
		
		if (cmd==null) return;
		
		cmd.eid = eid;
		cmd.seq = seq++;
		
		if(cmd.cmd == REP.REPCMD_INSERT){
			//cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq);
			cs.write(cmd);
			
		}else if(cmd.cmd == REP.REPCMD_DELETE){
			///String line = text.get(cmd.lineno);
			//cmd.setString(line);
			cmd.setString(replacedText);
			cs.write(cmd);
			
		}else if(cmd.cmd ==  REP.REPCMD_REPLACE){

			REPCommand deleteCmd = new REPCommand(REP.REPCMD_DELETE, 0, eid, seq-1, cmd.lineno, 0, "");
			//undoCmd.setString(text.get(cmd.lineno));
			deleteCmd.setString(replacedText);
			cs.write(deleteCmd);
			
			//cmd.setString("replaced by Editor"+cmd.eid+":"+cmd.seq);
			cmd.setCMD(REP.REPCMD_INSERT);
			cs.write(cmd);
			
		}else if(cmd.cmd == REP.SMCMD_QUIT){
			//cmd.setString("sent by Editor"+cmd.eid+":"+cmd.seq);
			cs.write(cmd);
		}

		//text.edit(cmd);

		//Thread.yield();
	}
	
}