view test/sematest/TestEditor.java @ 284:90965a3bd4f3

editor simulator
author kono
date Sun, 28 Sep 2008 14:16:13 +0900
parents 311847db7429
children b468f24c3e09
line wrap: on
line source

package test.sematest;

import java.io.IOException;
import java.net.InetSocketAddress;

import rep.REP;
import rep.REPCommand;
import rep.REPCommandPacker;
import rep.channel.REPLogger;
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;

	static private String[] text1d = {
		"aaa", "bbb", "ccc", "ddd", "eee",
		"fff", "ggg", "hhh", "iii", "jjj",
		"kkk", "lll", "mmm", "nnn", "ooo",
		"ppp", "qqq", "rrr", "sss", "ttt",
		"uuu", "vvv", "www", "xxx", "yyy", "zzz"
	};	
	
	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);
		else
			text = new Text(new String[0]);
	}

	public void run(){
		try {
			REPSocketChannel<REPCommand> channel;
			channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());

			ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" editor-"+myid, 1);
			while (!channel.connect(semaIP)){
				ns.writeLog("SeMa not listen to socket yet, wait", 1);
				Thread.yield();
			}
			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);

			mainloop(channel);
		} catch (IOException e) {
		}
	}

	private void mainloop(REPSocketChannel<REPCommand> channel) {

		
	}
}