view test/sematest/TestEditor.java @ 300:8f6b7a1890b2

*** empty log message ***
author kono
date Wed, 01 Oct 2008 16:20:04 +0900
parents d13dad30fd24
children 4ee012f19855
line wrap: on
line source

package test.sematest;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.util.LinkedList;

import rep.REP;
import rep.REPCommand;
import rep.REPCommandPacker;
import rep.channel.REPLogger;
import rep.channel.REPSelector;
import rep.channel.REPSocketChannel;
import test.Text;


/**
 * @author kono
 *	Basic Temote Editor client implementation
 *     should support multi-session
 *     currently multi-session requires new channel, that is
 *     only one session for this editor.
 */
public class TestEditor extends Thread{
	private InetSocketAddress semaIP;
	private REPLogger ns;
	private int seq = 0;
	public Text text;
	public LinkedList<REPCommand> cmds;
	private int eid = 0;
	private int sid = 0;
	REPSocketChannel<REPCommand> channel;
	boolean running = true;
	long timeout = 1;
	private String name;
	private REPCommand nop = new REPCommand(REP.REPCMD_NOP,0,0,0,0,"");
	private boolean inputLock=false;
	public boolean detached=false;
	public boolean master=false;
	private int syncCounter;
	private boolean hasInputLock=true;

	static private String[] text1d = {
		"aaa", "bbb", "ccc", "ddd", "eee",
	};	
	
	public TestEditor(String name, String _host,int _port, boolean master){
		super(name);
		semaIP = new InetSocketAddress(_host, _port);
		ns = REPLogger.singleton();
		this.name = name;
		cmds = new LinkedList<REPCommand>();
		if (master) {
			this.master=true;
			text = new Text(text1d);
			cmds.add(new REPCommand(REP.SMCMD_PUT,0,0,0,0,name+"-file"));
			cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"m0"));
			cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"m0"));
			cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,""));
		} else {
			text = new Text(new String[0]);
			cmds.add(new REPCommand(REP.SMCMD_JOIN,0,0,0,0,name));
			cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"c0"));
			cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"c0"));
			//cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,""));
		}
	}

	public void run(){
		try {
			channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
		} catch (IOException e) {	return;	}

		ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" "+name, 1);
		try {
			while (!channel.connect(semaIP)){
				ns.writeLog("SeMa not listen to socket yet, wait", 1);
			}
		} catch (IOException e) { return; }
		ns.writeLog("successes to connect "+name);
		/*
		 *         public REPCommand(REP cmd,int sid,int eid, int seq, int lineno,  String string) 
		 */
		try {
			mainloop();
		} catch (IOException e) {
		}
	}

	private void mainloop() throws IOException {
		
		channel.configureBlocking(false);
		REPSelector<REPCommand> selector = REPSelector.create();
		channel.register(selector, SelectionKey.OP_READ);
		while(running) {
			if (inputLock) {
				// No user input during merge mode (optional)
				if (selector.select(0)>0) {
					handle(channel.read());
				}
				continue;
			} else if (selector.select(timeout)<=0) {
				if (syncCounter>0) {
					syncText();
				}
				userInput();
			} else {
				handle(channel.read());
			}
		}
	}

	private void syncText() {
		if (syncCounter>text.size()) {
			syncCounter=0;
		} else {
			int i=syncCounter-1;
			REPCommand del = new REPCommand(REP.REPCMD_DELETE,sid,eid,0,i, text.get(i));
			REPCommand ins = new REPCommand(REP.REPCMD_INSERT,sid,eid,0,i, text.get(i));
			sendCommand(del);
			sendCommand(ins);
			syncCounter++;
		}
	}

	private void userInput() {
		REPCommand cmd = cmds.poll();
		if (cmd!=null) {
			switch(cmd.cmd) {
			case REPCMD_INSERT:
				text.insert(cmd.lineno, cmd.string);
				sendCommand(cmd);
				break;
			case REPCMD_DELETE:
				String del = text.delete(cmd.lineno);
				cmd.setString(del);
				sendCommand(cmd);
				break;
			case SMCMD_QUIT:
				cmds.clear();
				sendCommand(cmd);
				break;
			case SMCMD_JOIN:
			case SMCMD_PUT:
				sendCommand(cmd);
				inputLock = true; // wait until ACK
				break;
			default:
				assert(false);
			}
		} else {
			// no more command to send
			timeout = 0;
		}
	}


	private void sendCommand(REPCommand cmd) {
		cmd.setSEQID(seq++);
		cmd.setEID(eid);
		cmd.setSID(sid);
		ns.writeLog(name +" send "+cmd);
		channel.write(cmd);
	}

	private void handle(REPCommand cmd) {
		ns.writeLog(name +": read "+cmd);
		switch(cmd.cmd) {
		case REPCMD_INSERT	:
			text.insert(cmd.lineno, cmd.string);
			sendCommand(cmd);
			// sendCommand(nop); session manager do this for me
			break;
		case REPCMD_INSERT_ACK	:
			assert(false);
			break;
		case REPCMD_DELETE	:
			String del = text.delete(cmd.lineno);
			cmd.setString(del);
			sendCommand(cmd);
			// sendCommand(nop); session manager do this for me
			break;
		 case REPCMD_DELETE_ACK	:
				assert(false);
			 break;
		 case REPCMD_CLOSE	:
		 case REPCMD_CLOSE_2	:
				assert(false);
			 break;
		 case REPCMD_NOP		:
			 sendCommand(cmd);
			 sendCommand(nop);
			 break;
		 case SMCMD_JOIN_ACK	:
			 sid = cmd.sid;
			 eid = cmd.eid;
			 inputLock = false;
			 break;
		 case SMCMD_PUT_ACK	:
			 sid = cmd.sid;
			 eid = cmd.eid;
			 inputLock = false;
			 break;
		 case SMCMD_QUIT		:
			 sendCommand(cmd);
			 cmds.clear();
			 break;
		 case SMCMD_QUIT_ACK	:
			 assert(false);
			 break;
		 case SMCMD_START_MERGE :
			 // lock user input during merge (optional)
			 inputLock = hasInputLock;
			 cmd.cmd = REP.SMCMD_START_MERGE_ACK;
			 sendCommand(cmd);
			 break;
		 case SMCMD_START_MERGE_ACK :
			 assert(false);
			 break;
		 case SMCMD_END_MERGE :
			 inputLock = false;
			 break;
		 case SMCMD_QUIT_2 :
			 sendCommand(cmd);
			 running = false;
			 break;
		 case SMCMD_SYNC:
			 // start contents sync with newly joined editor
			 cmd.cmd = REP.SMCMD_SYNC_ACK; sendCommand(cmd);
			 syncCounter = 1;
			 break;
		 default:
			 assert(false);
		 	 break;
		}
	}
}