view test/sematest/TestEditor.java @ 285:b468f24c3e09

TestEditor
author kono
date Sun, 28 Sep 2008 14:50:46 +0900
parents 90965a3bd4f3
children 30c993e89286
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;


public class TestEditor extends Thread{
	private InetSocketAddress semaIP;
	private REPLogger ns;
	private boolean master;
	private int myid ;
	private int seq = 0;
	public static int editorNo = 0;
	public Text text;
	public LinkedList<REPCommand> cmds;
	private int eid;
	private int sid;
	REPSocketChannel<REPCommand> channel;
	boolean running = true;
	long timeout = 1;

	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.master = master;
		myid = editorNo++;
		if (master) {
			text = new Text(text1d);
			cmds = new LinkedList<REPCommand>();
			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.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+" editor-"+myid, 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 editor-"+myid, 1);
		REPCommand command;
		/*
		 *         public REPCommand(REP cmd,int sid,int eid, int seq, int lineno,  String string) 
		 */
		if (master)
			command = new REPCommand(REP.SMCMD_PUT,0,0,seq++,0,"master-file");
		else				
			command = new REPCommand(REP.SMCMD_JOIN,0,0,seq++,0,"editor-"+myid);
		channel.write(command);

		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 (selector.select(timeout)<=0) {
				REPCommand cmd = cmds.poll();
				if (cmd!=null) {
					text.edit(cmd);
					sendCommand(cmd);
				} else {
					// no more command to send
					timeout = 0;
				}
			} else {
				handle(channel.read());
			}
		}
	}


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

	private void handle(REPCommand cmd) {
		ns.writeLog("editor-"+myid+" read "+cmd);
		switch(cmd.cmd) {
		
		}
	}
}