view test/sematest/TestEditor.java @ 287:1ff8bfc0a99a test-editor

*** empty log message ***
author kono
date Sun, 28 Sep 2008 15:46:36 +0900
parents 30c993e89286
children d93b062eadaa
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 = 0;
	private int sid = 0;
	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++;
		cmds = new LinkedList<REPCommand>();
		if (master) {
			text = new Text(text1d);
			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) {
		 case REPCMD_INSERT	:
			 break;
		 case REPCMD_INSERT_ACK	:
			 break;
		 case REPCMD_DELETE	:
			 break;
		 case REPCMD_DELETE_ACK	:
			 break;
		 case REPCMD_CLOSE	:
		 case REPCMD_CLOSE_2	:
			 break;
		 case REPCMD_NOP		:
			 break;
		 case SMCMD_JOIN_ACK	:
			 sid = cmd.sid;
			 eid = cmd.eid;
			 break;
		 case SMCMD_PUT_ACK	:
			 sid = cmd.sid;
			 eid = cmd.eid;
			 break;
		 case SMCMD_QUIT		:
		 case SMCMD_QUIT_ACK	:
			 break;
		 case SMCMD_START_MERGE :
		 case SMCMD_START_MERGE_ACK :
			 break;
		 case SMCMD_END_MERGE :
			 break;
		 case SMCMD_QUIT_2 :
			 running = false;
			 break;
		 default:
			 assert(false);
		 	 break;
		}
	}
}