view test/sematest/Tester.java @ 239:f9b1a9ba1a73

*** empty log message ***
author pin
date Sun, 31 Aug 2008 19:49:35 +0900
parents b837feb00132
children c3969dd625b2
line wrap: on
line source

package test.sematest;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.List;

import rep.REP;
import rep.REPCommand;
import rep.REPCommandPacker;
import rep.channel.REPLogger;
import rep.channel.REPServerSocketChannel;
import rep.channel.REPSocketChannel;

public class Tester extends Thread{
	private SocketAddress semaIP;
	protected REPLogger ns;
	private boolean running = true;
	protected REPSocketChannel<REPCommand> channel;
	protected List<REPCommand> commandList;
	
	public Tester(String name, String _host,int _port){
		super(name);

		REPServerSocketChannel.isSimulation = false;
		semaIP = new InetSocketAddress(_host, _port);
		ns = REPLogger.singleton();
		ns.setLogLevel(5);
	}

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

			ns.writeLog("try to connect to SessionManager whose ip is "+semaIP, 1);
			while (!channel.connect(semaIP)){
				ns.writeLog("SeMa not listen to socket yet, wait", 1);
				Thread.yield();
			}
			ns.writeLog("successes to connect", 1);
			
			sendCommands();
			
			while(running){
				REPCommand packet = channel.read();
				ns.writeLog("gets return string==> `"+packet+"\'", 1);
				if(packet.cmd == REP.SMCMD_QUIT){
					running = false;
				}
			}

			ns.writeLog("testEditor exits.", 1);
			
			channel.close1();
			
		} catch (IOException e) {}
	}

	public void sendCommands(){
		if(commandList == null) return;
		if(channel == null) return;
		
		for(REPCommand command: commandList){
			channel.write(command);
		}
		
	}
	
	public void setCommands(List<REPCommand> commands){
		commandList = commands;
	}
	
}